I'm trying to figure out how to search on two criteria. One to buy and one to sell. For example if I buy for pe 17. I'm trying to do this using QTradeableUS() and order_optimal_portfolio() as required by the contest
I can create the pipeline for the buy using this:
base_universe = Q1500US()
sentiment_score = SimpleMovingAverage(
inputs=[stocktwits.bull_minus_bear],
window_length=3,
mask=QTradableStocksUS()
)
# Factor of yesterday's close price.
yesterday_close = USEquityPricing.close.latest
pe_ratio = Fundamentals.pe_ratio.latest
pe_ratio_filter = (pe_ratio < 14) & pe_ratio.notnull()
pipe = Pipeline(
screen = (base_universe & pe_ratio_filter & sentiment_score.notnull()),
...
)
And place the order using this:
context.output = pipeline_output('my_pipeline')
context.max_leverage = 1.0
context.max_pos_size = .01
context.max_turnover = 0.95
objective = opt.MaximizeAlpha(
context.output['sentiment_score']
)
# Create position size constraint
constrain_pos_size = opt.PositionConcentration.with_equal_bounds(
-context.max_pos_size,
context.max_pos_size
)
# Ensure long and short books
# are roughly the same size
dollar_neutral = opt.DollarNeutral()
# Constrain target portfolio's leverage
max_leverage = opt.MaxGrossExposure(context.max_leverage)
# Constrain portfolio turnover
max_turnover = opt.MaxTurnover(context.max_turnover)
# Rebalance portfolio using objective
# and list of constraints
order_optimal_portfolio(
objective=objective,
constraints=[
max_leverage,
dollar_neutral,
constrain_pos_size,
max_turnover,
]
)
But I can't see how to setup the second pipeline for the sell. I'd like to retain stocks that have pe between 14 and 17