Hi all,
I have been working on an implementation of the continuous futures prices for a universe of futures and in the process have found myself using "dirty hacks" so thought the community might help to achieve a better design.
My current implementation is the following (also in the attached backtest):
- Declare universe and store top-level contract names in a dictionary
- Inside OnData for each symbol add Daily consolidator for N most popular contracts, as advised here
- Inside DailyHandler, I create a pandas' Series object that contains Symbol, Close Price, Open Interest and Date and then append it to the `db` property of the algo class.
- A function scheduled to run daily then iterates through unique symbols in the `db` and for each symbol it creates a continuous price series.
The particular methodology is not that important because at this step one can use any method. In this case, I use Perpetual Series based on Open Interest as described here. For each symbol I contruct 2 pivot tables, the first one with Open Interest (normalized to 1 for each row, as it is used as weights) and the second one with Close Price. Then I take element-wise product of the 2 tables and sum columns to get a Series of weighted average prices.
All individual price series are then concatenated into a single data frame and written into algo's property `continuous_future`. - Now `self.continuous_future` can be used for longer term technical analysis.
There are several issues with that implementation:
- Price data is stored outside the `Slice` object, and not linked to a Symbol. This means that QC's rich toolkit, that includes Charting, Indicators and Insights cannot be used as it always requires Symbol object. I wonder if I create symbol manually through Symbol.Create method, can I then update data associated with this symbol, so that it is reflected in Slice later?
- This method generally is extremely slow. Not least because it uses pandas but I can't think of a cleaner way of creating the continuation logic without it. So any suggestion on how to speed it up would be great.
- Any other feedback is much appreciated.
Jared Broad
Hey Aram, just a note we're actively working on it. We had a prototype up this week trading on the underlying contracts. I'm guessing mid-December we should have a production version.
You can subscribe to the issue here: https://github.com/QuantConnect/Lean/issues/1650
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.
Aram Darakchian
Hey Jared, thank you for your quick response. Very good! Then I will stick to this crude implementation for the time being and then switch to using the framework when it is ready.
Zicai Feng
using some of Aram's logic, here is my attempt at building n collecting continuous futures. Major difference is I am collecting minute data, as opposed to daily (ie consolidator being updated minutely), hence to achieve better speed, some of the functions that look for rolling over the contract are called daily instead of minutely. Also I use “ratio” method to adjust previous data, as opposed a weighted price method. (if I understand Aram's codes correctly)
It would be great if someone can share their tried and tested methods for building continuous futures as I have not done much sanity check on my logic (eg is premium/discount vs underlying stable over time? ideally one should also use some average “ratio” to arrive at the final “ratio” as supposed a single point in time). Also to be honest, appending Pandas dataframe is slow. If you want minute data for years of backtest, should consider other objects to save the data.
On a similar note, here are some pro and con for demanding continuous futures from QC:
Pro:
Con: (ie maybe we don't need continuous data as desperately as we think)
Eugene
The mentioned issue was closed today:
https://github.com/QuantConnect/Lean/issues/1650
How can it be used? Is there an Doc update?
Thank you.
Spacetime
Aram Darakchian
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!