Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Cross Above or Cross below in the moving average

Hi! im trying to develop an strategy based on moving averages and i wanted to access to the past data to know when one line cross the other , to do something like that on mql4

buffer0[i+1]>buffer1[i+1] && buffer1[i]>buffer0[i]  

Is something like that on quantopia? i can only access to the most recent moving average but no the previous

ema1 = ta.EMA(timeperiod1=3)  
ema1data= ema1(data)  
moving_average1 = ema1data[context.stock]# The stocks 3 day moving average  

thank you for your help
Javier

6 responses

Hello Javier,

I don't yet fully understand your question, but I think a similar/identical question came up recently:

https://www.quantopian.com/posts/how-to-calculate-moving-average-price-with-dynamic-days

Are you needing to compute moving averages over a range of trailing window lengths? Or something else?

Grant

Hello Javier,

This may give you some ideas.

P.

thank you peter yes , the answer was here!!

context.ema1buffer.append(moving_average1)  
        context.ema2buffer.append(moving_average2)  
        if len(context.ema1buffer) == 2:  
            if context.ema1buffer[1]>context.ema2buffer[1] and context.ema2buffer[0]>context.ema1buffer[0]:  
                print "Cross!"  

Hello Javier,

Another method. This is subtly different - the same results are given but a day earlier.

P.

thank you for the fast response of everybody , and one question more... in the backtesting if im doing like in the last answer in minutes...its calculating the average in the minute bar with the close prices? only with selecting the minute in the bactestign options?
In the graph is still painting in days...

Hi Javier,

Unless something has changed, you can't plot down to the minute level. However, minutely OHLCV trade data are available and you can trade at the minute level. An algorithm written for daily bars can typically be tweaked to run on minute bars; I'd have to dig into the algos above to sort it out. Might be a good exercise for you, if you are wanting to get up the learning curve. Besides, to launch an algo into paper/live trading, it needs to run on minute bars (but if you are wanting to learn the system, a baby step would be to stick with daily bars for now).

Cheers,

Grant