Low Level Custom Backtest and MaxOpenPositions

Hi,

Could someone please point me in the direction of some documentation or give me some insight into how 'MaxOpenPositions' is handled when doing a 'Low Level' CBT.

I've checked out the following...

CBT user Guide section - http://www.amibroker.com/guide/a_custombacktest.html
CBT Tutorial in UKB - http://www.amibroker.org/userkb/2008/03/16/amibroker-custom-backtester-interface-2/
CBT examples in AB KB . http://www.amibroker.com/kb/category/afl/custom-backtest/

but I can't work out if you should set 'MaxOpenPositions' to a really high value and then manually manage the total number of trades that are entered in the CBT or whether there's an approach I'm not aware of? At the moment, even though I'm using 'SetBacktestMode( backtestRegularRaw );' and I'm not using 'ExRem' I'm losing signals for some reason that exist when I run an 'exploration'.

Here is an excerpt from https://www.amibroker.com/guide/a_custombacktest.html:

To conserve memory AmiBroker stores only (2*MaxOpenPositons) top-ranked entry signals per bar. It keeps however all exit and scaling signals.

Look at the paragraph with the heading "Signal Object".

1 Like

Thank you for your reply mradtke.

I was already aware of the '2 x MaxOpenPositions', but as some of the missing signals are on the first bar and are within the 'MaxOpenPositions' value when the built in back tester processes the signals, I was expecting the signals to have been available within the CBT.

Setting the 'MaxOpenPositions' to a very high number, processing all exit signals first in my CBT and then entering trades based upon the current number of open positions brings me very close to achieving the results of the built in back tester, but I'm still seeing signals in a slightly different order.

The literature says: 'During this first phase scan AmiBroker collects data from buy/sell/short/cover signal, price, position size and score arrays, performs sorting of signals and put top-ranked entry signals and all scale and exit signals into the list.'. Can someone explicitly define on what parameters and in what order the list is sorted? I assume for long entries all exit signals are added to the list first and processed first, and then all entries are added to the list in descending PositionScore order?

Just to add to my previous reply, my CBT on the first bar has the following signals, and for a 'MaxPositionSize' of 10 has the following buy signals that are processed, as expected the signals are in the correct descending position score order but for some reason the signal to buy 'MCDTA-200701' which is correctly skipped in my CBT as expected, is entered by the default back tester which instead skips ticker 'L'.

