Compare returned values at dates in date column

Hello all,

I am trying to create a new column that flags when the "peek" values are higher or lower than the prior peek value. My problem is that i dont know how to reference the dates in the date column or how to reference the prior vlaue of the peek value in the Dow peak column. I know abut "Ref" but I dont think its the answer...

The image shows my exploration and that I get alternating peek and troff values, but I am stuck trying to compare peek or troff values sequentially to see if each new peek is higher or lower than the prior peek.

Thanks in advance for any ideas...

SetSortColumns(-2);
SetChartOptions(0, chartShowDates);   
SetOption("warninglevel",1);
 
pr = Param("ZZ", 5, 1, 100);
  
EWpk = PeakBars(H, pr, 1) == 0;
EWtr = TroughBars(L, pr, 1) == 0;
 
zz = Zig(C, pr);
zzHi = Zig(H, pr);
zzLo = Zig(L, pr);
Avg = (zzHi+zzLo)/2;
 
RetroSuccessSecret = IIf(EWpk, zzHi, IIf(EWtr, zzLo, IIf(Avg > Ref(Avg,-1), H, L)));
EW = Zig(RetroSuccessSecret, pr);
Plot(C, "Close", colorBlack, styleCandle);
Plot(EW, "ZIG ZAG", colorBlue, styleThick);

SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
//Plot( zz ,"Zig", colorYellow, styleLine, Null, Null, 0, 0, 2 );

Troff = TroughBars(EW, pr, 1) == 0;
Peek = PeakBars(EW, pr, 1) ==0;


dist = 1*ATR(10);
for( i = 0; i < BarCount; i++ )
{

if( Troff[i] ) PlotText( "trough@" + l[ i ], i, L[ i ]-dist[i], colorred );
if( Peek[i] ) PlotText( "peak@" + h[ i ], i, H[ i ]+dist[i], colorgreen );

}

PlotShapes( shapeUpArrow * peek, coloryellow, 0, Low );
PlotShapes( shapeDownArrow * troff, coloryellow, 0, High );



 Filter = Troff OR Peek;
AddColumn(IIf(peek,H,Null),"Dow Peak",1.2,colorbrightGreen);
AddColumn(IIf(troff,L,Null),"Dow Trough",1.2,colorred);
AddColumn(IIf(peek,h,IIf(troff==1,l,null)),"All PT",1.2,IIf(peek,colorBrightGreen,colorRed));



a lot of code is available on the forum on peaks and troughs. I made some adjustments to you code. Also I renamed Peek to pk and Troff to tr so I could use some code of mine without retyping

SetSortColumns( -2 );
SetChartOptions( 0, chartShowDates );
SetOption( "warninglevel", 1 );

bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );

pr = Param( "ZZ", 5, 1, 100 );

EWpk = PeakBars( H, pr, 1 ) == 0;
EWtr = TroughBars( L, pr, 1 ) == 0;

zz = Zig( C, pr );
zzHi = Zig( H, pr );
zzLo = Zig( L, pr );
Avg = ( zzHi + zzLo ) / 2;

RetroSuccessSecret = IIf( EWpk, zzHi, IIf( EWtr, zzLo, IIf( Avg > Ref( Avg, -1 ), H, L ) ) );
EW = Zig( RetroSuccessSecret, pr );
Plot( C, "Close", colorBlack, styleCandle );
Plot( EW, "ZIG ZAG", colorBlue, styleThick );

SetChartOptions( 0, chartShowDates );
Plot( C, "C", colorWhite, styleCandle, Null, Null, 0, 0, 1 );
//Plot( zz ,"Zig", colorYellow, styleLine, Null, Null, 0, 0, 2 );

tr = TroughBars( EW, pr, 1 ) == 0;
pk = PeakBars( EW, pr, 1 ) == 0;

for( i = 0; i < 3; i++ )
{
    VarSet( "px" + i, ValueWhen( pk, bi, i ) );
    VarSet( "tx" + i, ValueWhen( tr, bi, i ) );
    VarSet( "ph" + i, ValueWhen( pk, H, i ) );
    VarSet( "tl" + i, ValueWhen( tr, L, i ) );
}

ll = tr AND tl1 < tl2;
hl = tr AND tl1 > tl2;
hh = pk AND ph1 > ph2;
lh = pk AND ph1 < ph2;
dt = pk AND ph1 == ph2;
db = tr AND tl1 == tl2;

//dist = 1 * ATR( 10 );
ft = "Arial Black";
sz = 10;

for( i = fvb; i <= lvb; i++ )
{
    //if( tr[i] ) PlotText( "trough@" + l[ i ], i, L[ i ] - dist[i], colorred );
    //if( pk[i] ) PlotText( "peak@" + h[ i ], i, H[ i ] + dist[i], colorgreen );
    if( ll[i] ) PlotTextSetFont( "LL @" + l[ i ], ft, sz, i, L[ i ], colorRed, colorDefault, -sz * 3 );
    if( hl[i] ) PlotTextSetFont( "HL @" + l[ i ], ft, sz, i, L[ i ], colorRed, colorDefault, -sz * 3 );
    if( db[i] ) PlotTextSetFont( "DB @" + l[ i ], ft, sz, i, L[ i ], colorRed, colorDefault, -sz * 3 );
    if( hh[i] ) PlotTextSetFont( "HH @" + h[ i ], ft, sz, i, H[ i ], colorGreen, colorDefault, sz * 2 );
    if( lh[i] ) PlotTextSetFont( "LH @" + h[ i ], ft, sz, i, H[ i ], colorGreen, colorDefault, sz * 2 );
    if( dt[i] ) PlotTextSetFont( "DT @" + h[ i ], ft, sz, i, H[ i ], colorGreen, colorDefault, sz * 2 );
}

PlotShapes( shapeUpArrow * pk, coloryellow, 0, Low );
PlotShapes( shapeDownArrow * tr, coloryellow, 0, High );

Filter = pk OR tr;
//AddColumn( IIf( pk, H, Null ), "Dow Peak", 1.2, colorbrightGreen );
//AddColumn( IIf( tr, L, Null ), "Dow Trough", 1.2, colorred );
//AddColumn( IIf( pk, h, IIf( tr == 1, l, null ) ), "All PT", 1.2, IIf( pk, colorBrightGreen, colorRed ) );
AddColumn( IIf( hh, H, Null ), "Dow Peak (HH)", 1.2, colorbrightGreen );
AddColumn( IIf( lh, H, Null ), "Dow Peak (LH)", 1.2, colorbrightGreen );
AddColumn( IIf( dt, H, Null ), "Dow Peak (DT)", 1.2, colorbrightGreen );
AddColumn( IIf( ll, L, Null ), "Dow Trough (LL)", 1.2, colorred );
AddColumn( IIf( hl, L, Null ), "Dow Trough (HL)", 1.2, colorred );
AddColumn( IIf( db, L, Null ), "Dow Trough (DB)", 1.2, colorred );
1 Like

For general purpose Lookup on any array use Lookup() function.

Thanks for the replies. Appreciated.

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