How to buy top 5 stock one time using EnableRotationalTrading()?

I use the code below:
But this code can't buy 5 top stock one time.
I only see one open position in one time.
Is anything wrong with my code?

Thank you vey much.

SetOption("InitialEquity", 100000); 
SetOption("MaxOpenPositions",5);
SetOption("CommissionMode", 3); //count hand cost by how much future
SetOption("CommissionAmount", 0.005); //one future hand cost 50 dollars
EnableRotationalTrading();

//The number of issues to hold at a time
NumberHeld=5;//Optimize("NumberHeld",1,1,10,2);

//Allocate funds equally among all issues
PositionSize = -100/(NumberHeld);

//Set worstRankHeld to be some number greater
//than the number of positions held
NumberExtras =  0;//Optimize("NumberExtras ",0,0,10,2);
WorstRank = NumberHeld + NumberExtras;
SetOption("WorstRankHeld",WorstRank);

//The LookBack period for the Rate of change indicator
LookBack =76;//Optimize("LookBack ",1,1,200,5);
//stop = Optimize( "max. loss stop level", 1, 1, 30, 10 );
//UpDown allows the ROC to be inverted
//to treat a rising ROC as a "sell' signal
UpDown =1;//Optimize("UpDown ",2,1,2,1);

//Value of 1 allows short positions
//Value of 2 blocks short positions
AllowShort =2;//Optimize("AllowShort ",1,1,2,1);

SPY = Foreign( "SPY", "Close" ) ; 
BigMA = 1;//Optimize("BigMA ",1,1,200,5);
MovAverage = MA(SPY,BigMA);


Multiplier=1;//IIf(UpDown==1,1,-1);
Score = ROC(C,LookBack);
Score=IIf(AllowShort==1,Score,Max(Score,0));

I have tested your code. It works well (but not the performance). lol
Probably, it was up to the market conditions that have only one stock to buy.

Sorry, I should simpily my afl code.
I try to use the simplest code below and it still only buy 1 stock one time.

Thank you very much.

SetOption("InitialEquity", 100000); 
SetOption("MaxOpenPositions",5);
SetOption("CommissionMode", 3); //count hand cost by how much future
SetOption("CommissionAmount", 0.005); //one future hand cost 50 dollars
EnableRotationalTrading();

//The number of issues to hold at a time
NumberHeld=5;//Optimize("NumberHeld",1,1,10,2);

//Allocate funds equally among all issues
PositionSize = -100/(NumberHeld);

SetOption("WorstRankHeld",5);

LookBack =76;//Optimize("LookBack ",1,1,200,5);


Score = ROC(C,LookBack);

PositionScore = Score;

It might help you to run your backtest and in your Backtester settings look for "detailed log". This will help you break down what is happening during your test. When I start with your code it begins by opening 5 positions.

But later, you run low on cash so it can not invest in all 5 positions.

Read up on

SetOption("AllowPositionShrinking", True ); 

```
**Allow position size shrinking**
"If you mark this box AmiBroker will shrink down positions if available equity is less than requested position size (via PositionSize variable). If this box is unmarked positions will not be entered in such case."

and perhaps make a couple of changes in your code like this,
```
SetOption("InitialEquity", 100000); 
SetOption("MaxOpenPositions",5);
SetOption("CommissionMode", 3); //count hand cost by how much future
SetOption("CommissionAmount", 0.005); //one future hand cost 50 dollars
SetBacktestMode( backtestRotational ); 
SetOption("AllowPositionShrinking", True ); 

//The number of issues to hold at a time
NumberHeld=5;//Optimize("NumberHeld",1,1,10,2);

//Allocate funds equally among all issues
PositionSize = -100/(NumberHeld);

SetOption("WorstRankHeld",5);

LookBack =76;//Optimize("LookBack ",76,10,200,2);


Score = ROC(C,LookBack);

PositionScore = Score;
```
It may help to search through the Knowledge Base for articles such as 
http://www.amibroker.com/kb/2016/04/17/long-only-rotational-back-test/

Good luck.
2 Likes

Dear portfoliobuilder,

It really solve my problem.
Thank you so much.:blush:

Hi Larry,
Could you tell me how you get detailed log? Is it a code?
Thanks,
Maury

Ideally this your question and this answer might be best in their own thread about "backtest settings".

When you run a backtest you need to go to the backtester settings. So first see this

Then switch Report mode to Detailed log and re-run backtest as shown here.

http://www.amibroker.com/kb/2014/11/10/troubleshooting-procedure-when-backtest-shows-no-trades/

1 Like

Thanks a lot portfoliobuilder! Maury

You are welcome my good man. How about showing me some love :heart_eyes: LOL!

2 Likes