Deleting entry in mid-level CBT when limit price is not reached

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):

cbt

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.

Are you setting the backtesting mode using SetBacktestMode(backtestRegularRaw)? If not, AB may be filtering out some of your entry signals.

Sorry I forgot to publish the settings

Yes, I use backtestRegularRaw:

SetOption("InitialEquity",50000);//
SetOption("AllowSameBarExit",False);//
SetOption("ActivateStopsImmediately",false);
SetOption("AllowPositionShrinking",False);
SetOption("FuturesMode",False);
SetOption("AccountMargin",50);//
SetOption("maxopenpositions",nMaxPos);
SetTradeDelays(0,0,0,0);// 

SetBacktestMode(backtestRegularRaw);
SetOption("usecustombacktestproc", True);

While doing future investingation I found out that when I set "AllowSameBarExit" to "TRUE" the limit trade on 10.10.2018 is executed.

Is there a connection between deleting entry in CBT using "sig.Price = -1" and AllowSameBarExit?

Thank you.

There is a connection between "AllowSameBarExit" and which signals AmiBroker filters out before calling your CBT.

1 Like