Processing Portfolio                                                                                                                                                                
----------------------------------------------------                                                                                                                                
--- Date: 03/01/2007                                                                                                                                                                
--- Current Open Trades: 0                                                                                                                                                          
--- Cash: 20000.000000                                                                                                                                                              
--- Equity: 20000.000000                                                                                                                                                            
----------------------------------------------------                                                                                                                                
Skipped Signal                        - Symbol: NEE          ( Pos. Score: 1008.623 ) Open Price: 54.42, Low Price: 54.32, High Price: 55.58                                           
Processing Signal - Share (1) Bought  - Symbol: FNF          ( Pos. Score: 1005.998 ) Open Price: 14.25786, Low Price: 13.81879, High Price: 14.30533, PosSize: -10    
Skipped Signal                        - Symbol: BMC-201309   ( Pos. Score: 1005.601 ) Open Price: 32.26, Low Price: 32.25, High Price: 33.8                                      
Processing Signal - Share (2) Bought  - Symbol: CMCSA        ( Pos. Score: 1004.808 ) Open Price: 14.12667, Low Price: 13.94667, High Price: 14.3, PosSize: -10       
Skipped Signal                        - Symbol: CNP          ( Pos. Score: 1004.114 ) Open Price: 16.63, Low Price: 16.62, High Price: 17.25                                           
Skipped Signal                        - Symbol: AEP          ( Pos. Score: 1003.938 ) Open Price: 42.49, Low Price: 42.45, High Price: 43.1                                            
Skipped Signal                        - Symbol: H-200704     ( Pos. Score: 1003.715 ) Open Price: 30.11, Low Price: 30.08, High Price: 30.3                                       
Skipped Signal                        - Symbol: PCG          ( Pos. Score: 1003.29  ) Open Price: 47.3, Low Price: 47.16, High Price: 47.95                                             
Processing Signal - Share (3) Bought  - Symbol: MAR          ( Pos. Score: 1003.288 ) Open Price: 44.46667, Low Price: 43.99124, High Price: 44.60651, PosSize: -10    
Skipped Signal                        - Symbol: PLCM-201609  ( Pos. Score: 1003.239 ) Open Price: 15.435, Low Price: 15.375, High Price: 15.9                                 
Skipped Signal                        - Symbol: MS           ( Pos. Score: 1003.193 ) Open Price: 67.9937, Low Price: 67.0725, High Price: 68.31735                                    
Processing Signal - Share (4) Bought  - Symbol: KMX          ( Pos. Score: 1003.184 ) Open Price: 26.82, Low Price: 26.185, High Price: 27.645, PosSize: -10           
Skipped Signal                        - Symbol: XEL          ( Pos. Score: 1003.11  ) Open Price: 23.18, Low Price: 23.01, High Price: 23.62                                            
Processing Signal - Share (5) Bought  - Symbol: USB          ( Pos. Score: 1003.09  ) Open Price: 35.61, Low Price: 35.61, High Price: 36.29, PosSize: -10               
Skipped Signal                        - Symbol: ALL          ( Pos. Score: 1003.06  ) Open Price: 65.14, Low Price: 65, High Price: 65.85                                               
Skipped Signal                        - Symbol: MENT-201703  ( Pos. Score: 1003.039 ) Open Price: 18.2, Low Price: 17.86, High Price: 18.52                                    
Skipped Signal                        - Symbol: KO           ( Pos. Score: 1002.983 ) Open Price: 24.18, Low Price: 24.14, High Price: 24.44                                           
Skipped Signal                        - Symbol: NVE-201312   ( Pos. Score: 1002.95  ) Open Price: 16.94, Low Price: 16.76, High Price: 17.1                                      
Skipped Signal                        - Symbol: L            ( Pos. Score: 1002.848 ) Open Price: 41.5, Low Price: 41.14, High Price: 41.84                                              
Skipped Signal                        - Symbol: SOV-200901   ( Pos. Score: 1002.818 ) Open Price: 25.5, Low Price: 25.42, High Price: 25.91                                     
Processing Signal - Share (6) Bought  - Symbol: PTV-201011   ( Pos. Score: 1002.777 ) Open Price: 35.55, Low Price: 35.09, High Price: 35.87, PosSize: -10       
Skipped Signal                        - Symbol: MET          ( Pos. Score: 1002.776 ) Open Price: 53.13013, Low Price: 53.04993, High Price: 53.94107                                 
Processing Signal - Share (7) Bought  - Symbol: LXK-201611   ( Pos. Score: 1002.515 ) Open Price: 72.89, Low Price: 71.3, High Price: 73.2, PosSize: -10        
Skipped Signal                        - Symbol: CAG          ( Pos. Score: 1002.482 ) Open Price: 21.08843, Low Price: 20.98727, High Price: 21.25963                                 
Skipped Signal                        - Symbol: CBS          ( Pos. Score: 1002.477 ) Open Price: 31.15, Low Price: 31.1, High Price: 31.85                                            
Skipped Signal                        - Symbol: SKS-201311   ( Pos. Score: 1002.416 ) Open Price: 17.95, Low Price: 17.71, High Price: 18.17                                    
Processing Signal - Share (8) Bought  - Symbol: HOG          ( Pos. Score: 1002.36  ) Open Price: 70.05, Low Price: 69.6, High Price: 71.45, PosSize: -10                
Skipped Signal                        - Symbol: TWX-201806   ( Pos. Score: 1002.25  ) Open Price: 43.0626, Low Price: 43.0032, High Price: 43.73576                             
Processing Signal - Share (9) Bought  - Symbol: PCAR         ( Pos. Score: 1002.24  ) Open Price: 36.58198, Low Price: 35.55867, High Price: 36.92308, PosSize: -10    
Skipped Signal                        - Symbol: KMB          ( Pos. Score: 1002.114 ) Open Price: 65.09669, Low Price: 64.98167, High Price: 65.86343                                 
Skipped Signal                        - Symbol: CCU-200807   ( Pos. Score: 1002.092 ) Open Price: 35.7, Low Price: 35.35, High Price: 35.75                                     
Processing Signal - Share (10) Bought - Symbol: HLT-200710   ( Pos. Score: 1002.081 ) Open Price: 34.91, Low Price: 34.42, High Price: 35.23, PosSize: -10       
Skipped Signal                        - Symbol: HAS          ( Pos. Score: 1002.03  ) Open Price: 27.3, Low Price: 27.2, High Price: 27.83                                              
Skipped Signal                        - Symbol: DIS          ( Pos. Score: 1001.877 ) Open Price: 33.73767, Low Price: 33.52071, High Price: 34.06312                                 
Skipped Signal                        - Symbol: JCP          ( Pos. Score: 1001.871 ) Open Price: 78.3, Low Price: 77.27, High Price: 79.5                                             
Skipped Signal                        - Symbol: ACV-201105   ( Pos. Score: 1001.809 ) Open Price: 21.6, Low Price: 21.54, High Price: 22.56                                     
Skipped Signal                        - Symbol: CSCO         ( Pos. Score: 1001.805 ) Open Price: 27.455, Low Price: 27.33, High Price: 27.98                                         
Skipped Signal                        - Symbol: BIG          ( Pos. Score: 1001.792 ) Open Price: 23.08, Low Price: 22.76, High Price: 23.47                                           
Skipped Signal                        - Symbol: SGP-200911   ( Pos. Score: 1001.77  ) Open Price: 23.26, Low Price: 23.25, High Price: 23.71                                     
Skipped Signal                        - Symbol: PRU          ( Pos. Score: 1001.769 ) Open Price: 85.97, Low Price: 85.73, High Price: 87.1                                            
Skipped Signal                        - Symbol: GS           ( Pos. Score: 1001.76  ) Open Price: 200.6, Low Price: 197.82, High Price: 203.32                                          
Skipped Signal                        - Symbol: C            ( Pos. Score: 1001.749 ) Open Price: 556.6, Low Price: 547.2, High Price: 562.8                                              
Skipped Signal                        - Symbol: IAC          ( Pos. Score: 1001.727 ) Open Price: 32.50345, Low Price: 32.32893, High Price: 33.18406                                 
Skipped Signal                        - Symbol: FOXA         ( Pos. Score: 1001.715 ) Open Price: 19.02822, Low Price: 18.86046, High Price: 19.15184                                
Skipped Signal                        - Symbol: TEX          ( Pos. Score: 1001.678 ) Open Price: 62.8, Low Price: 58.53, High Price: 62.8                                             
Skipped Signal                        - Symbol: T            ( Pos. Score: 1001.677 ) Open Price: 35.67, Low Price: 34.78, High Price: 35.78                                             
Skipped Signal                        - Symbol: CBRE         ( Pos. Score: 1001.677 ) Open Price: 33.4, Low Price: 33.14, High Price: 33.83                                            
Skipped Signal                        - Symbol: AXP          ( Pos. Score: 1001.664 ) Open Price: 61.18, Low Price: 60.05, High Price: 61.9                                            
Skipped Signal                        - Symbol: DD-201708    ( Pos. Score: 1001.657 ) Open Price: 46.2631, Low Price: 45.53162, High Price: 47.27955                            
Skipped Signal                        - Symbol: RTN          ( Pos. Score: 1001.651 ) Open Price: 52.3, Low Price: 51.97, High Price: 52.97                                             
Skipped Signal                        - Symbol: CELG         ( Pos. Score: 1001.644 ) Open Price: 29.05, Low Price: 28, High Price: 29.145                                           
Skipped Signal                        - Symbol: LMT          ( Pos. Score: 1001.624 ) Open Price: 91.6, Low Price: 91.51, High Price: 92.51                                            
Skipped Signal                        - Symbol: HPQ          ( Pos. Score: 1001.619 ) Open Price: 18.63694, Low Price: 18.63694, High Price: 18.95475                                  
Skipped Signal                        - Symbol: MCDTA-200701 ( Pos. Score: 1001.584 ) Open Price: 5.66, Low Price: 5.54, High Price: 5.78                                      
Skipped Signal                        - Symbol: YUM          ( Pos. Score: 1001.564 ) Open Price: 21.12222, Low Price: 20.97841, High Price: 21.39186                                 
Skipped Signal                        - Symbol: TRV          ( Pos. Score: 1001.563 ) Open Price: 53.69, Low Price: 53.31, High Price: 54.64                                           
Skipped Signal                        - Symbol: NKE          ( Pos. Score: 1001.545 ) Open Price: 12.25, Low Price: 12.10125, High Price: 12.46375                                    
Skipped Signal                        - Symbol: FE           ( Pos. Score: 1001.542 ) Open Price: 59.57117, Low Price: 59.38393, High Price: 60.33984                                  
Skipped Signal                        - Symbol: GE           ( Pos. Score: 1001.497 ) Open Price: 37.41, Low Price: 37.38, High Price: 38.15                                            
                                    
