Back

Simple Moving Average Filter

I am trying to create a simple moving average filter. I use a for loop to compute the percent difference of 3d and 45d sma for each stock symbol in the coarse universe. Then I sorted the dictionary based on the value and returned a list of the bottom 100 stocks.

For some reason, the sma().current.value gives me a "0". I read some docs and posts related to this but haven't figured out how to some this problem. Basically, I'm trying to create a sma filter based on the current universe instead of a single stock. I would be apprecieted if someone can help me out.

Update Backtest








Does an Indicator need to spool up or can it be initialized and then immediately used(assuming help from History)? Most examples show Indicator usage with a set universe via initialize. Does this example give any ideas for a way to achieve your goals with universe selection?

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/EmaCrossUniverseSelectionAlgorithm.py
1

Like Derek Tishler has suggested, please take a look at the EmaCrossUniverseSelectionAlgorithm, since it does what you are trying to.

SMA is a helper method that created an instance of SimpleMovingAverage and updates it with price data under the hood. Such methods should only be called once, otherwise Current.Value will gives you 0 (no updates were made).
So you would need to create an variable with the constructor:
self.sma = SimpleMovingAverage(45)

and update it manually:
self.sma.Update(self.Time, value)
where value is any number.

This is what you will see in the SymbolData class in example above.

1

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.


Thank you so much Alex and Derek for your quick response. I read the EmaCrossUniverseSelectionAlgorithm and it is very helpful. I have one more question about this ema algorithm: I tried to ran it in backtest, and I found it would always trade SPY first, and then after 2 month, it starts trading normally. Is it becasue it takes time to warm up? I tried SetWarmUp but it didn't work.

1

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