40Hz commited on
Commit
d648a0d
·
verified ·
1 Parent(s): e99d55c

autoresearch v2: honest grid

Browse files
Files changed (1) hide show
  1. autoresearch.py +145 -0
autoresearch.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """autoresearch.py - self-improvement loop for the quad autopilot.
2
+ Runs on GitHub Actions (hourly) / local cron. Cloud + local.
3
+
4
+ Tradeable grid = Polymarket 15-min Up/Down products only (verified live via
5
+ gamma-api: only 5-min and 15-min exist; no 1h). Signal = RSI(14) < threshold
6
+ on the 15-min close, buy YES-"up", win if close[t+1] > close[t] (+15m),
7
+ buy at 0.50 + 0.03 spread => breakeven hit 0.53.
8
+
9
+ 21-day full-data study (2026-08-09, 6048x5m bars/coin) FALSIFIED the old
10
+ baseline (hit 0.788 / roi +0.258 - not reproducible on fresh data). Honest
11
+ result: only ETH clears the spread-adjusted breakeven (thr=30: hit 0.609,
12
+ n=64, edge +0.079, WF OOS 0.680). BTC/SOL/XRP are coin-flips. This script
13
+ tracks that grid and flags the signal that still works.
14
+ """
15
+ import argparse
16
+ import csv
17
+ import json
18
+ import os
19
+ import urllib.request
20
+ from datetime import datetime, timezone
21
+
22
+ COINS = ["BTC", "ETH", "SOL", "XRP"]
23
+ BINANCE_HOSTS = os.environ.get("BINANCE_HOSTS", "data-api.binance.vision,api.binance.com").split(",")
24
+ BINANCE_TMPL = "https://{}/api/v3/klines?symbol={}USDT&interval=5m&limit=1200"
25
+ REPORT = os.environ.get("REPORT_PATH", "/home/neo/quad_research.csv")
26
+ OOS_BARS = int(os.environ.get("OOS_BARS", "200"))
27
+ THRESHOLDS = [int(x) for x in os.environ.get("THRESHOLDS", "20,25,30,35").split(",")]
28
+ BREAKEVEN = float(os.environ.get("BREAKEVEN", "0.53"))
29
+ EDGE_FLOOR = float(os.environ.get("EDGE_FLOOR", "0.03"))
30
+ MIN_N = int(os.environ.get("MIN_N", "10"))
31
+
32
+ FIELDS = ["ts", "coin", "bars", "oos_bars", "thr", "n", "hit", "edge",
33
+ "breakeven", "status"]
34
+
35
+
36
+ def fetch_closes(symbol):
37
+ for host in BINANCE_HOSTS:
38
+ host = host.strip()
39
+ url = BINANCE_TMPL.format(host, symbol)
40
+ try:
41
+ with urllib.request.urlopen(url, timeout=15) as r:
42
+ arr = json.load(r)
43
+ return [float(x[4]) for x in arr]
44
+ except Exception as e:
45
+ print(f"[{symbol}] {host} failed ({e}); trying next host")
46
+ continue
47
+ raise RuntimeError("all binance hosts failed")
48
+
49
+
50
+ def agg15(closes5):
51
+ """Aggregate 5m closes to 15m closes (bucket = floor to 15m; last close wins)."""
52
+ buckets = {}
53
+ n = len(closes5)
54
+ for i in range(n):
55
+ buckets[i // 3] = closes5[i]
56
+ return [buckets[k] for k in sorted(buckets)]
57
+
58
+
59
+ def rsi(closes, period=14):
60
+ d = [closes[i] - closes[i - 1] for i in range(1, len(closes))]
61
+ gains = [x if x > 0 else 0.0 for x in d]
62
+ losses = [-x if x < 0 else 0.0 for x in d]
63
+ ag = sum(gains[:period]) / period
64
+ al = sum(losses[:period]) / period
65
+ out = []
66
+ for i in range(len(d)):
67
+ if i >= period:
68
+ ag = (ag * (period - 1) + gains[i]) / period
69
+ al = (al * (period - 1) + losses[i]) / period
70
+ out.append(100 if al == 0 else 100 - 100 / (1 + ag / al))
71
+ return out
72
+
73
+
74
+ def validate(close15, thr, horizon=1, oos_bars=200):
75
+ """OOS-only: last oos_bars 15m closes. Signal on candle t if RSI(t)<thr,
76
+ win = close[t+1] > close[t] (+15m). edge = hit - breakeven."""
77
+ n = len(close15)
78
+ start = max(horizon + 1, n - oos_bars)
79
+ rs = rsi(close15)
80
+ rows = []
81
+ for t in range(start, n - horizon):
82
+ if rs[t - 1] < thr:
83
+ rows.append(close15[t + horizon] > close15[t])
84
+ if not rows:
85
+ return 0, None
86
+ hit = sum(rows) / len(rows)
87
+ return len(rows), hit
88
+
89
+
90
+ def now_iso():
91
+ return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
92
+
93
+
94
+ def append_report(row):
95
+ exists = os.path.exists(REPORT)
96
+ d = os.path.dirname(REPORT)
97
+ if d:
98
+ os.makedirs(d, exist_ok=True)
99
+ with open(REPORT, "a", newline="") as f:
100
+ w = csv.DictWriter(f, fieldnames=FIELDS)
101
+ if not exists:
102
+ w.writeheader()
103
+ w.writerow({k: row.get(k, "") for k in FIELDS})
104
+
105
+
106
+ def main():
107
+ ap = argparse.ArgumentParser()
108
+ ap.add_argument("--coin", choices=COINS + ["ALL"], default="ALL")
109
+ ap.add_argument("--thr", type=int, default=None)
110
+ args = ap.parse_args()
111
+ coins = COINS if args.coin == "ALL" else [args.coin]
112
+ thrs = [args.thr] if args.thr else THRESHOLDS
113
+
114
+ for coin in coins:
115
+ try:
116
+ closes5 = fetch_closes(coin)
117
+ except Exception as e:
118
+ print(f"[{coin}] fetch fail: {e}")
119
+ continue
120
+ c15 = agg15(closes5)
121
+ rs = rsi(c15)
122
+ current = round(rs[-1], 2) if rs[-1] is not None else None
123
+ for thr in thrs:
124
+ n, hit = validate(c15, thr, horizon=1, oos_bars=OOS_BARS)
125
+ edge = round(hit - BREAKEVEN, 3) if hit is not None else None
126
+ if n == 0 or hit is None:
127
+ status = "NO_SIGNALS"
128
+ elif edge is not None and edge >= EDGE_FLOOR and n >= MIN_N:
129
+ status = "EDGE_OK"
130
+ elif n < MIN_N:
131
+ status = "LOW_N" if edge is not None else "NO_SIGNALS"
132
+ else:
133
+ status = "NO_EDGE"
134
+ row = {"ts": now_iso(), "coin": coin, "bars": len(c15),
135
+ "oos_bars": OOS_BARS, "thr": thr, "n": n,
136
+ "hit": round(hit, 3) if hit is not None else "",
137
+ "edge": edge, "breakeven": BREAKEVEN, "status": status}
138
+ append_report(row)
139
+ print(f"[{coin}] thr={thr} n={n} hit={hit} edge={edge} "
140
+ f"(rsi_now={current}) -> {status}")
141
+ print("done", now_iso())
142
+
143
+
144
+ if __name__ == "__main__":
145
+ main()