I am trying to build a factor that measures the change in earnings using the morning star data. So I am trying to compare the current day's earnings with the previous day's earnings. I keep getting the following error and it points to the line where I am trying to access earnings data.
"zipline.pipeline.term.getitem() expected a value of type zipline.assets._assets.Asset for argument 'key', but got int instead."
Here is the code:
"""
This is a template algorithm on Quantopian for you to adapt and fill in.
"""
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import AverageDollarVolume
from quantopian.pipeline.data import morningstar
def initialize(context):
"""
Called once at the start of the algorithm.
"""
context.leverage_long = 0.5
context.leverage_short = -0.5
# Rebalance every day, 1 hour after market open.
schedule_function(my_rebalance, date_rules.every_day(), time_rules.market_open(hours=0.25))
# Record tracking variables at the end of each day.
schedule_function(my_record_vars, date_rules.every_day(), time_rules.market_close())
# Create our dynamic stock selector.
pipe = Pipeline()
pipe = attach_pipeline(pipe, name='my_pipeline')
earnings_change = (morningstar.earnings_report.basic_eps[-1])*(morningstar.valuation.shares_outstanding[-1])-(morningstar.earnings_report.basic_eps[-2]*morningstar.valuation.shares_outstanding[-2])
earnings_factor = earnings_change<0
#pipe.add(dollar_volume,'vol')
pipe.add(earnings_change,'ear_chg')
pipe.set_screen(earnings_factor)
def before_trading_start(context, data):
"""
Called every day before market open.
"""
context.output = pipeline_output('my_pipeline')
# These are the securities that we are interested in trading each day.
context.security_list = context.output.index
print context.output.head(2)
def my_assign_weights(context, data):
"""
Assign weights to securities that we want to order.
"""
pass
def my_rebalance(context,data):
"""
Execute orders according to our schedule_function() timing.
"""
pass
def my_record_vars(context, data):
"""
Plot variables at the end of each day.
"""
pass
def handle_data(context,data):
"""
Called every minute.
""