Hi community!
Exited to share that we have shipped continuous futures into production. We have implemented 3 contract rolling methods, 4 price scaling adjustments as well as adding support for back months.
From now on, the canonical future returned by the `AddFuture` calls, is the new continuous future. Through this same `AddFuture` API you can specify which mapping, price scaling and front or back month configuration you want to use. The new continuous future security is setup as tradable and will never expire since it will map underlying future contracts accordingly, these mapping events will be fed into the algorithm (as Equities) through the `SymbolChangedEvent`.
Note that previously by default the addition of the canonical future to the algorithm, would feed data of the future chain contracts who's expiration were in the 0 to 35 day window. We have disabled this behavior making the continuous future the default data feed. Previous behavior can be recovered by specifying the expiration filter `future.SetFilter(timedelta(0), timedelta(35))`/`future.SetFilter(TimeSpan.Zero, TimeSpan.FromDays(35));`.
Price Scalling
- ForwardPanamaCanal: Eliminates price jumps between two consecutive contracts, adding a factor based on the difference of their prices. First contract is the true one, factor 0
- BackwardsPanamaCanal: Eliminates price jumps between two consecutive contracts, adding a factor based on the difference of their prices. Last contract is the true one, factor 0
- BackwardsRatio: Eliminates price jumps between two consecutive contracts, multiplying the prices by their ratio. Last contract is the true one, factor 1.
- Raw: No price adjustment is made.
Contract Mapping
- LastTradingDay: The contract maps on the previous day of expiration of the front month.
- FirstDayMonth: The contract maps on the first date of the delivery month of the front month. If the contract expires prior to this date, then it rolls on the contract's last trading date instead.
- OpenInterest: The contract maps when the back month contract has a higher traded volume that the current front month.
Front and Back Months
From the `AddFuture` API `contractDepthOffset` will allow specifying which contract to use, 0 (default) is the front month, 1 the following back month and so on. Initially we have added support for the first 2 back months contracts.
At the same time Lean internals mapping and scaling refactor has moved us closer to easily supporting other markets that would require these features, like Indian or European markets.
As always please let us know if you have any issues or concerns, and we will look into them ASAP.
Enjoy!
Gil Sapir
Jared Broad Martin Molinero
But what actually will be bought when buying this canonical contract when using it on live trading?
And if the “oldest” contract will be bought, what happens if it's bought and during the holding period- the contract will be rolled/expired (again, asking on live trading)?
Manoj Agarwala
Excellent feature addition, it has eliminated a lot of headache for me. I was able to delete several hundred lines of code.
Though, I wish following flag was supported by AddFutures:
Auto_roll_forward = True | False
I also suggest the following flags should be supported by indicators (important for live trading):
Auto_update_for_splits
Auto_update_for_dividends
Auto_update_for_continuous_futures_symbol_change
Also, it will be awesome if you could bring similar type of complexity reduction in options. My first hand experience dealing with futures leads me to guesstimate tat continuous futures has led to 10x complexity reduction.
Johnny Cash
Hi,
regarding “The implementation that creates the mappings and factors is closed source at the moment sorry, the implementation that applies the factors and mappings is part of Lean itself, can see this with more detail in the linked PR.”,
Is this closed source software installed in local lean installations, or it only works on the cloud?
Are there other closed source components in the local lean installation?
Does the margin calculation support spread credits, as per CME SPAN calculator, or it only adds the out-rights margins? I am talking about any spread for which there is a SPAN credit, only exchange traded spreads. This is quite important also in live trading.
Thanks
Livermore
could you provide one example for research env?
how to GetFutureHistory for continuous future contract?
thanks.
below code does not work.
qb = QuantBook()
es = qb.AddFuture(Futures.Energies.CrudeOilWTI,
dataNormalizationMode = DataNormalizationMode.BackwardsRatio,
dataMappingMode = DataMappingMode.OpenInterest,
contractDepthOffset = 0)
start_time = datetime(2021, 6, 18, 10, 30)
end_time = datetime(2099, 7, 16, 16, 0)
future_history = es.GetFutureHistory(es.Symbol, start_time, end_time)
history = future_history.GetAllData()
history
Varad Kabade
Hi Livermore,We recommend using the following:
Refer to the attached notebook,Note that currently the default is es.SetFilter(0,0) therefore GetFutureHistory will not return any data. We can also use es.SetFilter(0,180) as an alternative to above.Best,Varad Kabade
Xinmin Cai
Martin Molinero
Regarding: “LastTradingDay: The contract maps on the previous day of expiration of the front month.”
I am not a future expert. But if I understand correctly, for physically settled contracts, such as gold (GC) and silver (si), brokers will close positions before the first notice date, which is usually 1 month before the last trade date. When the ticker is remapped using LastTradingDay, does it use the last trade date or the first notice date in the code?
Xinmin Cai
Can someone give an example of how the data consolidation should be done with the continuous future price? For example, consolidating data into 15 minutes per bar.
Do we still do the old way of consolidating future data like documented here? If not, what is the proper way to consolidate data?
I tried the following and it doesn't work out. I only get one-period data feed on 2021-12-26 18:15:00 from OnDataConsolidated. That is the first 15 min bar. After that, there is no log produced.
Martin Molinero
Hey Xinmin Cai !
Sorry for the delay. The code snippet you shared does work correctly, I'm attaching a backtest using it successfully.
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.
Xinmin Cai
Martin Molinero Thanks, Martin!
A follow-up question on this. If I specify fillDataForward=False in the self.AddFuture() call, does it work? I changed this but it feels there is no data coming through.
I am doing this is because I got a fill on a stale price. I want to make sure, my algo doesn't fill on stale price.
Appreciate the help!
Richard Thomas Harrison
Xinmin Cai re first notice date: I don't think the dataset that QuantConnect has for futures includes first notice dates, I've asked them about this before and I ended up contacting AlgoSeek and I'm pretty sure AlgoSeek doesn't have first notice dates. Martin Molinero any comment on this?
Varad Kabade
Hi Richard Thomas Harrison,
For now, all futures are cash-settled as we don't allow underlying (Live Cattle?) to be posted on the account, thus the FDN is not required (and we don't support this element).
Best,
Varad Kabade
Ariel Nechemia
Is anyone else able to get tick level data in the research notebook? I can get second and minute level, but despite defining a time period or a specific bar count, I only get error messages like this:
NullReferenceException: Object reference not set to an instance of an object.
Varad Kabade
Hi Ariel Nechemia,
Unfortunately, we cannot get Tick data in the research environment. Sorry for the inconvenience caused. We have opened a GitHub issue to get it resolved to subscribe to the following for updates:
https://github.com/QuantConnect/Lean/issues/4297
Best,
Varad Kabade
Mathieu Meynier
Hello,
Could you please provide an example with front AND back contracts (contractDepthOffset = 1 for instance). I dont get any data on the back contracts when trying.
Thanks
Mathieu
Louis Szeto
Hi Mathieu
We've created a GH issue on the "back contract" support by contractDepthOffset. Please subscribe to this issue for the progress.
For now, a workaround would be using GetFutureContractList and AddFutureContract.
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.
Mathieu Meynier
Hi Louis,
Ok, thanks !
I'll use the GetFutureContractList method for now
Best,
Mathieu
Flame
I've been doing more work with futures and I think that the mapping may need another option to change based on daily volume. I've been using the OpenInterest mapping mode, but I've found some issues. For example, Brent Last Day Financial (BZ) has a high open interest in December. This means that the current mapped contract is December 2022, but there is low daily volume compared to the June contract. Does anyone have any suggestions?
Laurent Crouzet
As Flame, I think that the daily volume (for instance the average of trading volume over the last 2 to 3 days?) would be the best to really trade the “most active future contract”.
Flame
For anyone wanting to trade front month futures based on daily trading volume I wrote the below code to help. Hopefully its useful
Tom M
@Flame - thanks a lot for this. Do you know how you would add indicators to this framework? I was able to do so with the continuous contracts but, as you stated, ran into some issues i.e. open interest ≠ front month. So, trying to use yours but not sure how to do it. Thanks!
Martin Molinero
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!