Back

How to pass variable through the framework

I am new to QC. After reading the BootCamp, I would like to try to develope strategies by following the framework. Here is my test case. I would like to pass a variable to Alpha Model from the QCAlgorithm class, is it possible?

Here is my test code:

class QuantityTesting_Insight(QCAlgorithm):
    isFirstOrder = True # variable I want to pass
    
    def Initialize(self):
        self.SetStartDate(2020, 1, 13)  # Set Start Date
        self.SetEndDate(2020, 1, 18)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        # user variables
        # self.isFirstOrder = True
        
        # Handle UniverseSettings
        self.UniverseSettings.Resolution = Resolution.Minute
        symbols = [Symbol.Create("SPY", SecurityType.Equity, Market.USA)]
        self.SetUniverseSelection(ManualUniverseSelectionModel(symbols))
        self.SetSecurityInitializer(lambda x: x.SetDataNormalizationMode(DataNormalizationMode.Raw))
        
        # Handle Alpha Model
        self.SetAlpha(TestingAlpha())
        
        # Handle Portfolio Model
        self.SetPortfolioConstruction(MyEqualWeightingPortfolioConstructionModel())
        self.SetExecution(ImmediateExecutionModel())
        self.SetRiskManagement(NullRiskManagementModel())

            
class TestingAlpha(AlphaModel):

    def __init__(self):
        pass
    
    # override
    def Update(self, algorithm, data):

        insights = []
        if not isinstance(algorithm, QuantityTesting_Insight):
            return insights
            
        algorithm.__class__ = QuantityTesting_Insight
        if not algorithm.isFirstOrder:
            return insights
return insights

I have a variable named isFirstOrder, which is a boolean. When I cast the class back to QunatityTesting_Insight, I get error message, `'QuantityTesting_Insight' object layout differs from 'QCAlgorithm'`

I would like to ask if there a way to do this action? Or I should use other method to pass variable instead of using QCAlgorithm class? Thank you very much.

Update Backtest







0

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.


There's a strong encouragement for Framework algorithms to follow the separation of concerns design principle and think of other ways to do it (for instance, defining isFirstOrder in the Alpha Model itself), but if you really need to:

class MyAlgo(QCAlgorithm):
def Initialize(self):
# Track instance of the Alpha Model
self.AlphaModel = MyAlphaModel()

self.SetAlpha(self.AlphaModel)

def OnData(self):
# Update the variable somewhere (or in a Scheduled Event)
self.AlphaModel.isFirstOrder = True

class MyAlphaModel(AlphaModel):
def __init__(self):
self.isFirstOrder = False

def Update(self, algorithm, data):
if self.isFirstOrder:
algorithm.Debug('Works.')

 

2

Update Backtest





0

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.


Loading...

This discussion is closed