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);