etc.                                            

Sorry, for the signal to buy 'MCDTA-200701' instead of ticker 'L' is on the 5th, as below.

----------------------------------------------------                                                                                                                                
--- Date: 05/01/2007                                                                                                                                                                
--- Current Open Trades: 10                                                                                                                                                         
--- Cash: 272.309021                                                                                                                                                                
--- Equity: 20098.080078                                                                                                                                                            
----------------------------------------------------                                                                                                                                
Share Sold                             - Symbol: CMCSA        ( Pos. Score: {EMPTY}) Price: 14.31667                                                                                                                    
Share Sold                             - Symbol: HLT-200710   ( Pos. Score: {EMPTY}) Price: 34.24                                                                                                                  
Share Sold                             - Symbol: HOG          ( Pos. Score: {EMPTY}) Price: 70.1                                                                                                                          
Share Sold                             - Symbol: LXK-201611   ( Pos. Score: {EMPTY}) Price: 71.95                                                                                                                  
Skipped Signal                         - Symbol: EOP-200702   ( Pos. Score: 1004.644 ) Open Price: 48.11, Low Price: 48.08, High Price: 48.37                                     
Processed Signal - Share (7) Bought    - Symbol: SRE          ( Pos. Score: 1003.908 ) Open Price: 56.27, Low Price: 55.08, High Price: 56.27, PosSize: -10              
Processed Signal - Share (8) Bought    - Symbol: DTV-201507   ( Pos. Score: 1003.616 ) Open Price: 24.77, Low Price: 24.34, High Price: 24.77, PosSize: -10       
Skipped Signal                         - Symbol: H-200704     ( Pos. Score: 1003.506 ) Open Price: 30.02, Low Price: 29.95, High Price: 30.07                                      
Processed Signal - Share (9) Bought    - Symbol: L            ( Pos. Score: 1002.265 ) Open Price: 40.55, Low Price: 40.29, High Price: 40.68, PosSize: -10                
Skipped Signal                         - Symbol: FOXA         ( Pos. Score: 1002.029 ) Open Price: 18.8428, Low Price: 18.6662, High Price: 18.94876                                  
Processed Signal - Share (10) Bought   - Symbol: IGT-201504   ( Pos. Score: 1002.007 ) Open Price: 45.9, Low Price: 45.41, High Price: 45.99, PosSize: -10       
Skipped Signal                         - Symbol: CCU-200807   ( Pos. Score: 1001.952 ) Open Price: 35.47, Low Price: 35.38, High Price: 35.53                                    
Skipped Signal                         - Symbol: TEX          ( Pos. Score: 1001.83  ) Open Price: 58.15, Low Price: 57.67, High Price: 58.66                                            
Skipped Signal                         - Symbol: C            ( Pos. Score: 1001.687 ) Open Price: 550, Low Price: 544.6, High Price: 550.5                                              
Skipped Signal                         - Symbol: GS           ( Pos. Score: 1001.652 ) Open Price: 198.43, Low Price: 197.9, High Price: 200                                            
Skipped Signal                         - Symbol: LMT          ( Pos. Score: 1001.559 ) Open Price: 91.69, Low Price: 91.37, High Price: 92.57                                           
Skipped Signal                         - Symbol: DD-201708    ( Pos. Score: 1001.517 ) Open Price: 45.69312, Low Price: 45.41763, High Price: 46.2061                            
Skipped Signal                         - Symbol: JCP          ( Pos. Score: 1001.487 ) Open Price: 76.9, Low Price: 76.14, High Price: 77.78                                            
Skipped Signal                         - Symbol: AXP          ( Pos. Score: 1001.477 ) Open Price: 59.65, Low Price: 58.9, High Price: 59.87                                            
Skipped Signal                         - Symbol: TRV          ( Pos. Score: 1001.436 ) Open Price: 53.16, Low Price: 52.34, High Price: 53.17                                            
Skipped Signal                         - Symbol: RTN          ( Pos. Score: 1001.428 ) Open Price: 51.34, Low Price: 51.31, High Price: 51.92                                           
Skipped Signal                         - Symbol: YUM          ( Pos. Score: 1001.42  ) Open Price: 21.10424, Low Price: 20.91369, High Price: 21.16177                                   
Skipped Signal                         - Symbol: MCDTA-200701 ( Pos. Score: 1001.375 ) Open Price: 5.41, Low Price: 5.38, High Price: 5.56                                      
Skipped Signal                         - Symbol: SWY-201501   ( Pos. Score: 1001.355 ) Open Price: 29.78369, Low Price: 29.67623, High Price: 30.04338
etc

