Hi all,
I've been trying to build an alpha model using an sklearn ML model, and I'm wondering about the best way to do so. In order to make the model itself accessible from the algorithm's Train() function, both the model and a related max training price dictionary are attributes of the algorithm object. When I try to access this dictionary from the alpha model as below, however, I get the error ‘QCAlgorithm object has no attribute “max_train_price”’:
# within the alpha model
def Update(self, algorithm, data):
insights = []
for security in algorithm.ActiveSecurities.Values:
symbol = security.Symbol
if data.ContainsKey(symbol) and data[symbol] is not None:
# this is the issue: algorithm.max_train_price[symbol] throws an error
if (data[symbol].Close < algorithm.max_train_price[symbol]
and data[symbol].Close > algorithm.min_train_price[symbol]):
# do other stuff
Am I structuring things wrong? It seems like any ML models and associated objects have to be a part of the algorithm object, since they need to be accessible by the Train() function, but it also seems like attributes of the algorithm aren't accessible from the alpha model (unless I messed something else up).
It's worth mentioning that I do define max_train_price in the algorithm's Initialize() function, before I add the alpha model, so it's not that my algorithm object doesn't actually have that attribute.
I appreciate any feedback! Let me know if this is too vague or if there's anything I can clarify.
Louis Szeto
Hi Jonathan
Yes it is a class structure framework issue so it cannot be accessed directly. The class QCAlgorithm is not the same from your backtest algo class so it is not cross-accessible. I'm not sure the solution, but I would suggest to try to define your ML method as a function in the AlphaModel rather than in main.py basic class if the computational work is not heavy so whole round can be done under 10 minutes limit.
Alternative: I haven't try this method in below, but as an idea you could think of. In your main.py, make your ML as a class,
in main.py:
then in your alpha model .py file:
CheersLouis
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.
Derek Melchin
Hi Jonathan,
We recommend reviewing the Gradient Boosting Model tutorial for an example of using the `Train` method inside an alpha model.
Best,
Derek Melchin
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.
Jonathan Stokely
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!