Basically, I am trying to rank stocks with the highest combination of two metrics and a market cap of at least $50 000 000 using pipeline.
I followed https://www.youtube.com/watch?v=J8RzPVtW4X8 to rank the stocks, but I keep getting this error:
AttributeError: 'UserBinaryExpression' object has no attribute 'rank'
There was a runtime error on line 38.
Can someone please help me fix this error? I cannot figure out how to fix this. Not knowing if the rest of my algo works is making this harder.
This is my code:
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data import morningstar
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
class MarketCap(CustomFactor):
# Pre-declare inputs and window_length
inputs = [USEquityPricing.close, morningstar.valuation.shares_outstanding]
window_length = 1
# Compute market cap value
def compute(self, today, assets, out, close, shares):
out[:] = close[-1] * shares[-1]
def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, name='my_pipeline')
#***Trying to filter out stocks with a market cap below $50 mill
mkt_cap = MarketCap()
mkt_cap_price = mkt_cap > 50000000
pipe.set_screen(mkt_cap_price)
#***I am trying to get these fundamentals from the last year.
ent_val = fundamentals.valuation.enterprise_value
ebt = fundamentals.income_statement.ebit
at = fundamentals.operation_ratios.assets_turnover
earnings_yield = ebt/ent_val
#I want to add the earnings_yield + roc and find 30 stocks with the highest combination of these two fundamentals.
pos = earnings_yield + at
pos_rank = pos.rank(ascending=False)
pipe.add(pos_rank, 'pos_rank')
context.my_leverage = 1
def before_trading_start(context, data):
context.output = pipeline_output('my_pipeline')
log.info((my_pipeline.sort('rank_pos')).head(30))
context.my_pipeline = my_pipeline.sort(['rev_rank'], ascending=False).iloc[:40]
update_universe(context.my_pipeline.index)
def handle_data(context, data):
pass
I know this does not make much sense, but I am trying to learn. The documentation did not help.