Back

Passing variables through Algorithm Framework

Can someone show me best way to pass variables from my "alpha" model to my "exec" model

I was taking the approach of 

in main class  __init__  file

declaring the variable with a default value

self.spread = None

then in the Update function of my Alpha model , which has a reference to the calling file as "algorithm",  setting this

variable as such

def Update(self, algorithm, data):
for security in algorithm.Securities:
if self.DataEventOccured(data, security.Key):
insights = []
return insights
algorithm.spread = self.pair[1].Price - self.pair[0].Price

  When I use the debugger code to iterate through the program , it recognizes "algorithm.spread" as the correct value of the difference of these two pairs, but when it has completed the AlphaModel and I try to reference the variable using "self.spread" I go back to the intially assined default "None" value. 

 

What should I be doing differently?

 

Thanks

 

 

 

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.


Once you "return insights" your algorithm is done with the update function and it doesn't go on to the next line. This also means when you are iterating through your securities it stops after the first iteration. See code below for how it probably should look like.

def Update(self, algorithm, data):
insights = []

for security in algorithm.Securities:
if self.DataEventOccured(data, security.Key):
algorithm.spread = self.pair[1].Price - self.pair[0].Price


return insights

 

This code still won't work though because algorithm.spread is a single variable that will be overwritten each security in your loop, you'd need it to be a dictionary keyed by security for it to work the way you want. For example:

algorithm.spread = {}

for pair in algorithm.pairs:

    algorithm.spread[pair] = pair[1].Price - pair[0].Price

 

you'd need to have pair be a list with two securities in it and spread to be a dictionary keyed by the lists

 

0

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