H[ i ] == H[ i - 1] is not a new high

Hello everyone !
I have very simple code and I haven't had any issues with code like that before but now I use big (>10000 symbols) database and I see that sometimes like in ~1% of the trades code works incorrectly. Basically it's just simple "Short at lowest low breakout, cover at trailing highest high" strategy but sometimes trail just doesn't work. Price goes up and trail doesn't do anything. Debugging shows this in this case H[i] actually equals to H[i-1] instead of H[i] that's why nothing happens.

Submitting code and screenshots of price series. Trail is just Ref(HHV(High,3),-1) so it's highest high of last 3 bars and on chart you can see that after short signal price goes up and up and nothing.

Problem appears quite rare. In last screenshot you can see portfolio backtesting where from 363 trades only 5 had this problem. Also I have noted that when I test absolutely the same script but long instead of short (i mean buy on highest high breakout, sell at trailing lowest low) problem disappears.

Just for peace of mind I have tried script on another database, problem appeared in 7 trades from 1214 trades overall.

I suppose that this is a bug but I'm sure that Tomacz is overloaded with work so at first I should submit it here.

Thans to everyone !

SetOption ("MaxOpenLong",10);
SetOption ("MaxOpenShort",10);
SetPositionSize( 1, spsShares );

Short=(Low<Ref(LLV(Low,82),-1));
ShortPrice=Ref(LLV(Low,82),-1);
Cover=(BarIndex()==LastValue(BarIndex()));

trailARRAY = Null;
trailstop = 0;

trt = Ref(HHV(High,3),-1);
plot(trt,"trt",colorBlue,1);

for( i = 1; i < BarCount; i++ )
 {

   if( trailstop == 0 AND Short[ i ] ) 
    { 
		trailstop = trt[i];  
    }
    else Short[ i ] = 0; 

   if( trailstop > 0 AND High[ i ] > trailstop )
    {
		Cover[ i ] = 1;
		CoverPrice[ i ]=trailstop;
        trailstop = 0;
    }   
    if( trailstop > 0 )
    {   
       trailstop = Min( trt[i], trailstop );
       trailARRAY[ i ] = trailstop;
    }

}
Plot (trailarray,"trail",colorYellow,1);

There is no "bug" anywhere. You answered yourself:

Debugging shows this in this case H[i] actually equals to H[i-1] instead of H[i] that's why nothing happens.

If H[i ] equals to H[ i - 1 ] then it IS NOT a new high.

1 Like