Hi, is the above code correct? Does the code lookback or look into the future?
I check on several AI its correct but Amibroker give warning 1001 the function ref() used here may be looking into the future 10 bars and result return is also not correct.
The Ref() function requires a negative value to reference past bars, so in my example I added a minus sign.
That said, your second line may not be necessary at all, because AmiBroker AFL already provides a built-in function to calculate the rate of change for a given period: ROC (as shown in the snippet).
Also, in this case there’s no need to add +1e-10 to the denominator, since you’re using Close prices, which will never be zero. More generally, when you do need to guard against potential division by zero in AFL, you can use the function SafeDivide().
Finally, I added a simple exploration to confirm that your (modified) line and the ROC function produce the same results.
While AI models can be helpful, I strongly recommend also getting familiar with the AmiBroker documentation and regularly checking the list of available AFL functions.