SPB Git

Repository Public

strategy.py 1.0 KB 31 lines python

1"""Cross-sectional momentum: hold the assets of the universe with the highest past return.23Universe-generic (version 3): the symbols are whatever the agent trades; same rule and parameters as version 1.4"""56META = {7    "name": "xs_momentum",8    "version": "3",9    "universe": ["*"],10    "min_symbols": 3,11    "interval": "1h",12    "lookback": 1500,13    "params": {"window": 720, "top_k": 2},14}151617def target_weights(data, params):18    window, top_k = int(params["window"]), int(params["top_k"])19    past = []20    for symbol, df in data.items():21        close = df["close"].to_numpy()22        if len(close) > window:23            past.append((-(close[-1] / close[-1 - window] - 1.0), symbol))24    weights = {symbol: 0.0 for symbol in data}25    if top_k < 1 or len(data) < 3 or len(past) < 2:   # a ranking needs at least 3 symbols: cash otherwise26        return weights27    winners = sorted(past)[:min(top_k, len(past))]   # best return first, ties broken by symbol28    for _, symbol in winners:29        weights[symbol] = 1.0 / len(winners)30    return weights31
Files main
Menu

Theme