As the title states, I'm in need of help weighting stocks based on their location on a list (ranked)
I am re-sorting my outputs before the rebalancing takes place, so the equities are in the correct order before purchase.
def before_trading_start(context, data):
# ascending=false for 'higher is better'
# ascending=true for 'lower is better'
context.output = pipeline_output('My_pipeline')
context.long_list = context.output.sort_values(['filter 1'],
ascending=False).iloc[:150]
context.long_list = context.long_list.sort_values(['filter 2'],
ascending=False).iloc[:50]
context.long_list = context.long_list.sort_values(['filter 3'],
ascending=False).iloc[:10]
# for weighting purposes, largest factor first
context.long_list = context.long_list.sort_values(['filter 1'],
ascending=False).iloc[:10]
So at this point, the equities are in order. Good. Now, let's buy the 10 equities in that list weighing each according to an arbitrary weight (just fiddling around with it, no alpha with these weights). This is where things go not so good. I tried creating a laaaarge for loop, to no avail:
for stock in context.long_list.index:
if context.long_list.index[0]:
long_weight = .13
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[1]:
long_weight = .115
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[2]:
long_weight = .106
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[3]:
long_weight = .101
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[4]:
long_weight = .097
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[5]:
long_weight = .094
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[6]:
long_weight = .092
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[7]:
long_weight = .09
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[8]:
long_weight = .088
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
if context.long_list.index[9]:
long_weight = .086
order_target_percent(stock, long_weight)
log.info("Bought " + str(stock))
Using this approach, leverage ended up scary large. No dice there.
Then I tried to apply the weights to each equity before the purchase for loop, again to no avail:
for stock in context.long_list.index:
for stock in context.long_list.index[0]:
long_weight = .13
for stock in context.long_list.index[1]:
long_weight = .115
for stock in context.long_list.index[2]:
long_weight = .106
for stock in context.long_list.index[3]:
long_weight = .101
for stock in context.long_list.index[4]:
long_weight = .097
for stock in context.long_list.index[5]:
long_weight = .094
for stock in context.long_list.index[6]:
long_weight = .092
for stock in context.long_list.index[7]:
long_weight = .09
for stock in context.long_list.index[8]:
long_weight = .088
for stock in context.long_list.index[9]:
long_weight = .086
Using this approach, the algo didn't run, but after some fiddling and finagling, it ran, but used .086 as the overall weight for each equity.
I am at a loss. Could anyone help out by pointing me in the right direction with my code, linking a simple-to-understand algo that implements a similar weighing strategy, etc?
Many thanks in advance.
Joe