Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem with moving averages calculation/slicing technique

Hello all,

I have been using a certain code to calculate the current and previous simple moving averages of a specific timeframe...

Ex:

history = data.history(context.securities,'close',11,'1d')
sma10 = history[-10:].mean()
sma10prev = history[-11:-1].mean()

When I log the results, the sma10 seems to calculate and fluctuate, as expected, but the sma10prev stays on static value and never fluctuates.

I could have sworn I've run into this before and this method was working, but I don't know what I'm doing wrong now. If anyone can help...?

Thanks!!

Danny

2 responses

Your code looks like it should work fine to me. I threw it into an algo and logged/recorded it and it looks right.

One thing to note is that the sma10prev will be a small bit different from yesterday's sma10 because the sma10 is calculated using the most recent minute's close price, and sma10prev is using the actual close.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Dan, Daniel,

This test shows that Quantopian calculations of sma10 on the same bar are perfectly match if calculated the right way.

# last_bar -> -1  
# prev_bar -> -2

sma10 = history[-10:].mean()  # -> 10 bar MA today (right)

sma10prev = history[-11:-1].mean()  # -> 11 bar MA today  (wrong)

sma10prev = history[-11:-2].mean()  # -> 10 bar MA yest (right)  

2016-12-01 12:59 record_ma:14 INFO sma10: 111.117777778 sma10prev: 111.061111111
2016-12-02 12:59 record_ma:14 INFO sma10: 111.064444444 sma10prev: 111.117777778
2016-12-05 12:59 record_ma:14 INFO sma10: 111.045555556 sma10prev: 111.064444444
2016-12-06 12:59 record_ma:14 INFO sma10: 110.754444444 sma10prev: 111.045555556
2016-12-07 12:59 record_ma:14 INFO sma10: 110.552222222 sma10prev: 110.754444444
2016-12-08 12:59 record_ma:14 INFO sma10: 110.53 sma10prev: 110.552222222
2016-12-09 12:59 record_ma:14 INFO sma10: 110.566666667 sma10prev: 110.53
2016-12-12 12:59 record_ma:14 INFO sma10: 110.832222222 sma10prev: 110.566666667
2016-12-13 12:59 record_ma:14 INFO sma10: 111.036666667 sma10prev: 110.832222222
2016-12-14 12:59 record_ma:14 INFO sma10: 111.555555556 sma10prev: 111.036666667
2016-12-15 12:59 record_ma:14 INFO sma10: 112.192222222 sma10prev: 111.555555556
2016-12-16 12:59 record_ma:14 INFO sma10: 112.851111111 sma10prev: 112.192222222
2016-12-19 12:59 record_ma:14 INFO sma10: 113.613666667 sma10prev: 112.851111111
2016-12-20 12:59 record_ma:14 INFO sma10: 114.357 sma10prev: 113.613666667
2016-12-21 12:59 record_ma:14 INFO sma10: 115.013666667 sma10prev: 114.357
2016-12-22 12:59 record_ma:14 INFO sma10: 115.563666667 sma10prev: 115.013666667