Hey guys! This is one of my first algos. My Clenow's momentum strategy implementation keeps going negative on cash raising leverage over 20! Can anyone look at my code and tell what I am doing wrong? Much appreciated!!
Hey guys! This is one of my first algos. My Clenow's momentum strategy implementation keeps going negative on cash raising leverage over 20! Can anyone look at my code and tell what I am doing wrong? Much appreciated!!
Normalize weights_pct:
weights_pct /= weights_pct.sum()
That's shorthand for:
weights_pct = weights_pct / weights_pct.sum()
All of the values will still be spaced proportionally to each other, just that if you add them up, the total will be 1.0.
Then if you find that the portfolio moves downward consistently, that's good, it means you do have something and just need to flip some logic.
I haven't heard this mentioned so I guess I will: If you're using the debugger (clicked a line number) and trying to examine context.output for example, it might kick out with an error that goes by too fast to read. It's just a bug based on size I think. You can do context.output.head(), context.output.tail(), context.output.head(9) and so forth.
thanks man! negative cash got resolved, the point of this strategy is to buy stocks from the top of the list until cash is zero whereas if weights are normalized does this mean that I am buying all the stocks that came out from the pipeline?
Depends on what your other code is doing. One route is to isolate long and short series' from the pipeline output and then normalize each of those. Could first normalize atr and score for example, combine them afterward, take some number of the head and tail at that point (the cream of the crop for each) as two new series, one for long and one for short, and then the total of their values will no longer be 1.0 (since an operation was done on them, added, multiplied or whatever), so then normalize those two (long and short) individually to return the values of each to 1.0. Experiment around in the debugger.
This would normalize all columns all at once:
context.output = pipeline_output('my_pipeline')
context.output /= context.output.sum(axis=0)
Then the sum for each column is 1.0:
context.output.sum()
Something along this line ...
def before_trading_start(context, data):
num = 20
# o as shortcut for less typing in debugger
o = context.output = pipeline_output('my_pipeline')
o /= o.sum(axis=0)
#o.sort_values('score', inplace=True, ascending=False)
o['z'] = (o['atr'] + o['score']) # a new column, combination of atr and score or or.
#longs = o.z[ o.z > o.z.mean() * 1.3 ]
#shrts = o.z[ o.z < o.z.mean() * .7 ]
context.longs = o.z.sort_values(ascending=False).head(num)
context.shrts = o.z.sort_values(ascending=False).tail(num)
# try working with these
context.longs /= context.longs.sum()
context.shrts /= context.shrts.sum()
# Including some original code ...
context.security_list = o.index
context.spy_ma200 = data.history(context.spy,'price',200,'1d').mean()
context.spy_price = data.current(context.spy,'price')
context.stocks_to_sell_immediately = o[o['maxmove'] > .15].index.tolist()