Back to Community
How To Use Weekly Data in MACD?

Instead of looking at daily data, I'd like to get the Open, High, Low, and Close of a stock on the weekly time interval. I'm trying to use this weekly price data to calculate the MACD as the crossover occur less often. Any ideas as to how to use weekly candlestick data to get MACD?

I have this so far:

if data.can_trade(sid(26807)):  
        daily_ohlc = data.history(sid(26807), ['open', 'high', 'low', 'close'], 5, '1d')

        weekly_ohlc = daily_ohlc.resample('1W').agg({'open': 'first',  
                                 'high': 'max',  
                                 'low': 'min',  
                                 'close': 'last'})

        macd_raw, signal, hist = talib.MACD(weekly_ohlc['close'], fastperiod=12,  
                                        slowperiod=26, signalperiod=9)  
        record(Raw = macd_raw, Signal = signal) "Weekly open for {0} is {1}".format(sid(26807).symbol, weekly_ohlc['close']))  

The "weekly_ohlc" does return accurate values for weekly data. However, when plugging that into the MACD code it returns errors. How can I get the MACD based on the close prices of the weekly data?


2 responses

Try this :

import talib  
# -------------------------  
stock = symbol('QQQ')  
Fast, Slow, Sig = 12, 26, 9  
# -------------------------  
def initialize(context):  
    schedule_function(macd_indicator, date_rules.week_start(), time_rules.market_open(minutes = 65))  

def macd_indicator(context, data):  
    bars = (Fast + Slow + Sig)*5

    C = data.history(stock, 'price',  bars, '1d')  
    C_w = C.resample('1W').last()  
    macd, signal, hist = talib.MACD(C_w, Fast, Slow, Sig)

    record(macd = macd[-1], signal = signal[-1], hist = hist[-1])  

This worked great Vladimir. Thanks for your help again man!