@Huynq99, I copied the code you posted and applied it to a watchlist exploration, and I didn't see any errors.
At the very least, to help us understand the problem, you should post screenshots of the errors you're seeing and indicate which version of Amibroker you're using.
The code SelectedValue( BarIndex() ) returns the zero-based index number of the bar that is currently selected by your mouse cursor on the chart.
And it is argument of the moving average or parameter to MA