Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Plot Average Sentiment Score of a Basket of Stocks

As the title indicates, I have a basket of the largest 25 stocks in SPY and I want to compute the average sentiment score to go long SPY when it is above 0 and short when it is below zero. These ~25 stocks are meant as a proxy for SPY's sentiment score since you can't run sentiment scores on ETFs, so I'm using this as a workaround.

I'm running into two issues with my backtest:

1) I can't plot the average sentiment score and receive the following error when trying to do so at the bottom of the alg:

"ValueError: Record only supports numeric values. Non-numeric value passed for key 'sentiment' There was a runtime error on line 101."

I don't understand why the value passed to sentiment under the record function is not interpreted as a numeric value since it seems to be running as such throughout the rest of the algorithm.

2) I'm also not sure this alg is running correctly because I want it to take the average sentiment score of the stocks listed in "BasketOfStocks" and go long SPY when this is above 0 and short SPY when this is below 0. The alg just stays long SPY the whole time which doesn't seem correct especially given all the market turmoil over the last few weeks

Lastly, (and this may be a very noob question), but I can't seem to upload or attach my algorithm so I'm copying and pasting it below. WHen I try to attach the algorithm, it allows me to select an algorithm but the field to choose a backtest is grayed out so I'm not able to attach anything

Really appreciate anyone that can assist with this!


Handpicked stock

basket_of_stocks = [
sid(24),
sid(5061),
sid(42950),
sid(26578),
sid(4151),
sid(25006),
sid(35920),
sid(5938),
sid(7792),
sid(3951),
sid(21839),
sid(32146),
sid(6653),
sid(3496),
sid(5029),
sid(5923),
sid(2190),
sid(700),
sid(4283)
]

mean_sentiment_5day = SimpleMovingAverage(inputs=[sentiment.sentiment_signal], window_length=5)

def myPipeline(context):
context.spy = sid(8554)

myUniverse = StaticAssets(basket_of_stocks) # & Q3000US  

mean_sentiment_5day = SimpleMovingAverage(inputs=[sentiment.sentiment_signal], window_length=5)  

myPipe = Pipeline(  
    columns = {  
        'close_price': USEquityPricing.close.latest,  
        'mean_sentiment_5day': mean_sentiment_5day  
    },  
    screen = myUniverse  
)  

return myPipe  

def initialize(context):

attach_pipeline(myPipeline(context), 'myPipeline')  

schedule_function(  
    buy_sell_func,  
    date_rules.every_day(),  
    time_rules.market_open(),  
    half_days = True  
)  

# schedule_function(  
#     record_vars,  
#     date_rules.every_day(),  
#     time_rules.market_open(),  
#     half_days = True  
# )  

def buy_sell_func(context,data):

if mean_sentiment_5day > 0:  
    order_target_percent(sid(8554), 1.0)  
    print("Long SPY")  

elif mean_sentiment_5day < 0:  
    order_target_percent(sid(8554), -1.0)  
    print("Short SPY")  

else:  
    order_target_percent(sid(8554), 0.0)  

This is where I want to record the average sentiment score of my basket of stocks

def record_vars(context,data):

# record(sentiment=SimpleMovingAverage(inputs=[sentiment.sentiment_signal], window_length=5))