48 lines
2.1 KiB
Python
Executable File
48 lines
2.1 KiB
Python
Executable File
"""Cross-sectional momentum rotation with an absolute-momentum filter ("dual momentum").
|
|
|
|
Evidence: Liu, Tsyvinski & Wu (2022) "Common Risk Factors in Cryptocurrency" (Journal of
|
|
Finance) find 1-4 week cross-sectional momentum is one of three factors pricing crypto
|
|
returns; Jegadeesh & Titman (1993) for equities; Antonacci (2014) "Dual Momentum
|
|
Investing" for the absolute-momentum filter that moves to cash in bear markets.
|
|
"""
|
|
|
|
from . import indicators as ind
|
|
from .base import Context, Strategy, register
|
|
|
|
|
|
@register("cross_sectional_momentum")
|
|
class CrossSectionalMomentum(Strategy):
|
|
"""Every `rebalance_every` candles, hold the `top_k` pairs with the best trailing
|
|
return (inverse-volatility weighted), but only those whose return is positive.
|
|
Current holdings are kept while they rank within top_k + hold_buffer, which cuts
|
|
turnover substantially at little cost to the signal."""
|
|
|
|
default_params = {
|
|
"lookback": 21, "top_k": 3, "hold_buffer": 2, "rebalance_every": 7, "vol_window": 30, "band": 0.05,
|
|
}
|
|
|
|
def decide(self, ctx: Context) -> dict[str, float]:
|
|
p = self.params
|
|
ref = ctx.candles[self.pairs[0]]
|
|
if not ind.is_rebalance_candle(ref, self.interval, p["rebalance_every"]):
|
|
return {}
|
|
|
|
need = max(p["lookback"], p["vol_window"]) + 1
|
|
scores = {}
|
|
for pair in self.pairs:
|
|
close = ctx.candles[pair]["close"]
|
|
if len(close) > need:
|
|
scores[pair] = ind.total_return(close, p["lookback"])
|
|
winners = ind.select_with_buffer(scores, ctx.weights, p["top_k"], p["hold_buffer"])
|
|
|
|
inv_vol = {
|
|
pair: 1 / max(ind.realized_vol(ctx.candles[pair]["close"], p["vol_window"], self.interval), 1e-9)
|
|
for pair in winners
|
|
}
|
|
total = sum(inv_vol.values())
|
|
# Each winner's slot is 1/top_k of capital, so fewer winners means more cash.
|
|
budget = len(winners) / p["top_k"]
|
|
targets = {pair: 0.0 for pair in self.pairs}
|
|
targets.update({pair: budget * v / total for pair, v in inv_vol.items()})
|
|
return ind.banded(targets, ctx.weights, p["band"])
|