Repository Public
1"""Mean reversion: buy an asset in proportion to how far it trades below its recent average (z-score).23Universe-generic (version 3): the symbols are whatever the agent trades; same rule and parameters as version 1.4"""56import numpy as np78META = {9 "name": "mean_reversion",10 "version": "3",11 "universe": ["*"],12 "interval": "1h",13 "lookback": 200,14 "params": {"window": 48, "z_full": 2.0},15}161718def target_weights(data, params):19 window, z_full = int(params["window"]), float(params["z_full"])20 n = len(data)21 weights = {}22 for symbol, df in data.items():23 close = df["close"].to_numpy()24 if len(close) < window or window < 2 or z_full <= 0:25 weights[symbol] = 0.026 continue27 recent = close[-window:]28 std = recent.std(ddof=1)29 if not std > 0:30 weights[symbol] = 0.031 continue32 z = (close[-1] - recent.mean()) / std33 weights[symbol] = float(np.clip(-z / z_full, 0.0, 1.0)) / n34 return weights35