I am backtesting limit entries on portfolio.
As I have limited capital I want to place only X limits per day. And of course only some are filled.
I am first collecting all buy signal on previous candle and calculate possible fill price:
Buy= Ref(buy1,-1);
BuyPrice= Min(O,limitprice );
Then I run mid-level CBT and check if the limit price was filled. If not I delete the signal using β sig.Price = -1 β.
if( Status("action") == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
dn = DateNum();
for( bar = 0; bar < BarCount; bar++ )
{
Pozic = 0;
for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
{
++Pozic;
}
for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) )
{
if (sig.IsEntry())
{
if (bo.FindOpenPos(sig.Symbol))
{
sig.Price = -1;
_TRACE(WriteVal(dn[bar],8.0,False)+": Signal skipped - position already opened: " +sig.Symbol);
}
else
{
if (Pozic<nMaxPos)
{
++Pozic;
fsmer="";
SetForeign(sig.Symbol);
pHigh = H;
pLow = L;
RestorePriceArrays();
if (sig.IsLong())
{
fLimit = pLow[bar] < sig.Price;
fsmer = "long";
}
else
{
fLimit = pHigh[bar] > sig.Price;
fsmer = "short";
}
if (NOT fLimit)
{
_TRACE(WriteVal(dn[bar],8.0,False)+": limit not reached: " +sig.Symbol);
sig.Price = -1;
}
else
_TRACE(WriteVal(dn[bar],8.0,False)+": entering on limit: " +sig.Symbol);
}
else
{
_TRACE(WriteVal(dn[bar],8.0,False)+": Signal canceled - no free slot: " +sig.Symbol);
sig.Price = -1;
}
}
}
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}
This works ok in most cases, but with one exception. If the limit was not filled one day it does not want to trade the stock next day (although there is a valid entry signal).
If I run detailed backtest I can see it posted βExit signalβ although there was no position on (as it was deleted using sig.Price = -1):
9.10.2018 β there was signal, but no fill. CBT used sig.Price = -1 for the ticker
10.10.2018 β there should be another entry, but instead I see Exit signal:VRTX=Sell (although there was no open position as I delete the entry signal using sig.Price = -1)
Do you have any hint how to solve this?
Thank you.