Hi folks,
I created a sample algo to get familiar with the QC Framework. When running the algo I a get a null value for the Insight..Magnitude attribute. The error and stack trace are shown below. Since the code came from standard classes that are part of the API I am not sure how to fix this issue. Here is the error message and stack trace.
Runtime Error: MeanVarianceOptimizationPortfolioConstructionModel does not accept 'null' as Insight.Magnitude. Please checkout the selected Alpha Model specifications: MacdAlphaModel(12,26,9,Simple,Daily)
Parameter name: ZIONSystem.ArgumentNullException: MeanVarianceOptimizationPortfolioConstructionModel does not accept 'null' as Insight.Magnitude. Please checkout the selected Alpha Model specifications: MacdAlphaModel(12,26,9,Simple,Daily)
Parameter name: ZION
I have also attached the code here. There are two questions that I have.
1. Would someone know why the Magnitude value is null and more importantly how to fix this?
2. The wizard never populated any code in the "OnData" method. So I don't even understand how this code can run as the documentation does not provide any hint how the execution happens?
from Alphas.MacdAlphaModel import MacdAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from Portfolio.EqualWeightingPortfolioConstructionModel import EqualWeightingPortfolioConstructionModel
class UncoupledHorizontalCoil(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 1, 1) # Set Start Date
self.SetEndDate(2019, 1, 20) # Set End Date
self.SetCash(100000) # Set Strategy Cash
self.SetUniverseSelection(QC500UniverseSelectionModel(filterFineData = True))
self.SetExecution(ImmediateExecutionModel())
self.SetPortfolioConstruction(MeanVarianceOptimizationPortfolioConstructionModel())
self.SetRiskManagement(TrailingStopRiskManagementModel(0.08))
self.AddAlpha(MacdAlphaModel(12, 26, 9, MovingAverageType.Simple, Resolution.Daily))
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
Thanks in advance for your help. Cheers,
Mitch
Rahul Chowdhury
Hey Mitch,
1. The insight magnitude is null because when MacdAlphaModel emits insights with Insight.Price, it emits null Insight magnitudes. Normally this isn't a problem, but here it is because Mean Variance Portfolio Construction needs insight magnitude data to construct a portfolio. To overcome this, you need to override the default insight creation in the MacdAlphaModel class.
To do this you have two options:
a. you can create a new class which extends MacdAlphaModel
and override the Update method
b. you can create a new class and copy the code from the MacdAlphaModel
class, and then change the code accordingly.You can learn more about creating insights here.
2. When using the Algorithm framework, all the data and trade executions are handled through the framework. You can learn about the algorithm framework here.
Best
Rahul
Mitch Christow
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!