Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Creating an MACD crossover signal with two time frames

Hey I was wondering if it was possible to create a MACD crossover algo that that used two different time periods for example If I wanted to use a 5 min and 1 min time period.

I was thinking of trying to make a signal that checks if the MACD line is above the signal line on the 5 min chart and then buys and sells the moving average crossovers on the 1 min chart.

Is it possible to do this? If so how would I code the if statements that checks the MACD with the corresponding time period ( 1 & 5 min)

Any help with this would be greatly appreciated!

1 response

In case it might help as a MACD starting point you could play around with this.
(Currently ignores signal and histogram, easy to add).

import talib  
import numpy as np

macd_fst_shrt_in    = 3  
macd_fst_long_in    = 18  
macd_slw_shrt_in    = 3  
macd_slw_long_in    = 42  
macd_signal_in      = 10

def handle_data(context, data):  
    macd_calc(prices_list)

def macd_calc(prices_list):  
    macd_fst = talib.MACD(  
        np.array(prices_list),  
        macd_fst_shrt_in,  
        macd_fst_long_in,  
        macd_signal_in  
    )[0][-1]

    macd_slw = talib.MACD(  
        np.array(prices_list),  
        macd_slw_shrt_in,  
        macd_slw_long_in,  
        macd_signal_in  
    )[0][-1]

    # 0 = macd, 1 = signal, 2 = histogram