stratforge 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- stratforge/__init__.py +17 -0
- stratforge/ccxt_bridge.py +478 -0
- stratforge/cfm/__init__.py +3 -0
- stratforge/cfm/bars.py +115 -0
- stratforge/cfm/download_cde.py +159 -0
- stratforge/cfm/isolation.py +14 -0
- stratforge/cfm/ledger.py +332 -0
- stratforge/cfm/replay.py +224 -0
- stratforge/cfm/run_cde_dozen.py +111 -0
- stratforge/cfm/run_cde_replay.py +107 -0
- stratforge/cfm/run_cde_universe.py +112 -0
- stratforge/cfm/run_research.py +110 -0
- stratforge/cfm/run_round2.py +78 -0
- stratforge/cfm/run_validate.py +196 -0
- stratforge/cfm/strats.py +427 -0
- stratforge/cfm/strats_cde.py +681 -0
- stratforge/cfm/test_isolation.py +43 -0
- stratforge/cfm/test_ledger.py +76 -0
- stratforge/cfm/test_venue.py +36 -0
- stratforge/cfm/venue.py +36 -0
- stratforge/live.py +15 -0
- stratforge/paper_family.py +15 -0
- stratforge/risk_limits.py +337 -0
- stratforge/short/__init__.py +5 -0
- stratforge/short/backfill_spot_gap.py +90 -0
- stratforge/short/engine.py +227 -0
- stratforge/short/indicators.py +58 -0
- stratforge/short/ledger.py +273 -0
- stratforge/short/regime_occupancy.py +156 -0
- stratforge/short/regime_stretches.py +50 -0
- stratforge/short/replay.py +390 -0
- stratforge/short/run_backtest.py +153 -0
- stratforge/short/run_regime_gates.py +105 -0
- stratforge/short/run_tsmom12.py +263 -0
- stratforge/short/test_short.py +269 -0
- stratforge/short/test_tsmom.py +139 -0
- stratforge/short/tsmom.py +160 -0
- stratforge/state.py +20 -0
- stratforge/strategies/bar_apply.py +36 -0
- stratforge/strategies/bar_freshness.py +53 -0
- stratforge/strategies/buy_protect.py +78 -0
- stratforge/strategies/config_load.py +82 -0
- stratforge/strategies/daily_data_feed.py +341 -0
- stratforge/strategies/day_risk.py +51 -0
- stratforge/strategies/display_time.py +72 -0
- stratforge/strategies/live_data_feed.py +353 -0
- stratforge/strategies/live_executor.py +1346 -0
- stratforge/strategies/live_node.py +430 -0
- stratforge/strategies/live_reconcile.py +358 -0
- stratforge/strategies/node_lock.py +48 -0
- stratforge/strategies/paper_executor.py +492 -0
- stratforge/strategies/paper_family_node.py +304 -0
- stratforge/strategies/qty_precision.py +44 -0
- stratforge/strategies/research_families.py +335 -0
- stratforge/strategies/research_replay.py +681 -0
- stratforge/strategies/signal_engine.py +606 -0
- stratforge/strategies/universe.py +60 -0
- stratforge/viz/__init__.py +1 -0
- stratforge/viz/projector.py +402 -0
- stratforge-0.1.0.dist-info/METADATA +51 -0
- stratforge-0.1.0.dist-info/RECORD +64 -0
- stratforge-0.1.0.dist-info/WHEEL +5 -0
- stratforge-0.1.0.dist-info/licenses/LICENSE +21 -0
- stratforge-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import sys
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
8
|
+
sys.path.insert(0, str(ROOT))
|
|
9
|
+
|
|
10
|
+
from cfm.replay import run_replay, write_json
|
|
11
|
+
from cfm.strats import DonchianTurtle, MonthlySma, WeeklySma
|
|
12
|
+
|
|
13
|
+
OUT = ROOT / "results" / "backtests" / "cfm"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def specs():
|
|
17
|
+
s = []
|
|
18
|
+
for n in (10, 40):
|
|
19
|
+
for lev in (2.0, 3.0):
|
|
20
|
+
for hc in (True, False):
|
|
21
|
+
for sh in (False, True):
|
|
22
|
+
s.append((WeeklySma(["BTC"], n=n, leverage=lev, short_enabled=sh, hold_contracts=hc), ["BTC"]))
|
|
23
|
+
s.append((WeeklySma(["BTC", "ETH", "SOL"], n=n, leverage=lev, short_enabled=False, hold_contracts=hc), ["BTC", "ETH", "SOL"]))
|
|
24
|
+
for lev in (2.0, 3.0):
|
|
25
|
+
for hc in (True, False):
|
|
26
|
+
s.append((MonthlySma(["BTC"], n=10, leverage=lev, hold_contracts=hc), ["BTC"]))
|
|
27
|
+
s.append((MonthlySma(["BTC", "ETH", "SOL"], n=10, leverage=lev, hold_contracts=hc), ["BTC", "ETH", "SOL"]))
|
|
28
|
+
s.append((DonchianTurtle(["BTC", "ETH", "SOL"], 330, 120, 120, 0.05, 2.0, 4.0, True), ["BTC", "ETH", "SOL"]))
|
|
29
|
+
s.append((DonchianTurtle(["SOL"], 330, 120, 120, 0.02, 2.0, 4.0, True), ["SOL"]))
|
|
30
|
+
return s
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def main():
|
|
34
|
+
windows = {
|
|
35
|
+
"3y": ("2023-08-28", "2026-08-27"),
|
|
36
|
+
"2022": ("2022-01-01", "2022-12-31"),
|
|
37
|
+
"full": ("2021-01-01", "2026-08-27"),
|
|
38
|
+
"y20_23": ("2020-08-28", "2023-08-27"),
|
|
39
|
+
"y21_24": ("2021-08-28", "2024-08-27"),
|
|
40
|
+
"y22_25": ("2022-08-28", "2025-08-27"),
|
|
41
|
+
}
|
|
42
|
+
rows = []
|
|
43
|
+
for wname, (a, b) in windows.items():
|
|
44
|
+
for strat, syms in specs():
|
|
45
|
+
r = run_replay(strat, symbols=syms, start=a, end=b, start_usd=1000.0, max_leverage=4.0, warmup_days=420)
|
|
46
|
+
rec = {
|
|
47
|
+
"window": wname,
|
|
48
|
+
"id": r["strategy_id"],
|
|
49
|
+
"mult": round(r["multiple"], 3),
|
|
50
|
+
"vs_btc": round(r["vs_btc_multiple"], 3),
|
|
51
|
+
"dd": round(r["max_dd"] * 100, 1),
|
|
52
|
+
"liq": r["liquidations"],
|
|
53
|
+
"trades": r["trades"],
|
|
54
|
+
"hit10m": r["hit_10x_btc_multiple"],
|
|
55
|
+
"btc_m": round(r["btc_multiple"], 3),
|
|
56
|
+
}
|
|
57
|
+
rows.append(rec)
|
|
58
|
+
print(
|
|
59
|
+
f"{wname:7} vsBTC={rec['vs_btc']:6.2f} mult={rec['mult']:7.2f} dd={rec['dd']:5.1f}% liq={rec['liq']} {rec['id'][:64]}",
|
|
60
|
+
flush=True,
|
|
61
|
+
)
|
|
62
|
+
write_json(r, OUT / "round2" / wname / f"{r['strategy_id']}.json")
|
|
63
|
+
write_json({"rows": rows}, OUT / "round2.json")
|
|
64
|
+
print("\n=== 3y rank ===")
|
|
65
|
+
y3 = [x for x in rows if x["window"] == "3y"]
|
|
66
|
+
y3.sort(key=lambda x: -x["vs_btc"])
|
|
67
|
+
for x in y3[:12]:
|
|
68
|
+
print(f" {x['vs_btc']:6.2f}xBTC {x['mult']:6.2f}x dd={x['dd']:5.1f}% {x['id']}")
|
|
69
|
+
print("\n=== hits 10x BTC multiple any window ===")
|
|
70
|
+
hits = [x for x in rows if x["hit10m"]]
|
|
71
|
+
if not hits:
|
|
72
|
+
print(" none")
|
|
73
|
+
for x in hits:
|
|
74
|
+
print(x)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
main()
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Stagger + stress for frozen wsma40 lev3 hold-contracts identities.
|
|
3
|
+
|
|
4
|
+
Not a spot stagger. Not paper. Overlapping births are sensitivity, not independent OOS.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import statistics
|
|
11
|
+
import sys
|
|
12
|
+
from datetime import date, datetime, timedelta, timezone
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
16
|
+
sys.path.insert(0, str(ROOT))
|
|
17
|
+
|
|
18
|
+
from cfm.replay import run_replay, write_json
|
|
19
|
+
from cfm.strats import WeeklySma
|
|
20
|
+
|
|
21
|
+
OUT = ROOT / "results" / "backtests" / "cfm" / "validate"
|
|
22
|
+
END = "2026-08-27"
|
|
23
|
+
PRIMARY = ("2023-08-28", END)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def add_months(d: date, months: int) -> date:
|
|
27
|
+
y = d.year + (d.month - 1 + months) // 12
|
|
28
|
+
m = (d.month - 1 + months) % 12 + 1
|
|
29
|
+
return date(y, m, 1)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def make(n: int = 40, lev: float = 3.0, ls: bool = False) -> WeeklySma:
|
|
33
|
+
return WeeklySma(["BTC"], n=n, leverage=lev, short_enabled=ls, hold_contracts=True)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def compact(r: dict, extra: dict | None = None) -> dict:
|
|
37
|
+
d = {
|
|
38
|
+
"id": r["strategy_id"],
|
|
39
|
+
"start": r["start"],
|
|
40
|
+
"end": r["end"],
|
|
41
|
+
"mult": round(r["multiple"], 3),
|
|
42
|
+
"vs_btc": round(r["vs_btc_multiple"], 3),
|
|
43
|
+
"btc_m": round(r["btc_multiple"], 3),
|
|
44
|
+
"dd": round(r["max_dd"] * 100, 1),
|
|
45
|
+
"liq": r["liquidations"],
|
|
46
|
+
"trades": r["trades"],
|
|
47
|
+
"flips": r.get("flips", 0),
|
|
48
|
+
"fees": round(r["fees_paid"], 2),
|
|
49
|
+
"funding": round(r["funding_paid"], 2),
|
|
50
|
+
"end_eq": round(r["end_equity"], 2),
|
|
51
|
+
}
|
|
52
|
+
if extra:
|
|
53
|
+
d.update(extra)
|
|
54
|
+
return d
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def run(ls: bool, start: str, end: str, **kw) -> dict:
|
|
58
|
+
n = int(kw.pop("n", 40))
|
|
59
|
+
lev = float(kw.pop("lev", 3.0))
|
|
60
|
+
return run_replay(
|
|
61
|
+
make(n=n, lev=lev, ls=ls),
|
|
62
|
+
symbols=["BTC"],
|
|
63
|
+
start=start,
|
|
64
|
+
end=end,
|
|
65
|
+
start_usd=float(kw.pop("start_usd", 1000)),
|
|
66
|
+
max_leverage=4.0,
|
|
67
|
+
fee_rate=float(kw.pop("fee_rate", 0.0004)),
|
|
68
|
+
funding_per_8h=float(kw.pop("funding_per_8h", 0.00003)),
|
|
69
|
+
integer_contracts=bool(kw.pop("integer_contracts", True)),
|
|
70
|
+
warmup_days=400,
|
|
71
|
+
signal_lag_bars=int(kw.pop("signal_lag_bars", 0)),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def summarize(rows: list[dict]) -> dict:
|
|
76
|
+
if not rows:
|
|
77
|
+
return {}
|
|
78
|
+
vs = [r["vs_btc"] for r in rows]
|
|
79
|
+
m = [r["mult"] for r in rows]
|
|
80
|
+
dd = [r["dd"] for r in rows]
|
|
81
|
+
liq = [r["liq"] for r in rows]
|
|
82
|
+
return {
|
|
83
|
+
"n": len(rows),
|
|
84
|
+
"vs_med": round(statistics.median(vs), 3),
|
|
85
|
+
"vs_avg": round(sum(vs) / len(vs), 3),
|
|
86
|
+
"vs_min": round(min(vs), 3),
|
|
87
|
+
"vs_max": round(max(vs), 3),
|
|
88
|
+
"mult_med": round(statistics.median(m), 3),
|
|
89
|
+
"mult_min": round(min(m), 3),
|
|
90
|
+
"mult_max": round(max(m), 3),
|
|
91
|
+
"dd_med": round(statistics.median(dd), 1),
|
|
92
|
+
"dd_max": round(max(dd), 1),
|
|
93
|
+
"beat_btc": int(sum(1 for x in vs if x > 1.0)),
|
|
94
|
+
"liq_any": int(sum(1 for x in liq if x > 0)),
|
|
95
|
+
"liq_med": statistics.median(liq),
|
|
96
|
+
"worst": min(rows, key=lambda r: r["mult"]),
|
|
97
|
+
"best": max(rows, key=lambda r: r["mult"]),
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def main() -> None:
|
|
102
|
+
report: dict = {"primary": [], "years": [], "rolling_3y": [], "same_end": [], "stress": [], "robust_n": []}
|
|
103
|
+
print("=== primary 3y (calendar-week SMA) ===", flush=True)
|
|
104
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
105
|
+
r = compact(run(ls, *PRIMARY), {"name": name})
|
|
106
|
+
report["primary"].append(r)
|
|
107
|
+
print(f" {name:4} vsBTC={r['vs_btc']:.2f} mult={r['mult']:.2f} dd={r['dd']:.1f}% liq={r['liq']} flips={r['flips']}", flush=True)
|
|
108
|
+
|
|
109
|
+
print("=== calendar years ===", flush=True)
|
|
110
|
+
years = [
|
|
111
|
+
("2021", "2021-01-01", "2021-12-31"),
|
|
112
|
+
("2022", "2022-01-01", "2022-12-31"),
|
|
113
|
+
("2023", "2023-01-01", "2023-12-31"),
|
|
114
|
+
("2024", "2024-01-01", "2024-12-31"),
|
|
115
|
+
("2025", "2025-01-01", "2025-12-31"),
|
|
116
|
+
("2026ytd", "2026-01-01", END),
|
|
117
|
+
]
|
|
118
|
+
for lab, a, b in years:
|
|
119
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
120
|
+
r = compact(run(ls, a, b), {"name": name, "year": lab})
|
|
121
|
+
report["years"].append(r)
|
|
122
|
+
print(f" {lab:7} {name:4} vsBTC={r['vs_btc']:.2f} mult={r['mult']:.2f} dd={r['dd']:.1f}% liq={r['liq']}", flush=True)
|
|
123
|
+
|
|
124
|
+
print("=== 3y rolling monthly births ===", flush=True)
|
|
125
|
+
d0 = date(2020, 9, 1)
|
|
126
|
+
d1 = date(2023, 8, 1)
|
|
127
|
+
d = d0
|
|
128
|
+
while d <= d1:
|
|
129
|
+
a = d.isoformat()
|
|
130
|
+
end_d = add_months(d, 36) - timedelta(days=1)
|
|
131
|
+
end = min(end_d.isoformat(), END)
|
|
132
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
133
|
+
r = compact(run(ls, a, end), {"name": name, "birth": a})
|
|
134
|
+
report["rolling_3y"].append(r)
|
|
135
|
+
print(f" {a} long vs={report['rolling_3y'][-2]['vs_btc']:.2f} ls vs={report['rolling_3y'][-1]['vs_btc']:.2f} liq={report['rolling_3y'][-2]['liq']}/{report['rolling_3y'][-1]['liq']}", flush=True)
|
|
136
|
+
d = add_months(d, 1)
|
|
137
|
+
|
|
138
|
+
print("=== same-end length stagger (end 2026-08-27) ===", flush=True)
|
|
139
|
+
for months in (6, 12, 18, 24, 30, 36):
|
|
140
|
+
start = add_months(date(2026, 8, 1), -months).isoformat()
|
|
141
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
142
|
+
r = compact(run(ls, start, END), {"name": name, "months": months})
|
|
143
|
+
report["same_end"].append(r)
|
|
144
|
+
print(f" m{months:02d} {name:4} vsBTC={r['vs_btc']:.2f} mult={r['mult']:.2f} dd={r['dd']:.1f}% liq={r['liq']}", flush=True)
|
|
145
|
+
|
|
146
|
+
print("=== stress on primary 3y ===", flush=True)
|
|
147
|
+
stresses = [
|
|
148
|
+
{"label": "fee2bps", "fee_rate": 0.0002},
|
|
149
|
+
{"label": "fee4bps", "fee_rate": 0.0004},
|
|
150
|
+
{"label": "fee8bps", "fee_rate": 0.0008},
|
|
151
|
+
{"label": "fund_x3", "funding_per_8h": 0.00009},
|
|
152
|
+
{"label": "fund_x5", "funding_per_8h": 0.00015},
|
|
153
|
+
{"label": "usd500", "start_usd": 500},
|
|
154
|
+
{"label": "usd10k", "start_usd": 10000},
|
|
155
|
+
{"label": "no_integer", "integer_contracts": False},
|
|
156
|
+
{"label": "lag1w", "signal_lag_bars": 42},
|
|
157
|
+
{"label": "lev2", "lev": 2.0},
|
|
158
|
+
]
|
|
159
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
160
|
+
for st in stresses:
|
|
161
|
+
kw = {k: v for k, v in st.items() if k != "label"}
|
|
162
|
+
r = compact(run(ls, *PRIMARY, **kw), {"name": name, "stress": st["label"]})
|
|
163
|
+
report["stress"].append(r)
|
|
164
|
+
print(f" {name:4} {st['label']:12} vsBTC={r['vs_btc']:.2f} mult={r['mult']:.2f} dd={r['dd']:.1f}% liq={r['liq']}", flush=True)
|
|
165
|
+
|
|
166
|
+
print("=== robustness n=30/40/50 lev=2/3 primary ===", flush=True)
|
|
167
|
+
for n in (30, 40, 50):
|
|
168
|
+
for lev in (2.0, 3.0):
|
|
169
|
+
for ls, name in ((False, "long"), (True, "ls")):
|
|
170
|
+
r = compact(run(ls, *PRIMARY, n=n, lev=lev), {"name": name, "n": n, "lev": lev})
|
|
171
|
+
report["robust_n"].append(r)
|
|
172
|
+
print(f" n={n} L={lev:g} {name:4} vsBTC={r['vs_btc']:.2f} mult={r['mult']:.2f} dd={r['dd']:.1f}% liq={r['liq']}", flush=True)
|
|
173
|
+
|
|
174
|
+
roll_long = [x for x in report["rolling_3y"] if x["name"] == "long"]
|
|
175
|
+
roll_ls = [x for x in report["rolling_3y"] if x["name"] == "ls"]
|
|
176
|
+
summary = {
|
|
177
|
+
"rolling_3y_long": summarize(roll_long),
|
|
178
|
+
"rolling_3y_ls": summarize(roll_ls),
|
|
179
|
+
"same_end_long": summarize([x for x in report["same_end"] if x["name"] == "long"]),
|
|
180
|
+
"same_end_ls": summarize([x for x in report["same_end"] if x["name"] == "ls"]),
|
|
181
|
+
}
|
|
182
|
+
# strip nested best/worst to ids
|
|
183
|
+
for k, v in summary.items():
|
|
184
|
+
if not v:
|
|
185
|
+
continue
|
|
186
|
+
v["worst"] = {"start": v["worst"]["start"], "mult": v["worst"]["mult"], "vs_btc": v["worst"]["vs_btc"], "dd": v["worst"]["dd"]}
|
|
187
|
+
v["best"] = {"start": v["best"]["start"], "mult": v["best"]["mult"], "vs_btc": v["best"]["vs_btc"], "dd": v["best"]["dd"]}
|
|
188
|
+
|
|
189
|
+
out = {"asof": END, "identities": ["cfm_wsma40_long_lev3_holdc_btc", "cfm_wsma40_ls_lev3_holdc_btc"], "rows": report, "summary": summary}
|
|
190
|
+
write_json(out, OUT / "stagger_stress.json")
|
|
191
|
+
print("\n=== ROLLING 3Y SUMMARY ===")
|
|
192
|
+
print(json.dumps(summary, indent=2))
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
if __name__ == "__main__":
|
|
196
|
+
main()
|