Back

Simple Moving Average (algorithm.SMA) calculation vs security.Price?

I have a couple questions after looking into security prices vs the SMA returned for a set of security...

I am logging the price for a given security by accessing 'security.Price' inside of an alpha. The first number is the price returned, and the second is the SMA with a window of 5 days.

2020-07-20 09:31:00 : UPRO: 50.07, 49.112
2020-07-21 09:31:00 : UPRO: 52.17, 49.984
2020-07-22 09:31:00 : UPRO: 51.42, 50.552
2020-07-23 09:31:00 : UPRO: 52.25, 51.018
2020-07-24 09:31:00 : UPRO: 49.59, 51.208

1) Are the prices returned by 'security.Price' the closing price for the day? Opening? I couldn't find them matching up perfectly with daily charts I was looking at from other sources.

2) How is the SMA calculated? It was my understanding that the simple moving average is the sum of the prices in the lookback window divided by the lookback length. In this case (50.07 + 52.17 + 51.42 + 52.25 + 49.59) / 5 would give me the 5 day SMA right? This equals $51.10, and this doesn't seem to align with the 51.208 displayed. Does the SMA calculation not use the same values that would be returned by 'security.Price'?

Note: I am using 'self.EnableAutomaticIndicatorWarmUp = True' this shouldn't have any affect on these values should it, as opposed to other warmup methods?

Thanks for any tips or info you have!

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.


Additional point I found interesting, when setting the window size to 1 day, shouldn't the price and the SMA for 1 day be identical? This is the case in mutliple charting websites I've tried, and makes sense. But for some reason here the values are different.

 

2020-07-20 09:31:00 : UPRO: 50.07, 50.06
2020-07-21 09:31:00 : UPRO: 52.17, 51.28
2020-07-22 09:31:00 : UPRO: 51.42, 51.62
2020-07-23 09:31:00 : UPRO: 52.25, 52.48
2020-07-24 09:31:00 : UPRO: 49.59, 50.6

 

0


Hi Taylor,

The reason the SMA values seem incorrect is because the algorithm compares the SMA of daily prices to the price of minute data. Another issue is that the algorithm faces is that it makes a new SMA object with each call to Update. I recommend reviewing this dicussion thread to resolve these issues. That thread uses the RelativeStrengthIndex indicator, but can be easily adjusted to use the SimpleMovingAverage.

Review our indicator documentation for more information.

Best,
Derek Melchin

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.


Hi Derek,
Thanks for the response, it looks like your link for `this discussion thread` is a 404 unfortunately!

 

0

Hi Derek,

The link is no longer 404'ing so I was able to see what you are talking about. I'm still a little confused, what is the purpose of the 'self.EnableAutomaticIndicatorWarmUp = True', does this not handle warm-up for me using the algorithm framework?

I can see what you are saying about this first minute data and doing the simple calculation doesn't make sense.... Does that mean that the provided SMA and setting the resolution to Daily uses the closing minute, and then I might see something more similar?

Are there examples of these consolidators being used in the Algorithm Framework?

0

Hi Taylor,

self.EnableAutomaticIndicatorWarmUp = True

warms up indicators automatically that are added using the QCAlgorithms shortcut methods (`self.SMA` for example). See the attached backtest and logs for reference.

Using the code line above does not automatically warm up indicators if we construct them using the indicator class (`SimpleMovingAverage` for example). And it is this class that I recommend to use in this situation.

The reason it only works with the QCAlgorithm shortcut methods is because these methods check the `EnableAutomaticIndicatorWarmUp` property while the constructors of the indicators do not. Compare the source codes here and here.

 

Does that mean that the provided SMA and setting the resolution to Daily uses the closing minute, and then I might see something more similar?

Yes, that is correct.

 

Are there examples of these consolidators being used in the Algorithm Framework?

See the discussion thread I linked to in my earlier post. It uses consolidators in the Algorithm Framework.

Best,
Derek Melchin

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.


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