Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Back to Simplicity: Another way to calculate average

What I don't like about calculating average like data[stock].mavg(5) is that the information across the whole week gets equal weight - while recent ones should be more reliable to predict future values. Hence I used an alternate way to calculate average: add up mavg(i) for i in range, say (1,6). It can be argued that this average is closer to exponential average as the number of days gets big.

However I've been debugging for many hours without knowing why

for k in range(1,6):  
avg_Price[i] += data[stock].mavg(k)  

always return a runtime error - silly question but I eventually decided to use the more traditional way to write the entire expression out. Again, any suggestion are enthusiastically welcomed!

6 responses

Hello Taibo,

Unless something has changed, the mavg transform cannot be called with a variable; you need to specify a fixed number of days. To Quantopian Support, I suggest adding guidance to your help page, under the "Simple Transforms" section.

To code a weighted moving average, I suggest considering the batch transform and a numpy average with weights. If you write one, please post it here--I'd be interested in seeing your example.

Grant

I do feel the error message could be better, since this is going to be a fairly common "gotchya"

def initialize(context):  
    pass

def handle_data(context, data):  
    n = 3  
    f = data[sid(24)].mavg(n)  # "There was a runtime error"  

Grant is right. The various transforms are created at the beginning of a backtest, and if you try to change the size of the window part way through, it barfs.

I agree we need a better error. I'll work on that and the docs.

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.

@Grant: Thanks for your suggestions! Here's what I did using batch transform and numpy average. We can tweak the weights to make it closer to exponential average or any other kind of average.

Taibo,

Yes, that's the idea. I'll take a look at your code when I get the chance. Glad it worked out.

Grant

Hello Taibo,

If you have access to a copy of Python for Data Analysis by Wes McKinney, there is a section Moving Window Functions in Chapter 10, Time Series. Code and examples are available there, including a discussion of exponentially-weighted functions.

One could also consider an algorithm that adaptively adjusts the weighting of the moving average, to optimize the portfolio performance as market conditions vary. Thomas Wiecki may have some ideas (for a general outline of walk-forward optimization, see his blog post http://blog.quantopian.com/parameter-optimization/).

Grant