I'm learning about Futures in Quantconnect, and I took this starter code from the documentation, and changed the instrument to Palladium Futures. A few questions:
- Why are all the rollovers getting filled at stale prices, and how do I fix it so that I get filled when the market is open?
- This strategy just basically buys 1 palladium future contract and continuously rolls it over. As you can see, the equity curve collapses, mainly because the rollovers are getting filled at weird prices sometimes. This is not how the palladium market tracked overall during this backtest period, so any ideas?
Mia Alissi
Here are the substantive bugs present in your current code, why rollovers are being filled at stale prices, and direct fixes to realize realistic futures rolling for Palladium in QuantConnect:
1. Incorrect use of
on_symbol_changed_events(Handler is Not Invoked)on_symbol_changed_eventsis not automatically invoked by LEAN. The actual symbol change events are available insideon_dataviadata.symbol_changed_events.on_datamethod, triggered whendata.symbol_changed_eventsis present.2. Market Orders on Mapping Change Are Filled at Stale Prices
market_order(new_symbol, quantity, tag=tag)) often execute at settlement prices, outside of market hours, or in illiquid periods, especially for commodities like Palladium.3. Data Mapping Mode Set to
LAST_TRADING_DAYIs Not Realistic for Live RolloversDataMappingMode.LAST_TRADING_DAYcauses the rollover to occur very late, often after most liquidity has shifted to the next contract, increasing slippage and stale fills.DataMappingMode.OPEN_INTEREST, which triggers roll when front-month liquidity is still adequate, better approximating real-world practice.4. No Check for Live/Regular Trading Hours or Contract Volume
data.bars.5. Rollover Execution is Not Deferred — Race Condition
Example Fix Implementation
In Summary
OPEN_INTEREST.symbol_changed_eventsinsideon_data.is_openbefore any initial or rollover trade for fill quality.By addressing these bugs, your algo’s fills and the resulting equity curve will much more closely track the true, investable Palladium futures returns, and not exhibit the artifact of “stale price” rollercoastering.
Sources: docs writing algorithms securities asset classes futures handling data, Basic Template Future Rollover Algorithm, Basic Template Eurex Futures Algorithm, docs writing algorithms securities asset classes futures requesting data universes, Futures - 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
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!