SetTradeDelays in a Non Rotational Portfolio

I thought I fully understood the implications of using the 'SetTradeDelays' setting, but for some reason the two sample pieces of code below, which I had believed would produce the same results, do not.

Can someone please tell me what I am missing or not appreciating?

Code Snippet 1

SetTradeDelays( 1, 1, 1, 1 );
BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;
SetPositionSize( 1000, spsValue );
Buy = Ref( BuySignal, 0 ); 
Sell = Ref( SellSignal, 0 );

Code Snippet 2

SetTradeDelays( 0, 0, 0, 0 );
BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;
SetPositionSize( 1000, spsValue );
Buy = Ref( BuySignal, -1 ); 
Sell = Ref( SellSignal, -1 );

They do produce same results here.
Are you 100% certain that you have used equal anaylsis settings as well as equal data?

SetTradeDelays( 0, 0, 0, 0 );
SetPositionSize( 1000, spsValue );

period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buysignal = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sellsignal = Cross( m, Close ); // sell when closes crosses BELOW moving average

BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;
Buy = Ref( BuySignal, -1 ); 
Sell = Ref( SellSignal, -1 );
SetTradeDelays( 1, 1, 1, 1 );
SetPositionSize( 1000, spsValue );

period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buysignal = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sellsignal = Cross( m, Close ); // sell when closes crosses BELOW moving average

BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;

Buy = Ref( BuySignal, 0 ); 
Sell = Ref( SellSignal, 0 );

Check out these ones
http://www.amibroker.com/kb/2014/05/07/why-do-backtest-results-change/
http://www.amibroker.com/kb/2015/01/28/why-analysis-results-and-chart-output-may-differ/
if you have not yet.

1 Like

Thank you for your reply @codejunkie and the links.

I've just done some further tests and have found that the problem is due to setting the position score...

PositionScore = ROC( C, 10 );

The problem is, I'm not sure why this is? Am I wrong in saying my code is not rotational when it has a position score?

You have to delay score also in the first one

SetTradeDelays( 0, 0, 0, 0 );
SetPositionSize( 1000, spsValue );

period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buysignal = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sellsignal = Cross( m, Close ); // sell when closes crosses BELOW moving average

BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;
Buy = Ref( BuySignal, -1 ); 
Sell = Ref( SellSignal, -1 );

PositionScore = Ref(ROC( C, 10 ),-1);

In the second one you don't have to.

SetTradeDelays( 1, 1, 1, 1 );
SetPositionSize( 1000, spsValue );

period = 20; // number of averaging periods 
m = MA( Close, period ); // simple moving average
Buysignal = Cross( Close, m ); // buy when close crosses ABOVE moving average
Sellsignal = Cross( m, Close ); // sell when closes crosses BELOW moving average

BuyPrice = ShortPrice = SellPrice = CoverPrice = Open;

Buy = BuySignal; 
Sell = SellSignal;

PositionScore = ROC( C, 10 );
2 Likes

Thank you for your reply @codejunkie.

That makes sense.

So when the help for 'SetTradeDelays' says...

Note that NO OTHER variables are affected by trade delays, therefore for example if your position sizing depends on values found in buy/sell/short/cover variables and if you are using non-zero trade delays you need to account for that in your code.

at https://www.amibroker.com/guide/afl/settradedelays.html

Should this not be pointed out? and are there any other parameters that need to accommodate the delaying of the signals?

In the first one there isn't a delay based on SetTradeDelays but there are delays inside Buy,Sell variables. Because of signal being checked at previous bar you need to check scores at previous bar's close to get same behaviour as by using SetTradeDelays.

In the second one you do not need extra delay of PositionScore because SetTradeDelays does that for you.

1 Like

Thank you for the clarification @codejunkie.

I guess there are no other variables that also need to cater for the delay in the signals.