I'm consuming analyst concensus estimate ratings from Zacks and I'm trying to build an algorithm that wieghs different ratings. It's currently in research phase, so I'm trying to implement it on Research, but get stuck to technical issues.
This is what I'm trying to do:
from quantopian.pipeline.data.zacks import broker_ratings
import numpy as np
def make_pipeline():
#Get analyst estimations
rating_strong_buys = broker_ratings.rating_cnt_strong_buys.latest
rating_buys = broker_ratings.rating_cnt_mod_buys.latest
rating_holds = broker_ratings.rating_cnt_holds.latest
rating_sells = broker_ratings.rating_cnt_mod_sells.latest
rating_strong_sells = broker_ratings.rating_cnt_strong_sells.latest
# Calculate rating coefficient
ratings = np.array([rating_strong_buys, rating_buys, rating_holds, rating_sells, rating_strong_sells], dtype=float)
if np.isnan(ratings).any():
ratings = 0
rating_weights = np.array([0.025, 0.015, 0.005, -0.005, -0.015])
rating_coefficient = (ratings*rating_weights).sum()
I want to replace the ratings array with 0 if any of the ratings is NaN for further calculation purposes, but there are 2 problems:
When a rating is not defined it seems to be a sequence
my_pipe = make_pipeline()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-76-dcf250cf5490> in <module>()
----> 1 my_pipe = make_pipeline()
<ipython-input-75-3e5260d3bc43> in make_pipeline()
24
25
---> 26 ratings = np.array([rating_strong_buys, rating_buys, rating_holds, rating_sells, rating_strong_sells], dtype=float)
27 rating_weights = np.array([0.025, 0.015, 0.005, -0.005, -0.015])
28 rating_coefficient = (ratings*rating_weights).sum()
ValueError: setting an array element with a sequence.
However, when I comment out calculations and run make_pipeline to see missing ratings, they are just NaNs.
How can I successfully replace these NaNs in Pipeline?