Back to Community
Why are these two moving averages different?

Hi,

Please help explain why these two moving averages have different values?

  1. data[stock].mavg(10)
  2. np.mean(history(30, "1d", "price")[stock].tail(10))

Am I missing something very obvious? Kindly help.

6 responses

It looks like the

data[stock].mavg(10)  

calculation operates only on data in the data set. So the first 10 daily "ticks" in the backtest are not really mavg(10) calculations, but rather
mavg(1), mavg(2), mavg(3) ... calculations. At day 10, the two calculations converge and stay the same for the remainder of the backtest. The take-away is don't use "data" for any backward looking functions. Although I have seen such uses here and there in the sample algos in this forum.

They are almost the same, but not quite:

2011-02-15 handle_data:22 INFO 50.4306  
2011-02-15 handle_data:23 INFO 50.4306  
2011-02-15 PRINT not equal  
2011-02-15 PRINT 50.430599999999991  
2011-02-15 PRINT 50.430600000000005  
Clone Algorithm
3
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5436a87244138208f0f3ed4c
There was a runtime error.

Hi Jack,

I believe what you're seeing are standard floating point issues. But if you look at the logs for the first 10 days of the backtest, you'll see the number diverge much more than that for the reason I explained in my previous post.

Alan was on the right track, the data[stock].mavg(10) function will return the true moving average once the window is full.

Here is what is reported, using MAVG(3) as an example:

Day 1: reports the first day price -- mavg(1)  
Day 2: report the average of the 2 day price -- mavg(2)  
Day 3: report the average of the 3 day price. At the end of day 3, the moving average is now "filled" and will act as true value -- mavg(3)  
Day 4: Moving average of days 1-3 -- mavg(3)  
Day 5: Moving average of days 2-4 -- mavg(3)

The moving average function doesn't "warm up" in backtesting, but it does warm up in paper trading and live trading with Interactive Brokers. This means that if you deploy your algo to IB and your code says "mavg(3)" it will act as follows:

Day 1: Takes the past 3 days historical market price (days -2 to 0) -- mavg(3)  
Day 2: Takes the last day and two historical prices (days -1 to 1) -- mavg(3)  
Day 3: Takes the last two days and one historical price (days 0 to 2) -- mavg(3)  
Day 4: Takes the last three days of trading (days 1-3) -- mavg(3)  
Day 5: Takes the last three days of trading (days 2-4) -- mavg(3)  

The history function is smart enough to warm up and will begin returning the true moving average right away, on day 1. In backtesting, paper trading, and live trading it will pull the prior day's data.

In general, it's more robust to use history() for to get a trailing window of data and for transformations, such as mavg, vwap, and stdev.

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.

Thanks, Alisa. Good to know you have this accounted for where it counts.

Thanks Alan and Alisa.