Use backtestRegular mode for per symbol stop Loss

Hi Amibroker Community,

I've noticed an issue with the stop loss when specifying the symbol name in AFL. Below I have 2 versions of my code. In version 1, I run the backtest on all symbols and but only choose to backtest the "SPX" symbol, while in the other version, I apply the code directly to the current symbol "SPX" without explicitly specifying its name. The logic and code are identical in both versions. And I thought they should work exactly the same.

However, I've observed that the stop loss doesn't seem to work when I specify the symbol name in 1st version. In these cases, no stop loss is triggered. I've searched for information on this topic but haven't found anything relevant. I would greatly appreciate it if someone could provide insights into this issue and suggest possible solutions to make it work.

Additionally, I would like to know if it's possible to run backtests on multiple symbols and set different backtest modes for each individual symbol.

Thank you in advance for your assistance.

Code Version 1 (Stop Loss doesn't work properly)

if (Name() == "SPX500")
{
SetBacktestMode( backtestRegularRawMulti );//set this in order to make it open more than one positions on the same ticker
SetOption("MaxOpenPositions", 3);
SetPositionSize(20, spsPercentOfEquity);
RoundLotSize = 0.1;

Buy = Buy_signal;
Sell = Sell_signal;
Short = Cover = 0;

ApplyStop(stopTypeLoss , stopModePercent, 2 ,ExitAtStop = 1);
}

Code Version 2 (Backtest apply to current symbol, Stop Loss works properly, backtest setting same as Version 1)

SetBacktestMode( backtestRegularRawMulti );//set this in order to make it open more than one positions on the same ticker
SetOption("MaxOpenPositions", 3);
SetPositionSize(20, spsPercentOfEquity);
RoundLotSize = 0.1;

Buy = Buy_signal;
Sell = Sell_signal;
Short = Cover = 0;

ApplyStop(stopTypeLoss , stopModePercent, 2 ,ExitAtStop = 1);

in AB, some functions are designed to be called at the beginning of the task.
For eg. a task here is say BackTest(BT) and a function that sets the environment of that BT is SetBacktestMode()

in this scenario, half way through the BT, imagine calling SetBacktestMode() and asking it to change the terms of BT.
backtestRegularRawMulti is not default, so it is likely that the Set... functions are running default settings or whatever is in the Analysis project.

Having different environment settings will then have an impact on other things like signals which then impact ApplyStop() etc but the problem is not where it appears. (ApplyStop() in your case.)

When any task is run, it seems such settings are deduced and probably run on the first symbol, the number of bars that may be needed etc then locked and loaded. Then all the other threads are fired for the other symbols.
In a task like BT, that is phase 1. Then there is phase 2 which actually filters trades and then gives you a final list.
Internals are quite complex and all this happens very fast :slight_smile: and i'm not the Dev so apologies if some things are not correct.

Hi nsm51, thank you for the insights. In this case, I wonder what's the solution to have different backtest mode for individual symbols when performing a portfolio backtest. For example, if I want SPX to trade with RawMulti mode and QQQ to trade with Regular mode, what's the best way of achieving this?

Only backtestRegular mode supports different stops for each symbol. In any other backtest mode ApplyStop has to be fixed for backtest.

Change mode to backtestRegular (the default)

1 Like

Got it. Thank you Tomas! Is there a better way we could test some symbols with backrestRegular opening one position at a time while others opening multiple positions at the same time with RawMulti?

Portfolio backtest mode is global (per backtest run). You can't have different mode for each symbol as portfolio backtest is done on portfolio, not individual symbols.

If you need to use backtestRegularRawMulti AND have different stop for every symbol, you have to implement stop inside formula as shown in the Knowledge Base AmiBroker Knowledge Base » How to plot a trailing stop in the Price chart

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