Hi,
This is (hopefully) a simple python, pandas, or numpy question. I built a custom factor that returns some macroeconomic data. When I try to use it in pipeline I attempt to do some simple math on it (multiply it by 2) but I get an error:
TypeError: unsupported operand type(s) for *: 'int' and 'Slice'
The offending line is this:
valid_price = price >= 2 * my_custom_factor_slice
Can someone please point out what I'm doing wrong and how to do this math correctly?
Thanks,
Brett
Here's the custom factor and pipeline code:
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]
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