If you like a good puzzle you'll like this. Just ignore these results and take a look at beta and leverage in the following backtest ...
Wouldn't it be great to have a function that can be plugged into a long-short strategy to help manage both beta and leverage? This backtest does ok in using zero-beta targeting, a simplified version of that. It modifies not the numbers of long-short sids but instead the overall pool of new value each day for each. It says, for example, if beta is -.05, then set long allocation to 1.05 and short to .95, basically.
Leverage flies to the moon at nearly 11, what's up with that? The leverage calculation behind context.account.leverage is ...
( abs(shorts_value) + longs_value) / portfolio_value )
Of course cash is part of portfolio_value (shorts_value + longs_value + cash).
One way leverage can increase is with profit on shorts. Growing profit on short shares will drive the numerator up (as abs turns the negative value of owned shorted shares into a positive) while the increasingly negative value of owned shorted shares (profitable) drives portfolio_value in the denominator downward.
Fiddling around with some numbers in Excel, it looks like leverage is held to 1 if cash is twice the absolute (made positive) value of shorted shares. Meanwhile seems one can have as many longs as they want with little effect as long as cash remains positive. Really? Anyway...
I see this as an irresistible complex conundrum, hope some of you do too. And I'll bet a couple of you could provide interesting ways to move this toward a more comprehensive function that can make adjustments for beta while keeping leverage in mind.
Link to the original is in the Source. Strategy and what the eye sees in returns are not important and can be replaced with any other both long both short code that you wish. There's some commented logging stuff at the bottom you can uncomment. The output looks like
2015-11-03 06:34 handle_data:353 INFO 175 ADI val 10542 cb 60.58 prc 60.24 pnl -59 lv 1.01
2015-11-03 06:34 handle_data:353 INFO 102 UPS val 10599 cb 103.68 prc 103.92 pnl 24 lv 1.01
2015-11-03 06:34 WARN Logging limit exceeded; some messages discarded
2015-11-03 06:43 handle_data:353 INFO 415 CNP val 7673 cb 18.50 prc 18.49 pnl -4 lv 1.54
2015-11-03 06:43 handle_data:353 INFO 164 RL val 18517 cb 111.06 prc 112.91 pnl 303 lv 1.54
2015-11-03 06:43 handle_data:353 INFO -1075 ARNC val -10051 cb 9.31 prc 9.35 pnl -42 lv 1.54
2015-11-03 06:43 handle_data:353 INFO 57 CI val 7575 cb 132.74 prc 132.91 pnl 9 lv 1.54
2015-11-03 06:43 handle_data:353 INFO -324 MUR val -10073 cb 30.92 prc 31.09 pnl -55 lv 1.54
In the chart, leverage hits one new high around the time that cash hits a low (you would have to zoom in to really see that, to undo the smoothing), then the next new leverage high when cash is high. Not everyone is aware that you can click chart legend items to toggle them off and on. Clicking 'cash' for example to turn that off makes it easier to see what leverage is doing. If anyone wants to nail down some of the specific details in this leverage, some one-liners might be useful.
Among many questions, one might be: As PnL for some shorts go up, driving leverage up, would it make sense to observe leverage climbing and be buying back some of the most profitable shorted shares if one wants to keep leverage under a particular ceiling? (contest limit is 3.0).