Notebook
In [21]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage,ExponentialWeightedMovingAverage
from quantopian.pipeline import CustomFactor,CustomFilter
import numpy as np
In [22]:
class StdDev(CustomFactor):
    def compute(self, today, asset_ids, out, values):
        # Calculates the column-wise standard deviation, ignoring NaNs
        out[:] = numpy.nanstd(values, axis=0)
In [ ]:
class Limitlow(CustomFactor):  
    
    inputs = [USEquityPricing.open,USEquityPricing.high,USEquityPricing.low,USEquityPricing.close,]  
    window_length = 1  
    def compute(self, today, assets, out, close):

        spy_index = np.where(assets == 8554)   # 8554 is the SID of SPY  
        # index -1 gets the latest price row. spy_index gets the SPY column.  
        out[:] = np.log(close[-1, spy_index]) 
In [23]:
class SPY_Close_Price(CustomFactor):  
    '''  
    One can use any inputs from any assets and set any factor outputs based upon them.  
    In this case, simply return the close price of SPY for every asset.  
    '''  
    inputs = [USEquityPricing.close]  
    window_length = 1  
    def compute(self, today, assets, out, close):

        spy_index = np.where(assets == 8554)   # 8554 is the SID of SPY  
        # index -1 gets the latest price row. spy_index gets the SPY column.  
        out[:] = np.log(close[-1, spy_index])  
In [24]:
class SidInList(CustomFilter):
    """
    Filter returns True for any SID included in parameter tuple passed at creation.
    Usage: my_filter = SidInList(sid_list=(23911, 46631))
    """    
    inputs = []
    window_length = 1
    params = ('sid_list',)

    def compute(self, today, assets, out, sid_list):
        out[:] = np.in1d(assets, sid_list)
In [25]:
class Get_underlying(CustomFactor):
    inputs = [USEquityPricing.close]
    window_length = 1
    def compute(self, today, assets, out, close):
        out[:] = np.log(close)
In [36]:
def make_pipeline():
    Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
    Asset = np.log(Asset_Px)
    Underlying = SPY_Close_Price()
    Px = Underlying/Asset
    FastMA = SimpleMovingAverage(inputs=[Px], window_length=8)
    #SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
    #Diff = np.abs(FastMA - SlowMA)
    #AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
    #Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
    
    #RM_LimitLow= SlowMA + (AvgDiff + Diff_StdDev * 1.618)
    #RM_LimitUpp= SlowMA + (AvgDiff + Diff_StdDev * 2.618)
    
    
    return Pipeline(
        columns={
            'RM_LimitLow': Px,
            'RM_LimitUpp': FastMA
        },
        screen=Q500US()
    )
In [37]:
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
result

NonWindowSafeInputTraceback (most recent call last)
<ipython-input-37-97e936fabd7c> in <module>()
----> 1 result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
      2 result

<ipython-input-36-acae6655596c> in make_pipeline()
      4     Underlying = SPY_Close_Price()
      5     Px = Underlying/Asset
----> 6     FastMA = SimpleMovingAverage(inputs=[Asset_Px], window_length=8)
      7     #SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
      8     #Diff = np.abs(FastMA - SlowMA)

/build/src/qexec_repo/zipline_repo/zipline/pipeline/mixins.pyc in __new__(cls, inputs, outputs, window_length, mask, dtype, missing_value, ndim, **kwargs)
    138             missing_value=missing_value,
    139             ndim=ndim,
--> 140             **kwargs
    141         )
    142 

/build/src/qexec_repo/zipline_repo/zipline/pipeline/term.pyc in __new__(cls, inputs, outputs, window_length, mask, *args, **kwargs)
    472             mask=mask,
    473             window_length=window_length,
--> 474             *args, **kwargs
    475         )
    476 

/build/src/qexec_repo/zipline_repo/zipline/pipeline/term.pyc in __new__(cls, domain, dtype, missing_value, window_safe, ndim, *args, **kwargs)
    129                     ndim=ndim,
    130                     params=params,
--> 131                     *args, **kwargs
    132                 )
    133             return new_instance

/build/src/qexec_repo/zipline_repo/zipline/pipeline/term.pyc in _init(self, inputs, outputs, window_length, mask, *args, **kwargs)
    480         self.window_length = window_length
    481         self.mask = mask
--> 482         return super(ComputableTerm, self)._init(*args, **kwargs)
    483 
    484     @classmethod

/build/src/qexec_repo/zipline_repo/zipline/pipeline/term.pyc in _init(self, domain, dtype, missing_value, window_safe, ndim, params)
    267         # should set this flag to True.
    268         self._subclass_called_super_validate = False