In a low-level CBT you do not typically process all signals in the order in which they appear in the AmiBroker signal list, but rather process one type of signal at a time in an order that makes sense for your strategy:

  • Process all entry signals
  • Process all scale-ins and scale-outs
  • Process all exit signals
  • Check for and enforce stops

The order in which the built-in backtester performs these steps is highly dependent on all of your settings, whether defined in the Analysis Settings window or via SetOption() calls within your AFL. If you're using ApplyStop(), then AFL calls related to stops will also affect the order in which different types of entries and exits get processed. The order that your own CBT processes the steps should reflect your live trading as well as the laws of space and time, e.g. you shouldn't process end-of-day exits before processing intraday limit orders.

4 Likes

I had something similar, it may however not apply to your case.
I do not use applystop, rather a custom stop. This caused some funny behavior here and there, your issue sounds similar.

Here's my 2 cents:

  1. Try setting a specific date when to perform the calcs (also in AA, but also in the code itself)
  2. Try to initialize the BUY/SELL etc with null
for( i = 0; i < Max(Entryperiod,100); i++ )  //Entryperiod is the longest lookback for any calc u use, ie MA(C,200), would be 200
{Buy=Sell=Cover=Short=null;}	//set all Buy/Sell/ etc to NULL

AAdate=DateNum()>=startdate;									// Start calculating stops only after specified date, this is espcially useful if your tickers have different starting dates, i.e ZC (CORN future) goes back to 1946 in my case
for( i = Max(Entryperiod,100); i < BarCount; i++ ) 				// STOP LOOP
{
   if(aadate[i] ==true)
   {
    // do your stuff
   }
}
1 Like

