I am trying to find a way to entry signal (long/short) positions that are repeatedly (multiple) limited until an exit signal (sell/short) appears for each stock/symbol in my watch list.
for example :
EntryLimit = 3 ;
Buy signals appear 3x, (buy1, buy2, buy3), and buy4 are ignored until the sell signal appears. likewise for short signals which are limited until the cover signal appears for each symbol.
if (Status("action") == actionPortfolio)
bo = GetBacktesterObject(); // Get backtester object
bo.PreProcess(); // Do pre-processing (always required)
for (i = 0; i < BarCount; i++) // Loop through all bars
k = 0;
for (sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
// do your custom signal processing
if( ++k > 5 ) sig.Price = -1; // tell backtester to ignore signal
bo.ProcessTradeSignals( i ); // Process trades at bar (always required)
bo.PostProcess(); // Do post-processing (always required)
From reading the first post, I thought you wanted to allow 3 entries in the same symbol over several bars (I didn't understand that you wanted to limit entries per bar)
If you say that your entry signals are continuous (I assume that your Buy/Short variables are true for sevral bars) you might also explore Backtest Modes available like backtestRegularRawMulti where you can have more than one entry per symbol. https://www.amibroker.com/guide/afl/setbacktestmode.html
One final comment on the code below: it limits entry signals to 2 each day and if you have several symbols in your backtest you might have more than 2 entry signals per bar/day.
for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
if( count< 2 )
sig.Price = -1 ; // ignore entry signal
If your limit is only in number of scale-ins per symbol you can start with the example above, where entries are limited to 4 (1 initial entry + 3 scale-ins) and adjust it based on your preferences/definitions.