--> 269         self._validate()
    270         assert self._subclass_called_super_validate, (
    271             "Term._validate() was not called.\n"

/build/src/qexec_repo/zipline_repo/zipline/pipeline/mixins.pyc in _validate(self)
     40     """
     41     def _validate(self):
---> 42         super(PositiveWindowLengthMixin, self)._validate()
     43         if not self.windowed:
     44             raise WindowLengthNotPositive(window_length=self.window_length)

/build/src/qexec_repo/zipline_repo/zipline/pipeline/mixins.pyc in _validate(self)
     85 
     86     def _validate(self):
---> 87         super(RestrictedDTypeMixin, self)._validate()
     88         assert self.ALLOWED_DTYPES is not NotSpecified, (
     89             "ALLOWED_DTYPES not supplied on subclass "

/build/src/qexec_repo/zipline_repo/zipline/pipeline/term.pyc in _validate(self)
    538             for child in self.inputs:
    539                 if not child.window_safe:
--> 540                     raise NonWindowSafeInput(parent=self, child=child)
    541 
    542     def _compute(self, inputs, dates, assets, mask):

NonWindowSafeInput: Can't compute windowed expression SimpleMovingAverage((NumExprFactor(expr='(((x_0 + x_1) + (x_2)) + (x_3)) * (0.25)', bindings={'x_2': Latest((USEquityPricing.low::float64,), window_length=1), 'x_3': Latest((USEquityPricing.close::float64,), window_length=1), 'x_0': Latest((USEquityPricing.open::float64,), window_length=1), 'x_1': Latest((USEquityPricing.high::float64,), window_length=1)}),), window_length=8) with windowed input NumExprFactor(expr='(((x_0 + x_1) + (x_2)) + (x_3)) * (0.25)', bindings={'x_2': Latest((USEquityPricing.low::float64,), window_length=1), 'x_3': Latest((USEquityPricing.close::float64,), window_length=1), 'x_0': Latest((USEquityPricing.open::float64,), window_length=1), 'x_1': Latest((USEquityPricing.high::float64,), window_length=1)}).
In [66]:
def make_pipeline():
    Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
    Asset = np,log(Asset_Px)
    
    Underlying = np.log(symbols('SPY'))
    Px = Underlying/Asset
    FastMA = ExponentialWeightedMovingAverage(inputs=[Px], window_length=8)
    SlowMA = SimpleMovingAverage(inputs=[Px], window_length=34)
    Diff = np.abs(FastMA - SlowMA)
    AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
    Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
    
    RM_LimitLow= SlowMA + (AvgDiff + Diff_StdDev * 1.618)
    RM_LimitUpp= SlowMA + (AvgDiff + Diff_StdDev * 2.618)
    
     return Pipeline(
        columns={
            'Asset_Px': Asset_Px,
            'Asset': Asset
        },
        screen=Q500US()
    )
  File "<ipython-input-66-cf6cd145f01c>", line 15
    return Pipeline(
    ^
IndentationError: unexpected indent
In [ ]:
 
In [91]:
def make_pipeline():
    Asset_Px = (USEquityPricing.open.latest + USEquityPricing.high.latest + USEquityPricing.low.latest + USEquityPricing.close.latest)*0.25
    Asset = np.log(Asset_Px)
    SPY = symbols('SPY').sid
    Underlying = np.log(USEquityPricing[(USEquityPriceing.sid==SPY)].close.latest)
     
    return Pipeline(
        columns={
            'Asset_Px': Asset_Px,
            'Asset': Underlying
        },
        screen=Q500US()
    )
In [6]:
result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
result

UnboundLocalErrorTraceback (most recent call last)
<ipython-input-6-97e936fabd7c> in <module>()
----> 1 result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')
      2 result

<ipython-input-5-4ec1c11ac1d0> in make_pipeline()
      3     Asset = np.log(Asset_Px)
      4     include_filter = SidInList(sid_list = (8554))
----> 5     Underlying = Underlying(mask=include_filter)
      6 
      7 

UnboundLocalError: local variable 'Underlying' referenced before assignment
In [ ]:
from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.filters.morningstar import Q500US
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline import CustomFactor
import numpy as np

class StdDev(CustomFactor):
    def compute(self, today, asset_ids, out, values):
        # Calculates the column-wise standard deviation, ignoring NaNs
        out[:] = numpy.nanstd(values, axis=0)


def make_pipeline():
    
    FastMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=8)
    SlowMA = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=34)
    Diff = np.abs(FastMA - SlowMA)
    AvgDiff = SimpleMovingAverage(inputs=[Diff], window_length=110)
    Diff_StdDev = StdDev(inputs=[Diff], window_length=110)
   
    return Pipeline(
        columns={
            'AvgDiff': AvgDiff,
            'Diff_StdDev': Diff_StdDev
        },
        screen=Q500US()
    )
In [ ]: