SniperGold_ML/ml/parity/probe_h4_window.py

83 lines
3 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
"""P2.1 probe: infer window/endpoint yang dipakai EA utk f1 (H4) pd row terpilih.
Row 2026-03-18 01:00 (tc=01:15): PY=CLOSED=ANCHOR=-1 tetapi EA=+1.
Cari kombinasi (endpoint, length) yang menghasilkan +1 pada data npz H4.
Endpoint kandidat: bar terakhir yg boleh masuk window (E).
Length kandidat: 120..300.
"""
import os
import datetime as dt
import numpy as np
DATA = r"D:\TradingTerminal\HFM Metatrader 5\MQL5\Files\AlgoForge\Data"
def load(name):
z = np.load(os.path.join(DATA, "XAUUSD_" + name + ".npz"))
return (z["time"].astype(np.int64), z["high"].astype(float),
z["low"].astype(float), z["close"].astype(float))
def bttf(h, l, c, need):
"""Replika BTTFBias EA (need=len window)."""
if need < 120:
return 0
s = 3
m = need - 1
up, dn = float("inf"), -float("inf")
upb = dnb = -1
trend = 0
for i in range(s + 1, m):
p = i - s
if p >= s:
isH = isL = True
for kk in range(1, s + 1):
if h[p] <= h[p + kk] or h[p] <= h[p - kk]:
isH = False
if l[p] >= l[p + kk] or l[p] >= l[p - kk]:
isL = False
if isH:
up, upb = h[p], p
if isL:
dn, dnb = l[p], p
if upb >= 0 and c[i] > up:
trend = 1
up, upb = float("inf"), -1
if dnb >= 0 and c[i] < dn:
trend = -1
dn, dnb = -float("inf"), -1
return trend
ht, hh, hl, hc = load("H4")
t = int(dt.datetime(2026, 3, 18, 1, 0, tzinfo=dt.timezone.utc).timestamp())
tc = t + 900
# endpoint kandidat (indeks H4)
ends = {}
ends["20:00 03-17 (E_ea)"] = int(np.searchsorted(ht, tc - 14400, side="right")) - 1
ends["00:00 03-18 (contain)"] = int(np.searchsorted(ht, t, side="right")) - 1
ends["00:00 03-18 -1 (E_py)"] = int(np.searchsorted(ht, t, side="right")) - 2
ends["04:00 03-18 (forming)"] = int(np.searchsorted(ht, tc, side="right")) - 1
ends["16:00 03-17 (stale-1)"] = ends["20:00 03-17 (E_ea)"] - 1
ends["12:00 03-17 (stale-2)"] = ends["20:00 03-17 (E_ea)"] - 2
print(f"row t={dt.datetime.fromtimestamp(t, dt.timezone.utc)} tc={dt.datetime.fromtimestamp(tc, dt.timezone.utc)} EA f1=+1")
for name, e in ends.items():
if e < 0 or e >= len(ht):
continue
ts = dt.datetime.fromtimestamp(int(ht[e]), dt.timezone.utc)
row = f" end={name:24s} e={e:5d} ({ts})"
vals = []
for L in (120, 150, 180, 200, 210, 220, 240, 250, 260, 280, 300):
start = max(0, e - L + 1)
v = bttf(hh[start:e + 1], hl[start:e + 1], hc[start:e + 1], e - start + 1)
vals.append(f"L{L}={v:+d}")
print(row + " | " + " ".join(vals))
# juga: cek apakah data H4 npz di sekitar window wajar (sanity)
print("\nH4 bars 03-17 16:00 .. 03-18 04:00:")
i0 = int(np.searchsorted(ht, int(dt.datetime(2026, 3, 17, 16, tzinfo=dt.timezone.utc).timestamp())))
for i in range(i0, i0 + 6):
print(f" {dt.datetime.fromtimestamp(int(ht[i]), dt.timezone.utc)} H={hh[i]:.2f} L={hl[i]:.2f} C={hc[i]:.2f}")