asf
This commit is contained in:
1 parent
3898609a52
commit
1cfbf11176
3003 files changed
+109
-6
No files matched your search
@@ -0,0 +1,68 @@
|
||||
"""Shared pieces: data loading, contract specs, bracket simulator (same logic as the main backtest engine)."""
|
||||
import os, numpy as np, pandas as pd
|
||||
|
||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||
DATA = os.path.join(HERE, "data")
|
||||
OUT = os.path.join(HERE, "out")
|
||||
LOCKBOX_START = np.datetime64("2025-07-01")
|
||||
DISC_START = np.datetime64("2016-01-01")
|
||||
MARKETS = {"main": ["NQ", "ES", "GC", "SI", "CL", "NG", "6E"], "hold": ["NQ", "GC", "SI", "CL"]}
|
||||
CLOCKS = [f"{(18 + (30 * k) // 60) % 24:02d}:{(30 * k) % 60:02d}" for k in range(1, 44)] # 18:30 ... 15:30
|
||||
|
||||
# Lucid round-trip commissions (support article "Approved products and commissions", per side x 2).
|
||||
# full: points value, tick, commission; small contract: ratio to full, commission, tick (None = none on Lucid)
|
||||
# NQ/MNQ, ES/MES, GC/MGC, SI/SIL (1/5), CL/MCL, NG/QG (E-mini, 1/4, tick 0.005; no micro NG on Lucid), 6E (no M6E on Lucid)
|
||||
SPEC = {"NQ": (20.0, 0.25, 3.50, 10, 1.00, 0.25), "ES": (50.0, 0.25, 3.50, 10, 1.00, 0.25), "GC": (100.0, 0.10, 4.60, 10, 1.60, 0.10),
|
||||
"SI": (5000.0, 0.005, 4.60, 5, 3.20, 0.005), "CL": (1000.0, 0.01, 4.00, 10, 1.00, 0.01), "NG": (10000.0, 0.001, 4.00, 4, 2.60, 0.005),
|
||||
"6E": (125000.0, 0.00005, 4.80, None, None, None)}
|
||||
BRACKETS = {"ev": (2000.0, 3000.0), "fu": (2000.0, 1640.0)}
|
||||
|
||||
def mins(clock):
|
||||
h, m = int(clock[:2]), int(clock[3:]); return (h * 60 + m - 1080) % 1440
|
||||
|
||||
class Sessions:
|
||||
"""One market/period: sessions 18:00 -> 16:39 of the front contract, prices in points."""
|
||||
def __init__(self, m, per):
|
||||
z = np.load(os.path.join(DATA, f"{m}_{per}.npz"))
|
||||
tk = float(z["tick"]); self.m, self.tick = m, tk
|
||||
self.dates = z["dates"].astype("datetime64[D]"); self.off = z["off"]; self.atr = z["atr"]
|
||||
self.t = z["t"].astype(np.int32); self.v = z["v"].astype(np.float64)
|
||||
self.o, self.h, self.l, self.c = (z[k].astype(np.float64) * tk for k in "ohlc")
|
||||
self.idx = {d: i for i, d in enumerate(self.dates)}
|
||||
def day(self, i):
|
||||
a, b = self.off[i], self.off[i + 1]; return slice(a, b)
|
||||
|
||||
def _bracket(m, micro, o, h, l, c, entry_close, atr, side, stop_usd, tgt_usd, harsh, maxc):
|
||||
ppt, tick, comm, ratio, mcomm, mtick = SPEC[m]
|
||||
if micro: ppt, comm, tick = ppt / ratio, mcomm, mtick
|
||||
k = 2 if m == "NQ" else 1; mult = 2 if harsh else 1
|
||||
eslip, sslip, xslip = k * mult * tick, 2 * k * mult * tick, k * mult * tick
|
||||
allowance = 3.0 if micro else 20.0
|
||||
n = int(np.clip(round(stop_usd / (ppt * 0.2 * atr)), 1, maxc))
|
||||
sp = (stop_usd - allowance * n) / (n * ppt); tp = (tgt_usd + comm * n) / (n * ppt)
|
||||
sp = np.floor(sp / tick + 1e-9) * tick; tp = np.ceil(tp / tick - 1e-9) * tick
|
||||
if sp <= 0: return np.nan
|
||||
e = entry_close + side * eslip; S = e - side * sp; T = e + side * tp; thr = tick
|
||||
if side > 0: hs = l <= S; ht = h >= T + thr; gs = o <= S; gt = o >= T + thr
|
||||
else: hs = h >= S; ht = l <= T - thr; gs = o >= S; gt = o <= T - thr
|
||||
i_s = np.argmax(hs) if hs.any() else 10**9; i_t = np.argmax(ht) if ht.any() else 10**9
|
||||
if i_s == 10**9 and i_t == 10**9:
|
||||
if len(c) == 0: return np.nan
|
||||
pts = side * (c[-1] - side * xslip - e)
|
||||
elif i_s < i_t or (i_s == i_t and (gs[i_s] or (not gt[i_s] and (harsh or (c[i_s] >= o[i_s]) == (side > 0))))):
|
||||
fill = o[i_s] if gs[i_s] else S
|
||||
pts = side * (fill - side * sslip - e)
|
||||
else:
|
||||
pts = tp
|
||||
return pts * n * ppt - comm * n
|
||||
|
||||
def trade(m, o, h, l, c, entry_close, atr, side, stop_usd, tgt_usd, harsh, maxc=4):
|
||||
"""ATR sizing: full contracts if >= 0.75 contract is called for, else small contracts (cap x ratio); 6E has none -> 1 contract."""
|
||||
ppt, tick, comm, ratio, mcomm, mtick = SPEC[m]
|
||||
if stop_usd / (0.2 * atr) >= 0.75 * ppt or ratio is None:
|
||||
return _bracket(m, False, o, h, l, c, entry_close, atr, side, stop_usd, tgt_usd, harsh, maxc if ratio else maxc)
|
||||
return _bracket(m, True, o, h, l, c, entry_close, atr, side, stop_usd, tgt_usd, harsh, maxc * ratio)
|
||||
|
||||
def tstat(v):
|
||||
v = np.asarray(v, float); v = v[~np.isnan(v)]
|
||||
return v.mean() / (v.std(ddof=1) / np.sqrt(len(v))) if len(v) > 20 and v.std() > 0 else np.nan
|
||||
Reference in new issue
Block a user