Suppose there are 4 long trades open on any day and I have 5 short trade signals out of which only 4 short trades will be opened.
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
{
L_entrycount=0;
shortcount=0;
for (sig = bo.GetFirstOpenPos(i); sig; sig = bo.GetNextOpenPos(i))
{
if (sig.IsLong())
{
L_entrycount=L_entrycount+1;//sig.GetPositionValue();
}
}
for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
{ // Loop through all signals at this bar
if (sig.IsEntry() && sig.IsLong()==False)
{
if (L_entrycount==0)
sig.PosSize = 0;
else if (L_entrycount>0 && shortcount>L_entrycount)
sig.PosSize = 0;
else
shortcount=shortcount+1;
// Set position size to zero to prevent purchase
}
} // End of for loop over signals at this bar
bo.ProcessTradeSignals(i); // Process trades at bar (always required)
}
bo.PostProcess(); // Do post-processing (always required)
}