Hello,
can I call an attribute initialized in the QCAlgorithmFramework class in other modules?
I initialize
class MyFrameworkAlgorithm(QCAlgorithmFramework):
def Initialize(self):
self.symbolDataBySymbol = {}
then I want to call this symbolDataBySymbol dictionary in Alpha and Execution.
When I do something like
class MyAlpha(AlphaModel):
def Update(self, algorithm, data):
symbolData = SymbolData()
algorithm.symbolDataBySymbol[symbol] = symbolData
class SymbolData:
'''Contains data specific to a symbol required by this model'''
I get
Runtime Error: AttributeError : 'QCAlgorithmFramework' object has no attribute 'symbolDataBySymbol' AttributeError : 'QCAlgorithmFramework' object has no attribute 'symbolDataBySymbol'
Why do I want to do that for now? When I get a Alpha signal, I want to determine the stop-order price, store it in SymbolData instance and use it in Execution module by calling algorithm.symbolDataBySymbol[symbol].enrty_price
I saw that in the github examples of Alpha and Execution models that SymbolData class approach is used. The examples are not complete algorithms but only showing the specific module. I figured it would be useful if I can use one SymbolData instance to be used all modules.
Best,
Atacan
Alexandre Catarino
An attribute defined in an python class cannot be used in framework models.
However, you can pass it as an argument:
class MyFrameworkAlgorithm(QCAlgorithmFramework): def Initialize(self): symbolDataBySymbol = {} self.SetAlpha(MyAlpha(symbolDataBySymbol)) self.SetExecution(MyExecution(symbolDataBySymbol)) class MyAlpha(AlphaModel): def __init__(self, symbolDataBySymbol): self.symbolDataBySymbol = symbolDataBySymbol def Update(self, algorithm, data): symbolData = SymbolData() self.symbolDataBySymbol[symbol] = symbolData class MyExecution(ExecutionModel): def __init__(self, symbolDataBySymbol): self.symbolDataBySymbol = symbolDataBySymbol def Execute(self, algorithm, targets): for target in targets: entry_price = self.symbolDataBySymbol[target.Symbol].entry_price
That being said, I would advice you to keep the concerns completely separated in the framework models,. In this case, your execution model is completely dependent of the AlphaModel and you may have a great execution model that cannot be used by other alphas.
Alternatively, we can get the entry price with algorithm.Securities[target.Symbol].Price when we have a new target(s), so there is no reason for defining this value in the AlphaModel.
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!