1) if self.short_ma[3] == self.old_ema:
2) return
3)
4) self.old_ema = self.short_ma[3]
The aim of the code above is to ONLY run scripts below line 4 whenever a Moving Average gets updated.
self.short_ma is a dict{} of EMA's, where self.short_ma[3] is but one of the moving averages.
In Initialise() self.old_ema = None.
Then line 4 is reached updating self.old_ema to equal the current moving average value.
This is where it gets weird... On reaching line 1, whenever self.short_ma[3] gets updated the self.old_ma value is automatically updated too... without ever reaching line 4. So the code in line 4 is effectively getting executed on each pass even though it is only being reached ONCE, on the inital assignment.
Why is that? It's almost like they are pointing to the same place in memory so that whenever the EMA gets updated the self.old_ma is automatically receiving the new value (when I want it to store the old value).
Do I perhaps need to use a rolling window instead?
Adam W
Bit hard to tell from that snippet - but if the EMAs are objects, then both self.short_ma and self.old_ma would point to the same memory address of the object in Python.
class MyObj: def __init__(self): self.x = 1 a = MyObj() id(a) # > 2191885791128 b = a id(b) # > 2191885791128 a.x = 5 b.x # > 5
Assuming the EMAs are the built-in `Indicator.EMA` class...if you just want to compare the value, do
if self.short_ma[3].Current.Value == self.old_ema: return self.old_ema = self.short_ma[3].Current.Value # Assign the current value instead of the object
Derek Melchin
Hi Mark,
If the solution above doesn't solve the issue, attach a backtest for further assistance.
Best,
Derek Melchin
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.
Mark Reeve
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!