Trying to access a property on the algorithm class from the alpha class in the Update method, but its saying that the property doesn't exist. I've tried using the @property along with setters and getters but when I call the getter in the Alpha, an error is thrown saying that no method exists on the algorithm.
How would I go about doing this?
Sample Code:
class BasicTemplateFrameworkAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2001, 12, 1) # Set Start Date
self.SetEndDate(2020,5,1)
self.SetCash(100000) # Set Strategy Cash
self.AddAlpha(CustomAlphaModel())
self.Locked = False
@property
def IsLocked(self):
return self.Locked
def Lock(self):
self.Locked = True
def Unlock(self):
self.Locked = False
class CustomAlphaModel(AlphaModel):
def __init__(self):
self.Name = '{}'.format(self.__class__.__name__)
def Update(self, algorithm, data):
# Throws Error: 'QCAlgorithm' object has no attribute 'IsLocked'
algorithm.Debug("Algo Locked: {} ".format(algorithm.IsLocked))
Derek Melchin
Hi John,
The algorithm parameter in Update does not refer to the instance of this algorithm but the base class QCAlgorithm. To access custom properties and methods of the algorithm class from inside our alpha model, we need to save an instance of the algorithm inside the alpha model constructor.
class CustomAlphaModel(AlphaModel): def __init__(self, algorithm): self.algo = algorithm def Update(self, algorithm, data): algorithm.Debug("Algo Locked Property: {} ".format(self.algo.Locked)) return []
See the attached backtest for a full example of this.
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.
John Radosta
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!