Notebook
In [1]:
# First, we need to import the basic pipline methods
from quantopian.pipeline import Pipeline, CustomFactor
from quantopian.research import run_pipeline

# Also get the built-in filters and/or factors to use
from quantopian.pipeline.filters import QTradableStocksUS, Q500US, StaticAssets
from quantopian.pipeline.factors import SimpleMovingAverage

# Finally get any data we want to use
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
from quantopian.pipeline.data.morningstar import Fundamentals

# One would normally import their self-serve data set here
# However, since self-serve datasets are private this will cause an error for ither users
# Just including for reference.
# Assumes the dataset name is 'my_macro_data_set'. 
# Also assume there is a field named 'macro_data_1'  with values mapped to the single asset SPY
# from quantopian.pipeline.data.user_5cb4fc8571ef9572db85c0af import my_macro_data_set
In [10]:
class custom_factor_data(CustomFactor):
    """
    Custom factor to generate which looks like a self serve data factor
    Just return a different number depending on the year
    """
    window_length = 1
    
    # The factor needs an input but this is ignored.
    inputs = [USEquityPricing.close]   
    
    # Associate this factor with the SPY asset
    mask = StaticAssets(symbols(['SPY']))
              
    def compute(self, today, assets, out, value):
        # Make a dictionary with year as the key, and some macro data as the value
        custom_factor_data_dict = {2018:100.0, 2019:150.0}
        # Returns a different number depending on the year
        out[:] = custom_factor_data_dict[today.year]
In [13]:
def make_pipeline():
    # However, for testing and speed one may want to limit to a much smaller set
    universe = Q500US()
    
    # Create the custom factor and get its slice
    my_custom_factor_data = custom_factor_data()
    my_custom_factor_slice = my_custom_factor_data[symbols('SPY')]

    price = USEquityPricing.close.latest

    # This works but the one below generates an error
    #valid_price = price >= my_custom_factor_slice
    
    # This generates an error: TypeError: unsupported operand type(s) for *: 'int' and 'Slice'
    # How do I multiply the value of the slice by an integer?
    valid_price = price >= 2 * my_custom_factor_slice

    # Create the pipeline with columns for all our factors
    pipe = pipe = Pipeline(
        columns = {
        'price' : price,
        'valid_price': valid_price
        },
        screen = universe
     )
 
    return pipe  
In [14]:
# Run the pipeline
results = run_pipeline(make_pipeline(), start_date = '12-24-2018', end_date = '1-4-2019')

# Display the results for a day in 2018 and one in 2019
display(results.loc['2018-12-24', : ].head(5))
display(results.loc['2019-01-04', : ].head(5))
TypeErrorTraceback (most recent call last)
<ipython-input-14-4a8c998f7156> in <module>()
      1 # Run the pipeline
----> 2 results = run_pipeline(make_pipeline(), start_date = '12-24-2018', end_date = '1-4-2019')
      3 
      4 # Display the results for a day in 2018 and one in 2019
      5 display(results.loc['2018-12-24', : ].head(5))

<ipython-input-13-5f148f4ec714> in make_pipeline()
     13 
     14     # How do I multiply the value of the slice by an integer?
---> 15     valid_price = price >= 2 * my_custom_factor_slice
     16 
     17 

TypeError: unsupported operand type(s) for *: 'int' and 'Slice'
In [ ]: