Hi all.
Trying to calculate an order to trade a forex pair (e.g. USDNOK) on Wednesday 2021 Feb 3 19:00 using “calculate_order_quantity” returns None. However, using a fixed order quantity the marker_order gets executed just fine, see attached backtest. Why?
I noticed this behaviour in a backtest where I used either a fixed quantity or calculating the quantity with calculate_order_quantity. In a backtest (over ~4 years, one order per day, so ~2000 orders) in about ¼ of cases, calculate_order_quantity returns None (~1500 total orders). However, if using the fixed quantity, the forex pairs can be traded (~2000 orders).
Is this a bug or am I missing something or doing something wrong? Any help or alternative method of calculating order size (considering portfolio/equity/etc. and remaining margin) would be appreciated. I just did not know how to do it manually, that's why I used calculate_order_quantity. If you could point me to a source on how to calculate it manually, I would be very grateful.
Mia Alissi
Sources: Basic Template Index Daily Algorithm, Handling Data - QuantConnect.com, Bybit Custom Data Crypto Regression Algorithm, Short Term Reversal With Futures, Momentum Effect In Commodities Futures
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.
Chris
In the meanwhile I found two possible solutions on how to calculate the position size, specifically for forex:
1. This one is probably the easiest alternative using set_holdings. Initially, I wanted to avoid set_holdings as it does not directly yield an order_ticket (which I need to keep track of the order and to get the fill price to close the position later). However, one can easily use the last_order_id to get the ticket.
2. This one uses leverage, margin and currency price (although might be difficult when using crosses to calculate the position size for a USD account…or needs cross-conversion rate applied)
Still, I wonder what's up with the calculate_order_quantity?
Chris
Okay, now I understand how calculate_order_quantity works. It seems closely related to how set_holdings works. If the position of a forex pair is 0, then calculate_order_quantity(symbol, 1) calculates and returns the position size. However, if a position is then opened (with market_order or set_holdings(symbol, 1) and calculate_order_quantity(symbol, 1) is called again, it returns None, as it is aware of currently existing positions. If calculate_order_quantity(symbol, 2) is called, it returns a value, like if set_holdings(symbol, 1) would be called again doing nothing or set_holdings(symbol, 2) increasing the position accordingly.
Chris
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!