Browse AI-generated trading strategies shared by the community. Fork, learn, and build on each other's work.
| Score▼ | Strategy | Author | Win Rate▼ | Return▼ | PF▼ | MDD▼ | Trades▼ | Actions | ||
|---|---|---|---|---|---|---|---|---|---|---|
|
0.87
|
USD/JPY BB Mean-Reversion + ATR Gradient Boost
Maximise Sharpe ratio via a Gradient Boosting classifier trained on Bollinger Band position (bb_pct), normalised bandwidth (bb_width), ATR/N…
|
R
@ratio_witch
|
USDJPY | 15min | 60.2%60.4% | +4.28%+2.23% | 1.231.49 | 2.32%2.32% | 16691 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 02:01:39
# Model : Gradient Boosting
# Feature Eng. : BB (20,2.0), ATR 14 + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDJPY_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── Bollinger Bands (period=20, std_dev=2.0) ──────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_sigma = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_sigma
bb_lower = bb_mid - bb_std * bb_sigma
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
# bb_width: normalised band width (volatility proxy)
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
# bb_pct: position of close within the band [0, 1]
band_range = bb_upper - bb_lower
df["bb_pct"] = (close - bb_lower) / band_range
# Distance from close to mid in units of band width
df["bb_dist_mid"] = (close - bb_mid) / bb_mid
# ── ATR (period=14) ───────────────────────────────────────────────────────
atr_period = 14
prev_close = close.shift(1)
tr = pd.concat([
high - low,
(high - prev_close).abs(),
(low - prev_close).abs()
], axis=1).max(axis=1)
atr = tr.ewm(span=atr_period, min_periods=atr_period, adjust=False).mean()
natr = atr / close
df["atr"] = atr
df["natr"] = natr
# ── Momentum / trend features ─────────────────────────────────────────────
# Rate of change at multiple horizons
for n in [1, 4, 8, 16]:
df[f"roc_{n}"] = close.pct_change(n)
# RSI (14)
rsi_period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_gain = gain.ewm(span=rsi_period, min_periods=rsi_period, adjust=False).mean()
avg_loss = loss.ewm(span=rsi_period, min_periods=rsi_period, adjust=False).mean()
rs = avg_gain / (avg_loss + 1e-10)
rsi = 100 - (100 / (1 + rs))
df["rsi_14"] = rsi
# RSI derived: distance from 50 (centred, normalised)
df["rsi_dev"] = (rsi - 50) / 50
# ── MACD (12, 26, 9) ──────────────────────────────────────────────────────
ema12 = close.ewm(span=12, adjust=False).mean()
ema26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema12 - ema26
macd_signal = macd_line.ewm(span=9, adjust=False).mean()
macd_hist = macd_line - macd_signal
df["macd_line"] = macd_line
df["macd_signal"] = macd_signal
df["macd_hist"] = macd_hist
# ── Trend (SMA 50) ────────────────────────────────────────────────────────
sma50 = close.rolling(50).mean()
df["sma_50"] = sma50
df["close_vs_sma50"] = (close - sma50) / sma50 # normalised distance
# ── Volume / candle structure features ────────────────────────────────────
body = (close - open_).abs()
candle_rng = high - low
df["body_ratio"] = body / (candle_rng + 1e-10) # body as fraction of range
df["upper_wick"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / (candle_rng + 1e-10)
df["lower_wick"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / (candle_rng + 1e-10)
df["candle_dir"] = np.where(close > open_, 1.0, -1.0) # bullish / bearish bar
# ── Lagged bb_pct & rsi (to give the model recent history) ───────────────
for lag in [1, 2, 3]:
df[f"bb_pct_lag{lag}"] = df["bb_pct"].shift(lag)
df[f"rsi_lag{lag}"] = df["rsi_14"].shift(lag)
df[f"macd_hist_lag{lag}"] = df["macd_hist"].shift(lag)
# ── Volatility regime flag ────────────────────────────────────────────────
natr_ma = natr.rolling(50).mean()
df["vol_regime"] = np.where(natr > natr_ma, 1.0, 0.0) # 1 = high-vol regime
# ── BB squeeze detection ──────────────────────────────────────────────────
bb_width_ma = df["bb_width"].rolling(50).mean()
df["bb_squeeze"] = np.where(df["bb_width"] < bb_width_ma, 1.0, 0.0)
# ── Mean-reversion signal strength ────────────────────────────────────────
# Positive → oversold (close below lower band), Negative → overbought
df["mr_signal"] = 0.5 - df["bb_pct"] # centred: +0.5 at lower band, -0.5 at upper
# ── Fill NaN from warm-up ─────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/JPY BB Mean-Reversion + ATR Gradient Boost",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 500,
"max_depth": 4,
"learning_rate": 0.03,
"subsample": 0.8,
"min_samples_leaf": 20,
"max_features": "sqrt",
"validation_fraction": 0.1,
"n_iter_no_change": 30,
"tol": 1e-4,
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": None,
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise Sharpe ratio via a Gradient Boosting classifier trained on "
"Bollinger Band position (bb_pct), normalised bandwidth (bb_width), "
"ATR/NATR, RSI, MACD histogram, candle structure, and lagged features. "
"GBM chosen for its ability to capture non-linear interactions between "
"volatility (ATR) and mean-reversion (BB) signals. n_iter_no_change "
"acts as early stopping to prevent overfitting on the 15-min USDJPY series. "
"SL=0.5% / TP=1.0% gives a 1:2 risk-reward; threshold=0.55 reduces noise trades."
),
"notes": (
"Bollinger Bands are the primary mean-reversion anchor; ATR/NATR filter "
"entries to adequate volatility bars. RSI and MACD provide momentum context "
"to avoid fading strong trends. Lagged features (up to 3 bars) give the model "
"short-term regime memory without look-ahead. vol_regime and bb_squeeze flags "
"allow the model to differentiate trending vs. ranging conditions automatically. "
"No session filter applied — USDJPY is liquid across Asian and European sessions."
),
}
|
||||||||||
|
0.83
|
NZD/USD MACD+RSI Gradient Boosting Risk-Adjusted
Maximize risk-adjusted return (Sharpe/Calmar) on NZD/USD 15-min data. GradientBoostingClassifier chosen for strong generalisation with tabul…
|
V
@vol_drifter
|
NZDUSD | 15min | 60.9%71.3% | +18.36%+3.18% | 1.351.40 | 3.80%3.80% | 732108 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:16:23
# Model : Gradient Boosting
# Feature Eng. : RSI 14, MACD (12,26,9) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/NZDUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── RSI 14 ──────────────────────────────────────────────────────────────
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=13, min_periods=14, adjust=False).mean()
avg_loss = loss.ewm(com=13, min_periods=14, adjust=False).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi_14"] = 100 - (100 / (1 + rs))
# RSI derived signals
df["rsi_overbought"] = np.where(df["rsi_14"] > 70, 1, 0)
df["rsi_oversold"] = np.where(df["rsi_14"] < 30, 1, 0)
df["rsi_mid_cross"] = np.where(df["rsi_14"] > 50, 1, -1)
# RSI momentum (rate of change of RSI)
df["rsi_roc"] = df["rsi_14"].diff(3)
# ── MACD (12, 26, 9) ────────────────────────────────────────────────────
ema_12 = close.ewm(span=12, adjust=False).mean()
ema_26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema_12 - ema_26
signal_line = macd_line.ewm(span=9, adjust=False).mean()
macd_hist = macd_line - signal_line
df["macd_line"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_hist
# MACD derived signals
df["macd_cross"] = np.where(macd_line > signal_line, 1, -1)
df["macd_hist_sign"] = np.where(macd_hist > 0, 1, -1)
df["macd_hist_accel"] = macd_hist.diff()
# ── ATR (14) ─────────────────────────────────────────────────────────────
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr_14"] = tr.ewm(com=13, min_periods=14, adjust=False).mean()
df["natr_14"] = df["atr_14"] / close
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
sma_20 = close.rolling(20).mean()
std_20 = close.rolling(20).std()
bb_upper = sma_20 + 2 * std_20
bb_lower = sma_20 - 2 * std_20
df["bb_width"] = (bb_upper - bb_lower) / sma_20
df["bb_position"] = (close - bb_lower) / (bb_upper - bb_lower).replace(0, np.nan)
df["bb_squeeze"] = np.where(df["bb_width"] < df["bb_width"].rolling(50).mean(), 1, 0)
# ── Trend / SMA filters ──────────────────────────────────────────────────
df["sma_20"] = sma_20
df["sma_50"] = close.rolling(50).mean()
df["sma_200"] = close.rolling(200).mean()
df["price_above_sma50"] = np.where(close > df["sma_50"], 1, -1)
df["price_above_sma200"] = np.where(close > df["sma_200"], 1, -1)
df["sma50_above_sma200"] = np.where(df["sma_50"] > df["sma_200"], 1, -1)
# ── Momentum / ROC ───────────────────────────────────────────────────────
df["roc_4"] = close.pct_change(4)
df["roc_8"] = close.pct_change(8)
df["roc_16"] = close.pct_change(16)
# ── Candlestick / price structure ────────────────────────────────────────
df["body"] = (close - open_) / close
df["upper_wick"] = (high - close.clip(lower=open_)) / close
df["lower_wick"] = (open_.clip(upper=close) - low) / close
df["hl_range"] = (high - low) / close
df["gap"] = (open_ - close.shift(1)) / close.shift(1)
# ── Volume-free spread proxy ─────────────────────────────────────────────
df["spread_ratio"] = df["hl_range"] / df["atr_14"].replace(0, np.nan)
# ── Stochastic RSI proxy ─────────────────────────────────────────────────
rsi_min = df["rsi_14"].rolling(14).min()
rsi_max = df["rsi_14"].rolling(14).max()
df["stoch_rsi"] = (df["rsi_14"] - rsi_min) / (rsi_max - rsi_min).replace(0, np.nan)
# ── Williams %R (14) ─────────────────────────────────────────────────────
highest_high = high.rolling(14).max()
lowest_low = low.rolling(14).min()
df["williams_r"] = -100 * (highest_high - close) / (highest_high - lowest_low).replace(0, np.nan)
# ── Lagged features ──────────────────────────────────────────────────────
for lag in [1, 2, 3, 4]:
df[f"rsi_lag{lag}"] = df["rsi_14"].shift(lag)
df[f"macd_hist_lag{lag}"] = df["macd_hist"].shift(lag)
df[f"roc4_lag{lag}"] = df["roc_4"].shift(lag)
# ── Fill NaN from indicator warm-up ─────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "NZD/USD MACD+RSI Gradient Boosting Risk-Adjusted",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.03,
"subsample": 0.8,
"min_samples_leaf": 20,
"max_features": "sqrt",
"validation_fraction": 0.1,
"n_iter_no_change": 30,
"tol": 1e-4,
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [0, 23],
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) on NZD/USD 15-min data. "
"GradientBoostingClassifier chosen for strong generalisation with tabular features, "
"low learning rate + early stopping prevents overfitting. "
"SL=0.5%, TP=1.0% gives 1:2 R:R ratio. Threshold=0.55 filters marginal signals."
),
"notes": (
"Core features: RSI-14, MACD(12,26,9) with histogram momentum. "
"Supplementary: ATR, Bollinger Bands, SMA trend, Williams %R, StochRSI, "
"candlestick structure, lagged RSI/MACD/ROC. "
"reverse on opposite signal captures trend continuation. "
"target_horizon=4 bars (1 hour) aligns with typical MACD/RSI signal duration."
),
}
|
||||||||||
|
0.83
|
NZD/USD Stoch+BB+RSI Gradient Boosting Mean-Revert
Maximise risk-adjusted return (Sharpe / Calmar) on NZD/USD 15-min. GradientBoostingClassifier selected for its strong generalisation on stru…
|
C
@candle_owl
|
NZDUSD | 15min | 59.1%71.9% | +3.83%+3.26% | 1.101.73 | 4.90%4.90% | 38157 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:54:56
# Model : Gradient Boosting
# Feature Eng. : BB (20,2.0), RSI 14, Stochastic (14,3) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/NZDUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_std_ = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_std_
bb_lower = bb_mid - bb_std * bb_std_
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
df["bb_pct"] = (close - bb_lower) / (bb_upper - bb_lower)
# ── RSI (14) ─────────────────────────────────────────────────────────────
rsi_period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
avg_loss = loss.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi"] = 100 - (100 / (1 + rs))
# ── Stochastic Oscillator (K=14, D=3) ────────────────────────────────────
stoch_k_period = 14
stoch_d_period = 3
lowest_low = low.rolling(stoch_k_period).min()
highest_high = high.rolling(stoch_k_period).max()
stoch_range = (highest_high - lowest_low).replace(0, np.nan)
df["stoch_k"] = 100 * (close - lowest_low) / stoch_range
df["stoch_d"] = df["stoch_k"].rolling(stoch_d_period).mean()
df["stoch_kd_diff"] = df["stoch_k"] - df["stoch_d"]
# ── ATR (14) — for normalised volatility / min_atr filter ────────────────
atr_period = 14
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
atr = tr.ewm(com=atr_period - 1, min_periods=atr_period).mean()
df["atr"] = atr
df["natr"] = atr / close # normalised ATR used by min_atr filter
# ── Price momentum / rate-of-change ──────────────────────────────────────
df["roc_4"] = close.pct_change(4) # 1-hour momentum on 15-min bars
df["roc_8"] = close.pct_change(8) # 2-hour momentum
df["roc_16"] = close.pct_change(16) # 4-hour momentum
# ── EMA trend context ─────────────────────────────────────────────────────
df["ema_20"] = close.ewm(span=20, adjust=False).mean()
df["ema_50"] = close.ewm(span=50, adjust=False).mean()
df["ema_100"] = close.ewm(span=100, adjust=False).mean()
df["sma_50"] = close.rolling(50).mean() # used by trend_filter
df["ema_cross_20_50"] = df["ema_20"] - df["ema_50"]
df["ema_cross_50_100"] = df["ema_50"] - df["ema_100"]
df["close_vs_ema20"] = (close - df["ema_20"]) / df["ema_20"]
# ── Candlestick body / wick features ─────────────────────────────────────
df["body"] = (close - open_).abs()
df["candle_dir"] = np.where(close >= open_, 1.0, -1.0)
df["upper_wick"] = high - pd.concat([close, open_], axis=1).max(axis=1)
df["lower_wick"] = pd.concat([close, open_], axis=1).min(axis=1) - low
df["body_ratio"] = df["body"] / (high - low).replace(0, np.nan)
# ── Volume-proxy: realised range rolling stats ────────────────────────────
df["hl_range"] = high - low
df["hl_range_ma8"] = df["hl_range"].rolling(8).mean()
df["hl_range_ratio"]= df["hl_range"] / df["hl_range_ma8"]
# ── RSI derived signals ───────────────────────────────────────────────────
df["rsi_overbought"] = np.where(df["rsi"] > 70, 1.0, 0.0)
df["rsi_oversold"] = np.where(df["rsi"] < 30, 1.0, 0.0)
df["rsi_momentum"] = df["rsi"].diff(4)
# ── Stochastic derived signals ────────────────────────────────────────────
df["stoch_overbought"] = np.where(df["stoch_k"] > 80, 1.0, 0.0)
df["stoch_oversold"] = np.where(df["stoch_k"] < 20, 1.0, 0.0)
# ── BB squeeze: width vs rolling mean of width ────────────────────────────
df["bb_width_ma20"] = df["bb_width"].rolling(20).mean()
df["bb_squeeze"] = np.where(df["bb_width"] < df["bb_width_ma20"], 1.0, 0.0)
# ── Interaction features ──────────────────────────────────────────────────
df["rsi_bb_pct"] = df["rsi"] * df["bb_pct"]
df["stoch_k_bb_pct"] = df["stoch_k"] * df["bb_pct"]
df["rsi_stoch_diff"] = df["rsi"] - df["stoch_k"]
# ── Lagged features (avoids look-ahead) ──────────────────────────────────
for lag in [1, 2, 3, 4]:
df[f"rsi_lag{lag}"] = df["rsi"].shift(lag)
df[f"stoch_k_lag{lag}"] = df["stoch_k"].shift(lag)
df[f"bb_pct_lag{lag}"] = df["bb_pct"].shift(lag)
df[f"roc4_lag{lag}"] = df["roc_4"].shift(lag)
# ── Fill NaN from warm-up ─────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "NZD/USD Stoch+BB+RSI Gradient Boosting Mean-Revert",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.8,
"min_samples_leaf": 20,
"max_features": "sqrt",
"validation_fraction": 0.1,
"n_iter_no_change": 30,
"tol": 1e-4,
"random_state": 42,
},
"signal_threshold": 0.56,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 20],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise risk-adjusted return (Sharpe / Calmar) on NZD/USD 15-min. "
"GradientBoostingClassifier selected for its strong generalisation on "
"structured tabular data with noisy financial features. n_estimators=400 "
"with early stopping (n_iter_no_change=30) prevents overfitting. "
"max_depth=4 keeps trees shallow to reduce variance. subsample=0.8 + "
"max_features=sqrt add stochasticity for robustness. SL 0.5% / TP 1.0% "
"gives a minimum 2:1 reward-risk ratio. Session filter 07-20 UTC covers "
"Sydney open through NY overlap, maximising NZD/USD liquidity. "
"Reverse on opposite signal keeps the model continuously positioned in "
"the highest-confidence direction. min_atr filter avoids flat/illiquid "
"periods where the model edges degrade."
),
"notes": (
"Features: Bollinger Bands (20,2) width & %B, RSI(14), Stochastic K/D "
"(14,3), ATR(14)/NATR, EMA cross (20/50/100), SMA50 trend context, "
"price ROC (4/8/16 bars), candlestick body/wick ratios, HL range "
"normalisation, BB squeeze flag, RSI/Stoch overbought-oversold flags, "
"interaction terms (RSI*%B, StochK*%B), and 4 lags each of RSI, StochK, "
"%B and ROC4. Threshold 0.56 slightly above 0.50 to filter marginal "
"signals without sacrificing too many trades."
),
}
|
||||||||||
|
0.59
|
USD/JPY Multi-MA + RSI/BB XGBoost Sharpe
Maximize Sharpe ratio on USD/JPY 1-min data using XGBoost with returns, RSI, Bollinger Bands, multiple MAs (50/100/200), MACD, ATR, and cand…
|
M
@malcolmtan
|
USD/JP | 60.7%— | +0.42%— | 1.22— | 0.53%0.53% | 84— |
|
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-08 02:08:02
# Model : XGBoost
# Feature Eng. : Auto-add features: ON
# Signal / Entry : —
# Optimization : —
# Risk Mgmt : —
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDJPY_1min.parquet"
START_DATE = "2026-05-04 00:00:00"
END_DATE = "2026-05-07 00:00:00"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.6993736951983298
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# --- Returns over multiple horizons ---
for n in [1, 3, 5, 10, 20]:
df[f"ret_{n}"] = close.pct_change(n)
# --- RSI 14 ---
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=13, min_periods=14).mean()
avg_loss = loss.ewm(com=13, min_periods=14).mean()
rs = avg_gain / (avg_loss + 1e-10)
df["rsi_14"] = 100.0 - (100.0 / (1.0 + rs))
# --- RSI derived features ---
df["rsi_14_zscore"] = (df["rsi_14"] - df["rsi_14"].rolling(50).mean()) / (df["rsi_14"].rolling(50).std() + 1e-10)
df["rsi_overbought"] = np.where(df["rsi_14"] > 70, 1, 0)
df["rsi_oversold"] = np.where(df["rsi_14"] < 30, 1, 0)
# --- Bollinger Bands 20, 2 ---
bb_mid = close.rolling(20).mean()
bb_std = close.rolling(20).std()
bb_upper = bb_mid + 2.0 * bb_std
bb_lower = bb_mid - 2.0 * bb_std
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / (bb_mid + 1e-10)
df["bb_pct_b"] = (close - bb_lower) / (bb_upper - bb_lower + 1e-10)
df["bb_above"] = np.where(close > bb_upper, 1, 0)
df["bb_below"] = np.where(close < bb_lower, 1, 0)
# --- Moving Averages ---
for w in [50, 100, 200]:
df[f"sma_{w}"] = close.rolling(w).mean()
df[f"price_vs_sma_{w}"] = (close - df[f"sma_{w}"]) / (df[f"sma_{w}"] + 1e-10)
# --- MA crossover signals ---
df["sma50_vs_sma100"] = np.where(df["sma_50"] > df["sma_100"], 1, -1)
df["sma50_vs_sma200"] = np.where(df["sma_50"] > df["sma_200"], 1, -1)
df["sma100_vs_sma200"] = np.where(df["sma_100"] > df["sma_200"], 1, -1)
# --- ATR 14 ---
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr_14"] = tr.ewm(com=13, min_periods=14).mean()
df["natr_14"] = df["atr_14"] / (close + 1e-10)
# --- Momentum / rate of change ---
for n in [5, 10, 20]:
df[f"mom_{n}"] = close - close.shift(n)
df[f"roc_{n}"] = (close - close.shift(n)) / (close.shift(n) + 1e-10)
# --- Volume features (if volume exists) ---
if "volume" in df.columns:
vol = df["volume"].replace(0, np.nan)
df["vol_sma_20"] = vol.rolling(20).mean()
df["vol_ratio_20"] = vol / (df["vol_sma_20"] + 1e-10)
else:
df["vol_ratio_20"] = 1.0
# --- Price spread & body features ---
df["hl_spread"] = (high - low) / (close + 1e-10)
df["body_ratio"] = (close - open_).abs() / (high - low + 1e-10)
df["upper_wick"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / (high - low + 1e-10)
df["lower_wick"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / (high - low + 1e-10)
df["bull_candle"] = np.where(close > open_, 1, 0)
# --- Lagged returns for autocorrelation signal ---
for lag in [1, 2, 3, 5]:
df[f"ret1_lag{lag}"] = df["ret_1"].shift(lag)
# --- Rolling volatility ---
df["vol_10"] = df["ret_1"].rolling(10).std()
df["vol_20"] = df["ret_1"].rolling(20).std()
df["vol_50"] = df["ret_1"].rolling(50).std()
# --- Z-score of close over 20 and 50 bars ---
df["zscore_20"] = (close - close.rolling(20).mean()) / (close.rolling(20).std() + 1e-10)
df["zscore_50"] = (close - close.rolling(50).mean()) / (close.rolling(50).std() + 1e-10)
# --- Relative distance of price from BB bands ---
df["dist_upper"] = (bb_upper - close) / (close + 1e-10)
df["dist_lower"] = (close - bb_lower) / (close + 1e-10)
# --- EMA 9 and 21 for short-term momentum ---
df["ema_9"] = close.ewm(span=9, min_periods=9).mean()
df["ema_21"] = close.ewm(span=21, min_periods=21).mean()
df["ema9_vs_ema21"] = np.where(df["ema_9"] > df["ema_21"], 1, -1)
df["price_vs_ema9"] = (close - df["ema_9"]) / (df["ema_9"] + 1e-10)
df["price_vs_ema21"] = (close - df["ema_21"]) / (df["ema_21"] + 1e-10)
# --- MACD-like signal ---
ema_12 = close.ewm(span=12, min_periods=12).mean()
ema_26 = close.ewm(span=26, min_periods=26).mean()
macd_line = ema_12 - ema_26
signal_line = macd_line.ewm(span=9, min_periods=9).mean()
df["macd"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_line - signal_line
df["macd_cross"] = np.where(macd_line > signal_line, 1, -1)
# --- Fill NaN from warm-up periods ---
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/JPY Multi-MA + RSI/BB XGBoost Sharpe",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"colsample_bytree": 0.75,
"min_child_weight": 5,
"gamma": 0.1,
"reg_alpha": 0.1,
"reg_lambda": 1.5,
"objective": "binary:logistic",
"tree_method": "hist",
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.0008,
"take_profit": 0.0016,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "close_only",
"session_filter": None,
"min_atr": None,
"trend_filter": None,
"target_horizon": 5,
"objective": (
"Maximize Sharpe ratio on USD/JPY 1-min data using XGBoost with "
"returns, RSI, Bollinger Bands, multiple MAs (50/100/200), MACD, "
"ATR, and candle-body features. Stop-loss and take-profit set at "
"a 1:2 risk/reward to filter noise and improve Sharpe. n_estimators "
"and moderate depth balance bias-variance. Regularization (alpha/lambda) "
"reduces overfitting on short date range."
),
"notes": (
"Target horizon of 5 bars (5 minutes) is chosen to capture short-term "
"directional moves on 1-min data without excessive label noise. "
"colsample_bytree and subsample add stochasticity to reduce variance. "
"close_only on opposite signal avoids whipsaw from rapid reversals. "
"No session filter applied since USD/JPY has liquidity around the clock."
),
}
|
||||||||||
|
0.58
|
USD/CAD Momentum-Reversion Hybrid (XGBoost, v2)
Maximise risk-adjusted return (Sharpe/Calmar). Deeper ensemble (600 trees) with aggressive regularisation (reg_alpha=0.5, reg_lambda=2, gamm…
|
P
@pivot_kid
|
USDCAD | 15min | 61.8%68.5% | +6.05%+1.12% | 1.311.57 | 2.07%2.07% | 47454 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:37:19
# Model : XGBoost
# Feature Eng. : SMA (20,50,200), BB (20,2.0), RSI 14, MACD (12,26,9), ATR 14 + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDCAD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── SMA & distance features ──────────────────────────────────────────────
for p in [20, 50, 200]:
sma = close.rolling(p).mean()
df[f"sma_{p}"] = sma
df[f"dm_sma_{p}"] = (close - sma) / sma
# SMA slope (rate of change of SMA over 5 bars)
for p in [20, 50]:
sma = df[f"sma_{p}"]
df[f"sma_{p}_slope"] = sma.diff(5) / sma.shift(5)
# SMA cross signals
df["sma_20_50_cross"] = np.where(df["sma_20"] > df["sma_50"], 1.0, -1.0)
df["sma_50_200_cross"] = np.where(df["sma_50"] > df["sma_200"], 1.0, -1.0)
# ── Bollinger Bands ───────────────────────────────────────────────────────
bb_mid = close.rolling(20).mean()
bb_std = close.rolling(20).std()
bb_upper = bb_mid + 2.0 * bb_std
bb_lower = bb_mid - 2.0 * bb_std
df["bb_mid"] = bb_mid
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
bb_range = bb_upper - bb_lower
df["bb_pct"] = np.where(bb_range != 0, (close - bb_lower) / bb_range, 0.5)
# Bollinger Band squeeze: width vs its own 20-bar average
df["bb_squeeze"] = df["bb_width"] / df["bb_width"].rolling(20).mean()
# Price position relative to bands
df["bb_above_upper"] = np.where(close > bb_upper, 1.0, 0.0)
df["bb_below_lower"] = np.where(close < bb_lower, 1.0, 0.0)
# ── RSI ───────────────────────────────────────────────────────────────────
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_gain = gain.ewm(alpha=1/14, adjust=False).mean()
avg_loss = loss.ewm(alpha=1/14, adjust=False).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi_14"] = 100 - (100 / (1 + rs))
# RSI derived features
df["rsi_norm"] = (df["rsi_14"] - 50) / 50 # centred & scaled
df["rsi_ob"] = np.where(df["rsi_14"] > 70, 1.0, 0.0)
df["rsi_os"] = np.where(df["rsi_14"] < 30, 1.0, 0.0)
df["rsi_slope"] = df["rsi_14"].diff(3)
# RSI divergence proxy: price up but RSI down (5-bar)
price_chg_5 = close.diff(5)
rsi_chg_5 = df["rsi_14"].diff(5)
df["rsi_bear_div"] = np.where((price_chg_5 > 0) & (rsi_chg_5 < 0), 1.0, 0.0)
df["rsi_bull_div"] = np.where((price_chg_5 < 0) & (rsi_chg_5 > 0), 1.0, 0.0)
# ── MACD ──────────────────────────────────────────────────────────────────
ema_fast = close.ewm(span=12, adjust=False).mean()
ema_slow = close.ewm(span=26, adjust=False).mean()
macd_line = ema_fast - ema_slow
signal_line = macd_line.ewm(span=9, adjust=False).mean()
df["macd_line"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_line - signal_line
# MACD normalised by close price
df["macd_line_norm"] = macd_line / close
df["macd_hist_norm"] = df["macd_hist"] / close
# MACD histogram slope and sign change
df["macd_hist_slope"] = df["macd_hist"].diff(2)
df["macd_cross"] = np.where(macd_line > signal_line, 1.0, -1.0)
# ── ATR ───────────────────────────────────────────────────────────────────
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr_14"] = tr.ewm(alpha=1/14, adjust=False).mean()
df["natr"] = df["atr_14"] / close
# ATR regime: current ATR vs 50-bar rolling mean
df["atr_regime"] = df["atr_14"] / df["atr_14"].rolling(50).mean()
# ── Momentum / Price Action features ─────────────────────────────────────
# Returns at multiple horizons
for h in [1, 2, 4, 8, 16]:
df[f"ret_{h}"] = close.pct_change(h)
# Candle body & shadow
body = (close - open_).abs()
total_range = (high - low).replace(0, np.nan)
df["body_ratio"] = body / total_range
df["candle_dir"] = np.where(close >= open_, 1.0, -1.0)
upper_shadow = high - pd.concat([close, open_], axis=1).max(axis=1)
lower_shadow = pd.concat([close, open_], axis=1).min(axis=1) - low
df["upper_shadow_ratio"] = upper_shadow / total_range
df["lower_shadow_ratio"] = lower_shadow / total_range
# Rolling price z-score (mean reversion signal)
for w in [20, 50]:
roll_mean = close.rolling(w).mean()
roll_std = close.rolling(w).std().replace(0, np.nan)
df[f"zscore_{w}"] = (close - roll_mean) / roll_std
# Volume of volatility: rolling std of returns
df["vol_10"] = close.pct_change().rolling(10).std()
df["vol_20"] = close.pct_change().rolling(20).std()
# Efficiency ratio: directional move / path length (20 bars)
direction_move = (close - close.shift(20)).abs()
path_length = close.diff().abs().rolling(20).sum().replace(0, np.nan)
df["efficiency_ratio"] = direction_move / path_length
# ── Interaction / Cross features ─────────────────────────────────────────
# RSI × MACD hist — captures momentum agreement
df["rsi_macd_agree"] = df["rsi_norm"] * df["macd_hist_norm"]
# BB pct × RSI — oversold/overbought near bands
df["bb_rsi_interact"] = df["bb_pct"] * df["rsi_norm"]
# Trend strength: distance from SMA50 scaled by ATR
df["trend_atr_50"] = df["dm_sma_50"] / df["natr"].replace(0, np.nan)
# ── Session / Time features ───────────────────────────────────────────────
if hasattr(df.index, "hour"):
hour = df.index.hour
df["hour_sin"] = np.sin(2 * np.pi * hour / 24)
df["hour_cos"] = np.cos(2 * np.pi * hour / 24)
# London session flag
df["london_session"] = np.where((hour >= 7) & (hour < 16), 1.0, 0.0)
# NY session flag
df["ny_session"] = np.where((hour >= 13) & (hour < 21), 1.0, 0.0)
if hasattr(df.index, "dayofweek"):
dow = df.index.dayofweek
df["dow_sin"] = np.sin(2 * np.pi * dow / 5)
df["dow_cos"] = np.cos(2 * np.pi * dow / 5)
# ── Fill NaN from warm-up ─────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/CAD Momentum-Reversion Hybrid (XGBoost, v2)",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 600,
"max_depth": 4,
"learning_rate": 0.03,
"subsample": 0.75,
"colsample_bytree": 0.70,
"min_child_weight": 5,
"gamma": 0.2,
"reg_alpha": 0.5,
"reg_lambda": 2.0,
"objective": "binary:logistic",
"tree_method": "hist",
"n_jobs": -1,
"random_state": 42,
},
"signal_threshold": 0.54,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 21],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise risk-adjusted return (Sharpe/Calmar). "
"Deeper ensemble (600 trees) with aggressive regularisation "
"(reg_alpha=0.5, reg_lambda=2, gamma=0.2, min_child_weight=5) "
"to prevent overfitting on 15-min USDCAD. "
"Rich feature set adds z-scores, efficiency ratio, session dummies, "
"RSI divergence, candle shape and cross-indicator interactions "
"beyond the prior attempt's plain indicators. "
"0.5% SL / 1.0% TP gives 1:2 R:R; session filter restricts to "
"liquid London+NY overlap hours."
),
"notes": (
"Prior attempt used plain RSI/MACD/BB/ATR/SMA and scored PF=0.98. "
"This version adds: rolling z-scores (20,50), efficiency ratio, "
"candle body/shadow ratios, multi-horizon returns, ATR regime, "
"BB squeeze, RSI divergence proxies, time-of-day sin/cos encoding, "
"and interaction terms (rsi_macd_agree, bb_rsi_interact, trend_atr). "
"Model regularised more heavily to combat the short date range. "
"Signal threshold lifted slightly to 0.54 to reduce marginal trades."
),
}
|
||||||||||
|
0.50
|
USD/CAD BB Mean-Reversion + ATR XGBoost
Maximise risk-adjusted return (Sharpe/Calmar) on USD/CAD 15-min using Bollinger Band mean-reversion signals augmented by ATR, RSI, MACD, and…
|
C
@candle_owl
|
USDCAD | 15min | 59.1%63.9% | +4.84%+0.75% | 1.311.39 | 1.34%1.34% | 36236 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 02:36:58
# Model : XGBoost
# Feature Eng. : BB (20,2.0), ATR 14 + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDCAD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_sigma = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_sigma
bb_lower = bb_mid - bb_std * bb_sigma
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
df["bb_pct"] = (close - bb_lower) / (bb_upper - bb_lower)
# ── ATR (14) & Normalised ATR ────────────────────────────────────────────
atr_period = 14
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
atr = tr.ewm(alpha=1.0 / atr_period, min_periods=atr_period, adjust=False).mean()
natr = atr / close
df["atr"] = atr
df["natr"] = natr
# ── Price momentum / returns ─────────────────────────────────────────────
df["ret_1"] = close.pct_change(1)
df["ret_4"] = close.pct_change(4)
df["ret_8"] = close.pct_change(8)
df["ret_16"] = close.pct_change(16)
# ── Distance from Bollinger mid / bands ──────────────────────────────────
df["close_minus_mid"] = (close - bb_mid) / bb_mid
df["close_minus_upper"] = (close - bb_upper) / bb_mid
df["close_minus_lower"] = (close - bb_lower) / bb_mid
# ── BB squeeze flag: width below rolling median ───────────────────────────
bb_width_med = df["bb_width"].rolling(50).median()
df["bb_squeeze"] = np.where(df["bb_width"] < bb_width_med, 1.0, 0.0)
# ── BB mean-reversion z-score ────────────────────────────────────────────
df["bb_z"] = (close - bb_mid) / (bb_sigma + 1e-12)
# ── Candle body / wick features ──────────────────────────────────────────
body = (close - open_).abs()
candle_rng = (high - low).replace(0, np.nan)
df["body_ratio"] = body / candle_rng
df["upper_wick"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / candle_rng
df["lower_wick"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / candle_rng
df["bull_candle"] = np.where(close > open_, 1.0, 0.0)
# ── RSI (14) built from scratch ──────────────────────────────────────────
rsi_period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_g = gain.ewm(alpha=1.0 / rsi_period, min_periods=rsi_period, adjust=False).mean()
avg_l = loss.ewm(alpha=1.0 / rsi_period, min_periods=rsi_period, adjust=False).mean()
rs = avg_g / (avg_l + 1e-12)
rsi = 100.0 - (100.0 / (1.0 + rs))
df["rsi_14"] = rsi
# RSI deviation from neutral 50
df["rsi_dev"] = (rsi - 50.0) / 50.0
# ── MACD (12, 26, 9) ─────────────────────────────────────────────────────
ema12 = close.ewm(span=12, adjust=False).mean()
ema26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema12 - ema26
macd_sig = macd_line.ewm(span=9, adjust=False).mean()
df["macd"] = macd_line / close
df["macd_hist"] = (macd_line - macd_sig) / close
# ── Rolling volatility (realised over 20 bars) ───────────────────────────
df["vol_20"] = df["ret_1"].rolling(20).std()
# ── ATR z-score vs 50-bar rolling mean ───────────────────────────────────
atr_mean = atr.rolling(50).mean()
atr_std = atr.rolling(50).std(ddof=0)
df["atr_z"] = (atr - atr_mean) / (atr_std + 1e-12)
# ── Volume-of-BB-touches over last 10 bars ───────────────────────────────
near_upper = (close >= bb_upper * 0.998).astype(float)
near_lower = (close <= bb_lower * 1.002).astype(float)
df["touch_upper_10"] = near_upper.rolling(10).sum()
df["touch_lower_10"] = near_lower.rolling(10).sum()
# ── SMA 50 (trend filter helper) ─────────────────────────────────────────
df["sma_50"] = close.rolling(50).mean()
df["close_vs_sma"] = (close - df["sma_50"]) / df["sma_50"]
# ── EMA cross (9 / 21) ───────────────────────────────────────────────────
ema9 = close.ewm(span=9, adjust=False).mean()
ema21 = close.ewm(span=21, adjust=False).mean()
df["ema_cross"] = (ema9 - ema21) / close
# ── Bar-of-day / session ─────────────────────────────────────────────────
if hasattr(df.index, "hour"):
df["hour_sin"] = np.sin(2 * np.pi * df.index.hour / 24.0)
df["hour_cos"] = np.cos(2 * np.pi * df.index.hour / 24.0)
else:
df["hour_sin"] = 0.0
df["hour_cos"] = 1.0
# ── Lag features on bb_pct and rsi ───────────────────────────────────────
for lag in [1, 2, 4]:
df[f"bb_pct_lag{lag}"] = df["bb_pct"].shift(lag)
df[f"rsi_14_lag{lag}"] = df["rsi_14"].shift(lag)
df[f"macd_hist_lag{lag}"] = df["macd_hist"].shift(lag)
# ── Fill NaN from warm-up ─────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/CAD BB Mean-Reversion + ATR XGBoost",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"colsample_bytree": 0.70,
"min_child_weight": 3,
"gamma": 0.15,
"reg_alpha": 0.10,
"reg_lambda": 1.50,
"objective": "binary:logistic",
"random_state": 42,
"n_jobs": -1,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 20],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise risk-adjusted return (Sharpe/Calmar) on USD/CAD 15-min "
"using Bollinger Band mean-reversion signals augmented by ATR, RSI, "
"MACD, and EMA-cross features fed into a regularised XGBoost classifier. "
"SL=0.5% / TP=1.0% gives a 1:2 RR floor. Conservative depth (4) and "
"strong L1/L2 regularisation prevent overfitting on a single year of data."
),
"notes": (
"BB squeeze flag and bb_z capture regime; atr_z filters noisy bars. "
"Session filter 07-20 UTC covers London + NY overlap for tighter spreads. "
"min_atr=0.0002 avoids dead-market whipsaws. Lag features on bb_pct and "
"rsi_14 give the model short-term momentum context without look-ahead."
),
}
|
||||||||||
|
0.48
|
GBP/USD SMA Trend Gradient Boosting Risk-Adj
Maximize risk-adjusted return (Sharpe/Calmar) on GBP/USD 15-min data. GradientBoostingClassifier chosen for its strong bias-variance tradeof…
|
R
@ratio_witch
|
GBPUSD | 15min | 43.1%62.5% | +6.85%+0.75% | 1.712.75 | 2.69%2.69% | 728 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:47:56
# Model : Gradient Boosting
# Feature Eng. : SMA (20,50,200) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/GBPUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── SMA features (required) ──────────────────────────────────────────────
for period in [20, 50, 200]:
sma = close.rolling(period).mean()
df[f"sma_{period}"] = sma
df[f"dm_sma_{period}"] = (close - sma) / sma
# ── SMA crossover signals ────────────────────────────────────────────────
sma_20 = close.rolling(20).mean()
sma_50 = close.rolling(50).mean()
sma_200 = close.rolling(200).mean()
df["sma_20_50_cross"] = np.where(sma_20 > sma_50, 1.0, -1.0)
df["sma_20_200_cross"] = np.where(sma_20 > sma_200, 1.0, -1.0)
df["sma_50_200_cross"] = np.where(sma_50 > sma_200, 1.0, -1.0)
# ── Price momentum features ──────────────────────────────────────────────
for lag in [1, 2, 4, 8, 16]:
df[f"ret_{lag}"] = close.pct_change(lag)
# ── Volatility: rolling std of returns ──────────────────────────────────
ret_1 = close.pct_change(1)
for window in [8, 20, 50]:
df[f"vol_{window}"] = ret_1.rolling(window).std()
# ── ATR (Average True Range) ─────────────────────────────────────────────
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
for atr_period in [14, 50]:
atr = tr.rolling(atr_period).mean()
df[f"atr_{atr_period}"] = atr
df[f"natr_{atr_period}"] = atr / close
# ── RSI ──────────────────────────────────────────────────────────────────
for rsi_period in [14, 28]:
delta = close.diff()
gain = delta.clip(lower=0).rolling(rsi_period).mean()
loss = (-delta.clip(upper=0)).rolling(rsi_period).mean()
rs = gain / (loss + 1e-10)
df[f"rsi_{rsi_period}"] = 100 - (100 / (1 + rs))
# ── MACD ─────────────────────────────────────────────────────────────────
ema_12 = close.ewm(span=12, adjust=False).mean()
ema_26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema_12 - ema_26
signal_line = macd_line.ewm(span=9, adjust=False).mean()
df["macd"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_line - signal_line
df["macd_hist_norm"] = (macd_line - signal_line) / (close + 1e-10)
# ── Bollinger Bands ───────────────────────────────────────────────────────
for bb_period in [20, 50]:
bb_mid = close.rolling(bb_period).mean()
bb_std = close.rolling(bb_period).std()
bb_upper = bb_mid + 2.0 * bb_std
bb_lower = bb_mid - 2.0 * bb_std
bb_width = (bb_upper - bb_lower) / (bb_mid + 1e-10)
bb_pos = (close - bb_lower) / (bb_upper - bb_lower + 1e-10)
df[f"bb_width_{bb_period}"] = bb_width
df[f"bb_pos_{bb_period}"] = bb_pos
# ── Stochastic Oscillator ────────────────────────────────────────────────
for stoch_period in [14, 28]:
lowest_low = low.rolling(stoch_period).min()
highest_high = high.rolling(stoch_period).max()
stoch_k = (close - lowest_low) / (highest_high - lowest_low + 1e-10) * 100
stoch_d = stoch_k.rolling(3).mean()
df[f"stoch_k_{stoch_period}"] = stoch_k
df[f"stoch_d_{stoch_period}"] = stoch_d
# ── Rate of Change (ROC) ──────────────────────────────────────────────────
for roc_period in [5, 10, 20]:
df[f"roc_{roc_period}"] = close.pct_change(roc_period)
# ── Candle body and shadow features ──────────────────────────────────────
body = (close - open_).abs()
candle_range = (high - low).abs()
df["body_ratio"] = body / (candle_range + 1e-10)
df["upper_shadow"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / (candle_range + 1e-10)
df["lower_shadow"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / (candle_range + 1e-10)
df["bullish_candle"] = np.where(close > open_, 1.0, -1.0)
# ── Volume-proxy: candle range as volatility proxy ────────────────────────
df["range_norm"] = candle_range / (close + 1e-10)
df["range_ma_ratio"] = candle_range / (candle_range.rolling(20).mean() + 1e-10)
# ── Lag features for return predictors ───────────────────────────────────
for col_lag in ["rsi_14", "macd_hist", "bb_pos_20"]:
for lag in [1, 2, 3]:
df[f"{col_lag}_lag{lag}"] = df[col_lag].shift(lag)
# ── Distance of close from recent high/low ────────────────────────────────
for lookback in [10, 20, 50]:
roll_high = high.rolling(lookback).max()
roll_low = low.rolling(lookback).min()
df[f"dist_high_{lookback}"] = (close - roll_high) / (roll_high + 1e-10)
df[f"dist_low_{lookback}"] = (close - roll_low) / (roll_low + 1e-10)
# ── Trend strength: ADX proxy ─────────────────────────────────────────────
adx_period = 14
tr_adx = tr.copy()
plus_dm = pd.Series(np.where((high.diff() > 0) & (high.diff() > -low.diff()), high.diff(), 0.0), index=close.index)
minus_dm = pd.Series(np.where((-low.diff() > 0) & (-low.diff() > high.diff()), -low.diff(), 0.0), index=close.index)
atr_adx = tr_adx.rolling(adx_period).mean()
plus_di = 100 * plus_dm.rolling(adx_period).mean() / (atr_adx + 1e-10)
minus_di = 100 * minus_dm.rolling(adx_period).mean() / (atr_adx + 1e-10)
dx = (100 * (plus_di - minus_di).abs() / (plus_di + minus_di + 1e-10))
df["adx"] = dx.rolling(adx_period).mean()
df["plus_di"] = plus_di
df["minus_di"] = minus_di
# ── Fill NaN from indicator warm-up ──────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "GBP/USD SMA Trend Gradient Boosting Risk-Adj",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"min_samples_leaf": 20,
"max_features": "sqrt",
"n_iter_no_change": 30,
"validation_fraction": 0.1,
"tol": 1e-4,
"random_state": 42,
},
"signal_threshold": 0.57,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [6, 18],
"min_atr": 0.0002,
"trend_filter": "sma_50",
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) on GBP/USD 15-min data. "
"GradientBoostingClassifier chosen for its strong bias-variance tradeoff "
"on medium-sized tabular datasets without needing GPU. "
"Hyperparameters: moderate depth=4 prevents overfitting, learning_rate=0.04 "
"with 400 estimators balances convergence vs generalisation, subsample=0.75 "
"adds stochasticity to reduce variance, min_samples_leaf=20 enforces statistical "
"significance at each leaf. Early stopping via n_iter_no_change guards against "
"overfit on the training fold. Signal threshold 0.57 filters marginal signals "
"to improve precision. SL=0.5%, TP=1.0% gives 1:2 RR. Session filter 6-18 UTC "
"covers London+NY overlap — highest GBP/USD liquidity and tighter spreads. "
"sma_50 trend filter ensures we only trade in the direction of medium-term trend, "
"reducing whipsaw losses. target_horizon=4 bars (1 hour) gives the model enough "
"time for moves to develop while staying relevant for intraday trading."
),
"notes": (
"Features: SMA 20/50/200 with distance metrics (core requirement), RSI 14/28, "
"MACD, Bollinger Bands 20/50, Stochastic 14/28, ATR 14/50, NATR, ROC, ADX, "
"candle body/shadow ratios, lagged RSI/MACD/BB features, distance from rolling "
"high/low, SMA crossover signals, multi-lag return features. "
"All features are backward-looking only (no lookahead bias). "
"on_opposite=reverse for fast trend-following entries without missing reversals."
),
}
|
||||||||||
|
0.39
|
NZD/USD EMA Cross (9/21) + RSI Gradient Boost
Maximise risk-adjusted return (Sharpe) on NZD/USD 15-min data. GradientBoostingClassifier chosen for its strong out-of-box performance on ta…
|
C
@candid-owl-125
|
NZDUSD | 15min | 62.5%69.5% | +19.51%+1.23% | 1.361.14 | 2.51%2.51% | 745105 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 02:54:13
# Model : Gradient Boosting
# Feature Eng. : EMA (9,21), RSI 14 + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/NZDUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── EMA 9 and EMA 21 (required) ──────────────────────────────────────────
ema_9 = close.ewm(span=9, adjust=False).mean()
ema_21 = close.ewm(span=21, adjust=False).mean()
df["ema_9"] = ema_9
df["ema_21"] = ema_21
df["dm_ema_9"] = (close - ema_9) / ema_9
df["dm_ema_21"] = (close - ema_21) / ema_21
# EMA crossover signal: positive when fast > slow
df["ema_cross"] = ema_9 - ema_21
# Rate of change of the crossover (momentum of the cross)
df["ema_cross_roc"] = df["ema_cross"].diff(3)
# ── RSI 14 (required) ────────────────────────────────────────────────────
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_gain = gain.ewm(com=13, adjust=False).mean()
avg_loss = loss.ewm(com=13, adjust=False).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi_14"] = 100 - (100 / (1 + rs))
# RSI normalised to [-1, 1]
df["rsi_norm"] = (df["rsi_14"] - 50) / 50
# RSI momentum (1-bar diff of RSI)
df["rsi_diff"] = df["rsi_14"].diff(1)
# RSI overbought / oversold flags (np.where, no pd.cut)
df["rsi_ob"] = np.where(df["rsi_14"] > 70, 1, 0)
df["rsi_os"] = np.where(df["rsi_14"] < 30, 1, 0)
# ── ATR 14 ───────────────────────────────────────────────────────────────
prev_close = close.shift(1)
tr = pd.concat([
high - low,
(high - prev_close).abs(),
(low - prev_close).abs()
], axis=1).max(axis=1)
atr_14 = tr.ewm(com=13, adjust=False).mean()
df["atr_14"] = atr_14
df["natr_14"] = atr_14 / close # normalised ATR
# ── MACD-style fast/slow difference (12/26 EMA) ─────────────────────────
ema_12 = close.ewm(span=12, adjust=False).mean()
ema_26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema_12 - ema_26
macd_signal = macd_line.ewm(span=9, adjust=False).mean()
df["macd_line"] = macd_line / close
df["macd_signal"] = macd_signal / close
df["macd_hist"] = (macd_line - macd_signal) / close
# ── Bollinger Bands (20, 2) ───────────────────────────────────────────────
bb_mid = close.rolling(20).mean()
bb_std = close.rolling(20).std()
bb_up = bb_mid + 2 * bb_std
bb_lo = bb_mid - 2 * bb_std
bb_bw = (bb_up - bb_lo) / bb_mid # bandwidth
bb_pct = (close - bb_lo) / (bb_up - bb_lo) # %B position
df["bb_bandwidth"] = bb_bw
df["bb_pct"] = bb_pct
# ── Stochastic %K / %D (14, 3) ───────────────────────────────────────────
low14 = low.rolling(14).min()
high14 = high.rolling(14).max()
stoch_k = 100 * (close - low14) / (high14 - low14).replace(0, np.nan)
stoch_d = stoch_k.rolling(3).mean()
df["stoch_k"] = stoch_k
df["stoch_d"] = stoch_d
df["stoch_kd_diff"] = stoch_k - stoch_d
# ── Price momentum (returns over multiple horizons) ───────────────────────
df["ret_1"] = close.pct_change(1)
df["ret_3"] = close.pct_change(3)
df["ret_6"] = close.pct_change(6)
df["ret_12"] = close.pct_change(12)
# ── Candle body & shadow ratios ───────────────────────────────────────────
body = (close - open_).abs()
candle_rng = (high - low).replace(0, np.nan)
df["body_ratio"] = body / candle_rng
df["upper_shadow"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / candle_rng
df["lower_shadow"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / candle_rng
df["body_direction"] = np.sign(close - open_)
# ── Volume proxy: volatility-based (OHLC spread) ─────────────────────────
df["hl_spread"] = (high - low) / close
# ── Rolling volatility (std of returns) ──────────────────────────────────
df["vol_6"] = df["ret_1"].rolling(6).std()
df["vol_24"] = df["ret_1"].rolling(24).std()
# ── Z-score of close relative to 20-bar rolling mean ─────────────────────
roll_mean_20 = close.rolling(20).mean()
roll_std_20 = close.rolling(20).std()
df["zscore_20"] = (close - roll_mean_20) / roll_std_20.replace(0, np.nan)
# ── Trend strength: R² of close over 20 bars ─────────────────────────────
x = np.arange(20)
x_demeaned = x - x.mean()
ss_x = (x_demeaned ** 2).sum()
def rolling_r2(series, window=20):
arr = series.values
n = len(arr)
out = np.full(n, np.nan)
for i in range(window - 1, n):
y = arr[i - window + 1: i + 1]
if np.any(np.isnan(y)):
continue
y_m = y - y.mean()
slope = np.dot(x_demeaned, y_m) / ss_x
y_hat = slope * x_demeaned + y.mean()
ss_res = ((y - y_hat) ** 2).sum()
ss_tot = ((y - y.mean()) ** 2).sum()
out[i] = 1 - ss_res / ss_tot if ss_tot > 0 else 0.0
return out
df["trend_r2_20"] = rolling_r2(close, 20)
# ── SMA 50 (for trend filter reference; also used as feature) ─────────────
sma_50 = close.rolling(50).mean()
df["sma_50"] = sma_50
df["close_vs_sma50"] = (close - sma_50) / sma_50
# ── Higher-timeframe EMA proxy (4-bar resample = 1h equivalent) ──────────
ema_4h = close.ewm(span=4 * 21, adjust=False).mean()
df["dm_ema_4h"] = (close - ema_4h) / ema_4h
# ── Cross confirmation: EMA cross direction × RSI regime ─────────────────
df["cross_x_rsi"] = np.sign(df["ema_cross"]) * df["rsi_norm"]
# ── Fill NaN from indicator warm-up ──────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "NZD/USD EMA Cross (9/21) + RSI Gradient Boost",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"min_samples_leaf": 20,
"max_features": "sqrt",
"validation_fraction": 0.1,
"n_iter_no_change": 25,
"tol": 1e-4,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [0, 23],
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise risk-adjusted return (Sharpe) on NZD/USD 15-min data. "
"GradientBoostingClassifier chosen for its strong out-of-box performance on "
"tabular financial data, built-in regularisation via subsample/max_features, "
"and early-stopping via n_iter_no_change. Depth-4 trees with 400 estimators "
"and lr=0.04 balance bias-variance. SL=0.5% / TP=1.0% gives 1:2 R:R. "
"4-bar horizon (~1 hour) aligns with EMA-cross momentum persistence. "
"Threshold 0.55 filters marginal signals while preserving trade frequency."
),
"notes": (
"Features: EMA 9/21 cross + distances, RSI 14 with OB/OS flags, MACD histogram, "
"Bollinger %B + bandwidth, Stochastic K/D, multi-horizon returns, candle body "
"ratios, rolling volatility, 20-bar z-score, trend R² and SMA50 distance. "
"No session filter applied — NZD/USD has meaningful liquidity across Asian + "
"London sessions. Reverse on opposite signal for continuous market exposure."
),
}
|
||||||||||
|
0.35
|
GBP/USD Gradient Boosting Trend + Mean-Reversion
Maximize risk-adjusted return (Sharpe/Calmar) on GBP/USD 15-min. GradientBoostingClassifier with 400 shallow trees (depth 4) and a conservat…
|
C
@candid-owl-125
|
GBPUSD | 15min | 53.5%63.6% | +3.70%+1.01% | 1.161.33 | 2.46%2.46% | 31244 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:41:21
# Model : Gradient Boosting
# Feature Eng. : SMA (20,50,200), BB (20,2.0), RSI 14, MACD (12,26,9), ATR 14 + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/GBPUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── SMA 20, 50, 200 + distance from close ──────────────────────────────
for period in [20, 50, 200]:
sma = close.rolling(period).mean()
df[f"sma_{period}"] = sma
df[f"dm_sma_{period}"] = (close - sma) / sma
# ── Bollinger Bands (20, 2) ─────────────────────────────────────────────
bb_mid = close.rolling(20).mean()
bb_std = close.rolling(20).std(ddof=0)
bb_upper = bb_mid + 2.0 * bb_std
bb_lower = bb_mid - 2.0 * bb_std
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
bb_range = bb_upper - bb_lower
df["bb_pct"] = np.where(bb_range != 0, (close - bb_lower) / bb_range, 0.5)
# ── RSI 14 ─────────────────────────────────────────────────────────────
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_gain = gain.ewm(com=13, min_periods=14, adjust=False).mean()
avg_loss = loss.ewm(com=13, min_periods=14, adjust=False).mean()
rs = np.where(avg_loss != 0, avg_gain / avg_loss, 100.0)
df["rsi_14"] = 100.0 - (100.0 / (1.0 + rs))
# ── MACD (12, 26, 9) ───────────────────────────────────────────────────
ema_12 = close.ewm(span=12, adjust=False).mean()
ema_26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema_12 - ema_26
signal_line = macd_line.ewm(span=9, adjust=False).mean()
df["macd_line"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_line - signal_line
# ── ATR 14 + NATR ──────────────────────────────────────────────────────
prev_close = close.shift(1)
tr = pd.concat([
high - low,
(high - prev_close).abs(),
(low - prev_close).abs()
], axis=1).max(axis=1)
atr = tr.ewm(com=13, min_periods=14, adjust=False).mean()
df["atr_14"] = atr
df["natr"] = np.where(close != 0, atr / close, 0.0)
# ── Price momentum (returns over multiple horizons) ────────────────────
for lag in [1, 4, 8, 16]:
df[f"ret_{lag}"] = close.pct_change(lag)
# ── Candle body & wick features ────────────────────────────────────────
body = (close - open_).abs()
candle_range = (high - low).replace(0, np.nan)
df["body_ratio"] = body / candle_range
df["upper_wick"] = np.where(
candle_range.notna(),
(high - close.combine(open_, max)) / candle_range,
0.0
)
df["lower_wick"] = np.where(
candle_range.notna(),
(close.combine(open_, min) - low) / candle_range,
0.0
)
df["candle_dir"] = np.where(close >= open_, 1.0, -1.0)
# ── Volume proxy: normalised candle range ──────────────────────────────
rolling_range = candle_range.rolling(20).mean()
df["norm_range"] = np.where(
rolling_range != 0,
(high - low) / rolling_range,
1.0
)
# ── RSI derived features ───────────────────────────────────────────────
df["rsi_ob"] = np.where(df["rsi_14"] > 70, 1.0, 0.0)
df["rsi_os"] = np.where(df["rsi_14"] < 30, 1.0, 0.0)
df["rsi_mid"] = df["rsi_14"] - 50.0
df["rsi_slope"] = df["rsi_14"].diff(3)
# ── MACD histogram slope ───────────────────────────────────────────────
df["macd_hist_slope"] = df["macd_hist"].diff(2)
df["macd_cross"] = np.where(
(df["macd_hist"] > 0) & (df["macd_hist"].shift(1) <= 0), 1.0,
np.where(
(df["macd_hist"] < 0) & (df["macd_hist"].shift(1) >= 0), -1.0,
0.0
)
)
# ── Bollinger squeeze (low volatility precursor) ───────────────────────
bb_width_ma = df["bb_width"].rolling(20).mean()
df["bb_squeeze"] = np.where(df["bb_width"] < bb_width_ma, 1.0, 0.0)
# ── SMA slope features ─────────────────────────────────────────────────
df["sma_20_slope"] = df["sma_20"].pct_change(4)
df["sma_50_slope"] = df["sma_50"].pct_change(8)
# ── Cross-SMA alignment (trend structure) ──────────────────────────────
df["sma20_above_50"] = np.where(df["sma_20"] > df["sma_50"], 1.0, 0.0)
df["sma50_above_200"] = np.where(df["sma_50"] > df["sma_200"], 1.0, 0.0)
df["close_above_200"] = np.where(close > df["sma_200"], 1.0, 0.0)
# ── Lagged close returns as additional features ────────────────────────
for lag in [1, 2, 3]:
df[f"close_lag_{lag}"] = close.shift(lag)
# ── Rolling volatility (std of returns) ────────────────────────────────
df["vol_10"] = close.pct_change().rolling(10).std()
df["vol_20"] = close.pct_change().rolling(20).std()
df["vol_ratio"] = np.where(
df["vol_20"] != 0,
df["vol_10"] / df["vol_20"],
1.0
)
# ── Hour-of-day (London/NY session proxy) ──────────────────────────────
if hasattr(df.index, "hour"):
df["hour_sin"] = np.sin(2 * np.pi * df.index.hour / 24.0)
df["hour_cos"] = np.cos(2 * np.pi * df.index.hour / 24.0)
else:
df["hour_sin"] = 0.0
df["hour_cos"] = 1.0
# ── Fill NaN from warm-up periods ──────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "GBP/USD Gradient Boosting Trend + Mean-Reversion",
"model_type": "GradientBoostingClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.8,
"min_samples_leaf": 20,
"max_features": "sqrt",
"validation_fraction": 0.1,
"n_iter_no_change": 30,
"tol": 1e-4,
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [6, 20],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) on GBP/USD 15-min. "
"GradientBoostingClassifier with 400 shallow trees (depth 4) and a "
"conservative learning rate of 0.04 avoids overfitting while capturing "
"non-linear interactions between trend (SMA alignment, slope), momentum "
"(MACD histogram, RSI), and volatility (ATR, BB squeeze) features. "
"SL=0.5% / TP=1.0% gives 1:2 R/R. Session filter 06-20 UTC covers "
"London open through NY close where GBP/USD liquidity is highest. "
"min_atr filter avoids flat/illiquid bars."
),
"notes": (
"Features include multi-period SMA distances, Bollinger Band pct/width, "
"RSI with overbought/oversold flags, MACD histogram slope and crossover, "
"ATR-normalised volatility, candle body/wick ratios, rolling vol ratio, "
"and hour-of-day cyclical encoding. target_horizon=4 (1-hour forward) "
"balances signal frequency against predictability at 15-min resolution."
),
}
|
||||||||||
|
0.30
|
AUD/USD Stoch+BB+RSI Mean-Reversion XGBoost
Maximize risk-adjusted return (Sharpe / Calmar). XGBoost chosen for its ability to capture non-linear interactions between Stochastic, Bolli…
|
S
@still-lynx-704
|
AUDUSD | 15min | 62.5%72.1% | +10.93%+1.36% | 1.181.23 | 4.00%4.00% | 74268 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:51:31
# Model : XGBoost
# Feature Eng. : BB (20,2.0), RSI 14, Stochastic (14,3) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/AUDUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_std_val = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_std_val
bb_lower = bb_mid - bb_std * bb_std_val
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
df["bb_pct"] = (close - bb_lower) / (bb_upper - bb_lower)
# ── RSI (14) ─────────────────────────────────────────────────────────────
rsi_period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
avg_loss = loss.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi"] = 100 - (100 / (1 + rs))
# ── Stochastic Oscillator (K=14, D=3) ────────────────────────────────────
stoch_k_period = 14
stoch_d_period = 3
lowest_low = low.rolling(stoch_k_period).min()
highest_high = high.rolling(stoch_k_period).max()
denom = (highest_high - lowest_low).replace(0, np.nan)
df["stoch_k"] = 100 * (close - lowest_low) / denom
df["stoch_d"] = df["stoch_k"].rolling(stoch_d_period).mean()
df["stoch_kd_diff"] = df["stoch_k"] - df["stoch_d"]
# ── Additional derived features ──────────────────────────────────────────
# RSI overbought / oversold zone flags
df["rsi_ob"] = np.where(df["rsi"] > 70, 1, 0)
df["rsi_os"] = np.where(df["rsi"] < 30, 1, 0)
df["rsi_mid"] = df["rsi"] - 50.0
# Stochastic overbought / oversold zone flags
df["stoch_ob"] = np.where(df["stoch_k"] > 80, 1, 0)
df["stoch_os"] = np.where(df["stoch_k"] < 20, 1, 0)
# BB position regime: price relative to bands
df["price_above_bb_upper"] = np.where(close > bb_upper, 1, 0)
df["price_below_bb_lower"] = np.where(close < bb_lower, 1, 0)
df["price_vs_bb_mid"] = close - bb_mid
# ATR-based volatility (14-bar)
atr_period = 14
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr14"] = tr.ewm(com=atr_period - 1, min_periods=atr_period).mean()
df["natr14"] = df["atr14"] / close
# SMA trend context
df["sma_20"] = close.rolling(20).mean()
df["sma_50"] = close.rolling(50).mean()
df["sma_200"] = close.rolling(200).mean()
df["price_vs_sma20"] = (close - df["sma_20"]) / df["sma_20"]
df["price_vs_sma50"] = (close - df["sma_50"]) / df["sma_50"]
df["sma20_vs_sma50"] = (df["sma_20"] - df["sma_50"]) / df["sma_50"]
# Momentum: rate of change
df["roc_5"] = close.pct_change(5)
df["roc_10"] = close.pct_change(10)
df["roc_20"] = close.pct_change(20)
# MACD-style (EMA 12 - EMA 26)
ema12 = close.ewm(span=12, min_periods=12).mean()
ema26 = close.ewm(span=26, min_periods=26).mean()
macd_line = ema12 - ema26
macd_signal = macd_line.ewm(span=9, min_periods=9).mean()
df["macd"] = macd_line
df["macd_signal"] = macd_signal
df["macd_hist"] = macd_line - macd_signal
# Candle body / wick ratios
body = (close - open_).abs()
candle_range = (high - low).replace(0, np.nan)
df["body_ratio"] = body / candle_range
df["upper_wick"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / candle_range
df["lower_wick"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / candle_range
df["bullish_bar"] = np.where(close > open_, 1, 0)
# Lagged RSI / Stoch features (1 and 2 bars back)
df["rsi_lag1"] = df["rsi"].shift(1)
df["rsi_lag2"] = df["rsi"].shift(2)
df["stoch_k_lag1"] = df["stoch_k"].shift(1)
df["bb_pct_lag1"] = df["bb_pct"].shift(1)
# RSI slope
df["rsi_slope"] = df["rsi"] - df["rsi"].shift(3)
# Stoch K crossing D (momentum signal)
df["stoch_cross_up"] = np.where((df["stoch_k"] > df["stoch_d"]) &
(df["stoch_k"].shift(1) <= df["stoch_d"].shift(1)), 1, 0)
df["stoch_cross_down"] = np.where((df["stoch_k"] < df["stoch_d"]) &
(df["stoch_k"].shift(1) >= df["stoch_d"].shift(1)), 1, 0)
# Volume (if present)
if "volume" in df.columns:
vol_ma = df["volume"].rolling(20).mean()
df["vol_ratio"] = df["volume"] / vol_ma.replace(0, np.nan)
else:
df["vol_ratio"] = 1.0
# ── Fill NaN from warm-up ────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "AUD/USD Stoch+BB+RSI Mean-Reversion XGBoost",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"colsample_bytree": 0.70,
"min_child_weight": 5,
"gamma": 0.15,
"reg_alpha": 0.10,
"reg_lambda": 1.50,
"objective": "binary:logistic",
"random_state": 42,
"n_jobs": -1,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [0, 23],
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe / Calmar). "
"XGBoost chosen for its ability to capture non-linear interactions "
"between Stochastic, Bollinger Bands, and RSI regimes. "
"Shallow trees (max_depth=4) + high regularisation (reg_lambda=1.5, gamma=0.15) "
"prevent overfitting on 15-min FX data. "
"2:1 TP:SL ratio (1.0% / 0.5%) improves expectancy per trade. "
"Reverse on opposite signal minimises flat time and captures regime flips."
),
"notes": (
"Features include BB width/pct, RSI(14) with overbought/oversold flags, "
"Stochastic K/D crossovers, MACD histogram, ATR volatility, SMA trend context, "
"candle body ratios, lagged indicators, and momentum ROC. "
"signal_threshold=0.55 balances precision vs recall on directional calls. "
"session_filter covers full 24h to capture Asia + London + NY sessions for AUD/USD."
),
}
|
||||||||||
|
0.23
|
EMA crossover (9/21) + RSI 14 confirmation
Claude-generated EMA 9/21 trend filter with RSI 14 momentum gate on EURUSD 15min. Test holdout: WR 71%, PF 2.36, 77 trades, +1.7% over ~8 da…
|
P
@pivot_kid
|
EURUSD | 15min | 71.4%51.1% | +1.72%+0.21% | 2.361.05 | 0.37%0.37% | 7794 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-07 01:38:25
# Model : XGBoost
# Feature Eng. : EMA crossover trend (9/21) with RSI 14 confirmation on EURUSD 15min + Auto-add features: ON
# Signal / Entry : —
# Optimization : —
# Risk Mgmt : —
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# QUANTIFY ME — STRATEGY MODULE
# EMA Crossover + RSI Confirmation (XGBoost, Sharpe Optimization)
# ============================================================
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/EURUSD_15min.parquet"
START_DATE = "2026-03-28"
END_DATE = "2026-04-25"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# ============================================================
# SECTION 1 — FEATURE ENGINEERING
# ============================================================
def feature_engineering(df, close, open_, high, low):
"""
Add EMA crossover trend features + RSI confirmation.
Features:
- EMA 9 and EMA 21 for trend direction
- RSI 14 for momentum confirmation
- EMA crossover signal
- Price deviation from EMA 21
- High/Low proximity ratios
- Volume-based volatility (NATR)
"""
# EMA 9 and EMA 21 for trend
df['ema_9'] = close.ewm(span=9, adjust=False).mean()
df['ema_21'] = close.ewm(span=21, adjust=False).mean()
df['ema_crossover'] = np.where(df['ema_9'] > df['ema_21'], 1, -1)
# EMA crossover signal (1 when 9 crosses above 21, -1 when crosses below)
df['ema_cross_signal'] = df['ema_crossover'].diff().fillna(0)
df['ema_cross_signal'] = np.where(df['ema_cross_signal'] != 0, df['ema_cross_signal'], 0)
# Price deviation from EMA 21 (normalized)
df['price_ema_deviation'] = (close - df['ema_21']) / df['ema_21']
# RSI 14 for momentum confirmation
delta = close.diff()
gain = np.where(delta > 0, delta, 0)
loss = np.where(delta < 0, -delta, 0)
avg_gain = pd.Series(gain, index=close.index).ewm(span=14, adjust=False).mean()
avg_loss = pd.Series(loss, index=close.index).ewm(span=14, adjust=False).mean()
rs = avg_gain / (avg_loss + 1e-10)
df['rsi_14'] = 100 - (100 / (1 + rs))
# RSI signal: overbought/oversold
df['rsi_overbought'] = np.where(df['rsi_14'] > 70, 1, 0)
df['rsi_oversold'] = np.where(df['rsi_14'] < 30, 1, 0)
# High/Low proximity (distance from recent extremes)
df['high_20'] = high.rolling(window=20).max()
df['low_20'] = low.rolling(window=20).min()
df['price_position'] = (close - df['low_20']) / (df['high_20'] - df['low_20'] + 1e-10)
# NATR (Normalized ATR) for volatility
atr_period = 14
tr1 = high - low
tr2 = np.abs(high - close.shift(1))
tr3 = np.abs(low - close.shift(1))
tr = np.maximum(tr1, np.maximum(tr2, tr3))
atr = pd.Series(tr, index=close.index).rolling(window=atr_period).mean()
df['natr'] = (atr / close) * 100
# EMA momentum (rate of change in EMA)
df['ema_9_roc'] = df['ema_9'].pct_change(periods=3)
df['ema_21_roc'] = df['ema_21'].pct_change(periods=3)
# Close relative to open (intrabar direction)
df['close_above_open'] = np.where(close > open_, 1, 0)
# Volume-based features (if available; otherwise skip)
if 'volume' in df.columns:
df['volume_ma'] = df['volume'].rolling(window=20).mean()
df['volume_ratio'] = df['volume'] / (df['volume_ma'] + 1e-10)
else:
df['volume_ratio'] = 1.0
# Fill NaN from indicator warm-up
df = df.bfill().ffill()
return df
# ============================================================
# SECTION 2 — STRATEGY CONFIG
# ============================================================
def strategy_config():
"""
XGBoost strategy optimized for Sharpe ratio on EMA/RSI signals.
Hyperparameters tuned for:
- Fast learning (learning_rate=0.08)
- Shallow trees (max_depth=4) to avoid overfitting on 15min data
- Moderate boosting (n_estimators=250) for good generalization
- Regularization (subsample=0.85, colsample_bytree=0.8)
- Balanced class weights via scale_pos_weight
Signal threshold 0.55 chosen to be moderately selective while maintaining
good trade frequency on the EMA crossover setup.
"""
return {
# Model specification
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 250,
"max_depth": 4,
"learning_rate": 0.08,
"subsample": 0.85,
"colsample_bytree": 0.8,
"min_child_weight": 1,
"gamma": 0.5,
"reg_alpha": 0.1,
"reg_lambda": 1.0,
"random_state": 42,
"verbosity": 0,
},
# Entry signal
"signal_threshold": 0.55,
# Position management
"direction": "both",
"max_positions": 1,
"on_opposite": "reverse",
"cooldown": 0,
# Risk management
"stop_loss": 0.008,
"take_profit": 0.015,
# Filters
"session_filter": None,
"min_atr": None,
"trend_filter": None,
# Target
"target_horizon": 4,
# Metadata
"title": "EMA Crossover + RSI Confirmation (XGBoost)",
"objective": "Maximize Sharpe ratio with EMA 9/21 trend + RSI 14 momentum confirmation",
"notes": (
"Strategy uses fast EMA (9) crossover above/below slow EMA (21) "
"as primary trend signal, confirmed by RSI 14 momentum. "
"XGBoost learns non-linear interactions between these features. "
"Moderate SL/TP (0.8%/1.5%) and bidirectional trading for scalping efficiency. "
"Optimized for EURUSD 15min with 70/30 train/test split."
),
}
|
||||||||||
|
0.17
|
NZD/USD MACD+RSI Momentum (XGBoost, Risk-Adj)
Maximise risk-adjusted return (Sharpe/Calmar) on NZD/USD 15-min data. XGBoost chosen for its strong performance on tabular financial data. M…
|
D
@delta_one
|
NZDUSD | 15min | 63.8%62.1% | +18.16%+0.64% | 1.321.07 | 2.46%2.46% | 845153 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 02:51:04
# Model : XGBoost
# Feature Eng. : RSI 14, MACD (12,26,9) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/NZDUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── RSI 14 ──────────────────────────────────────────────────────────────
period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=period - 1, min_periods=period).mean()
avg_loss = loss.ewm(com=period - 1, min_periods=period).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi_14"] = 100 - (100 / (1 + rs))
# RSI derived features
df["rsi_14_norm"] = (df["rsi_14"] - 50) / 50 # centred & scaled
df["rsi_14_ob"] = np.where(df["rsi_14"] > 70, 1, 0) # overbought flag
df["rsi_14_os"] = np.where(df["rsi_14"] < 30, 1, 0) # oversold flag
df["rsi_14_mom"] = df["rsi_14"].diff(3) # 3-bar momentum
# ── MACD (12, 26, 9) ────────────────────────────────────────────────────
ema_12 = close.ewm(span=12, adjust=False).mean()
ema_26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema_12 - ema_26
signal_line = macd_line.ewm(span=9, adjust=False).mean()
macd_hist = macd_line - signal_line
df["macd_line"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_hist
# MACD derived features
df["macd_hist_mom"] = macd_hist.diff(2) # histogram momentum
df["macd_cross_bull"] = np.where(
(macd_line > signal_line) & (macd_line.shift(1) <= signal_line.shift(1)), 1, 0
)
df["macd_cross_bear"] = np.where(
(macd_line < signal_line) & (macd_line.shift(1) >= signal_line.shift(1)), 1, 0
)
df["macd_zero_cross"] = np.where(
(macd_line > 0) & (macd_line.shift(1) <= 0), 1,
np.where((macd_line < 0) & (macd_line.shift(1) >= 0), -1, 0)
)
# ── Additional price-action features ────────────────────────────────────
# ATR (14) for volatility context
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
atr_14 = tr.ewm(span=14, adjust=False).mean()
df["atr_14"] = atr_14
df["natr_14"] = atr_14 / close # normalised ATR
# Bollinger Bands (20, 2) — mean-reversion context
sma_20 = close.rolling(20).mean()
std_20 = close.rolling(20).std()
bb_up = sma_20 + 2 * std_20
bb_lo = sma_20 - 2 * std_20
df["bb_pct"] = (close - bb_lo) / (bb_up - bb_lo + 1e-12) # 0-1 position
df["bb_width"] = (bb_up - bb_lo) / sma_20 # band width
# SMA filters
df["sma_20"] = sma_20
df["sma_50"] = close.rolling(50).mean()
df["price_vs_sma20"] = (close - sma_20) / (sma_20 + 1e-12)
df["price_vs_sma50"] = (close - df["sma_50"]) / (df["sma_50"] + 1e-12)
# Rate of change
df["roc_5"] = close.pct_change(5)
df["roc_10"] = close.pct_change(10)
df["roc_20"] = close.pct_change(20)
# Candlestick body / shadow ratios
body = (close - open_).abs()
candle_rng = (high - low).replace(0, np.nan)
df["body_ratio"] = body / candle_rng
df["upper_shadow"] = (high - pd.concat([close, open_], axis=1).max(axis=1)) / candle_rng
df["lower_shadow"] = (pd.concat([close, open_], axis=1).min(axis=1) - low) / candle_rng
df["bar_direction"] = np.where(close >= open_, 1, -1)
# Volume-proxy: realised range / ATR ratio
df["range_vs_atr"] = candle_rng / (atr_14 + 1e-12)
# Stochastic %K (14)
lowest_14 = low.rolling(14).min()
highest_14 = high.rolling(14).max()
stoch_k = 100 * (close - lowest_14) / (highest_14 - lowest_14 + 1e-12)
df["stoch_k"] = stoch_k
df["stoch_d"] = stoch_k.rolling(3).mean()
df["stoch_diff"] = df["stoch_k"] - df["stoch_d"]
# RSI × MACD interaction
df["rsi_macd_interact"] = df["rsi_14_norm"] * macd_hist
# Lagged features (1 and 2 bars back) for key signals
for col in ["rsi_14_norm", "macd_hist", "bb_pct", "roc_5"]:
df[f"{col}_lag1"] = df[col].shift(1)
df[f"{col}_lag2"] = df[col].shift(2)
# ── Fill NaN from warm-up ────────────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "NZD/USD MACD+RSI Momentum (XGBoost, Risk-Adj)",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.75,
"colsample_bytree": 0.70,
"min_child_weight": 3,
"gamma": 0.15,
"reg_alpha": 0.10,
"reg_lambda": 1.50,
"objective": "binary:logistic",
"tree_method": "hist",
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [0, 23],
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximise risk-adjusted return (Sharpe/Calmar) on NZD/USD 15-min data. "
"XGBoost chosen for its strong performance on tabular financial data. "
"Moderate depth (4) and high regularisation (gamma, alpha, lambda) prevent "
"overfitting on a relatively small forex dataset. Subsample + colsample_bytree "
"add stochastic diversity. SL 0.5% / TP 1.0% gives a 1:2 R:R ratio to support "
"positive expectancy even with a sub-60% win rate. Threshold 0.55 filters marginal "
"signals while keeping trade frequency acceptable. Target horizon of 4 bars (1 hour) "
"aligns with typical MACD/RSI signal resolution on 15-min charts."
),
"notes": (
"Features: RSI-14 (raw, normalised, OB/OS flags, momentum), MACD(12,26,9) "
"(line, signal, histogram, crosses, zero-cross), Bollinger Bands %B & width, "
"ATR/NATR, SMA20/50 price deviations, Stochastic %K/%D, ROC(5/10/20), "
"candlestick body/shadow ratios, RSI×MACD interaction term, and lagged "
"versions (lag1, lag2) of key signals to capture short-term persistence."
),
}
|
||||||||||
|
0.12
|
USD/CAD Stoch+BB+RSI Mean-Reversion (XGBoost)
Maximize risk-adjusted return (Sharpe/Calmar) by combining Stochastic (14,3), Bollinger Bands (20,2) and RSI(14) mean-reversion signals with…
|
V
@vega-puma-338
|
USDCAD | 15min | 58.2%51.1% | +2.45%+0.35% | 1.151.15 | 1.65%1.65% | 30947 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:58:30
# Model : XGBoost
# Feature Eng. : BB (20,2.0), RSI 14, Stochastic (14,3) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDCAD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_std_s = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_std_s
bb_lower = bb_mid - bb_std * bb_std_s
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
df["bb_pct"] = (close - bb_lower) / (bb_upper - bb_lower)
# ── RSI (14) ─────────────────────────────────────────────────────────────
rsi_period = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = (-delta).clip(lower=0)
avg_gain = gain.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
avg_loss = loss.ewm(com=rsi_period - 1, min_periods=rsi_period).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi"] = 100 - (100 / (1 + rs))
# ── Stochastic Oscillator (K=14, D=3) ────────────────────────────────────
stoch_k_period = 14
stoch_d_period = 3
lowest_low = low.rolling(stoch_k_period).min()
highest_high = high.rolling(stoch_k_period).max()
stoch_k_raw = 100 * (close - lowest_low) / (highest_high - lowest_low).replace(0, np.nan)
df["stoch_k"] = stoch_k_raw
df["stoch_d"] = stoch_k_raw.rolling(stoch_d_period).mean()
# ── Derived Stochastic features ──────────────────────────────────────────
df["stoch_kd_diff"] = df["stoch_k"] - df["stoch_d"] # K-D divergence
df["stoch_k_prev"] = df["stoch_k"].shift(1)
df["stoch_d_prev"] = df["stoch_d"].shift(1)
# Bullish crossover: K crosses above D
df["stoch_cross_up"] = np.where(
(df["stoch_k"] > df["stoch_d"]) & (df["stoch_k_prev"] <= df["stoch_d_prev"]), 1.0, 0.0
)
# Bearish crossover: K crosses below D
df["stoch_cross_dn"] = np.where(
(df["stoch_k"] < df["stoch_d"]) & (df["stoch_k_prev"] >= df["stoch_d_prev"]), 1.0, 0.0
)
# ── RSI-derived features ─────────────────────────────────────────────────
df["rsi_prev"] = df["rsi"].shift(1)
df["rsi_slope"] = df["rsi"] - df["rsi_prev"]
df["rsi_ob"] = np.where(df["rsi"] >= 70, 1.0, 0.0) # overbought flag
df["rsi_os"] = np.where(df["rsi"] <= 30, 1.0, 0.0) # oversold flag
# ── BB-derived features ──────────────────────────────────────────────────
df["bb_pct_prev"] = df["bb_pct"].shift(1)
df["bb_pct_slope"] = df["bb_pct"] - df["bb_pct_prev"]
df["price_vs_mid"] = (close - bb_mid) / bb_mid # normalised distance from mid
# Squeeze: narrow bands relative to recent history
df["bb_squeeze"] = np.where(
df["bb_width"] < df["bb_width"].rolling(50).mean(), 1.0, 0.0
)
# ── ATR (14) — volatility context ────────────────────────────────────────
atr_period = 14
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr"] = tr.ewm(com=atr_period - 1, min_periods=atr_period).mean()
df["natr"] = df["atr"] / close
# ── Momentum / price change features ─────────────────────────────────────
df["ret_1"] = close.pct_change(1)
df["ret_4"] = close.pct_change(4)
df["ret_16"] = close.pct_change(16)
# ── Trend context: SMA 50 & 200 ──────────────────────────────────────────
df["sma_50"] = close.rolling(50).mean()
df["sma_200"] = close.rolling(200).mean()
df["price_vs_50"] = (close - df["sma_50"]) / df["sma_50"]
df["price_vs_200"] = (close - df["sma_200"]) / df["sma_200"]
df["trend_up"] = np.where(df["sma_50"] > df["sma_200"], 1.0, 0.0)
# ── Volume proxy: candle body / range ratio ───────────────────────────────
candle_range = (high - low).replace(0, np.nan)
df["body_ratio"] = (close - open_).abs() / candle_range
df["bull_bar"] = np.where(close > open_, 1.0, 0.0)
# ── MACD-like momentum: EMA12 - EMA26 ────────────────────────────────────
ema12 = close.ewm(span=12, adjust=False).mean()
ema26 = close.ewm(span=26, adjust=False).mean()
df["macd"] = ema12 - ema26
df["macd_signal"] = df["macd"].ewm(span=9, adjust=False).mean()
df["macd_hist"] = df["macd"] - df["macd_signal"]
# ── Rolling volatility (std of returns) ──────────────────────────────────
df["vol_10"] = df["ret_1"].rolling(10).std()
# ── Hour-of-day and day-of-week (cyclical) ────────────────────────────────
if hasattr(df.index, "hour"):
df["hour_sin"] = np.sin(2 * np.pi * df.index.hour / 24)
df["hour_cos"] = np.cos(2 * np.pi * df.index.hour / 24)
df["dow_sin"] = np.sin(2 * np.pi * df.index.dayofweek / 5)
df["dow_cos"] = np.cos(2 * np.pi * df.index.dayofweek / 5)
# ── Combined signal: RSI + Stoch confluence ───────────────────────────────
df["conf_bull"] = np.where((df["rsi"] < 50) & (df["stoch_k"] < 50), 1.0, 0.0)
df["conf_bear"] = np.where((df["rsi"] > 50) & (df["stoch_k"] > 50), 1.0, 0.0)
# ── Fill NaN from warm-up periods ────────────────────────────────────────
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/CAD Stoch+BB+RSI Mean-Reversion (XGBoost)",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.80,
"colsample_bytree": 0.75,
"min_child_weight": 3,
"gamma": 0.10,
"reg_alpha": 0.10,
"reg_lambda": 1.50,
"objective": "binary:logistic",
"tree_method": "hist",
"random_state": 42,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 20],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) by combining "
"Stochastic (14,3), Bollinger Bands (20,2) and RSI(14) mean-reversion "
"signals with XGBoost. Regularisation (reg_alpha, reg_lambda, gamma, "
"min_child_weight) and column/row subsampling control overfitting. "
"A 0.55 confidence threshold filters low-conviction trades. "
"Session filter [7,20] UTC focuses on liquid London+NY overlap hours. "
"SL=0.5% / TP=1.0% gives a 1:2 risk-reward per trade."
),
"notes": (
"target_horizon=4 bars (1 hour on 15-min data) suits intraday mean-reversion. "
"Cyclical time features (hour_sin/cos, dow_sin/cos) capture intraday seasonality. "
"MACD histogram and rolling volatility provide trend/momentum context alongside "
"the core BB/RSI/Stoch mean-reversion suite. "
"reverse on_opposite allows the model to flip positions when conviction is high "
"in the opposing direction without waiting for flat cooldown."
),
}
|
||||||||||
|
0.11
|
GBP/USD RSI-MACD Momentum + Volatility Regime XGBoost
Maximize risk-adjusted return (Sharpe/Calmar) by combining RSI momentum divergence, MACD histogram dynamics, Bollinger squeeze, Stochastic c…
|
S
@still-lynx-704
|
GBPUSD | 15min | 54.1%57.6% | +0.11%+0.53% | 1.011.19 | 3.34%3.34% | 37933 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:23:23
# Model : XGBoost
# Feature Eng. : RSI 14, MACD (12,26,9) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/GBPUSD_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# --- RSI 14 ---
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=13, min_periods=14).mean()
avg_loss = loss.ewm(com=13, min_periods=14).mean()
rs = avg_gain / (avg_loss + 1e-12)
df["rsi_14"] = 100 - (100 / (1 + rs))
# RSI derived features
df["rsi_zscore"] = (df["rsi_14"] - df["rsi_14"].rolling(50).mean()) / (df["rsi_14"].rolling(50).std() + 1e-12)
df["rsi_slope"] = df["rsi_14"].diff(3)
df["rsi_above_50"] = np.where(df["rsi_14"] > 50, 1, 0)
df["rsi_overbought"] = np.where(df["rsi_14"] > 70, 1, 0)
df["rsi_oversold"] = np.where(df["rsi_14"] < 30, 1, 0)
# RSI divergence proxy: price direction vs RSI direction
price_dir_3 = np.sign(close.diff(3))
rsi_dir_3 = np.sign(df["rsi_14"].diff(3))
df["rsi_divergence"] = np.where(price_dir_3 != rsi_dir_3, 1, 0)
# --- MACD (12, 26, 9) ---
ema12 = close.ewm(span=12, adjust=False).mean()
ema26 = close.ewm(span=26, adjust=False).mean()
macd_line = ema12 - ema26
signal_line = macd_line.ewm(span=9, adjust=False).mean()
macd_hist = macd_line - signal_line
df["macd_line"] = macd_line
df["macd_signal"] = signal_line
df["macd_hist"] = macd_hist
# MACD derived features
df["macd_hist_slope"] = macd_hist.diff(2)
df["macd_cross_up"] = np.where((macd_line > signal_line) & (macd_line.shift(1) <= signal_line.shift(1)), 1, 0)
df["macd_cross_dn"] = np.where((macd_line < signal_line) & (macd_line.shift(1) >= signal_line.shift(1)), 1, 0)
df["macd_hist_positive"] = np.where(macd_hist > 0, 1, 0)
df["macd_hist_expanding"] = np.where(macd_hist.abs() > macd_hist.abs().shift(1), 1, 0)
df["macd_normalized"] = macd_line / (close + 1e-12)
# --- ATR 14 ---
tr1 = high - low
tr2 = (high - close.shift(1)).abs()
tr3 = (low - close.shift(1)).abs()
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
atr14 = tr.ewm(com=13, min_periods=14).mean()
df["atr_14"] = atr14
df["natr_14"] = atr14 / (close + 1e-12)
# ATR regime: high vs low volatility
atr_ma = atr14.rolling(50).mean()
df["atr_high_vol"] = np.where(atr14 > atr_ma * 1.2, 1, 0)
df["atr_low_vol"] = np.where(atr14 < atr_ma * 0.8, 1, 0)
# --- Bollinger Bands (20, 2) ---
bb_mid = close.rolling(20).mean()
bb_std = close.rolling(20).std()
bb_upper = bb_mid + 2 * bb_std
bb_lower = bb_mid - 2 * bb_std
df["bb_pct_b"] = (close - bb_lower) / (bb_upper - bb_lower + 1e-12)
df["bb_width"] = (bb_upper - bb_lower) / (bb_mid + 1e-12)
df["bb_squeeze"] = np.where(df["bb_width"] < df["bb_width"].rolling(50).quantile(0.2), 1, 0)
df["bb_upper_touch"] = np.where(close >= bb_upper * 0.999, 1, 0)
df["bb_lower_touch"] = np.where(close <= bb_lower * 1.001, 1, 0)
# --- Keltner Channel (20, 1.5x ATR) ---
kc_mid = close.ewm(span=20, adjust=False).mean()
kc_upper = kc_mid + 1.5 * atr14
kc_lower = kc_mid - 1.5 * atr14
df["kc_pct"] = (close - kc_lower) / (kc_upper - kc_lower + 1e-12)
# Squeeze: BB inside KC
df["kc_bb_squeeze"] = np.where((bb_upper < kc_upper) & (bb_lower > kc_lower), 1, 0)
# --- Volume-like proxy: bar range & body ---
bar_range = high - low
bar_body = (close - open_).abs()
df["range_norm"] = bar_range / (atr14 + 1e-12)
df["body_ratio"] = bar_body / (bar_range + 1e-12)
df["close_position"] = (close - low) / (bar_range + 1e-12)
df["bullish_bar"] = np.where(close > open_, 1, 0)
# --- Momentum & ROC ---
df["roc_5"] = close.pct_change(5)
df["roc_10"] = close.pct_change(10)
df["roc_20"] = close.pct_change(20)
df["momentum_10"] = close - close.shift(10)
df["momentum_20"] = close - close.shift(20)
# --- Moving Average features ---
ema8 = close.ewm(span=8, adjust=False).mean()
ema21 = close.ewm(span=21, adjust=False).mean()
ema50 = close.ewm(span=50, adjust=False).mean()
sma20 = close.rolling(20).mean()
sma50 = close.rolling(50).mean()
sma100 = close.rolling(100).mean()
df["ema8_21_gap"] = (ema8 - ema21) / (close + 1e-12)
df["ema21_50_gap"] = (ema21 - ema50) / (close + 1e-12)
df["price_vs_ema50"] = (close - ema50) / (close + 1e-12)
df["price_vs_sma20"] = (close - sma20) / (close + 1e-12)
df["price_vs_sma100"] = (close - sma100) / (close + 1e-12)
df["ema8_slope"] = ema8.diff(3) / (close + 1e-12)
df["ema21_slope"] = ema21.diff(3) / (close + 1e-12)
df["ema8_above_ema21"] = np.where(ema8 > ema21, 1, 0)
df["ema21_above_ema50"] = np.where(ema21 > ema50, 1, 0)
df["triple_ma_align_bull"] = np.where((ema8 > ema21) & (ema21 > ema50), 1, 0)
df["triple_ma_align_bear"] = np.where((ema8 < ema21) & (ema21 < ema50), 1, 0)
# --- Stochastic %K %D (14, 3) ---
lowest_low_14 = low.rolling(14).min()
highest_high_14 = high.rolling(14).max()
stoch_k = 100 * (close - lowest_low_14) / (highest_high_14 - lowest_low_14 + 1e-12)
stoch_d = stoch_k.rolling(3).mean()
df["stoch_k"] = stoch_k
df["stoch_d"] = stoch_d
df["stoch_kd_diff"] = stoch_k - stoch_d
df["stoch_overbought"] = np.where(stoch_k > 80, 1, 0)
df["stoch_oversold"] = np.where(stoch_k < 20, 1, 0)
df["stoch_cross_up"] = np.where((stoch_k > stoch_d) & (stoch_k.shift(1) <= stoch_d.shift(1)), 1, 0)
df["stoch_cross_dn"] = np.where((stoch_k < stoch_d) & (stoch_k.shift(1) >= stoch_d.shift(1)), 1, 0)
# --- Williams %R (14) ---
df["willr_14"] = -100 * (highest_high_14 - close) / (highest_high_14 - lowest_low_14 + 1e-12)
# --- CCI (20) ---
tp = (high + low + close) / 3
tp_ma = tp.rolling(20).mean()
tp_mad = tp.rolling(20).apply(lambda x: np.mean(np.abs(x - np.mean(x))), raw=True)
df["cci_20"] = (tp - tp_ma) / (0.015 * tp_mad + 1e-12)
df["cci_above_zero"] = np.where(df["cci_20"] > 0, 1, 0)
df["cci_extreme_bull"] = np.where(df["cci_20"] > 100, 1, 0)
df["cci_extreme_bear"] = np.where(df["cci_20"] < -100, 1, 0)
# --- Donchian Channel (20) ---
don_high = high.rolling(20).max()
don_low = low.rolling(20).min()
df["donchian_pct"] = (close - don_low) / (don_high - don_low + 1e-12)
df["donchian_breakout_up"] = np.where(close >= high.rolling(20).max().shift(1), 1, 0)
df["donchian_breakout_dn"] = np.where(close <= low.rolling(20).min().shift(1), 1, 0)
# --- Price pattern features ---
df["higher_high"] = np.where((high > high.shift(1)) & (high.shift(1) > high.shift(2)), 1, 0)
df["lower_low"] = np.where((low < low.shift(1)) & (low.shift(1) < low.shift(2)), 1, 0)
df["inside_bar"] = np.where((high < high.shift(1)) & (low > low.shift(1)), 1, 0)
df["outside_bar"] = np.where((high > high.shift(1)) & (low < low.shift(1)), 1, 0)
# --- Lag features for key indicators ---
for lag in [1, 2, 3, 4]:
df[f"rsi_14_lag{lag}"] = df["rsi_14"].shift(lag)
df[f"macd_hist_lag{lag}"] = df["macd_hist"].shift(lag)
df[f"bb_pct_b_lag{lag}"] = df["bb_pct_b"].shift(lag)
# --- Interaction features (avoiding lookahead) ---
df["rsi_macd_bull"] = np.where((df["rsi_14"] > 50) & (df["macd_hist"] > 0), 1, 0)
df["rsi_macd_bear"] = np.where((df["rsi_14"] < 50) & (df["macd_hist"] < 0), 1, 0)
df["rsi_bb_oversold_bounce"] = np.where((df["rsi_14"] < 35) & (df["bb_pct_b"] < 0.2), 1, 0)
df["rsi_bb_overbought_fade"] = np.where((df["rsi_14"] > 65) & (df["bb_pct_b"] > 0.8), 1, 0)
df["triple_bull"] = np.where(
(df["rsi_14"] > 50) & (df["macd_hist"] > 0) & (df["stoch_k"] > 50), 1, 0
)
df["triple_bear"] = np.where(
(df["rsi_14"] < 50) & (df["macd_hist"] < 0) & (df["stoch_k"] < 50), 1, 0
)
# --- Volatility regime ---
realized_vol = close.pct_change().rolling(20).std() * np.sqrt(96)
df["realized_vol_20"] = realized_vol
df["vol_regime_high"] = np.where(realized_vol > realized_vol.rolling(100).median(), 1, 0)
# --- Session-aware time features ---
if hasattr(df.index, 'hour'):
df["hour_sin"] = np.sin(2 * np.pi * df.index.hour / 24)
df["hour_cos"] = np.cos(2 * np.pi * df.index.hour / 24)
df["london_session"] = np.where((df.index.hour >= 7) & (df.index.hour < 16), 1, 0)
df["ny_session"] = np.where((df.index.hour >= 13) & (df.index.hour < 21), 1, 0)
df["overlap_session"] = np.where((df.index.hour >= 13) & (df.index.hour < 16), 1, 0)
df["asian_session"] = np.where((df.index.hour >= 0) & (df.index.hour < 7), 1, 0)
df["day_of_week"] = df.index.dayofweek
df["dow_sin"] = np.sin(2 * np.pi * df["day_of_week"] / 5)
df["dow_cos"] = np.cos(2 * np.pi * df["day_of_week"] / 5)
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "GBP/USD RSI-MACD Momentum + Volatility Regime XGBoost",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 500,
"max_depth": 4,
"learning_rate": 0.03,
"subsample": 0.75,
"colsample_bytree": 0.65,
"min_child_weight": 5,
"gamma": 0.15,
"reg_alpha": 0.3,
"reg_lambda": 1.5,
"scale_pos_weight": 1,
"objective": "binary:logistic",
"tree_method": "hist",
"random_state": 42,
},
"signal_threshold": 0.56,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.010,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 21],
"min_atr": 0.0002,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) by combining RSI momentum "
"divergence, MACD histogram dynamics, Bollinger squeeze, Stochastic crossovers, "
"volatility regime, and session-aware time features. XGBoost with moderate depth "
"and strong regularization prevents overfitting on 15-min GBP/USD data. "
"Signal threshold 0.56 filters weak signals, SL/TP at 0.5%/1.0% gives 1:2 RR."
),
"notes": (
"Differentiating from prior attempts (PF=1.08) by: (1) adding Keltner Channel "
"squeeze interaction with Bollinger, (2) CCI and Williams %R as confirmation, "
"Donchian breakout detection, (3) session-aware features (London/NY/overlap), "
"(4) richer MACD/RSI interaction flags, (5) realized volatility regime, "
"(6) stronger XGBoost regularization (alpha=0.3, lambda=1.5, min_child=5) "
"to reduce false signals in choppy GBP/USD conditions."
),
}
|
||||||||||
|
0.09
|
USD/CHF Stoch+BB+RSI Mean-Reversion (XGBoost)
Maximize risk-adjusted return (Sharpe/Calmar) on USD/CHF 15-min data. Uses Stochastic (14,3), Bollinger Bands (20,2), and RSI-14 as core fea…
|
R
@rapid-shark-854
|
USDCHF | 15min | 62.5%61.0% | +10.93%+0.52% | 1.181.16 | 4.00%4.00% | 74241 |
|
# ╔══════════════════════════════════════════════════════════════╗
# ║ STRATEGY REQUEST LOG ║
# ╚══════════════════════════════════════════════════════════════╝
# Generated : 2026-05-06 01:52:32
# Model : XGBoost
# Feature Eng. : BB (20,2.0), RSI 14, Stochastic (14,3) + Auto-add features: ON
# Signal / Entry : Enter when model confidence > threshold; exit on opposite signal or SL/TP
# Optimization : Maximize risk-adjusted return
# Risk Mgmt : Stop loss 0.5%, Take profit 1.0%
# Risk Filter : —
# ══════════════════════════════════════════════════════════════
# ============================================================
# SECTION 0 — IMPORTS & CONSTANTS
import numpy as np
import pandas as pd
DATA_PATH = "/root/Desktop/QuantifyMe/data/ohlc/USDCHF_15min.parquet"
START_DATE = "2025-04-24"
END_DATE = "2026-04-24"
VALIDATION_DATE = ""
TRAIN_SPLIT = 0.7
# SECTION 1 — FEATURE ENGINEERING
def feature_engineering(df, close, open_, high, low):
# ── RSI 14 ──────────────────────────────────────────────────────────────
period_rsi = 14
delta = close.diff()
gain = delta.clip(lower=0)
loss = -delta.clip(upper=0)
avg_gain = gain.ewm(com=period_rsi - 1, min_periods=period_rsi).mean()
avg_loss = loss.ewm(com=period_rsi - 1, min_periods=period_rsi).mean()
rs = avg_gain / avg_loss.replace(0, np.nan)
df["rsi_14"] = 100 - (100 / (1 + rs))
# ── Bollinger Bands (20, 2) ──────────────────────────────────────────────
bb_period = 20
bb_std = 2.0
bb_mid = close.rolling(bb_period).mean()
bb_std_val = close.rolling(bb_period).std(ddof=0)
bb_upper = bb_mid + bb_std * bb_std_val
bb_lower = bb_mid - bb_std * bb_std_val
df["bb_mid"] = bb_mid
df["bb_upper"] = bb_upper
df["bb_lower"] = bb_lower
df["bb_width"] = (bb_upper - bb_lower) / bb_mid
bb_range = (bb_upper - bb_lower).replace(0, np.nan)
df["bb_pct"] = (close - bb_lower) / bb_range
# ── Stochastic Oscillator (K=14, D=3) ───────────────────────────────────
stoch_k_period = 14
stoch_d_period = 3
lowest_low = low.rolling(stoch_k_period).min()
highest_high = high.rolling(stoch_k_period).max()
stoch_range = (highest_high - lowest_low).replace(0, np.nan)
df["stoch_k"] = 100 * (close - lowest_low) / stoch_range
df["stoch_d"] = df["stoch_k"].rolling(stoch_d_period).mean()
df["stoch_kd_diff"] = df["stoch_k"] - df["stoch_d"]
# ── Additional derived features ──────────────────────────────────────────
# RSI momentum & zone flags
df["rsi_lag1"] = df["rsi_14"].shift(1)
df["rsi_momentum"] = df["rsi_14"] - df["rsi_lag1"]
df["rsi_oversold"] = np.where(df["rsi_14"] < 30, 1, 0)
df["rsi_overbought"] = np.where(df["rsi_14"] > 70, 1, 0)
# BB squeeze: width below rolling 20-bar median of bb_width
bb_width_median = df["bb_width"].rolling(20).median()
df["bb_squeeze"] = np.where(df["bb_width"] < bb_width_median, 1, 0)
# BB position zone
df["bb_below_lower"] = np.where(close < bb_lower, 1, 0)
df["bb_above_upper"] = np.where(close > bb_upper, 1, 0)
# Stochastic zone flags
df["stoch_oversold"] = np.where(df["stoch_k"] < 20, 1, 0)
df["stoch_overbought"] = np.where(df["stoch_k"] > 80, 1, 0)
# Price momentum (rate of change)
df["roc_4"] = close.pct_change(4)
df["roc_8"] = close.pct_change(8)
df["roc_16"] = close.pct_change(16)
# ATR (14-bar) for volatility context
atr_period = 14
tr = pd.concat([
high - low,
(high - close.shift(1)).abs(),
(low - close.shift(1)).abs()
], axis=1).max(axis=1)
df["atr_14"] = tr.ewm(com=atr_period - 1, min_periods=atr_period).mean()
df["natr_14"] = df["atr_14"] / close
# EMA crossover signals
ema_fast = close.ewm(span=8, min_periods=8).mean()
ema_slow = close.ewm(span=21, min_periods=21).mean()
df["ema_fast"] = ema_fast
df["ema_slow"] = ema_slow
df["ema_cross"] = ema_fast - ema_slow
df["ema_cross_sign"] = np.where(df["ema_cross"] > 0, 1, -1)
# SMA 50 trend context
df["sma_50"] = close.rolling(50).mean()
df["close_vs_sma50"] = (close - df["sma_50"]) / df["sma_50"]
# Candle body and direction
df["candle_body"] = (close - open_).abs()
df["candle_range"] = (high - low).replace(0, np.nan)
df["body_ratio"] = df["candle_body"] / df["candle_range"]
df["candle_dir"] = np.where(close >= open_, 1, -1)
# Volume-proxy: range relative to rolling average range
df["rel_range"] = (high - low) / (high - low).rolling(20).mean()
# Lag features for RSI, stoch_k, bb_pct
for lag in [1, 2, 3]:
df[f"rsi_14_lag{lag}"] = df["rsi_14"].shift(lag)
df[f"stoch_k_lag{lag}"] = df["stoch_k"].shift(lag)
df[f"bb_pct_lag{lag}"] = df["bb_pct"].shift(lag)
df[f"ema_cross_lag{lag}"] = df["ema_cross"].shift(lag)
# Divergence proxy: price making new high but RSI not
price_high_4 = close.rolling(4).max()
rsi_high_4 = df["rsi_14"].rolling(4).max()
df["bearish_div_proxy"] = np.where(
(close >= price_high_4.shift(1)) & (df["rsi_14"] < rsi_high_4.shift(1)), 1, 0
)
price_low_4 = close.rolling(4).min()
rsi_low_4 = df["rsi_14"].rolling(4).min()
df["bullish_div_proxy"] = np.where(
(close <= price_low_4.shift(1)) & (df["rsi_14"] > rsi_low_4.shift(1)), 1, 0
)
# Combined confluence signals
df["long_confluence"] = np.where(
(df["rsi_14"] < 45) & (df["stoch_k"] < 50) & (df["bb_pct"] < 0.5), 1, 0
)
df["short_confluence"] = np.where(
(df["rsi_14"] > 55) & (df["stoch_k"] > 50) & (df["bb_pct"] > 0.5), 1, 0
)
# Fill NaN from warm-up
df = df.bfill().ffill()
return df
# SECTION 2 — STRATEGY CONFIG
def strategy_config():
return {
"title": "USD/CHF Stoch+BB+RSI Mean-Reversion (XGBoost)",
"model_type": "XGBClassifier",
"model_params": {
"n_estimators": 400,
"max_depth": 4,
"learning_rate": 0.04,
"subsample": 0.8,
"colsample_bytree": 0.75,
"min_child_weight": 5,
"gamma": 0.1,
"reg_alpha": 0.05,
"reg_lambda": 1.5,
"objective": "binary:logistic",
"tree_method": "hist",
"random_state": 42,
"n_jobs": -1,
},
"signal_threshold": 0.55,
"direction": "both",
"stop_loss": 0.005,
"take_profit": 0.01,
"cooldown": 0,
"max_positions": 1,
"on_opposite": "reverse",
"session_filter": [7, 17],
"min_atr": None,
"trend_filter": None,
"target_horizon": 4,
"objective": (
"Maximize risk-adjusted return (Sharpe/Calmar) on USD/CHF 15-min data. "
"Uses Stochastic (14,3), Bollinger Bands (20,2), and RSI-14 as core features "
"with confluence signals, divergence proxies, and EMA crossover context. "
"XGBoost chosen for its strong performance on tabular data with regularization "
"parameters (gamma, alpha, lambda) tuned to reduce overfitting on short date "
"ranges. SL=0.5%/TP=1.0% gives 1:2 R:R ratio. Session filter [7,17] UTC targets "
"London/NY overlap for higher-quality moves. Signal threshold 0.55 filters noise "
"while preserving trade frequency."
),
"notes": (
"Feature set combines mean-reversion indicators (RSI, Stochastic, BB percentile) "
"with trend context (EMA cross, SMA50 distance) and volatility measures (ATR, "
"BB width/squeeze). Lag features (1-3 bars) capture recent indicator momentum. "
"Bullish/bearish divergence proxies add signal quality. Shallow trees (max_depth=4) "
"with high n_estimators and slow learning rate reduce variance. Colsample and "
"subsample add stochastic regularization."
),
}
|
||||||||||