In an attempt to learn how Futures work in Quantconnect, I tried to code up Andreas Clenow's Futures strategy from his book “Follow the Trend”. He basically takes a diversified basket of futures, buys when the price hits a 100 day high, and sells on a 50 day low. The idea is to catch futures that are on an uptrend, and ride the wave. We use ATR to position size based on volatility.
The backtest results just loses all of its equity, which is not what is expected, so something seems way off. Would anyone who understands how futures contracts work in Quantconnect be willing to take a look and help me out?
Mia Alissi
Here are concrete bugs and issues in Chetan's code that would lead to major problems or cause the strategy to lose equity or break futures handling in QuantConnect:
1. Incorrect Property and API Attribute References (Critical)
The code uses lower-case property names for core QuantConnect objects, which are case-sensitive:
self.portfolio→ should beself.Portfolioself.securities→ should beself.Securitiesself.is_warming_up→ should beself.IsWarmingUpself.futures_list's items:.mapped,.symbol_properties→ should be.Mapped,.SymbolPropertiesdata.symbol_changed_events→ should bedata.SymbolChangedEventsdata.bars→ should bedata.BarsThese will all raise AttributeError at runtime, causing the algorithm to halt or behave unpredictably. No trades or signals will execute as intended.
2. Indicator Calculation and Price Reference Are on the Canonical Symbol (Logical Bug)
self.close) are based on the root/canonicalSymbol.3. Rollover/Contract Mapping Issues
MarketOrder(old_symbol, -quantity, tag="Rollover"), but any existing stop or risk orders on the old contract are left open—these could be triggered later, leading to positions in illiquid or expired contracts.4. Position Sizing Is Unsafe
atr20is zero (indicator not fully ready/warmed up). This creates zero-division or very large numbers..IsReadybefore using.Current.Value.5. Entry/Exit and Signal Handling
6. Dictionary Keys and Duplicates
7. Wrong DataMappingMode Settings
DataMappingMode.OPEN_INTERESTwithcontract_depth_offset=0means the canonical can "jump" to any contract that suddenly gets highest OI. On illiquid contracts, this can cause premature or unexpected rolls away from front-month, increasing noise and trading costs.8. Trailing Stop and Order Event Logic
Summary:
To fix:
Would you like concrete examples on how to correct any specific part of the code?
Sources: Debugging - QuantConnect.com, Key Concepts - QuantConnect.com, docs writing algorithms securities asset classes futures handling data, Key Concepts - QuantConnect.com, Key Concepts - QuantConnect.com
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.
Chetan Prabhu
Mia's answer wasn't really helpful, if anyone else has any ideas, please let me know
Graeham
Hey Chetan, I'd be happy to help you build this strategy! From what I've found online it looks like the strategy employs the entry model you referenced, an ATR based sizing model, and a trailing stop loss = 3 * ATR(100)If you'd be willing to share your logic I'd love to help you build it out and get it running!
Chetan Prabhu
Thanks Graham! The logic is:
Universe: A large basket of diversified futures (Currencies, Dairy, Energy, Financials, Grains, Indices, Meats, Metals, Softs). Maybe pick 50 that are regularly traded.
Entry: If EMA(50) > EMA(100) and Price is at 100 day high, go long
Exit: If Price hits 50 day low, exit
Sizing: Using ATR(20), risk approx 0.15% of the portfolio daily on any one position.
Stops: It does not appear that Clenow uses stops in his strategy. Feel free to optionally add one.
I think that covers it. You can take a look at the backtest that I attached in the original post to give you an idea about how I thought about it. Thank you again for your help!
Chetan Prabhu
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!