Need help using ROC

Hi,

I am trading in 5 minutes time frame. In 5 minutes chart first candle time is 9:15. I want to calculate Rate of Change of close price from 1st candle of the day to which ever candle I am currently in the day. I wrote a code like below.

tn = TimeNum();
STARTRANGETIME = ParamTime( "Startrange Time?", "9:15:00" );
StartBar = tn == StartRangeTime;
Plot(ROC(C, LastValue(BarsSince(StartBar))),"ROC",colorDefault,styleNoLine | styleOwnScale);

When I verify syntax, it is saying Error 52 Invalid Argument value for ROC(): range must be greater than zero (and not Null).

I am not able to figure out the mistake and also not sure how to achieve this. Please help…

Also when I did Code check and profile, it says that I am referring future quotes. If I remove the plot line then future reference warning message is gone…

Please help…

All help required is ALREADY in the error message:
Error 52 Invalid Argument value for ROC(): range must be greater than zero

In your code the range is LastValue(BarsSince(StartBar) and if you get the error it means that this value is zero. It may be zero in number of cases, including that “now” is the 9:15, or that 9:15 bar does not exist in your data at all or is somewhere outside your viewed range (search for ‘QuickAFL’)

Again, you need to use these tools to improve understanding of your OWN code: How do I debug my formula?

tn = TimeNum();
STARTRANGETIME = ParamTime( "Startrange Time?", "9:15:00" );
StartBar = tn == StartRangeTime;
BarsSinceStartBar = BarsSince(StartBar) + 1;
myROC = 100*( C - ref( C, -BarsSinceStartBar ) )/ref( C, -BarsSinceStartBar );
Plot(myROC,"ROC",colorDefault, styleNoLine | styleOwnScale);

2 Likes

That Bar didn’t exist, so was the error, thanks.