Hi,

I'm trying to build an algo to backtest a risk management model with EURUSD and simple 200-day SMA/price crossovers.

Essentially, when price closes above the 200-day SMA, the algo should look to enter a position with a market stop order set at the level of the 200-day SMA on the previous day's close. The algo should then exit the position once either the stop is hit or price crosses back below the 200-day SMA. This seems to be working somewhat fine,

However, I am running into an issue with position sizing and order entry. The position size is determined using max risk per trade (i.e. 1% of capital) divided by max loss per lot (i.e. entry price less stop). However, when an order is placed, the position size differs from what I think it should be based on the above formula.

This issue can be seen in the attached backtest. The log entries marked “Order Size: ” are the position sizes being entered as orders, while the log entries marked “Max Risk Quantity: ” are what I think they should be.

I have tried rounding the figures, unrounding them, getting rid of margin, and referencing the formula outlined in the “ordersize” variable each time it is called rather than the variable itself, but none of this has worked.

I'd be very grateful if anyone who can see where I am going wrong could point me in the right direction and apologies if It's plainly obvious.

Many thanks,

Eoin