Understanding the Relationship between Backtest, TradeDelays, and Explore

I am tying hard to understand the relationship between Backtest, TradeDelays, and Explore.
I am relatively new to AFL and Amibroker.

I have this code below and have the following questions...

ShortPrice = Open ;
CoverPrice = Close;

SetTradeDelays( 0, 0, 1, 1 );
SetOption( "ActivateStopsImmediately", True );

Short =    

       Ref(C, -1) < MA(Ref(C, -2), 20) 
   AND Ref(C, -1) < MA(Ref(C, -2), 50)  
   AND Ref(C, -1) < MA(Ref(C, -2), 200) 
      
   AND Ref(C, -1) >= 2.0 
         
   AND MA(Ref(V, -1), 20) >= 500000;
      
Cover = 0;

ApplyStop( stopTypeLoss,  stopModePercent, 10, 1 );
ApplyStop( stopTypeNBar, stopModeBars, 0, ExitAtStop = 0 );

Buy = 0;
Sell = 0;


Filter = Short;


AddColumn( Ref(O, -1), "Aug 7 Open", 1.2 );
AddColumn( Ref(C, -1), "Aug 7 Close", 1.2 );
AddColumn( RSIa(Ref(C, -1), 2), "Aug 7 RSI", 1.2 );
AddColumn( Ref(O, -0), "Aug 8 Open", 1.2 );
AddColumn( Ref(C, -0), "Aug 8 Close", 1.2 );
AddColumn( Ref(H, -0), "Aug 8 High", 1.2 );
AddColumn( RSIa(Ref(C, -0), 2), "Aug 8 RSI", 1.2 );
AddColumn( Ref(BuyPrice, 0), "Aug 8 BuyPrice", 1.2 );
AddColumn( Ref(Low, 0), "Aug 8 Low", 1.2 );
AddColumn( Ref(SellPrice, 0), "Aug 8 SellPrice", 1.2 );
Profit = ( Ref(SellPrice, 0) - Ref(BuyPrice, 0) ) * ( 1000 / O );
AddColumn( Max( Profit, -100.0), "Profit", 1.2 );
AddColumn( IIf( Profit > 0, 1, 0), "Win", 1.2 );
AddColumn( IIf( BuyPrice > L, 1, 0) , "Possible", 1.0 );
AddColumn( RSIa(Ref(C, -1), 14), "RSIa(Ref(C, -1), 14)", 1.2 );

  1. If you click "Backtest" and then click "Explore", given "TradeDelays(0,0,1,1)", you get DIFFERENT output results in the result window. How is this possible since in the code "Filter = Short"? Shouldn't they have the same signals and thereby produce the same results? They do not in this case so I am confused.

  2. If you click "Backtest" and then click "Explore", given "TradeDelays(0,0,0,0)", you get the SAME output results in the result window. Why does this happen?

First of all Exploration is not equal to backtest. Exploration outputs (raw) signals and backtester outputs trading simulation results. Secondly SetTradeDelays is a backtester function.

3 Likes

But shouldn't the stocks used in the "Backtest" be a subset of the stocks found in the Exploration?

The symbols in backtest output and exploration output may be the same ones or not.

Again, exploration is not backtest. Backtester has several features that are not available in exploration.

1 Like

I completely understand that Exploration is NOT Backtesting.

However, I am trying to understand how the AFL language/system works. Please bear with me. I have programmed for many years in .Net, C++, Matlab, etc. So I am familiar with programming languages in general.

From my limited experience with AFL, it is almost always the case that the tickers Backtest uses to generate a report is different from the ones generated by Explore. The only case I found so far that they are the SAME is when you set the following parameter: TradeDelays(0, 0, 0, 0) - then the tickers they produce are the same (as far as I can see)

The reason I am going through this "exercise" is that I am trying to compare, for a given trading system, what stocks are "candidates" that could be utilized by the Backtester with those that actually are. I am unclear how that works as well. It appears AmiBroker picks trades for the BackTest that 1) meet the criteria of the order, and 2) appears to select based on alphabetical order of the ticker. But I am not 100% positive.

This is important to me (at least) because I am paper trading to compare the results generated by Amibroker with that of a "live" paper trading system. I.e., I have written code using the IB TWS Api to automatically load candidate trades generated by Amibroker into the IB paper trading system.

So, I guess what I am asking is that I want to find a way to definitively determine the set of "candidate" trades that the Backtester will use in its analysis.

Does this make sense?

Use PositionScore variable then.

Without using PositionScore the order of selection is as follows.
http://www.amibroker.com/kb/2015/09/28/symbol-selection-when-positionscore-is-not-defined/


There is also general purpose ranking via StaticVarGenerateRanks
https://www.amibroker.com/guide/h_ranking.html
(But for backtester use PostionScore ("whenever you can" as the manual says))

2 Likes

That is pretty obvious.

You need to understand that SetTradeDelays is a backtester-ONLY function. If you call it, then in backtest mode only Buy/Sell/Short/Cover arrays get shifted by specified amount of bars (implicit Ref() is performed on those arrays). SetTradeDelays does NOT affect other modes (so it does NOT affect exploration). Obviously then, exploration and backtest would produce different (shifted) signals between each other if you use non-zero trade delay.

1 Like