Previous Pivot Value or Highest Pivot Value on the left hand side

Hello, I am trying to find the pivot value on the left hand side of the chart. My current code gives me the current value but I trying to get the '-1' array value in the function. Please refer to an example - the red array is what I am trying to get in exploration.

image

//Base Breakout Weekly//
//Credits
// Swing Pivot Low and High Logic from an online source that I cant find now !:(
// Multiple return value function inspired from here -https://forum.amibroker.com/t/custom-function-return-multiple-values-arrays/20787

function Pivot(nbar,vHv,vLv,vDiffHi,vDiffLo)
{
    // High Pivots
    vPHigh 		= (H > Ref(HHV(H, nbar) , -1)) AND (Ref(HHV(H, nbar), nbar) < H);
    vPHighP0 	= ValueWhen(vPHigh, H);
    vPHighP1 	= IIf(vPHighP0 AND (BarsSince(vPHigh) > nbar), vPHighP0, Null);
    vPHighP2    = IIf(vPHighP0 AND (BarsSince(vPHigh) <= nbar), vPHighP0, Null);
    
    // Low Pivots
    vPLow 		= ((L < Ref(LLV(L, nbar), -1)) AND (Ref(LLV(L, nbar), nbar) > L));
    vPLowP0 	= ValueWhen(vPLow, L);
    vPLowP1 	= IIf(vPLowP0 AND (BarsSince(vPLow) > nbar), vPLowP0, Null );
    vPLowP2    	= IIf(vPLowP0 AND (BarsSince(vPLow) <= nbar), vPLowP0, Null );
    
     // High - Low Difference
    vHV 		=  ValueWhen(vPHighP2, vPHighP0);
    vLV 		=  ValueWhen(vPLowP2, vPLowP0);
    
    vDiffHi 	= (C - vHV ) /vHV * 100;
	vDiffLo 	= (C - vLV ) / vLV * 100;

}

Pivot(5,&vHV5,&vLV5,&vDiffHi5,&vDiffLo5);//Call Function
Breakout_Level=vHV5; //Breakout Array define
Previous_BL=Ref(Breakout_Level,-1);//Previous Breakout Level
Below_Base = Ref( HHV( C, 5 ), -1 ) < vHV5 OR Ref( HHV( H, 5 ), -1 ) < vHV5;//Checking no voilations
Breakout= Below_base AND Cross(C,vHV5);// Breakout 
Time=Param("Time",1,1,20,1);
Time_Breakout=Sum(Breakout,Time)>=1;

// Bar Since the Breakout and for Sort 
Bar_Since=BarsSince(Breakout);
AddColumn(Bar_Since,"Time Elapsed",1.2);
AddColumn(Previous_BL,"Breakout Level",1.2);
SetSortColumns(-3); //Sort with the latest 

Filter=1;
1 Like

addendum- I run the code on Weekly Periodicity

@fxshrat @beppe - can any one of you guide me thanks in advance.

@Tomasz can you please help or guide how can i solve this

Use Ref( array, -1 ) to get previous bar value AFL Function Reference - REF

Hi Tomaz, I tried that - it is in the code
Previous_BL=Ref(Breakout_Level,-1);//Previous Breakout Level

but it gives me same values
Screenshot 2021-09-08 093925

Hi, Do you mean like vHV = ValueWhen(vPHighP2, vPHighP0, 2 );

1 Like

yes that is correct @JohnHT

@abhishektiwari you need to read this:
http://www.amibroker.com/kb/2014/09/29/debugging-techniques-part-1-exploration/

Ref gives PREVIOUS BAR value.
ValueWhen does NOT give you previous bar value. It gives you Value that was in the array WHEN specified Condition was true. With Nth = 1 it gives you "most recent" occurence, with Nth = 2 gives you 2nd most recent occurence.

If your read this:
http://www.amibroker.com/kb/2014/09/29/debugging-techniques-part-1-exploration/

you would quickly find that value is HELD constant during bars when condition is false. That is why going directly to previous bar (not last occurrence), would give you unchanged value.

1 Like

Thank you for guidance- let me read and come back if any questions.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.