Hi,
Trying to figure out the framework algorithms, in particular this bit of code from the documentation:
# Define alpha model as a composite of the rsi and ema cross models
self.SetAlpha(CompositeAlphaModel([RsiAlphaModel(), EmaCrossAlphaModel()]))
class CompositeAlphaModel:
# Updates this alpha model with the latest data from the algorithm. This is
# called each time the algorithm receives data for subscribed securities.
# This method patches this call through the each of the wrapped models.
self.Update(algorithm, data):
return List<Insight>
# Event fired each time the we add/remove securities from the data feed. This
# method patches this call through the each of the wrapped models.
self.OnSecuritiesChanged(algorithm, changes):
pass
I couldn't find a CompositeAlphaModel.py on Github only CompositeAlphaModel.cs but trying to insert that code directly into my algorithm or import it with:
from QuantConnect.Algorithm.Framework.Alphas import CompositeAlphaModel
didn't work, both results gave the error message below:
During the algorithm initialization, the following exception has occurred: NotImplementedException : IAlphaModel.Update must be implemented. Please implement this missing method on <class 'list'>
at QuantConnect.Algorithm.Framework.Alphas.AlphaModelPythonWrapper..ctor (Python.Runtime.PyObject model) [0x00052] in <3f615569dfef474bba414f7d7810702f>:0
at QuantConnect.Algorithm.Framework.Alphas.CompositeAlphaModel..ctor (Python.Runtime.PyObject[] alphaModels) [0x0004d] in <3f615569dfef474bba414f7d7810702f>:0
at QuantConnect.Algorithm.Framework.Alphas.CompositeAlphaModel..ctor (Python.Runtime.PyObject alphaModel) [0x00000] in <3f615569dfef474bba414f7d7810702f>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <2e7c1c96edae44d496118948ca617c11>:0
at Initialize in main.py:line 46
NotImplementedException : IAlphaModel.Update must be implemented. Please implement this missing method on <class 'list'>
at QuantConnect.Algorithm.Framework.Alphas.AlphaModelPythonWrapper..ctor (Python.Runtime.PyObject model) [0x00052] in <3f615569dfef474bba414f7d7810702f>:0
at QuantConnect.Algorithm.Framework.Alphas.CompositeAlphaModel..ctor (Python.Runtime.PyObject[] alphaModels) [0x0004d] in <3f615569dfef474bba414f7d7810702f>:0
at QuantConnect.Algorithm.Framework.Alphas.CompositeAlphaModel..ctor (Python.Runtime.PyObject alphaModel) [0x00000] in <3f615569dfef474bba414f7d7810702f>:0
at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <2e7c1c96edae44d496118948ca617c11>:0
Also, secondary question once I get this part working. Based on this sentence in the documentation:
"Provides an implementation of QuantConnect.Algorithm.Framework.Alphas.IAlphaModel that combines multiple alpha models into a single alpha model and properly sets each insights 'SourceModel' property"
Does this mean that I can access which alpha model generated by looking at Insight.SourceModel? If so then what type of data does that return to me?
My goal here is that because I have multiple alphas generating insights on the same security I don't want them to overwrite eachother. It seems most the example code in the documentatino is been geared towards single alpha insights that are designed to overwrite previous insights as a new one appears. I'm having a hard time writing a custom portoflio construction model based on multiple alphas that are possibly firing on different timeframes. For example, if one alpha out of 3 gives a down insight on SPY that doesn't mean I want my portfolio to sell all SPY.
Alexandre Catarino
We haven't write a python version of CompositeAlphaModel, because it is just a helper to combine multiple alphas.
If you use, this import statement:
from QuantConnect.Algorithm.Framework.Alphas import *
you should be able to use it.
Insight.SourceModel is a string with the name of the model. It can be user by your portfolio construction model to decide what to do based on the insight source.
Let me use your example:
First, we have to set up a Portfolio Construction Model (PCM) that consume multiple alphas. In this model, we have to implement a InsightCollection (see EqualWeightingPortfolioConstructionModel). Then assume we have three alpha models (A, B, C) that emit alphas at different periods for SPY.
Every data event, the PCM will be called and we will have to call the GetActiveInsights method:
# Get insight that haven't expired of each symbol that is still in the universe activeInsights = self.insightCollection.GetActiveInsights(algorithm.UtcTime)
to decide what to do with the active insights. For simplicity let's assume that the three models only emit insights for SPY and that in activeInsights we have three insights: one down and two up. At this point we have all elements to decide which insight or a combination of insights should be used to create the Portfolio Target.
For instance, if the down insight is from model A, we should use it. If the down insight is from model C, we should discard it, If the down insight if from model B, we should average the magnitude to decide the final direction, etc...
LukeI
Thanks this helps me understand a little better what the framework is doing under the hood.
One more question I had speaking of under the hood, collections seem like they can do a lot of heavy lifting in the background on your insights and targets based on the way the examples use them but I couldn't find any documentation on these two functions.
self.targetsCollection = PortfolioTargetCollection()
self.insightCollection = InsightCollection()
Alexandre Catarino
We have created those helper classes to manage collections of Insight and PortfolioTarget, since it can be used in every Portfolio Construction Model and Execution Model respectively.
Basically we add the insights and targets that arrive in the IAlphaModel.CreateTargets and IExecutionModel.Execute methods, so that we keep a history of all insigths and targets (until we remove it). Such collections where they are organized as dictionaries:
spy_insights = self.insightCollection['SPY'] # list of SPY insights spy_targets = self.portfolioTargetCollection['SPY'] # list of SPY portfolio targets
We have also added some methods to the collection that help us manage them (add, remove, get, etc).
Here is InsightCollection and the PortfolioTargetCollection code with documentation.
If you have any further question, please let us know.
We will add docs for those classes in the Algorithm Framework documentation too.
LukeI
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!