Optimizing rank position taken in monthly rotation strategy.

Hello, I am trying to optimize a monthly rotation strategy. I use a scan that that uses 1,3, and 6 month percent change to rank stocks on S&P 500. I then buy and hold the top stock for 1 month. However, I have a feeling that the number one position is not actually the best performer so I would like amibroker to run an optimization testing all 500 positions. I want the test to run a rotation every month and test the same number spot over a few year time period. I have attempted to code the optimization but the problem I keep getting the same values in every line of the optimization. Thank your for your help in advance!


SetBacktestMode( backtestRotational );

SetOption("initialequity",100000);

SetOption("MaxOpenPositions",1);


SetPositionSize( 99.5, spsPercentOfEquity );

SetOption("WorstRankHeld",1);

AverageVolume = MA(Volume, 30);
BuyPrice= Close;

Condition= Close>0 ;

isNewMonth = Month()!= Ref(Month(), -1);

Filter = Condition;

addcolumn( Close, "Close price", 1.4 );
AddColumn(Ref(Close,-22),"Close Price 1 month ago",1.2);
AddColumn(Ref(Close,-65),"Close price 3 months ago",1.2); 
AddColumn(Ref(Close,-130), "close 6 months ago",1.2);



pct1= Close- Ref(Close,-22);
xpctone= (pct1 / Ref(Close, -22)) ;
xpctonep= xpctone*100;
Addcolumn(xpctonep, "1 month percent change") ;

pct3= Close-Ref(Close, -65);
xpct3= (pct3/Ref(Close,-65));
xpct3p= (xpct3*100);
AddColumn(xpct3, "3 Month percent change");

pct6= Close- Ref(Close, -130);
xpct6= (pct6/ Ref(Close, -130));
xpct6p= (xpct6*100);
AddColumn(xpct6, " 6 month percent change");

Sumpct= (pct1 + pct3 + pct6);
pctchange= (sumpct/3);
AddColumn(pctchange, "Percent change");


PositionScore = pctchange ;


variable = Optimize("WorstRankHeld",1 , 1, 500, 1 );



Just giving some thoughts because I like the idea... but unfortunatly far from good enough to give you the solution...

My guess is that you have to search in the direction of positionscore.

If you look in the user guid in the Portfolio-level back testing Portfolio-level backtesting section there is some info there.

Now you have positionscore set to pctchange. That will always take the top pick. You will have to replace that pctchange to reflect the position you want to trade....

My guess would be you would first have to rank the symbols and than for the positionscore you use the rank....

Just some thoughts... that you probably also thought off but if not, maybe it helps.

The reason is that you DO NOT use the optimization variable anywhere in the code. You just added this:

variable = Optimize("WorstRankHeld",1 , 1, 500, 1 );

but you are not using variable anywhere in the code.

If you want to have WorstRankHeld to change you have to use variable in SetOption call (at the END, not at the beginning)

variable = Optimize("WorstRankHeld",1 , 1, 500, 1 );
SetOption("WorstRankHeld", variable ); // YOU HAVE TO USE the variable!
1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.