Thank you for the information and the advice.

I'm not using any stops.

I've now spent a fair amount of time analysing my code and guess it's as you suggest, down to the order in which I'm processing the signals.

Thank you for your suggestion, but at the moment I'm not currently using any stops.

@Dionysos, why would you assign the Buy/Sell/Short/Cover arrays 100 or more times? Each time you do this:

Buy=Sell=Cover=Short=null;

every element of each array gets assigned the value null. Therefore, doing it 100+ times inside a loop is entirely redundant.

Regarding your Entryperiod variable, AmiBroker already takes care of that for you in most cases. For example, if your analysis starts on 1-Jan-2010 and you use MA(C,200) in your AFL, then bar number 0 will correspond to a date that is at least 200 bars prior to 1-Jan-2010. That way all of your indicators will be valid by the first date of the analysis range.

2 Likes

image

This is a Donchian Channel, developing from the start of a run. It messes with my buy/sell + stops if I don't do it this way, also the trailing stops are visualized wrongly. I wanna make sure there's nothing triggered while the channel develops. May not be the most efficient way, yet it does the trick.

@Dionysos Your problem must be somewhere else, because (for the reasons explained by @mradtke), this part of your code is wrong:

for( i = 0; i < Max(Entryperiod,100); i++ )  
{Buy=Sell=Cover=Short=null;}	

You are not using subscript operators [] allowing to access/assign individual array elements, so it just repeats the same line (assigning all elements of each array with the value null) minimum 100 times. One such line is enough. Your two above lines equal:

Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
Buy=Sell=Cover=Short=null;
...etc.

I think that you agree, that repeating this line x times doesn't make much sense :wink:

Accessing array elements: [ ] - subscript operator:
www.amibroker.com/guide/a_language.html

1 Like

thanks gentlemen. I will have a look. Let's not distort the initial meaning of this thread anymore. Anyways thanks for the pointer.

1 Like