Notebook
In [24]:
# talib MACD in Pipeline 

from quantopian.pipeline.data.builtin import USEquityPricing  
from quantopian.pipeline import Pipeline, CustomFactor  
import numpy as np  
import talib

# -------------------------  
FAST, SLOW, SIG = 12, 26, 9  
# -------------------------

class MACD(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = FAST + SLOW + SIG
    # creating 3 different outputs
    outputs = ['Line', 'Signal','histogram']
    def compute(self, today, assets, out, close): 
        
        macd_raw = []
        sig_lines = []
        macd_hist = []
        
        for col in close.T:  
            try:  
                macd, signal_line,hist = talib.MACD(col,  FAST,  SLOW, SIG)  
                macd_raw.append(macd[-1])
                sig_lines.append(signal_line[-1]) 
                macd_hist.append(hist[-1])
            except:  
                macd_raw.append(np.nan)
                sig_lines.append(np.nan)
                macd_hist.append(np.nan)
        
        out.Line[:] = macd_raw
        out.Signal[:] = sig_lines
        out.histogram[:] = macd_hist
In [27]:
def make_pipeline():
    # Selecting universe to screen
    universe=Q1500US()
    # returning the 3 factors in a single line
    macd_line, macd_sig, macd_histo=MACD()
    # create Pipeline
    pipe = Pipeline(
        columns={'MACD_Line':macd_line,'MACD_Signal':macd_sig,'MACD_Histo':macd_histo
        },
        screen=universe
    )
    return pipe
In [29]:
from quantopian.research import run_pipeline

my_pipe=make_pipeline()  
results=run_pipeline(my_pipe, '2019-04-30', '2019-04-30')  
results.head(10)
Out[29]:
MACD_Histo MACD_Line MACD_Signal
2019-04-30 00:00:00+00:00 Equity(2 [ARNC]) -0.006008 0.416621 0.422629
Equity(24 [AAPL]) -0.466045 5.296062 5.762106
Equity(52 [ABM]) -0.003304 0.506653 0.509957
Equity(53 [ABMD]) 2.733897 -11.150991 -13.884888
Equity(62 [ABT]) 0.139315 -0.175175 -0.314490
Equity(67 [ADSK]) 0.519782 4.222777 3.702995
Equity(76 [TAP]) 0.447105 0.724419 0.277314
Equity(110 [RAMP]) 0.328758 0.735389 0.406631
Equity(114 [ADBE]) 1.083548 4.443452 3.359904
Equity(122 [ADI]) -0.006344 1.762108 1.768452
In [ ]:
 
In [ ]: