Hello, I've been stuck on calculating order quantities for the past 2 weeks. I wrote this simple algorithm that buys NZDUSD every weekday at 10am, placing a stoploss and takeprofit in a 1:2 ratio and waiting until either is hit. The error I get is that instead of algorithm losing only 2% per trade or winning 4%, Average Loss and Average win are very varying.
Formula for calculating required leverage:
leverage = 0.02 * self.Securities["NZDUSD"].Close / (self.atr.Current.Value * self.ATRfactor)
where: (self.atr.Current.Value * self.ATRfactor) is calculating stoploss distance from current price using ATR
Formula I derived for calculating order quantity is: Q = (leverage * 1000) / self.Securities["NZDUSD"].Close
where RISKED_AMOUNT = 2% * SetCash(1000) = 0.02 * 1000$ = 20$
If someone could tell me what piece of puzzle I'm missing, I'd be very grateful.
Edit: In case I wasn't clear, I don't need help with strategy, I need help figuring out why quantity calculation which works for me on paper doesn't work in QC.
Shile Wen
Hi Vedran,
Could you elaborate on what the issue is? I was thinking that the stoploss and takeprofit distances will vary because the ATR varies. Furthermore, another reason for the varying average loss and average win is due to bid/ask spreads.
Best,
Shile Wen
Vedran Mrkonjic Durokovic
Hi Shile,
Thanks for you reply. Luckly after a lot of testing and research I found out there are multiple factors in play and managed to resolve the problem. I've fixed it but in case someone reads this in the future here is what I found:
1. As Shile mentioned, bid/ask spreads were a big issue especially considering timeframe is 15 min, which requires purchasing larger quantities to profit from smaller pip moves. - I've found 2 ways around, first being increasing timeframe to 2h, which reduces quantities that need to be bought so spreads cause less of an impact. Naturally this reduces trading frequency as a tradeoff. Second method would be sniping market volume for higher liquidity, aka trading during relevant market sessions, particullarly first 2h of NY session opening. Most traders already have this requirement built in because trading off hours usually isn't worth it due to lack of volatility.
2. Second reason for big quantity variations comes from choosing brokerage. FXCM's lot size is 1000 units, and for accounts smaller than 100 000$ rounding lot sizes to nearest 1000 causes quantity calculation errors. Way around this is by using Oanda which offers lot sizes of 1 (not sure if there is a tradeoff though).
3. Last and I think biggest problem is being subscribed to wrong data resolution. I've read somewhere on QC forums that submitted SL orders only trigger when OnData() is fired, and if timeframe is 15 being subscribed to minute timeframe means it checks 15 times per OnDataConsolidated check, which every 5th or 6th trade results in SL/TP being filled at old price.
For example if Equity at (Hour:Min:Sec) 10:11:00 = 100$ and SL is at 98$, then 30s later at 10:11:30 it becomes 98$, and then at 10:12:00 it drops again to 96$, SL will be filled at 96$ price and not submitted 98$. This is because It seems QC StopMarketOrder checks every Minute OnData() to which it's subscribed to. Way around this is by subscribing 1 level below data you wish to use (so subscribing to seconds when using minute TF, using minutes for hour TF, using hours for daily TF). I've noticed this makes backtesting take significally longer (logicly you need more data loaded in) but it greatly increased accuracy of Average Loss and Average Win parameters. Just don't forget to adjust your indicators and else accordingly (like registering to timedelta 15min = 900 seconds)
Vedran Mrkonjic Durokovic
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!