Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
SMA for 15 minute bars

I'm working on an algorithm for 20 period and 40 period SMA crossover. My goal is to compare different time frames (i.e. 1m, 15m, 30m, 45m) to understand how securities behave historically when an SMA crossover occurs.

The prices_1m_20bar_sma and prices_1m_40bar_sma is identical to what I'm seeing in my tradingview.com SPY chart. However, the prices_15m_20bar_sma is off by 9 cents and the prices_15m_40bar_sma is off by 6 cents.

Am I doing this right?

#1 minute bar - 20 period SMA  
prices_1m_20bar = data.history(context.spy, 'price', 20, '1m')  
prices_1m_20bar_sma = prices_1m_20bar.mean()


#1 minute bar - 40 period SMA  
prices_1m_40bar = data.history(context.spy, 'price', 40, '1m')  
prices_1m_40bar_sma = prices_1m_40bar.mean()  


#15 minute bar - 20 period SMA  
prices_15m_20bar = data.history(context.spy, 'price', 15*20, '1m')  
prices_15m_20bar_sma = pd.rolling_mean(prices_15m_20bar, window=15).mean()  


#15 minute bar - 40 period SMA  
prices_15m_40bar = data.history(context.spy, 'price', 15*40, '1m')  
prices_15m_40bar_sma = pd.rolling_mean(prices_15m_40bar, window=15).mean()  
5 responses

The first two are OK
The others you should re-sample:

    #15 minute bar - 20 period SMA  
    prices_15m_20bar = data.history(context.spy, 'price', 15*20, '1m')  
    prices_15m_20bar_sma = prices_15m_20bar.resample('15T', label='right').mean()

    #15 minute bar - 40 period SMA  
    prices_15m_40bar = data.history(context.spy, 'price', 15*40, '1m')  
    prices_15m_40bar_sma = prices_15m_40bar.resample('15T', label='right').mean()

Much closer thanks! Within a couple cents for each.

This is exactly what I have been looking for. Now, is it possible to use this to filter for stocks using pipeline. For instance, to get the 30 stocks that have the the fast SMA furthest below the slow SMA percent-wise?

While still using these 20min and 40min bars

By the sounds of it the pipeline data is only updated daily so i'd have to narrow down the list first with pipeline and then cycle through every stock in the list and computer these on each stock. I think I have an idea of how to do that. If somebody has an example already made that I could reference it'd be appreciated, thanks!