Backtest Differs from Exploration in PositionScore

I have read a post with similar trouble, but mine seems to be different: so I ask help for a big difficult issue for me, but also I believe a simple one for more expert people.

My code calls for different Positionsize in different circumstances, and in Explore all seems correct:
but when I run the same code in Backtest, sometimes the value of the position isn't correct and the trade is opened for a lesser amount. As you see, I used AllowPositionShrinking.

I observed two possible issues:
first, sometimes it's possible that in my database some symbols may have not a walue and other have one, because they may be traded in different markets: but I don't believe this very important
second, if I put the values to wich choose the PositinSize at a very inferior limit (eg: all= 10000 instead of all=100000, big=6000 instead of 60000 and so on), all seems to work well: but using 'AllowPositionShrinking' I believe is sufficient, and in other trade it is working well.

So, perhaps the key is in some Setting in 'Backtester Setting' ? I tried all what seems reasonable, but there is something I miss . May anybody hepl me? It'll be very appreciated !! Thanks in advance.

Here is my code


symlist = CategoryGetSymbols( categoryWatchlist, 133 );

PosQty = 99; 
SetOption("MaxOpenPositions", posqty);
SetOption( "CommissionMode", 2); 
SetOption( "CommissionAmount", 10.00 ); 
SetOption( "CommissionAmount", 0.00 ); 
SetOption( "InitialEquity", 100000 ); 
SetOption( "AllowPositionShrinking", True );  
SetTradeDelays( 0, 1, 1, 1 ); //  zero for seamless month 
SetOption( "AllowSameBarExit", True );  
SetOption( "HoldMinBars", 1 ); 
BuyPrice  = Close;   SellPrice = Close; 


// definitions 
ML= momlength = Optimize( "momlenght", 127 , 10, 300, 5); //120 che sono 6 mesi circa 
malen         = Optimize( "malen", 50, 5, 100, 5);  
bsw= Risk_on   = 0; 
 

// set limit
SetForeign("EM13.MI");
RFR= RiskFreeROC= ROC( C, ML );
RestorePriceArrays();




 // generate variables
 if( Status( "stocknum") == 0)
 {
 StaticVarRemove( "values*");
 List = symlist = CategoryGetSymbols( categoryWatchlist, 133 ); 

 for( n = 0; ( Symbol = StrExtract( List, n ) ) != ""; n++ )
 {
 SetForeign( symbol );
 Values = MOM = ROC( C, ML );
 RestorePriceArrays();
 StaticVarSet( "values"+ symbol, values );
 _TRACE( symbol );
 }

 StaticVarGenerateRanks( "rank", "values", 0, 1224);
 }

// generate rank 
 symbol = Name();
 values = StaticVarGet( "values"+ symbol );
 rank   = StaticVarGet( "rankvalues"+ symbol );
 

limite= StaticVarGet ( "rankvaluesEM13.MI" ) ;
bsw = Risk_on = IIf ( limite <=4 , 0, 1);

all   = 100000 ; big=60000 ; mid=40000 ; small=0 ;less =0;  nil=0 ;  
 
PositionSize =  
   IIf ( symbol == "EMG.MI"    AND  Risk_on == 0 , all ,  
    
   IIf ( symbol == "DAXX.MI"   AND  StaticVarGet("rankvaluesDAXX.MI") == 1      AND  Risk_on == 1 OR    
         symbol == "IUSA.MI"   AND  StaticVarGet("rankvaluesIUSA.MI") == 1      AND  Risk_on == 1 OR 
         symbol == "EMG.MI"    AND  StaticVarGet("rankvaluesEMG.MI") == 1       AND  Risk_on == 1 OR  
         symbol == "MEU.MI"    AND  StaticVarGet("rankvaluesMEU.MI") == 1       AND  Risk_on == 1 , big , 
    
   IIf ( symbol == "DAXX.MI"   AND  StaticVarGet("rankvaluesDAXX.MI") == 2      AND  Risk_on == 1 OR  
         symbol == "IUSA.MI"   AND  StaticVarGet("rankvaluesIUSA.MI") == 2      AND  Risk_on == 1 OR 
         symbol == "EMG.MI"    AND  StaticVarGet("rankvaluesEMG.MI") == 2       AND  Risk_on == 1 OR  
         symbol == "MEU.MI"    AND  StaticVarGet("rankvaluesMEU.MI") == 2       AND  Risk_on == 1 , mid,  
     
   IIf ( symbol == "DAXX.MI"   AND  StaticVarGet("rankvaluesDAXX.MI") >= 3      AND  Risk_on == 1 OR   
         symbol == "IUSA.MI"   AND  StaticVarGet("rankvaluesIUSA.MI") >= 3      AND  Risk_on == 1 OR 
         symbol == "EMG.MI"    AND  StaticVarGet("rankvaluesEMG.MI") >= 3       AND  Risk_on == 1 OR     
         symbol == "MEU.MI"    AND  StaticVarGet("rankvaluesMEU.MI") >= 3       AND  Risk_on == 1  , small,  nil )))); 
 


monthly = Month () != Ref( Month () , 1); 
monthlyclose = IIf ( Ref (monthly, 1) ==1, 1, 0); 
 
 
Buy= monthly ;  sell= monthlyclose; Short = Cover = 0; 
ExRem (Buy,Sell); ExRem (Sell, Buy);

// explore routine 
colorsell = cols= IIf ( sell ==1 , colorGreen, colorWhite); 
colorbuy = colb= IIf ( Buy ==1 , colorGreen, colorWhite);
colorbsw= IIf ( bsw==0, colorRed, colorwhite);
colorRiskFree = colRF= IIf (Name() == "EM13.MI", colorYellow, colorWhite); 
AddColumn( values, "values");
AddColumn( RFR, "riskfreeMOM");
AddColumn( rank, "rank");
AddColumn( risk_on, "switch risk_on",3.3,1, colorbsw );
AddColumn ( PositionSize , "possize");
AddColumn( rank, "rank");
AddTextColumn( Name()    , "Tickers"          , 1.0, 1, colRF ); 
AddColumn( buy, "Buy", 1.0,1,colb);
AddColumn( sell, "Sell" , 1.0,1,cols);
Filter= 1;
SetSortColumns( 2, 5);





Try turning on the Detailed Log from Backtester Settings / Report. AmiBroker will usually tell you why it's reducing your position size. The most obvious reason is that you don't have enough cash in your account to enter a full position.

Also make sure that on the Backtester Settings / Portfolio tab you aren't limiting trade size as a % of entry volume.

2 Likes

dear Mr. mradtke, thaks a lot for the kindness and the prompt reply
You solved my trouble perfectly !!
I can't say how many hours I have lost, and how many you saved for me.

If I may ask, perhaps you know how to control the 'trade size as % volume' in the code itself?
it would be useful to bring into control this variable in the AFL

Thanks again for your precious help

In that case, it would be nice to hit the heart-shaped "like" icon or mark the response as the the solution to your question. Good etiquette will encourage others to help you in the future, and will also make it easier for other forum users to find answers.

As far as I know, there is no way to control that setting via AFL. If there was, it would most likely be one of the field names supported by the SetOption() function.

3 Likes

I apologize for my inattention, becaise of my poor handling ot the media and scarce use of forum, but I want to improve.

and thanks again for your help:
it would be useful have a 'SetOption' dedicated, but maybe in the future will be
Best regards !
Cesare