How to write afl for unequal asset allocation?

I know how to write an asset allocation of 50% QQQ and 50% SPY.

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

NumberHeld=2;//Optimize("NumberHeld",1,1,10,2);

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

Buy = 1;
Sell= 0;

How to write afl for unequal asset allocation?
I want an asset allocation of 60% QQQ and 40% SPY.

Anyone know how to write unequal asset allocation using AFL?

Thank you very much.

nm = Name();

switch( nm ) {
	case "QQQ":
		SetPositionSize( 60, spsPercentOfEquity ); 
		break;
	case "SPY":
		SetPositionSize( 40, spsPercentOfEquity ); 
		break;
}
1 Like

Hi fxshrat,

I use code below:

SetOption("InitialEquity", 100000); 
SetOption("CommissionMode", 3); //count hand cost by how much future
SetOption("CommissionAmount", 0.01); //one future hand cost 50 dollars
SetOption("MaxOpenPositions",2);
nm = Name();

switch( nm ) {
	case "QQQ":
		SetPositionSize( 60, spsPercentOfEquity ); 
		break;
	case "SPY":
		SetPositionSize( 40, spsPercentOfEquity ); 
		break;
}
Buy = 1;
Sell= 0;

But it can't buy 60% QQQ and 40% SPY.
BUY_QQQ
It will only buy 60% QQQ.

How to modify my code?

Thannk you.

You are applying backtestRegular mode in your code!
https://www.amibroker.com/guide/afl/setbacktestmode.html

2 Likes