Backtester giving error on fresh entry

As shown above, backtester is not entering fresh trades on the starting date giving the reason:"trade not entered because of not reaching minimum re-entry delay after stop"

This wonders me because I am not using any applystop function and disabled as stops in settings.
I am backtesting with custom backtest procedure as given in the following code.
I am trying to backtest a positional strategy with Buy, ScaleIn and Sell custom signal only.

// backtest code

SetCustomBacktestProc("", True);
SetOption("InitialEquity", initial_capital);
RoundLotSize = 1;
TickSize = 0.05;
// deviding capital for original trade and scalein trades
base_eqper = (100 - scalein_position*maxtotal_scalein)/100;
maxpos = N;

// current symbol's datetime

dt = DateTime();

if( Status("action") == actionPortfolio )
{
	bo = GetBacktesterObject();
	bo.PreProcess();
	StaticVarRemove("OpenPos");
	
	for( i = 0; i < BarCount; i++ )
	{
		for( sig = bo.GetFirstSignal( i ); sig; sig = bo.GetNextSignal( i ) )
		{
			sbuy = StaticVarGet( sig.Symbol + "sbuy");
			ssi = StaticVarGet( sig.Symbol + "ssi");
			ssell = StaticVarGet( sig.Symbol + "ssell");
			sbuyprice = StaticVarGet( sig.Symbol + "sbuyprice");
			ssellprice = StaticVarGet( sig.Symbol + "ssellprice");
			ssiprice = StaticVarGet( sig.Symbol + "ssiprice");
			sdatetime = StaticVarGet( sig.Symbol + "sdatetime");

			// buy entry
			
			if( sbuy[i] AND bo.Cash > 0 AND Nz(StaticVarGet("OpenPos")) < maxpos AND NOT Nz(StaticVarGet( sig.Symbol + "IsLong" )) )
			{
				possize = bo.Equity*base_eqper/maxpos;
				bo.EnterTrade( i, sig.Symbol, True, sbuyprice[i], possize );
				StaticVarSet( sig.Symbol + "IsLong", 1 );
				StaticVarSet( "OpenPos", Nz(StaticVarGet( "OpenPos" )) + 1 );
				StaticVarSet( sig.Symbol + "PosValue", possize );
			}
			
			// scale-in entry
			
			if( ssi[i] AND bo.Cash > bo.Equity*sipos AND Nz(StaticVarGet( sig.Symbol + "IsLong" )) 
			AND Nz(StaticVarGet( sig.Symbol + "sicount" )) < maxsiperstock 
			AND Nz(StaticVarGet( "totalsicount" )) < maxtotalsi )
			{
				possize = sipos * bo.Equity;
				bo.ScaleTrade( i, sig.Symbol, True, ssiprice[i], possize );
			
				StaticVarSet( sig.Symbol + "sicount", Nz(StaticVarGet( sig.Symbol + "sicount" )) + 1 );
				StaticVarSet( "totalsicount", Nz(StaticVarGet( "totalsicount" )) + 1 );
				StaticVarSet( sig.Symbol + "PosValue", Nz(StaticVarGet( sig.Symbol + "PosValue" )) + possize );
			}
			
			// sell exit

			if( Nz(StaticVarGet( sig.Symbol + "IsLong" )) AND ssell[i] )
			{
				bo.ExitTrade( i, sig.Symbol, ssellprice[i] );
				StaticVarSet( "OpenPos", Nz(StaticVarGet( "OpenPos" )) - 1 );
				StaticVarSet( sig.Symbol + "IsLong", 0 );
				StaticVarSet( "totalsicount", Nz(StaticVarGet( "totalsicount" )) - Nz(StaticVarGet( sig.Symbol + "sicount" )) );
				StaticVarSet( sig.Symbol + "sicount", 0 );
				StaticVarSet( sig.Symbol + "PosValue", 0 );
			}

		}

		bo.UpdateStats( i, 1 );
		bo.UpdateStats( i, 2 );
	}
	
	bo.PostProcess();
}

StaticVarSet( Name() + "sbuy", Buy);
StaticVarSet( Name() + "ssi", ssi);
StaticVarSet( Name() + "ssell", Sell);
StaticVarSet( Name() + "sbuyprice", BuyPrice);
StaticVarSet( Name() + "ssellprice", SellPrice);
StaticVarSet( Name() + "ssiprice", siprice);
StaticVarSet( Name() + "sdatetime", DateTime() );

Due to some reason on the first entry signal the bo.EnterTrade() function is giving the above error. Please suggest solution. Thanks.

@Rakesh, I saw the same error some time ago using version 6.43.1 64-bit (identical code executed in an older 6.30 32-bit version was running without any issue. I'm now using a more recent version and no longer see this behavior).

What Amibroker version are you using?

At that time, I contacted support and received some suggestions which, in my case, did not solve the problem, and I was told that "There were no backtester changes between 6.43 and 6.30 in the source codes", so the cause of the matter has not been ascertained.
In my case, the matter was not critical, so I did not investigate further.

I hope that this note of mine can be useful to @Tomasz, or to other users who may have had the same type of anomaly, to provide you with further information on what the cause and solution could be.

1 Like

Using custom low-level backtester is perfect way to shoot yourself in the foot. Don't do this.

By using low-level custom backtest you are at mercy of your own programming errors. Since you take over of everything, you are ON YOUR OWN. There are absolutely NO GUARANTEES in low-level backtest as to what you did to yourself.

When I see the code like the one you posted I see BIG RED FLAG. I see problems. This code is disaster.

Keep things SIMPLE. Going low-level is recipe for failure.

In 99.999% of cases you don't need low-level backtest.

I am using 6.49.1 64-bit. As I have no data with 32-bit, I have not checked with 32-bit version, I will try it and let you know.

Thanks, Tomasz, for reply. Your advice is very true and I would try to avoid low-level backtester use. But still I expect some error specific general hints where I can correct myself.

1 Like

It is also worth mentioning, that I want to control scale-in counts per stock and also at portfolio level, so this came as a natural code to achieve that.