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
Louis Szeto
Hi Eoin
The algorithm is working as expected. The 2 logged values are different because the order could be filled already at the moment of logging since market orders are filled synchronously in backtesting. The buying power is changed, and so is the "Max Risk Quantity" logged value. If you move the self.Log lines before the MarketOrder, they are the same.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Eoin
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!