Notebook
In [3]:
from quantopian.pipeline import Pipeline
In [4]:
from quantopian.research import run_pipeline
In [5]:
from quantopian.pipeline.data.builtin import USEquityPricing
In [6]:
from quantopian.pipeline.filters import QTradableStocksUS
In [7]:
import talib as tb
In [8]:
import numpy as np
In [9]:
from quantopian.pipeline.factors import EWMA
In [10]:
from quantopian.pipeline.factors import CustomFactor
In [11]:
import pandas as pd
In [12]:
class MACDHISTO(CustomFactor):
    #inputs=[USEquityPricing.close]
    #window_length=200
    def compute(self, today, assets, out, close):
        ema12 = pd.stats.moments.ewma(close, span=12)  
        ema26 = pd.stats.moments.ewma(close, span=26)  
        signal = pd.stats.moments.ewma(ema12[-10:] - ema26[-10:], span=9)  
        out[:] = (ema12[-1] - ema26[-1]) - signal[-1]
In [84]:
class MACD_HISTO_STD(CustomFactor):
    inputs=[USEquityPricing.close]
    #window_length=200
    def compute(self, today, assets, out, close):
        ema12 = pd.stats.moments.ewma(close, span=12)
        ema26 = pd.stats.moments.ewma(close, span=26)
        signal = pd.stats.moments.ewma(ema12[-100:] - ema26[-100:], span=9)
        #out[:] = pd.rolling(70).std(ema12[-100:] - ema26[-100:] - signal[-100:])
        df=ema12[-100:] - ema26[-100:] - signal[-100:]
        out[:]=np.std(df,ddof=1)
In [87]:
def make_pipeline():
    universe=QTradableStocksUS()
    '''
    ewma12=EWMA(inputs=[USEquityPricing.close], window_length=12, decay_rate=0.5)
    ewma26=EWMA(inputs=[USEquityPricing.close], window_length=26, decay_rate=0.5)
    signal_input=ewma12-ewma26
    #signal=EWMA(inputs=signal_input, window_length=9, decay_rate=0.5)
    '''
    histo=MACDHISTO(inputs=[USEquityPricing.close], window_length=26, mask=universe)
    std=MACD_HISTO_STD(inputs=[USEquityPricing.close], window_length=100, mask=universe)
    pipe = Pipeline(
        columns={
            'histo':histo,
            'std':std
        },
        screen=universe
    )
    return pipe
    
In [93]:
result = run_pipeline(make_pipeline(), '2015-04-05', '2015-5-12')
result.head()

/venvs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:6: FutureWarning: pd.ewm_mean is deprecated for ndarrays and will be removed in a future version
  
/venvs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:7: FutureWarning: pd.ewm_mean is deprecated for ndarrays and will be removed in a future version
  import sys
/venvs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:8: FutureWarning: pd.ewm_mean is deprecated for ndarrays and will be removed in a future version
  
/venvs/py35/lib/python3.5/site-packages/ipykernel_launcher.py:5: FutureWarning: pd.ewm_mean is deprecated for ndarrays and will be removed in a future version
  """
Pipeline Execution Time: 5.17 Seconds
Out[93]:
histo std
2015-04-06 00:00:00+00:00 Equity(2 [HWM]) 0.033371 0.423016
Equity(24 [AAPL]) -0.037639 0.423016
Equity(31 [ABAX]) -0.120296 0.423016
Equity(39 [DDC]) -0.045574 0.423016
Equity(41 [ARCB]) -0.161344 0.423016
In [ ]:
 
In [ ]: