Notebook
In [4]:
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters.morningstar import Q500US, Q1500US
from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume
from quantopian.pipeline.factors import MovingAverageConvergenceDivergenceSignal

from quantopian.pipeline import Pipeline

from talib import MACD, RSI, SMA

from quantopian.pipeline.filters import StaticAssets
from quantopian.research import run_pipeline

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
In [5]:
aapl=get_pricing(symbols('aapl'), start_date='20160101', end_date='20160301', fields=['close_price', 'high', 'low','open_price'])
In [6]:
aapl.tail()
Out[6]:
close_price high low open_price
2016-02-24 00:00:00+00:00 96.1000 96.38 93.32 93.99
2016-02-25 00:00:00+00:00 96.7500 96.76 95.25 96.05
2016-02-26 00:00:00+00:00 96.9100 98.03 96.58 97.20
2016-02-29 00:00:00+00:00 96.6501 98.23 96.65 96.86
2016-03-01 00:00:00+00:00 100.5300 100.77 97.42 97.65
In [8]:
aapl_close = aapl['close_price'].values
aapl_close
Out[8]:
array([ 105.33  ,  102.71  ,  100.7   ,   96.54  ,   96.98  ,   98.53  ,
         99.96  ,   97.41  ,   99.51  ,   97.081 ,   96.68  ,   96.81  ,
         96.29  ,  101.42  ,   99.466 ,   99.98  ,   93.41  ,   94.08  ,
         97.14  ,   96.38  ,   94.48  ,   96.34  ,   96.6   ,   94.01  ,
         95.01  ,   95.02  ,   94.27  ,   93.66  ,   93.99  ,   96.62  ,
         98.11  ,   96.26  ,   96.03  ,   96.89  ,   94.69  ,   96.1   ,
         96.75  ,   96.91  ,   96.6501,  100.53  ])
In [95]:
macd, signal, hist = MACD(aapl_close,slowperiod=26,fastperiod=12,signalperiod=9)
In [96]:
macd, signal, hist
Out[96]:
(array([        nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan, -0.76270183, -0.8158336 ,
        -0.73568518, -0.6126551 , -0.49651866, -0.420603  , -0.04682403]),
 array([        nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan, -1.3111652 , -1.21209888,
        -1.11681614, -1.01598393, -0.91209088, -0.8137933 , -0.66039944]),
 array([        nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,         nan,         nan,
                nan,         nan,         nan,  0.54846337,  0.39626528,
         0.38113096,  0.40332883,  0.41557222,  0.3931903 ,  0.61357542]))
In [97]:
def make_mcd():
    base = StaticAssets([symbols('aapl')])
    mcd = MovingAverageConvergenceDivergenceSignal(inputs=[USEquityPricing.close],fast_period=12, slow_period=26,signal_period=9, mask= base)
    
    return Pipeline(columns={'mcd':mcd, 'close':USEquityPricing.close.latest}, screen=base)
In [101]:
mcd = run_pipeline(make_mcd(), start_date='20160101', end_date='20160302')
In [102]:
mcd.tail()
Out[102]:
close mcd
2016-02-25 00:00:00+00:00 Equity(24 [AAPL]) 96.10 -0.163990
2016-02-26 00:00:00+00:00 Equity(24 [AAPL]) 96.76 -0.063588
2016-02-29 00:00:00+00:00 Equity(24 [AAPL]) 96.90 0.035894
2016-03-01 00:00:00+00:00 Equity(24 [AAPL]) 96.65 0.121403
2016-03-02 00:00:00+00:00 Equity(24 [AAPL]) 100.53 0.291331
In [ ]: