Short not occuring where I expect it, was: ApplyStop produces incorrect signals occassionally

image

In the above picture where the whiteline is there that short signal should have been produced after a sell signal logically.

It happened at other places in the chart but not there.

Below is the code. Please let me know where I went wrong ad how to handle it correctly.

//////////////chart & back ground color////////////////////
SetChartBkGradientFill(colorBlack,colorBlack,colorBlack);
//Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
Plot( Close, "C", colorWhite, styleCandle); 
separator = Day() != Ref( Day(), -1 );
Plot( separator, "", colorPink, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 5 );
starttime = 091500;
endtime = 151500;
separator = Day() != Ref( Day(), -1 );
insession =  timenum() >= starttime && timenum() <= 151000;
endsession = timenum() >= endtime;
endsession = ( endsession - Ref( endsession, -1 ) ) == 1 ;//OR Ref( separator, 1 );
startsession = timenum() >= starttime;
startsession = ( startsession - Ref( startsession, -1 ) ) == 1;
timelmits =    insession  && !endsession; 
Plot( endsession, "", colorWhite, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
Plot( startsession, "", colorViolet, styleHistogram | styleOwnScale | styleNoLabel | styleNoRescale, 0, 1, 0, -2, 1 );
GraphXSpace=85;


//////////////////////
ema1 = EMA( C, 5 );
ema2 = EMA( C, 10 );
Plot( ema1, "", colorBlue, styleThick );
Plot( ema2, "", colorRed, styleThick );

Buy = timelmits && ( ema1 > ema2 ) && (C > Ref (C,-1));
Short = timelmits && ( ema1 < ema2 ) && (C < Ref (C,-1));
Buy= Buy && Ref( Buy, -1 ) ;
Short= Short && Ref( Short, -1 ) ;

Sell = endsession ;
Cover = endsession ;

ApplyStop( stopTypeLoss, stopModePoint, 15, ExitAtStop = 1, 
Volatile = False, ReentryDelay = 1, ValidFrom = 0, -1 );

ApplyStop( stopTypeProfit, stopModePoint, 20,  
ExitAtStop = 1, Volatile = False, ReentryDelay = 1, ValidFrom = 1, -1 );

Equity( 1 );

HaClose =EMA((O+H+L+C)/4,3); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );  

PlotShapes( IIf( Buy, shapeUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);
PlotShapes( IIf( SHORT, shapeDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
PlotShapes( IIf( SELL, shapeHollowDownArrow,0) , colorRed ,layer = 0, yposition = HaHigh, offset = -8);
PlotShapes( IIf( COVER, shapeHollowUpArrow,0) , colorGreen ,layer = 0, yposition = HaLow, offset = -8);


bi = Barindex();
fvb = FirstVisiblevalue( bi );
lvb = LastVisiblevalue( bi );

sellorcovertrigger = "Regular,Stop,Profit,Trail,N-bars,Ruin";

for( i = fvb; i <= lvb; i++ )
{
    if( Buy[i] )
        PlotText( "Buy\n" + BuyPrice[i], i, BuyPrice[i], colorGreen, -1, 0 ); 

    if( Sell[i] )
        PlotText( StrExtract(sellorcovertrigger, Sell[i]-1) + "\n" + SellPrice[i], i, SellPrice[i], colorRed, -1, 0 );

    if( Short[i] )
        PlotText( "Short\n" + ShortPrice[i], i, ShortPrice[i], colorRed, -1, 0 ); 

    if( Cover[i] )
        PlotText( StrExtract(sellorcovertrigger, Cover[i]-1) + "\n" + CoverPrice[i], i, CoverPrice[i], colorGreen, -1, 0 );
}
////////////////////////////////////

ApplyStop is only reponsible for exits not entries. You say that there is no Short signal (entry short) where you expect it. But ApplyStop is about exits not entries.

Please follow advice given here:

Also keep in mind that there is a signal sequencing, if exit happens at bar CLOSE then you could not open new position on the same bar. Stops triggered by ApplyStop may occur at the close if you enabled certain options (like "activate stops immediately").

Third thing is your use of depreciated (old) single-security inline backtester - Equity(1). This function is provided for backward compatibility with code written for version 4.80 and earlier. New code should not use that, see http://www.amibroker.com/guide/a_equity.html

1 Like

I am talking about sell signal only?

I am well aware that apply stop is for exits only and not for entries.