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!
Spacetime
Awesome! Can not wait to use it! Thank You!!
Spacetime
hello, can someone please provide a simple sma crossover example using continuous contracts.
Can I just pass the continuous contract as shown below in onData()?
.ekz.
What a great update Martin Molinero! Fantastic work, team.
I'm new to futures. Can anyone share tips on what to choose for price scaling, contract mapping and month selection?
Would love any resources articles / tutorials / videos / etc.
Thanks in advance.
Martin Molinero
Hey!
Just sharing a continuous future backtest using some SMAs.
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.
Spacetime
oh i see ! got it ! thanks!! I was confused at first and now I know.
cheers!
Johnny Cash
Can you provide an example of the same backrest using SCF data?
Johnny Cash
I ll try again..
There is a code on git-hub, which I mentioned in my first post, using SCF continuous future data. When I back-test it, it gives no trade, while it should , since it is a simple buy and hold strategy. I think Martin is the last editor of that code, so I post the question here, also because I would like to compare the results of different test, or in research just the time series.Testing data is of fundamental importance in any back-testing, especially when using continuous future.
Some kind of reply would be appreciated. I have tested other continuous future data in the past, and found it very important to do it before doing any trading, For example SCF and Tradestation can differ substantially in some cases. I can do some comparison outside quantconnect , but first I would like to have a definitive answer about SCF data use on quantconnect,. In the past I noticed SCF data format changed but quantconnect did not update the import methods, and my post regarding it was removed, and I need to know if anything has changed since then.
Thanks
.ekz.
Wish I could help. Perhaps try asking in the slack or discord.
Martin Molinero
Hey Johnny Cash !
There is an issue reported for this, Quandl was bought by Nasdaq and their API changed and broke the Lean custom data definition. We will try to address this ASAP, you can subscribe for notifications on the issue.
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.
Jared Broad
Hi Johnny Cash
Our continuous futures support is different from the SCF Quandl Dataset. We have generated our own continuous futures mappings and compared them to third-party sources and are able to make that available for free, built into the QuantConnect Cloud. For discussion about SCF and Quandl please spin up a new thread and we can try and help there.
Best
Jared
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.
Laurent Crouzet
Hey Martin Molinero
Thanks for having added Continuous Futures: that is a very useful way to avoid spending too much time managing the individual contracts.
However, when trying to use the Continuous futures in a Notebook, nothing is returned (it seems to be also the case for individual futures, while it worked before).
Could you let us know how we can ask for a history of prices for Continous Futures data from now on?
For instance, the following example only show an empty Dataframe in a Notebook (for “continuousContract.Symbol” and “continuousContract.Symbol.ID.Symbol”):
qb_future = QuantBook()
continuousContract = qb_future.AddFuture(Futures.Indices.SP500EMini, dataNormalizationMode = DataNormalizationMode.BackwardsRatio, dataMappingMode = DataMappingMode.OpenInterest, contractDepthOffset = 0)
history = qb_future.History([continuousContract.Symbol], 50, Resolution.Daily)
print(history)
Laurent Crouzet
Actually, I also tried to use the qb_future.GetFutureHistory (which used to work in Notebooks before the Continuous Futures implementation), but using the standard method described in the docs…
the DataFrames keep being empty (using either continuousContract.Symbol.ID.Symbol or continuousContract.Symbol):
start_time = datetime(2021, 11, 1, 9, 30)
end_time = datetime(2021, 11, 23, 16, 0)
future_history = qb_future.GetFutureHistory(continuousContract.Symbol.ID.Symbol, start_time, end_time)
historyall = future_history.GetAllData()
print(historyall)
Do I miss something obvious? Or the Continuous Futures did break the previous way to request historical data in a Notebook?
Happy Thanksgiving to you and to the whole QC Community!
Jared Broad
I set the date to make it work. I've made a Github issue to make it set default dates (or throw with an error message) when not provided.
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.
Laurent Crouzet
Thanks Jared Broad!
I am now able to confirm that:
The Github you opened (Not returning silently an empty array in research Notebooks) would be very worthwhile!
Yesterday, while trying to find a way to correctly ask for data using futures, I changed the resolution to Daily (to reduce the quantity of data - so I thought!), and I should have been more careful: that was probably the issue for the History call, in addition to the lack of “SetStartDate”, as the Daily data are not available for futures!
Similar to the Github you just made, I also think that returning an error message if no data can be available in the research Notebook (Hour / Daily resolution are still not available for any future) would help avoiding such a stupid error!
At last, I tried to use the previously advised way of handling history calls (the solution I used until last week, which used to work for each individual future), as explained in the docs here:
"Making History CallsSimilar to options, future historical data can't be accessed using qb.History. Instead, we need to use qb.GetFutureHistory, which lets us access historical futures data."
Good news is that thanks to your great work to provide Continuous Futures, the standard (and simpler) History calls works for futures from now on (so the Docs can be amended).
However, I cannot manage to make it work in the previous way, using GetFutureHistory and GetAllData.
Even by providing a SetStartDate
Maybe because we cannot add a 4th argument for GetFutureHistory to ask for a precise resolution in the history call, while it was Resolution.Minute by default previously?
This code used to work (showing data for the different futures, order by their expiration dates) until last week, and adding qb_future.SetStartDate(2021, 11, 1) do not help:
qb_future = QuantBook()
es = qb_future.AddFuture(Futures.Indices.SP500EMini, Resolution.Minute)
start_time = datetime(2021, 11, 1, 9, 30)
end_time = datetime(2021, 11, 9, 16, 0)
future_history = qb_future.GetFutureHistory(es.Symbol, start_time, end_time)
historyall = future_history.GetAllData()
historyall
Maybe the GetFutureHistory() will be deprecated in the future?
If yes, will there be a way to still use individual future contracts in the research Notebook, as before?
Nothing urgent here, I will use the Continuous Futures instead for my current research, using my new more powerful R2-8 node!
Happy Thanksgiving again!
Jared Broad
Hi All!
We tweaked the implementation this evening to let you reference the underlying contracts to trade them in live trading – #first-in-the-world.
When you add the contract you're given the reference to the continuous-contract-security, which has unrealistic prices in many of the adjustment methods. You can't trade this - but the underlying contract is available with a new Mapped property;
This way you can develop identical algorithms for backtesting and live trading without tweaking the code much or at all.
Please note some continuous contracts will have discontinuities in live trading! This is due to how the contracts are mapped and not something we have control over. If you're using them for technical indicators you should ideally
- Bind to the symbol changed event
- Reset your indicators
- Re-seed your indicators from a history call.
The history calls should be smooth.
Happy Trading!
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.
Ariel Nechemia
Hi Jared!
Thanks for the great work and the resources! Is there any way you could give us an example of what the code could look like resetting the indicators given the change in contract? I've found this template but I'm struggling to implement it with my code for continuous futures. It would be great to see an example, perhaps using the crossovers example posted in the github above.
Cheers!
Johnny Cash
Hi,
Great work!
Can you please post a link to the github source code of the continuous future calculation implementation? I tried to look at it here , but i cannot see where is splicing performed.
Thanks
Varad Kabade
Hi Johnny Cash,
We recommend going through the different files changed after integrating this issue to find what you are looking for. Refer to the following link.
Best,
Varad Kabade
Johnny Cash
Hi,
I have created a simple notebook to check carefully the attributes of a Future object:
to my surprise the margin model does not have any margins set up. The only other attribute which seem to be relevant is the Leverage, which is set to 1 (strange..it should be much higher for futures), and at least in the notebook, I cannot modify it. I have also checked how contract multipliers are handled, and they are ok. One way to backtest future would be to use some indicative leverage (10 or 20 lets say) and ignore the contract specific margins. In any case the contract specific margins seem not to be available, and leverage cannot be changed from 1.
Are future strategies being properly margined? If yes, where is the margin data?
I have also checked several strategy library future algorithms using custom data and they do not set up the contractMultiplier attribute, so I doubt the results are correct. At least the native future QC data has the multiplier correctly set up.
PS
How can notebooks be share directly instead of copying the code as done above? Below I can only see the back-test sharing option.
Martin Molinero
Hi all!
Big Chai
-What would be the rolling method when the current contract which we have bought expires? Does buying the _currentContact roll the futures automatically as they expire?
The positions are currently not being rolled automatically, you will have to handle this manually. Please take a look at this basic template algorithm it's rolling the position.
Ariel Nechemia
-Is there any way you could give us an example of what the code could look like resetting the indicators given the change in contract?
Please take a look at this basic template algorithm it's rolling the position, resetting the indicators should be done in a similar pattern
Johnny Cash
-Can you please post a link to the github source code of the continuous future calculation implementation?
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.
-Are future strategies being properly margined? If yes, where is the margin data?
Future margins are in the data folder, see for example cme. They require at least one data point to be able to determine which date is it see implementation that's why you are not seeing it being loaded on research.
-I have also checked several strategy library future algorithms using custom data and they do not set up the contractMultiplier attribute, so I doubt the results are correct.
Contract multiplier is driven by the symbol properties data base, see spdb. Custom data will need specifying the symbol properties manually with the desired settings.
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.
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!