"""TRD12 — Triple-MA alignment (SMA10 > SMA50 > SMA200). Long only when all three SMAs are in full bullish alignment; flat otherwise. No look-ahead: SMA values at i use close[0..i], position held during bar i+1. """ import sys sys.path.insert(0, "/opt/docker/PythagorasGoal/scripts/research/alt") import altlib as al import numpy as np def triple_ma_weights(df, short=10, mid=50, long=200, use_vol_target=True): """Return position array: +1 when SMA_short > SMA_mid > SMA_long, else 0.""" c = df["close"].values s = al.sma(c, short) m = al.sma(c, mid) l = al.sma(c, long) # Bullish alignment: short > mid > long bullish = (s > m) & (m > l) # Direction: +1 or 0 (long-only) direction = np.where(bullish, 1.0, 0.0) # Replace NaN regions (first `long` bars) with 0 direction = np.where(np.isnan(s) | np.isnan(m) | np.isnan(l), 0.0, direction) if use_vol_target: return al.vol_target(direction, df, target_vol=0.20, vol_win_days=30, leverage_cap=2.0) else: return direction # Run study on 1d and 12h timeframes (Triple-MA needs long history, so >=12h) # We try two configurations: with and without vol-targeting # That's 2 configs x 2 TFs = 4 total backtests (within the <=6 limit) print("=" * 60) print("TRD12 — Triple-MA alignment (SMA10 > SMA50 > SMA200)") print("=" * 60) # Config 1: with vol-targeting rep_vt = al.study_weights( "TRD12-VT", lambda df: triple_ma_weights(df, use_vol_target=True), tfs=("1d", "12h"), ) print("\n--- Vol-targeted ---") print(al.fmt(rep_vt)) print("JSON:", al.as_json(rep_vt)) # Config 2: raw (no vol-targeting, simple long/flat) rep_raw = al.study_weights( "TRD12-RAW", lambda df: triple_ma_weights(df, use_vol_target=False), tfs=("1d", "12h"), ) print("\n--- Raw (no vol-target) ---") print(al.fmt(rep_raw)) print("JSON:", al.as_json(rep_raw))