Hi,
Another follow-up basic Q: Am I approaching the Long/Short setup correctly?
The quantile format is decile
I want to be long the top decile and short the bottom decile
Am I approaching this the right way the code below?
Thanks!
def make_pipeline():
test_factor1=operation_ratios.roic.latest
test_factor2=valuation_ratios.dividend_yield.latest
test_factor3=valuation_ratios.cash_return.latest
test_factor4=volatility
universe=(Q1500US() &
test_factor1.notnull() &
test_factor2.notnull() &
test_factor3.notnull() &
test_factor4.notnull())
testing_factor1=test_factor1.rank(mask=universe, method='average')
testing_factor2=test_factor2.rank(mask=universe, method='average')
testing_factor3=test_factor3.rank(mask=universe, method='average')
testing_factor4=test_factor3.rank(mask=universe, method='average')
factor_test=testing_factor1 + testing_factor2 + testing_factor3 + testing_factor4
quantile_test=factor_test.quantiles(10)
pipe=Pipeline(columns={
'factor_test':factor_test,
'shorts' :quantile_test.eq(0),
'longs' :quantile_test.eq(9)},
screen=universe)
return pipe
def before_trading_start(context, data):
try:
"""
Called every day before market open.
"""
context.output = pipeline_output('my_pipeline')
context.security_list = context.output.index.tolist()
except Exception as e:
print(str(e))
def my_rebalance(context,data):
"""
Place orders according to our schedule_function() timing.
"""
# Compute our portfolio weights.
long_secs = context.output[context.output['longs']].index
long_weight = 0.5 / len(long_secs)
short_secs = context.output[context.output['shorts']].index
short_weight = -0.5 / len(short_secs)
for security in long_secs:
if data.can_trade(security):
order_target_percent(security, long_weight)
for security in short_secs:
if data.can_trade(security):
order_target_percent(security, short_weight)
for security in context.portfolio.positions:
if data.can_trade(security) and security not in long_secs and security not in short_secs:
order_target_percent(security, 0)