# Position size problem

I am getting some strange position sizing and can't figure out where i am going wrong.

I am using the following to set the number of contracts that I would like to purchase.

``````port = Equity();
risk = port * 0.01;
R = (High + TickSize) - (Low - TickSize);
Min_fluc = TickSize * PointValue;
risk_per_contract = (R / TickSize) * Min_fluc;

num_contracts = int(risk / risk_per_contract);

SetPositionSize((Ref(num_contracts, -1)), spsShares); // Ref -1 is used as the signal relies on the price exceeding the high/low of the signal bar
``````

I have also tried

``````port = Equity();
risk = port * 0.01;
R = (High + TickSize) - (Low - TickSize);
Min_fluc = TickSize * PointValue;
risk_per_contract = (R / TickSize) * Min_fluc;

//num_contracts = Max(1, int(risk / risk_per_contract));
num_contracts = int(risk / risk_per_contract);

// have a go at percentage account
Position_value = num_contracts * (entry_price * PointValue);

perc_of_account = Position_value / risk;

SetPositionSize((Ref(perc_of_account, -1)), spsPercentOfEquity);
``````

Both of these are producing the same result, I am suspecting that the SetPositionSize is being totally ignored as if I increase my equity to 250,000,000 and set 'port' to 250,000 it purchases more shares than it should.

The idea here is to risk 1% of my account size on each trade. I have tested the maths outside of the strategy and it works, however in the strategy i get some strange results, please see below as an example;

Ticker &6J, entry date: 03/01/2007, High = 0.85155, Low = 0.84915, Equity = \$250,000, tickSize = 0.00005, PointValue = 125000

From this I would expect

risk = 2500
R = 0.0024
Min_fluc = 6.25
risk_per_contract = 300

num contracts = 8

However when backtesting I get 1 contract. I am also entering a maximum of 5 open trades and have tried multiple margin settings.

Any help would be greatly appreciated.

Thanks!

I think i figured this out. I had 2 SetPositionSize arguments in the code, 1 for the initial contract positioning and another for scaling out of half the position.

combining them into one argument seems to have fixed the issue