Hello All!
Yesterday we presented a webinar for Interactive Brokers on using Mean Variance Optimization for ETF Portfolio Rebalancing. We implemented it in the Algorithm Framework -- a new way we're offering for users to design algorithms which is more modular and extensible.
The Framework was built using the most common abstractions we see in the community. It has 5 modules - Universe Selection, Alpha Creation, Portfolio Construction, Execution and Risk Management. If you're interested I'd recommend checking it out as a way to improve your algorithm foo!
The key module we discuss here is the Mean Variance Portfolio Construction Model. This takes the historical returns of the assets presented by the Historical Returns Alpha Model and uses them to construct the portfolio! Its fascinating stuff on the cutting edge of Quant finance so I'd highly recommend learning the new framework.
Check out the video and slides below!
Jared Broad
Here are the backtest and notebook for the webinar
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.
Quant Trader
Interesting stuff!
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.
Serge d'Adesky
Kudos, guys, QC just took a major step forward!
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
Thanks Serge :) We're very excited by its potential. Still working out last couple of API changes but it'll be polished this week.
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.
Roberto M
Hi, thanks for sharing. This opens quite new interesting perspectives. I have one issue tough. Yesterday I cloned the algo and run it ok. Today I am trying to run it and I am getting and error - see below. I cloned it again, but same problem. Any idea?
Runtime Error: TypeError : no constructor matches given arguments at Update in HistoricalReturnsAlphaModel.py:line 55 TypeError : no constructor matches given argumentsThe 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
Apologies Roberto M - we've been heavily editing the API in some last minute polishing. We standardized the Insight() creation API between the method overloads -- and renamed the SetPortfolioSelection -> SetUniverseSelection so its not confused with SetPortfolioConstruction methods.
We just merged a bunch of demonstrations in Python and C#. More documentation to follow! See the updated building backtest -
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.
Mochunhei
I think this is amazing!! I really like the idea of modularization!!
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.
Mochunhei
Jared, which language do you suggest us to work with this framework?
I see that on GitHub, some modules are only available for C# (e.g. StandardDeviationExecutionModel.cs), some modules are only available for Python (e.g. MeanVarianceOptimizationPortfolioConstructionModel.py), some modules are available for both.
Will all modules have both versions eventually? Which language will get more updates? Thanks.
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.
Alexandre Catarino
We aim to have versions for both languages as examples like we have for algorithms.
Some modules, for example Universe Selection, may only have a C# version, because users can plug their code (the universe selection method) into them.
The MVO-PC model are only available in python because it uses some scipy magic. :-) In this case, a brave C# quant will probably make a C# version using MathNet (please checkout PortfolioOptimizationNumericsAlgorithm).
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
You can actually use all the C# modules in Python; C#-users can't use the Python ones =).
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.
Mochunhei
I didn't think about that, how do you do that?
Will you give us an example? Or maybe describe it in the docs? Thanks.
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.
Eric Summers
Wow! Great idea. Can't wait to get into it
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.
Jonathon Rogane
I seem to be having trouble implementing a fundamental universe selection into this. there doesnt seem to be any documents on this.
Thanks
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.
Lucas Silva
Cloned and it gives error:
5 | 04:30:19:
Successfully sent backtest request for 'Jumping Brown Buffalo', (Compile Id: 4f6d6fbcded5cedf1bea2a4a7e53d826-5b9f35497d14d48d10be6379c60792c6)
6 | 04:30:34:
During the algorithm initialization, the following exception has occurred: AttributeError : 'MeanVarianceOptimizationAlgorithm' object has no attribute 'SetPortfolioSelection'
at Initialize in main.py:line 37
AttributeError : 'MeanVarianceOptimizationAlgorithm' object has no attribute 'SetPortfolioSelection'
7 | 04:30:44:
Your log was successfully created and can be retrieved from: https://www.quantconnect.com/backtest/5915/1480838/efef14a303b7d288b971607a130ed4e2-log.txt
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
Sorry Lucas this was renamed to "SetUniverseSelection"
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.
Lexx7
Still doesn't work after changing that
Runtime Error: TypeError : no constructor matches given arguments at Update in HistoricalReturnsAlphaModel.py:line 92 TypeError : no constructor matches given argumentsThe 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.
Lexx7
Nevermind.... cloned the wrong one.
I've read through the docs, where do you set things like:
- Rebalance frequency
- Number of assets to hold at any time
- Lookback period for estimation
I assume these would be variables in the framework?
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.
Gurumeher Sawhney
The number of assets to hold at anytime is determined during the Initialize() handler, based upon the universe selection and portfolio construction. The rebalancing frequency is dependent on OnSecuritiesChanged(), and will maintain the portfolio target that was initialized. Refer to the first link to see how these variables work. Some portfolios (i.e Black-Litterman) use lookback period, and that can be initialized in the constructor. The second link below shows Black-Litterman's implementation.
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
Lexx7 those are all characteristics of your alpha model.
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.
Lexx7
Ok so I have tried playing around with the parameters. I set the below
self.SetPortfolioConstruction(MeanVarianceOptimizationPortfolioConstructionModel(lookback=180, period=20, minimum_weight=0, maximum_weight=0.99))
Questions
1. How do I set it to rebalance monthly? This is not clear to me & I can't find this in any of the algorithm framework examples.
2. What is the difference between lookback & period? One should be the lookback for returns & the other for volatility but it is not clear to me based on the descriptions provided in the code.
Errors
Why does it fail to make fills & crash if I try run the attached algo over a longer timeframe? What decides when it goes into margin?
Trying to emulate the attached example as a sanity check:
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.
Andrew Czeizler
Hi all!
I am also getting errors when trying to backtest this algorithm.
Best,
Andrew
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.
Jing Wu
andrew_czeizler, this is an old post. Now we've already put "HistoricalReturnsAlphaModel" and "MeanVarianceOptimizationPortfolioConstructionModel" into the LEAN framework library. Please see the attached algorithm.
You'll find the model source code on Github
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.
Chadwick Adebiyi
I cloned the algo from above, and I went to run the backtest and I got a bunch of insufficient buying power errors. Any idea as to why this is happening? Below is an example of the errors I am seeing.
Backtest Handled Error: Order Error: id: 101, Insufficient buying power to complete order (Value:8129.9063), Reason: Id: 101, Initial Margin: 4065.9531329395, Free Margin: 1253.9592675246086301998343
Backtest Handled Error: Unable to compute order quantity of DBC. Reason: The portfolio does not have enough margin available.. Returning null.
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.
Jack Simonson
Lexx7,
To answer your questions in order:
1) The portfolio rebalances daily as it is, so if you only want to trade monthly, then you can add logic in the Alpha Model so that it only sends out signals when you want it to, i.e. in monthly intervals. You can see the backtest I've attached for an example in the HistoricalReturnsAlphaModel.py file.
2) Period refers to the number of periods (dependent on the data resolution) for which we want to examine prices, whereas lookback is the number of periods for which we want to examine returns. If you subscribe to daily data and ask the algorithm to rebalance the portfolio every day, it will try to look back for n-days of returns and use an n-day period of prices to find the optimal portfolio. Since you're subscribed to daily-data, this will result in an equal-weighted portfolio every time, and so the resolution of the data universe needs to be higher than that of the HistoricalReturnsAlphaModel.
3) Margin requirements get used up when the algorithm tries to take any position and so orders that require more capital than available will be canceled. You can find more information about margin accounts and other account types here.
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.
Alexandre Catarino
Hi all,
We have improved the base PortfolioConstructionModel and the algorithm shared by Jack is out-of-date.
Here is an updated version:
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!