Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get 90 day moving average in minute mode?

Hi,
I realized if I use

historical_data = history(390*100, '1m', 'price')  
last_10_day_mean = historical_data.tail(390*10).mean()  
last_30_day_mean = historical_data.tail(390*30).mean()  
last_90_day_mean = historical_data.tail(390*90).mean()

It dramatically slows down or even errors out.

QdbExecutionTimeout: Executing 'historical_data = history(390*100, '1m', 'price')' exceeded the max time of 50 second

due to tool it takes to get the data. Is there anyway to use mavg() to accomplish this? Do I have to do mavg(390*day)?

The reason, I went with history(390*100, '1m', 'price') to begin with is because it fits better with my existing algo. But I guess I have to alter it if I have to.

Thank you

5 responses

Ujae,

The best way to get a 90 day moving average in minute mode is to just get the history in days like:

historical_data = history(90, '1d', 'price')  
last_10_day_mean = historical_data.tail(10).mean()  
last_30_day_mean = historical_data.tail(30).mean()  
last_90_day_mean = historical_data.tail(90).mean()  

As you can see if you try to get that much price information at that resolution its just going to take forever.

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 for your writing. But I am running the algorthem in minutes.
Also I am curious who come so many shared algos in the Quantopian
communities are mostly in daily mode. While shouldn't it be minute
mode to make it more practical? I would think that the most people
don't want to trade on end of day prices.

So Ujae even though you are running your algorithm in minute mode, as everyone should, you can still fetch the history at a resolution of days. The thing about daily vs minute, for me at least, is really a matter of time, it takes a long time to run a full back test on minute mode if its a long window. Personally, when I'm proofing an algo, the first few times I'll run it in daily mode to see if the idea is has merit, if it does I'll switch to minute mode, to certify my hopes. I agree more people should be back testing in minute mode, and I'm sure many are, but what we see in posts in the community I feel is people who are still in those early stages of idea formulation and, like me, start out with tests in daily mode. So be aware that the bias in posted back tests maybe more skewed in that direction.

I didn't realize that you can use
prices = history(40, '1d', 'price')
in minutes mode, because when I tried to run
prices = history(390*40, '1m', 'price')
in daily mode it gave me an error message and I thought '1d' is only for daily and '1m' is only for minutes.
Thanks

So when you run a backtest you need to run it in the mode that provides a resolution of data equal to what you use in your program, so you can always use history(90, "1d", "price") in any minute and daily mode, but you can only use history(90, "1m", "price") in minute mode because you need that resolution in the data. I hope that clears things up!