Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Trying to use Moving Average on minute data

Hi,

I am new member and I am currently trying to program a moving average based on minute prices. That means a mva(x) that is calculating the average price of the last 20 closíng prices of the past 20 min.

Unfort, i didnt find any function for that mvag(int) seems to use only day.

Thanks

8 responses

If the moving average works with a '1d' chart type, then there is no reason it shouldn't work against minute data. You just need to set your chart type to '1m' in the IDE.

I am not sure if understood right, but when I use the mvag(20) as signal to buy or sell when it reaches certain level, the algo is buying every minute for sometimes few consecutive hours. So this cant be ok

Hi Pythi,

As stated in our documentation, the mvag(int) function can only be used for daily data. However, it is possible to calculate the moving average for minutely data.

To do this for the last 20 minutes, you could make the call:

history(20, '1m', 'price').mean()

This will give you a Series with the moving average for each of the stocks in your universe.

Let me know if this helps!

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.

Hi Jamie,

yes, def helped me.

It works now like intended.

Thanks

@jamie. Where in the code are you putting that?
history(20, '1m', 'price').mean()

I also have the same problem. I am trying to use MovingAverage over minute timeframe but generally MA works only over Daily timeframes. Can someone explain me how and where to put history () . My idea is to use MA crossover for let's say sid(24) but in a minute series ?

So I know that I need to import Pandas library? And than to use history function ? Can you tell me if I am thinking in the right way

import pandas as pd

def initialize(context):  
    context.market = sid(24)

def handle_data(context, data):  
    minute_mavg = history(20, '1m', 'price').mean()  
    MA1 = data[context.market].mavg(5)  
    MA2 = data[context.market].mavg(18)  
    current_price = data[context.market].price  
    positions = context.portfolio.positions[sid(24)].amount  
    cash = context.portfolio.cash  
    if (MA1 > MA2) and positions == 0:  
        shares = int(cash/current_price*.6)  
        order(context.market, shares)  
    elif (MA1<MA2) and positions != 0:  
         order(context.market, -positions)  

@Anton, i understand correctly you are trying to trigger trades on a minute data, but have the trigger operate on the daily data. If so then check the documentation for a schedule function

Hello Darell,

It looks like a schedule function cannot help me to run the moving average at a minute graph.
I am still searching for solution. Would be very happy if someone knows how this could be done.

:)