ApplyStop different result on backtest

Hello there,

I have problem using ApplyStop, and try to solve it by search in similar topic and didn't find it.
here the guide https://www.amibroker.com/guide/afl/applystop.html that provide me the idea and code to learn from owner ( thank you @Tomasz ).

and here my code

BuyPrice  = C ;
SellPrice = C;

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

ema1 = EMA(C, 5);
ema2 = EMA(C, 20 );

Plot( ema1, "", colorYellow, styleLine);
Plot( ema2, "", colorBlue, styleLine);

Buy = Cross(ema1, ema2 );
Sell = Cross (ema2, ema1);

PlotShapes (IIf (Buy, shapeSmallUpTriangle, shapeNone), colorWhite, 0,L, -15 );
PlotShapes (IIf (Sell, shapesmallDownTriangle, shapeNone), colorBlue, 0, H, -15);

`ApplyStop(stopTypeProfit, stopModePercent, 1,exitatstop=0, True );` 

The rule is, after buy condition true and than will sell it when profit is 1% (applystop), am i wrong ?!

but, when i backtest it, it show different result here
Capture

somebody please give me explanation or correct my wrong code.

thank you

You have set volatile to True. For that setting you should use stopTypeTrailing. But apparently you do not want trailing stop so why setting volatile to true? Then you have set exitatstop to zero. So you exit at trade price (so profit may be >= 1%).

Read the manual carefully.

If you do not want trailing stop but exit at profit when reached intraday and trading at today's close then scenario 2 applies:

//Scenario 2
//https://www.amibroker.com/guide/afl/applystop.html
SetTradedelays(0,0,0,0);
SetOption("ActivateStopsImmediately", False);
ApplyStop(stopTypeProfit, stopModePercent, amount=1,exitatstop=1, volatile=False);
1 Like

thanks for help, your explanation is clear and easy to undestand.

here the new one

BuyPrice  = C ;
SellPrice = C;
SetOption("ActivateStopsImmediately", False);
SetTradeDelays (0,0,0,0);

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

ema1 = EMA(C, 5);
ema2 = EMA(C, 20 );
Plot( ema1, "", colorYellow, styleLine);
Plot( ema2, "", colorBlue, styleLine);
Buy = Cross(ema1, ema2 );
Sell = Cross (ema2, ema1);
PlotShapes (IIf (Buy, shapeSmallUpTriangle, shapeNone), colorWhite, 0,L, -15 );
PlotShapes (IIf (Sell, shapesmallDownTriangle, shapeNone), colorBlue, 0, H, -15);
ApplyStop(stopTypeProfit, stopModePercent, amount=1,exitatstop=1, volatile=False);

But after i run back test again, is still the same.
xxx

xqwdw

it supposed to be tiggered on around 374-376 righ ? cz StopModepercent is 1%, but its's triggered on 396 thats mean 7%.

ApplyStop works perfectly fine!

Your symbol and analysis settings are unknown. Upload APX file then to check for settings.
You possibly run futures mode (see Ticksize). Have you set it (ticksize) greater 1??

Also you should disable UI stops if you just want to run just profit target stop of code.

Either disable in analysis settings
9

Or disable by code

// Disable all UI stops before
for ( i = 0; i <= 3; i++)
	ApplyStop( i, 0, 0, 0 );

That being said I can't reproduce your results.
It works perfectly fine (as to be seen in below picture).
If profit is larger than set target of ApplyStop then it is result of gaps (New bar's Open minus entry price being larger than target price minus entry price).

8

_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();


SetOption("ActivateStopsImmediately", False);
SetTradeDelays (0,0,0,0);

ema1 = EMA(C, 5);
ema2 = EMA(C, 20 );

BuyPrice  = C;
SellPrice = C;

Buy = Cross(ema1, ema2 );
Sell = Cross (ema2, ema1);

Short = Cover = 0;

// Disable all UI stops before
for ( i = 0; i <= 3; i++)
	ApplyStop( i, 0, 0, 0 );

ApplyStop(stopTypeProfit, stopModePercent, amount=1, exitatstop=1, volatile=False);


if ( Status("action" ) == actionIndicator ) {
	eq = Equity(1, 0); //evaluate stops in chart pane
	
	Plot( ema1, "", colorYellow, styleLine);
	Plot( ema2, "", colorBlue, styleLine);
	PlotShapes (Buy*shapeSmallUpTriangle, colorWhite, 0, L, -15 );
	PlotShapes ((Sell>0)*shapesmallDownTriangle, colorBlue, 0, H, -15);
}
1 Like

Thanks for your time to help me.

ApplyStop works perfectly fine, yes of course my code is not fine :smiley:

I try all suggestion from you and find the problem. as u said before its Ticksize, when i add Ticksize = 0 ( i don't know its proper or not) in my code its work like you explain, some more than 1% because of gap.

so for all of you that found the same issue in similar topic, till now my solution from @fxshrat is Ticksize.