Khanna, Videh Rakesh Rakesh commited on
Commit
7839455
Β·
1 Parent(s): f7bbcd1

Add dip order probe research script

Browse files
Files changed (1) hide show
  1. research/dip_order_probe.py +208 -0
research/dip_order_probe.py ADDED
@@ -0,0 +1,208 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """research/dip_order_probe.py β€” PROTOTYPE: can the model learn "dip-before-high"?
3
+
4
+ Dip-entry (limit buy at the modeled pullback) only makes money if the intraday LOW
5
+ occurs BEFORE the HIGH β€” price dips first, then rallies. The production quantile model
6
+ predicts only MAGNITUDES (max up / max down excursion), never their ORDER, so dip-entry
7
+ today rests on an ordering the model cannot forecast.
8
+
9
+ This probe tests whether that ordering is *learnable*:
10
+ 1. From the cached 15-minute bars (the ONLY source of true intraday sequence, ~60 days),
11
+ build a label low_before_high = (time of session Low) < (time of session High)
12
+ for each (ticker, day, prediction-time T), using bars in [T, 15:00 IST].
13
+ 2. Train a classifier on the SAME production features (as-of previous daily close) β†’ the
14
+ probability the dip precedes the high. Report out-of-time AUC vs the base rate.
15
+ 3. Back-test dip-entry three ways on the held-out dates and compare win-rate + expectancy:
16
+ (a) MARKET entry, (b) UNCONDITIONAL dip entry, (c) dip entry ONLY when the model
17
+ says P(dip-first) is high. If (c) > (b) > (a), the ordering head adds real value.
18
+
19
+ This is research only β€” NOT wired into production. It is inherently limited by the ~60-day
20
+ 15-min history (small sample; a real feature would need a longer intraday archive).
21
+
22
+ Usage:
23
+ python research/dip_order_probe.py --n-universe 40
24
+ python research/dip_order_probe.py --tickers SBIN.NS,TATASTEEL.NS --prob-threshold 0.6
25
+ """
26
+ from __future__ import annotations
27
+
28
+ import argparse
29
+ import os
30
+ import sys
31
+
32
+ import numpy as np
33
+ import pandas as pd
34
+
35
+ _PROJ_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
36
+ if _PROJ_ROOT not in sys.path:
37
+ sys.path.insert(0, _PROJ_ROOT)
38
+
39
+ from ml_predictor.features import compute_features, FEATURE_COLUMNS # noqa: E402
40
+ from ml_predictor.infer import MLPredictor, intraday_session_scale # noqa: E402
41
+ from research.ml_intraday_backtest import ( # noqa: E402
42
+ _fetch_15m, _watchlist, _sample_universe, _dip_path_pnl,
43
+ PRED_TIMES, CLOSE_TIME, ROUND_TRIP_COST_PCT,
44
+ )
45
+
46
+ OUT_CSV = os.path.join(os.path.dirname(os.path.abspath(__file__)), "dip_order_probe.csv")
47
+
48
+
49
+ def _collect(tickers: list[str], nifty_c, vix_c) -> pd.DataFrame:
50
+ """Walk cached 15-min bars β†’ per (ticker, day, T): features, ordering label, trade outcomes."""
51
+ from data_sources import fetch_ohlcv
52
+ predictor = MLPredictor()
53
+ if not predictor.available:
54
+ raise SystemExit("ml_predictor model not loaded β€” run ml_predictor/train.py first.")
55
+ rows = []
56
+ for tk in tickers:
57
+ intr = _fetch_15m(tk)
58
+ if intr is None or intr.empty:
59
+ continue
60
+ try:
61
+ sc, sh, sl, sv = fetch_ohlcv(tk, "2y")
62
+ c, h, l, v = sc[tk].dropna(), sh[tk].dropna(), sl[tk].dropna(), sv[tk].dropna()
63
+ except Exception:
64
+ continue
65
+ if len(c) < 210:
66
+ continue
67
+ intr["t"] = intr.index.strftime("%H:%M")
68
+ intr["d"] = intr.index.date
69
+ for day, g in intr.groupby("d"):
70
+ day_ts = pd.Timestamp(day)
71
+ prior = c.index[c.index < day_ts]
72
+ if len(prior) < 210:
73
+ continue
74
+ feat = compute_features(c, h, l, v, nifty_c, vix_c, date=prior[-1])
75
+ if feat is None:
76
+ continue
77
+ feat_row = [feat.get(k, float("nan")) for k in predictor.feature_columns]
78
+ atrp = feat.get("atr_pct")
79
+ for T in PRED_TIMES:
80
+ bar_T = g[g["t"] == T]
81
+ if bar_T.empty:
82
+ continue
83
+ entry = float(bar_T["Open"].iloc[0])
84
+ if entry <= 0:
85
+ continue
86
+ win = g[(g["t"] >= T) & (g["t"] <= CLOSE_TIME)]
87
+ if len(win) < 2:
88
+ continue
89
+ # ── True ordering label: did the LOW precede the HIGH? ──
90
+ low_time = win["Low"].idxmin()
91
+ high_time = win["High"].idxmax()
92
+ low_before_high = int(low_time < high_time)
93
+
94
+ atr14 = (atrp / 100 * entry) if (atrp and np.isfinite(atrp)) else None
95
+ hh, mm = int(T[:2]), int(T[3:])
96
+ scale = intraday_session_scale(hh * 60 + mm)
97
+ pred = predictor._predict_tf(feat_row, "INTRADAY", entry, atr14,
98
+ live_price=entry, today_high=None, news_score=0,
99
+ anchor_close=entry, intraday_scale=scale)
100
+ exp_ret = (pred["expected_target_price"] / entry - 1) * 100.0
101
+ dip_pct = float(pred.get("quantiles", {}).get("down_q50", 0.0))
102
+ stop_pct = pred["stop_loss_pct"]
103
+
104
+ # Market-entry outcome (win if target touched before stop, bar-walk).
105
+ mkt_pnl, _ = _dip_path_pnl(win, entry, 0.0, exp_ret, stop_pct) # dip=0 β†’ fills at open
106
+ dip_pnl, dip_filled = _dip_path_pnl(win, entry, dip_pct, exp_ret, stop_pct)
107
+
108
+ row = {k: feat.get(k, float("nan")) for k in FEATURE_COLUMNS}
109
+ row.update({
110
+ "ticker": tk, "date": str(day), "pred_time": T,
111
+ "direction": pred["direction"], "should_buy": int(pred["should_buy"]),
112
+ "low_before_high": low_before_high,
113
+ "mkt_pnl": mkt_pnl, "dip_pnl": dip_pnl, "dip_filled": int(dip_filled),
114
+ })
115
+ rows.append(row)
116
+ return pd.DataFrame(rows)
117
+
118
+
119
+ def _win(x):
120
+ x = x[~pd.isna(x)]
121
+ return float((x > 0).mean()) if len(x) else float("nan")
122
+
123
+
124
+ def run(tickers: list[str], nifty_c, vix_c, prob_threshold: float = 0.55):
125
+ df = _collect(tickers, nifty_c, vix_c)
126
+ if df.empty:
127
+ raise SystemExit("No rows produced (need cached 15-min bars β€” run ml_intraday_backtest first).")
128
+ df.to_csv(OUT_CSV, index=False)
129
+ dates = sorted(df["date"].unique())
130
+ if len(dates) < 6:
131
+ raise SystemExit(f"Only {len(dates)} distinct dates β€” too few for a time split.")
132
+ cut = dates[int(len(dates) * 0.7)]
133
+ train, test = df[df["date"] < cut], df[df["date"] >= cut]
134
+
135
+ print("\n" + "=" * 82)
136
+ print(" DIP-BEFORE-HIGH ORDERING PROBE β€” can the model learn intraday sequence?")
137
+ print(f" {df['ticker'].nunique()} tickers Β· {len(dates)} days Β· {len(df):,} rows "
138
+ f"(train<{cut}: {len(train):,}, testβ‰₯{cut}: {len(test):,})")
139
+ print("=" * 82)
140
+ base_rate = df["low_before_high"].mean()
141
+ print(f" Base rate P(low precedes high) = {base_rate:.0%} (a coin-flip baseline)")
142
+
143
+ # ── Train the ordering classifier on production features ──
144
+ from sklearn.ensemble import HistGradientBoostingClassifier
145
+ from sklearn.metrics import roc_auc_score
146
+ Xtr = train[FEATURE_COLUMNS].to_numpy(float)
147
+ ytr = train["low_before_high"].to_numpy(int)
148
+ Xte = test[FEATURE_COLUMNS].to_numpy(float)
149
+ yte = test["low_before_high"].to_numpy(int)
150
+ auc = float("nan")
151
+ p_te = np.full(len(test), base_rate)
152
+ if len(np.unique(ytr)) == 2:
153
+ clf = HistGradientBoostingClassifier(max_iter=300, max_leaf_nodes=31,
154
+ learning_rate=0.06, l2_regularization=1.0,
155
+ random_state=0)
156
+ clf.fit(Xtr, ytr)
157
+ p_te = clf.predict_proba(Xte)[:, 1]
158
+ if len(np.unique(yte)) == 2:
159
+ auc = roc_auc_score(yte, p_te)
160
+ print(f" Classifier out-of-time AUC = {auc:.3f} "
161
+ f"(0.50 = no skill, >0.55 = weak signal, >0.60 = usable)")
162
+
163
+ # ── Compare entry strategies on the TEST slice (BULLISH long signals only) ──
164
+ test = test.copy()
165
+ test["p_dip_first"] = p_te
166
+ buys = test[test["should_buy"] == 1]
167
+ print(f"\n ENTRY-STRATEGY COMPARISON on test BULLISH signals (n={len(buys)}):")
168
+ print(f" {'strategy':<34}{'#trades':>8}{'win%':>7}{'exp/sig%':>10}")
169
+ print(" " + "-" * 59)
170
+ # (a) market
171
+ mk = buys["mkt_pnl"]
172
+ print(f" {'MARKET entry':<34}{mk.notna().sum():>8}{_win(mk):>7.0%}{mk.fillna(0).mean():>+10.2f}")
173
+ # (b) unconditional dip
174
+ dp = buys[buys["dip_filled"] == 1]["dip_pnl"]
175
+ exp_sig_dip = buys["dip_pnl"].where(buys["dip_filled"] == 1).fillna(0).mean()
176
+ print(f" {'DIP entry (unconditional)':<34}{len(dp):>8}{_win(dp):>7.0%}{exp_sig_dip:>+10.2f}")
177
+ # (c) conditional dip β€” only where model predicts dip-first
178
+ cond = buys[(buys["dip_filled"] == 1) & (buys["p_dip_first"] >= prob_threshold)]["dip_pnl"]
179
+ exp_sig_cond = buys["dip_pnl"].where(
180
+ (buys["dip_filled"] == 1) & (buys["p_dip_first"] >= prob_threshold)).fillna(0).mean()
181
+ print(f" {f'DIP entry (Pβ‰₯{prob_threshold:.2f} dip-first)':<34}{len(cond):>8}{_win(cond):>7.0%}{exp_sig_cond:>+10.2f}")
182
+ print("\n win% = of trades taken, how many profited. exp/sig = avg P&L per SIGNAL")
183
+ print(" (unfilled/skipped = 0, so it reflects deploying capital across every signal).")
184
+ print(f"\n βœ“ Wrote per-row probe data β†’ {OUT_CSV}")
185
+
186
+
187
+ def main():
188
+ ap = argparse.ArgumentParser()
189
+ ap.add_argument("--tickers", default=None, help="comma-separated (default = watchlist + sample)")
190
+ ap.add_argument("--n-universe", type=int, default=40, help="add N liquid universe names")
191
+ ap.add_argument("--prob-threshold", type=float, default=0.55,
192
+ help="min P(dip-first) to take a conditional dip trade")
193
+ args = ap.parse_args()
194
+ if args.tickers:
195
+ tickers = [t.strip() for t in args.tickers.split(",")]
196
+ else:
197
+ tickers = sorted(set(_watchlist()) | set(_sample_universe(args.n_universe)))
198
+ try:
199
+ import yfinance as yf
200
+ raw = yf.download(["^NSEI", "^INDIAVIX"], period="1y", auto_adjust=True, progress=False)
201
+ nifty_c, vix_c = raw["Close"]["^NSEI"].dropna(), raw["Close"]["^INDIAVIX"].dropna()
202
+ except Exception:
203
+ nifty_c = vix_c = None
204
+ run(tickers, nifty_c, vix_c, prob_threshold=args.prob_threshold)
205
+
206
+
207
+ if __name__ == "__main__":
208
+ main()