Hi all, I am new to QuantConnect and now learning the algorithmic framework by reading docs and source code. I have found some places in the implementation of MeanVarianceOptimizationPortfolioConstructionModel that do not make sense to me. But I am unsure whether those are real issues or just my misunderstanding.
- Within the OnSecuritiesChanged method, when a security is added, it initializes the symbolData of the security using its historical data. But I do not understand why traling period is self.lookback * self.period instead of self.lookback + self.period. Although they should result in the same symbolData, the latter is more efficient when self.lookback is greater than 1.
def OnSecuritiesChanged(self, algorithm, changes):
'''Event fired each time the we add/remove securities from the data feed
Args:
algorithm: The algorithm instance that experienced the change in securities
changes: The security additions and removals from the algorithm'''
# clean up data for removed securities
super().OnSecuritiesChanged(algorithm, changes)
for removed in changes.RemovedSecurities:
symbolData = self.symbolDataBySymbol.pop(removed.Symbol, None)
symbolData.Reset()
# initialize data for added securities
symbols = [x.Symbol for x in changes.AddedSecurities]
for symbol in [x for x in symbols if x not in self.symbolDataBySymbol]:
self.symbolDataBySymbol[symbol] = self.MeanVarianceSymbolData(symbol, self.lookback, self.period)
history = algorithm.History[TradeBar](symbols, self.lookback * self.period, self.resolution)
for bars in history:
for symbol, bar in bars.items():
symbolData = self.symbolDataBySymbol.get(symbol).Update(bar.EndTime, bar.Value)
2. If I understand correctly, the current implementation estimates the expected return and covariance using historical forecasts from insights. However, in my view, forecasts of alpha models are usually very noisy, which makes the estimates inaccurate. In fact, since we have have historical returns available, why not just use them for estimation? For example, it makes more sense to me if we use actual historical returns to estimate covariance, and use current forecasts as the expected return.
Mia Alissi
Hi Haochuan,
OnSecuritiesChanged
method is used to handle the event of securities being added or removed from the data feed. When a security is added, it initializes thesymbolData
of the security using its historical data. The trailing period is set toself.lookback * self.period
instead ofself.lookback + self.period
because it's designed to capture a longer historical period for analysis. This is a design choice and may not necessarily be the most efficient in all scenarios. It's worth noting that you can modify this to suit your specific needs.Remember, the Algorithm Framework is designed to be flexible and customizable, so feel free to adjust it to better suit your needs.
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.
Haochuan Li
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!