Previous value of indicator

I have simple box indicator, this looks for HHV and LLV : - the values of topbox and bottombox remain the same until HHV and LLV changes during the period.

I need the previous value of topbox or botbox , how do i code this? by using the ref (function) it returns the value as on previous candle, but box value would have been the same for previous few candles.

the code needs to - return the indicator value that was before the current value

PS: i have started learning programming and AFL; i have looked around and will need some guidance on this.

boxHigh1 = Ref(H,-6)>=Ref(HHV(H,5),-8) AND 
                  Ref(H,-6)>Ref(H,-4) AND Ref(H,-6)>Ref(H,-2) AND Ref(H,-6)>H;

Topbox1=ValueWhen(boxHigh1, Ref(H,-6)); 
Botbox1=ValueWhen(boxHigh1,LLV(L,8)); 

Hello
You need to look / read at the 3rd parameter of ValueWhen() function

2 Likes

Thank you so much!

is the below syntax correct (apologies - i don't have coding background)

PreviousTop = ValueWhen(Topbox != Topbox,Topbox,1);

Hi

Always is nice to debug your code to find out if there are any mistakes.

I am going to use here the printf() function.
Note that the return values are visible in the interpretation window

boxHigh1 = Ref(H,-6)>=Ref(HHV(H,5),-8) AND 
                  Ref(H,-6)>Ref(H,-4) AND Ref(H,-6)>Ref(H,-2) AND Ref(H,-6)>H;

Topbox_0=ValueWhen(boxHigh1, Ref(H,-6));	printf("\nTopbox_0 "+ Topbox_0);
Topbox_1=ValueWhen(boxHigh1, Ref(H,-6),1);  printf("\nTopbox_1 "+ Topbox_1);
Topbox_2=ValueWhen(boxHigh1, Ref(H,-6),2); 	printf("\nTopbox_2 "+ Topbox_2);
Topbox_3=ValueWhen(boxHigh1, Ref(H,-6),3);	printf("\nTopbox_3 "+ Topbox_3);

(Finally, I kindly invite you to get the "Verified" badge so you can to continue to post in this section of the Forum).

I really like the phrase here and i took it form this post

2 Likes