Hi All, I'm trying to figure out how to update an insight inside an AlphaModel using knowledge about the Portfolio's actions.
Normally, I'd do the following inside a `QCAlgorithm` object in the `OnData` method.
if self.Portfolio.Invested:
self.SetHoldings(symbol, 1)
if not self.Portfolio.Invested:
self.Liquidate(symbol)
My questions is how can I do that inside a `AlphaModel`?
Can I access the Portfolio object?
What I'm doing here is inside `Update` method:
```
if condition:
insights.append(Insight.Price(symbol, self.insightPeriod, InsightDirection.Up))
else:
insights.append(Insight.Price(symbol, self.insightPeriod, InsightDirection.Down))
```
But my conditions rely on me knowing the state of my Portfolio. how can I access it?
Thank you for your help!
Derek Melchin
Hi Yaz Khoury,
The algorithm's portfolio can be accessed anywhere a pointer to the algorithm is available. Within OnData, that is the keyword `self' because OnData resides within QCAlgorithm. When the Update method is called for an alpha model, the method receives a reference to the algorithm object. Thus, inside Update, we can access the portfolio object through this reference. Here’s a quick example:
def Update(self, algorithm, data): if algorithm.Portfolio.Invested: return [] else: return [Insight.Price("SPY", timedelta(days=10), InsightDirection.Up)]
See the attached backtest for a sample algorithm which fully implements this functionality. However, we don't recommend using `Portfolio[symbol].Invested` because of separation of concerns in the framework. Ideally, insights should not be influenced by the portfolio since the alpha model can be used with different portfolio construction and/or risk models. Our RsiAlphaModel demonstrates the process of saving states within an alpha model, so the model doesn't need to consult the portfolio object at all inside Update. We recommend developing alpha models in this fashion.
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.
Yaz Khoury
Hi Derek Melchin thank you very much for you response! I was basing my framework for building my algorithm on the RSI model you posted, so I'm glad I'm on the right track. I see how the `algorithm` allows me to access the Portfolio and I get it shouldn't be accessed from the Alpha module for separation of concerns.
But, I'm sorta confused now on `Insights`.
How would I be able to take actions on Insights list inside the Algorithm Module that's being returned by `Update` from the AlphaModule?
Is there a recommended framework for doing this?
Ideally, I'd be able to access the insight list insight `OnData` and use it to take actions inside the Portfolio.
Thank you for your help, best wishes.
Derek Melchin
Hi Yaz,
When utilizing the Algorithm Framework, Insights are sent from the alpha model to the portfolio construction model to determine position sizing. In this sense, the processing logic for Insights is not inside OnData, it is in the portfolio construction model. I recommend reading our documentation on the Algorithm Framework and completing our Bootcamp lesson on the topic. They do a great job of explaining this process is detail.
Best,
Derek.
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.
Yaz Khoury
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!