Backtesting trades getting trail-stopped out without trailing stop-loss

Hi all,

I am trying to backtest a strategy, few of the backtesting trades are getting exited before the actual exit point. I have not set any trailing stop loss. Kindly help me out in getting rid of such exits.

any coded trail stop in your afl will override the analysis settings.
From the chart there appears to be a trail stop both long and short. (red and green lines)
You need to post the code for a definitive answer.


there is not trailing stop loss in the code.

@odhruvji, did you not see this!

Also show your Analysis Stop Settings.

1 Like

Cant help without seeing the code
Somewhere in the code is either applystop(…
or sell =4; and cover = 4;),
otherwise the backtest report would not have “(trail)” listed .

See knowledgebase how to debug your code.
Do your research on apply stop and if you are using loops there are plenty of references here to hel debug the code.


There are at least three reasons that AmiBroker will show Long (trail) in the trade list:

  1. You used ApplyStop() in your code
  2. You enabled stops in your Analysis Settings
  3. You assigned a value of 4 to some elements of the Sell array

I would echo what others have said here and encourage you to share your code. Otherwise, you're forcing people to guess what's wrong, and that's just a waste of time.


Well, you are just repeating what others have written above already. So there is not new information.
And if you look at the picture in first post you see that UI stops are disabled. So it is either 1. or 3.

To the OP

Rather read this article (entire KB is mandatory read for any newbie):

It is worth to mention that values 1 to 9 are reserved for built-in stops and used internally by the backtester, and have special meaning:

  1. normal exit
  2. maximum loss stop
  3. profit target stop
  4. trailing stop
  5. n-bar stop
  6. ruin stop (losing 99.96% of entry value)
  7. reserved
  8. reserved
  9. reserved

Long story short do not add/multiply values lower than 10 to exit signals.
If you want to differentiate exits then do as been done in sell code example of that KB article.

With regard to exit signals they can be visualized in a similar way as shown above, but there is also an additional functionality in the backtester, which allows to indicate the exit condition directly in the trade list. This can be done by assigning values higher than 1 (but not more than 127) to Sell variable.

Sell1 = Cross( Signal(), MACD() );
sell2 = Cross( MA(Close, 50), Close );
Sell = Sell1 * 10 + Sell2 * 20;




Apologies, can't share the whole code. Here is the part of code where i am defining the buy and sell conditions.

BuyCond= H>SPHaboveLPH AND SPHaboveLPH>LPH_Line AND (EuroTiming OR USTiming);// AND  H>LPH_Line;
	ShortCond= L<SPLbelowLPL AND SPLbelowLPL< LPL_Line AND (EuroTiming OR USTiming) ;//L<LPL_Line and ;

	Buy = BuyCond;
	Short = ShortCond;
	SellCond= (L<=SPL_Line)  OR ShortCond ;
	CoverCond= (H>=SPH_Line) OR BuyCond ;
	Sell = SellCond; 
	Cover = CoverCond;

	Buy = ExRem(Buy,Sell);
	Sell = ExRem(Sell,Buy);

	Short = ExRem(Short,Cover);
	Cover = ExRem(Cover,Short);


	BuyPrice = ValueWhen(Buy,Max(SPHaboveLPH,LPH_Line));
	ShortPrice = ValueWhen(short,Min(SPLbelowLPL,LPL_Line));
	SellPrice = ValueWhen(sell, SPL_Line);
	CoverPrice = ValueWhen(Cover,SPH_Line);

thanks, i shall go though this article.

As others wrote, the reason is YOUR FORMULA. If you did not use stops, then you are assigning number 4 to sell array instead of true/false. Exit reason = 4 means trailing exit so if you assign that exit code you will get (trail) in the result list. Read the Knowledge Base as others told you.


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