Percentage change of a Moving average

Hi, I am relatively new to Python and am struggling to get the percentage change of a moving average. I want to apply this to the Sentdex's data, sentiment signal.

def make_pipeline():
sentiment_factor = sentiment.sentiment_signal.latest
universe = (sentiment_factor.notnull())
mavg30 = SimpleMovingAverage(inputs=[sentiment.sentiment_signal], window_length=30)

percent_difference = (mavg30[0] - mavg30[-1]) / mavg30[-1]

return Pipeline(
columns={'mavg30': mavg30,
'percent_difference': percent_difference
},screen=universe)



I get this error 'TypeError: zipline.pipeline.term.getitem() expected a value of type zipline.assets._assets.Asset for argument 'key', but got int instead.'
Any help would be great Thanks!

3 responses

universe = (sentiment_factor.notnull())

You cannot do this. Universe has to be equities (which is why it's throwing that error). It wants something like Q1500 which is a universe of highly liquid stocks pre-picked and updated daily or a make_us_equity_universe() which allows you to define your own universe of assets of which stocks you want in it.

Also, you need inputs to be [USEquityPricing.close] or something like that in your SimpleMovingAverage and add the sentiment to your screen and/or do .rank(mask=universe, ascending=False) :)

The simple moving average for sentiment seems to work though, i just can't get the change of the moving average. I know for equities i could use some talib functions for the rate of change of the moving average but i am not sure if that is applicable. Thanks for the reply :)

from quantopian.pipeline import Pipeline
from quantopian.research import run_pipeline
from quantopian.pipeline.factors import SimpleMovingAverage
from quantopian.pipeline.data.sentdex import sentiment_free as sentiment


def make_pipeline():
sentiment_factor = sentiment.sentiment_signal.latest
universe = (sentiment_factor.notnull())
pipe = Pipeline(columns={'sentiment':sentiment_factor,
'mavg': (sma(inputs=[sentiment.sentiment_signal], window_length=30))}
,screen = universe)
return pipe

result = run_pipeline(make_pipeline(), start_date='2015-01-01', end_date='2015-01-01')