Notebook
In [124]:
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.research import run_pipeline
from quantopian.pipeline.filters import StaticAssets
from quantopian.pipeline.factors import EWMA
from quantopian.pipeline.data.builtin import USEquityPricing
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
#from numpy import average, full, arange, nan, isnan

EMA Weekly Calculations

In [220]:
def exponential_weights(length, decay_rate):
    return full(length, decay_rate) ** arange(length + 1, 1, -1)
In [221]:
class EWMAWeekly(CustomFactor):
    """
    Exponentially Weighted Moving Average

    **Default Inputs:** None

    **Default Window Length:** None

    Parameters
    ----------
    inputs : length-1 list/tuple of BoundColumn
        The expression over which to compute the average.
    window_length : int > 0
        Length of the lookback window over which to compute the average.
    ema_lookback : int > 0. This should be the 
        number of days back to calculate ema.
    decay_rate : float, 0 < decay_rate <= 1
        Weighting factor by which to discount past observations.

        When calculating historical averages, rows are multiplied by the
        sequence::

            decay_rate, decay_rate ** 2, decay_rate ** 3, ...

    Notes
    -----
    - This class can also be imported under the name ``EWMA``.

    See Also
    --------
    :func:`pandas.ewma`
    """
    params = ('decay_rate',)
    
    def compute(self, today, assets, out, data, decay_rate):
        out[:] = average(
            data[::5],
            axis=0,
            weights=exponential_weights(len(data[::5]), decay_rate),
        )
In [222]:
class PreviousEWMAWeekly(CustomFactor):
    """
    Exponentially Weighted Moving Average

    **Default Inputs:** None

    **Default Window Length:** None

    Parameters
    ----------
    inputs : length-1 list/tuple of BoundColumn
        The expression over which to compute the average.
    window_length : int > 0
        Length of the lookback window over which to compute the average.
    ema_lookback : int > 0. This should be the 
        number of days back to calculate ema.
    decay_rate : float, 0 < decay_rate <= 1
        Weighting factor by which to discount past observations.

        When calculating historical averages, rows are multiplied by the
        sequence::

            decay_rate, decay_rate ** 2, decay_rate ** 3, ...

    Notes
    -----
    - This class can also be imported under the name ``EWMA``.

    See Also
    --------
    :func:`pandas.ewma`
    """
    params = ('decay_rate',)
    
    def compute(self, today, assets, out, data, decay_rate):
        out[:] = average(
            data[:-5:5],
            axis=0,
            weights=exponential_weights(len(data[:-5:5]), decay_rate),
        )

Pipeline and Graph

In [223]:
my_asset = symbols('SPY')

my_filter = StaticAssets([my_asset])

# MACD Calculations
# CURRENT
ewma_12wk = EWMAWeekly(inputs=[USEquityPricing.close], window_length=60, decay_rate=0.94, mask=my_filter)
ewma_26wk = EWMAWeekly(inputs=[USEquityPricing.close], window_length=130, decay_rate=0.94, mask=my_filter)
macd_fast = ewma_26wk - ewma_12wk
# ??? SIGNAL LINE ???

# PREVIOUS WEEK
prev_ewma_12wk = PreviousEWMAWeekly(inputs=[USEquityPricing.close], window_length=65, decay_rate=0.94, mask=my_filter)
prev_ewma_26wk = PreviousEWMAWeekly(inputs=[USEquityPricing.close], window_length=135, decay_rate=0.94, mask=my_filter)
prev_macd_fast = prev_ewma_26wk - prev_ewma_12wk
# ??? PREVIOUS SIGNAL LINE ???

# Current and Previous MACD-H
macd = myMACD()

my_pipe = Pipeline(
        screen = my_filter,
        columns = {
            'macd_fast': macd_fast,
            'prev_macd_fast': prev_macd_fast
        }
    )
In [226]:
start_date = '2-5-2009'
end_date = '3-5-2009'
results = run_pipeline(my_pipe, start_date, end_date)

Pipeline Execution Time: 0.45 Seconds
In [227]:
results
Out[227]:
macd_fast prev_macd_fast
2009-02-05 00:00:00+00:00 Equity(8554 [SPY]) 8.525327 9.009619
2009-02-06 00:00:00+00:00 Equity(8554 [SPY]) 8.241159 8.656349
2009-02-09 00:00:00+00:00 Equity(8554 [SPY]) 8.163558 8.326601
2009-02-10 00:00:00+00:00 Equity(8554 [SPY]) 7.761258 8.277565
2009-02-11 00:00:00+00:00 Equity(8554 [SPY]) 7.357523 8.136793
2009-02-12 00:00:00+00:00 Equity(8554 [SPY]) 7.831638 8.525327
2009-02-13 00:00:00+00:00 Equity(8554 [SPY]) 7.431573 8.241159
2009-02-17 00:00:00+00:00 Equity(8554 [SPY]) 7.253421 8.163558
2009-02-18 00:00:00+00:00 Equity(8554 [SPY]) 6.750152 7.761258
2009-02-19 00:00:00+00:00 Equity(8554 [SPY]) 6.647247 7.357523
2009-02-20 00:00:00+00:00 Equity(8554 [SPY]) 6.911363 7.831638
2009-02-23 00:00:00+00:00 Equity(8554 [SPY]) 6.413913 7.431573
2009-02-24 00:00:00+00:00 Equity(8554 [SPY]) 6.415504 7.253421
2009-02-25 00:00:00+00:00 Equity(8554 [SPY]) 5.695459 6.750152
2009-02-26 00:00:00+00:00 Equity(8554 [SPY]) 5.345112 6.647247
2009-02-27 00:00:00+00:00 Equity(8554 [SPY]) 5.866233 6.911363
2009-03-02 00:00:00+00:00 Equity(8554 [SPY]) 5.742414 6.413913
2009-03-03 00:00:00+00:00 Equity(8554 [SPY]) 5.720421 6.415504
2009-03-04 00:00:00+00:00 Equity(8554 [SPY]) 5.251811 5.695459
2009-03-05 00:00:00+00:00 Equity(8554 [SPY]) 4.999924 5.345112
In [ ]: