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()