Back

Portfolio Optimization with Math.NET Numerics

In this example project, we use Math.NET Numerics library to optimize a fictitious 4-stock portfolio with known mean returns, standard deviations and correlation matrix.
In order to compute the optimal weights for a given target return and risk-free rate, we use linear algebra operations made possible with Numerics Linear Algebra module.

Update 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.



Now with the right backtest! :)

0

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.


In this version, we are using real data and Math.NET Numerics Statistics to compute correlation coefficient.

0

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.


I looked up each one individually, IBM tanked that period -- but the rest did well. Why do you think the whole portfolio did so poorly? Perhaps there's a bug in the weighting? That last share performed worse than any of the individual constituents... Even IBM tanking was only -20%

What happens when you rebalance each month?

0

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.


Denitely, it did poorly because of the stock picking method (there was none, I just used some of the available symbols at GitHub/Lean/Data). I am pretty confident there is no bug in the weighting, since I checked it against a theorical exercise.
We could use this algorithm is two different ways:

  1. We pick stocks with a long term outlook: calculate the covariance with a bigger lookback period (~ 10 years?) and rebalance monthly the portfolio with the constant weights. 
  2. We pick stocks with a short term outlook: rebalance monthly with weights we calculate with rolling covariance with a shorter lookback (< 1 year).

I will be go after #2, using Meb Faber GTAA stock piking method.

0

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.


In this implementation, we use Meb Faber GTAA assets and rebalance monthly with weights that are recalculated monthly. Like Faber, we use a lookback period of 200 trading days.

The strategy is now profitable, but cannot beat the benchmark.

0

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, 

Very interesting stuff.  Thanks for sharing.  

What if, instead of starting with a fixed list of instruments, you use a similar approach to select the instruments for your algorithm from a larger universe of instruments?

I've been thinking a lot about the different ways to implement a similar approach, but one that uses dynamic filtering to select and modify the current portfolio based on strength, correlation of instruments and volatility of instruments and portfolio.  In effect you would have a fixed 'parking lot' of X instruments from a possible universe of Y instruments. 

In theory this type of approach sounds appealing to me, but there are a lot of moving parts. Here's a brief summary of my thinking on some different ways to approach to this. I would be curious to hear your and the community's thoughts on implementing similar approaches.

Risk Management 

  • Total Risk Budget: X%
  • Maximum Initial Risk Per Trade: X%
  • Maximum Open risk Per Trade: X%

Correlation

  • If X% of positions have correlation coefficient > Y to each other, close position and take next new signal  OR

Volatility

  • If X day ATR increases by X% over last Y days, reduce/liquidate position  
  • If X day ATR for increases by X% over last Y days, reduce positions

Dynamic Portfolio Selection 

Ranking

  • Add/Remove/Reduce/Replace positions if
    • Oldest position has been open for X periods or
    • Ranking falls below some threshold

 

·        

 

0

Update Backtest





0

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.


Loading...

This discussion is closed