Problem with SetTradeDelays & buyprice

There is a problem with SetTradedelay & buyprice that I can’t figure out; apologies for potential stupid error here and thanks in advance if someone can help me find the problem. I detected an issue with a rotation system, as it did not seem to be correctly implementing executing my settings from the afl code, yet the problem was not occurring on non-rotation systems.

To debug the problem, I have gone back to the most basic version:

  • New clean install of 6.20.1 x32 bit.
  • Only used the unmodified database that comes with the install.
  • Using the version that comes with install.

Always in the past, as consistent with the manual, the afl code commands with SetTradedelay & buyprice (etc) always overrode whatever the Backtester settings from the Trades tab. However, now when I change the afl code for SetTradedelay & buyprice, the results do not properly change, further, changing the backtester settings trade tab choices are somehow overriding the afl code settings.
To illustrate and simply this, I ran it with the simple rotation with following alf settings lines
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = CoverPrice = SellPrice = ShortPrice = Close;
1st run settings in pdf “Sample Rotation SDC +in system options.PNG”, using the backtester settings trades of “trade current bar on close”.
2nd run attached pdf “Sample Rotation SDC +different in system options.PNG”, everything identical, with identical afl, only changing the backtester settings trades to “trade next bar on open”. The trading results change, overriding the afl settings, and you can see the buy price is now set to open, not close.

Here is a Dropbox link ( ) where you can download PDFs of the two full test runs, including stats, trades, afl used, settings. The naming convention is the same as the PNG settings picture.

What am I missing?

formn = ParamStr( "set name", "Rotation Test SDC" );

MaxPositions = 4;
SetOption("MaxOpenPositions", MaxPositions  );
SetOption("WorstRankHeld", MaxPositions + 2 );
SetPositionSize( 100 / MaxPositions, spsPercentOfEquity ); 

// ----------- Execution Delay Time --------------------------

		// Same Day Close
	  SetTradeDelays( 0, 0, 0, 0 );  // same day
	  BuyPrice = Close;
	  CoverPrice = SellPrice = ShortPrice = Close;  // trade the close
			// Nxt day Open
		//SetTradeDelays( 1,1,1,1);  // 1 day delay
		//BuyPrice = CoverPrice = SellPrice = ShortPrice = open;  // trade the open
SetBacktestMode( backtestRotational );

// offsetting by large positive number 
// makes sure that our score is always positive and we don't enter short trades
PositionScore = 10000 - ROC( C, 252 ); type or paste code here

This is expected behavior as documented here

The rotational trading mode uses "buy price" and "buy delay" from the Settings | Trade page as trade price and delay for both entries and exits (long and short)

1 Like

Thanks for that, much appreciated.
I thought I read elsewhere that the afl settings override that, but I must have been wrong.
Why then would the example rotationsystem provided with the install come with the TradeDelay and BuyPrice lines as the example, when it is ignored? Also, in my testing, changing those settings in code without changing the system setting trades does change results, just not completely. Hmm, quite strange.

I doubled checked the runs. When I changed the Backtest Settings page to 1 day delay, and open price, it trades in the 1st day of the From date. It does successfully switch to using the open price, but shouldn't the tradedelay cause the 1st potential trade & price to be from the 2nd day of the From to date window, not the 1st?
You can see this in the pdf files Dropbox link ( )

Thanks again.

@swalk10, those sound like questions for @Tomasz. :slight_smile: I seldom use AB's rotational mode, as I'm typically doing other things in my rotational strategies that are most easily accomplished with a CBT.

I will do some checking that makes it clearer if the rotation tradedelay is working from system settings at least in what I am doing, and post back.

Update, when I pulled all references inside the afl to settradedelay & buyprice, etc, and used the System Settings, the delay and the buyprice is properly functioning.
Thanks again.

1 Like