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.
unfortunately my code does a continuous entry untill the rule change. If only 1 symbol can be used a limit with MaxOpenPositions but I have more than 1 symbol.
SetCustomBacktestProc("");
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( sig.IsEntry())
{
if( count< 2 )
count ++;
else
sig.Price = -1 ; // ignore entry signal
}
}
One additional post with a very simple code snippet just to illustrate the idea of adding to same position via scale-in (in this case it is limited to 4 entries in the same symbol)
Buy=Cross(C,MA(C,5));
Short=Cross(MA(C,5),C);
Sell=Cross(MA(C,50),C);
Cover=Cross(C,MA(C,50));
Ladd=0;
Sadd=0;
Max_scalein=4;
for( i = 0; i < BarCount; i++ )
{
if ( Buy[i]==1 && Ladd<=max_scalein)
{
Buy[i]=sigScaleIn;
Ladd=Ladd+1;
}
if (Sell[i]==1)
{
Sell[i]=1;
Ladd=0;
}
if (Short[i]==1 && Sadd<=Max_scalein)
{
Short[i]=sigScaleIn;
Sadd=Sadd+1;
}
if (Cover[i]==1)
{
Cover[i]=1;
Sadd=0;
}
}
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.