Hello!
I wanted to thank the developers for the great Pipeline API, and especially all who were involved with making such a superb and helpful webinar. I had a quick question for understanding the filters needed by pipe.set_screen().
I'm currently trying to make an algorithm that lists companies by ROA (descending) that have a market cap of at least X amount.
I am trying to base my code off of one of the examples:
market_cap = morningstar.valuation.market_cap
market_cap_filter = (market_cap > 500000000000)
pipe.set_screen(market_cap_filter)
but it hasn't been effective and brings an error:
TypeError: zipline.pipeline.pipeline.set_screen() expected a value of type zipline.pipeline.filters.filter.Filter for argument 'screen', but got bool instead.
Here is my full code! Any help for the new API would be greatly appreciated!!
Thanks!
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline import CustomFactor
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.data import morningstar
class ROA(CustomFactor):
# Custom Factor for ROA
inputs = [morningstar.operation_ratios.roa]
window_length = 1
def compute(self, today, assets, out, roa):
out[:] = roa
class MarketCap(CustomFactor):
# Shares Outstanding
inputs = [morningstar.valuation.market_cap]
window_length = 1
def compute(self, today, assets, out, market_cap):
out[:] = market_cap
def initialize(context):
pipe = Pipeline()
attach_pipeline(pipe, "pipeline")
pipe.add(ROA(), "ROA")
pipe.add(MarketCap(), "Market_Cap")
market_cap = morningstar.valuation.market_cap
market_cap_filter = (market_cap > 500000000000)
pipe.set_screen(market_cap_filter)
def before_trading_start(context):
context.output = pipeline_output("pipeline")
context.output = context.output.sort(['ROA'], ascending=False)
context.stock_list = context.output
update_universe(context.stock_list.index)
def handle_data(context, data):
print "List"
log.info("\n" + str(context.stock_list))
cash = context.portfolio.cash
current_positions = context.portfolio.positions
for stock in data:
current_position = context.portfolio.positions[stock].amount
stock_price = data[stock].price
plausible_investment = cash / 10.0
amount_to_buy = int(plausible_investment / stock_price)
if stock_price < plausible_investment:
if current_position == 0:
order_target(stock, 1)