-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive_runner.py
More file actions
269 lines (216 loc) · 8.91 KB
/
Copy pathlive_runner.py
File metadata and controls
269 lines (216 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
"""
Entry point for live testnet execution.
Calibrates from prod API, runs on testnet, writes CSV log.
Usage:
export BINANCE_TESTNET_KEY=...
export BINANCE_TESTNET_SECRET=...
python live_runner.py --scheduler AC --tactic GLFT --qty 1.0
"""
from __future__ import annotations
import argparse
import asyncio
import csv
import logging
import os
import sys
from dataclasses import asdict
from datetime import datetime
from pathlib import Path
import numpy as np
from binance import AsyncClient
from live_calibration import calibrate_live
from live_execution import LiveExecutor, FillRecord
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(name)s] %(message)s",
datefmt="%H:%M:%S",
)
logger = logging.getLogger("live_runner")
def write_csv(fills: list[FillRecord], path: Path):
if not fills:
print("No fills to write.")
return
fieldnames = list(asdict(fills[0]).keys())
with open(path, "w", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for fill in fills:
writer.writerow(asdict(fill))
print(f" Wrote {len(fills)} records to {path}")
def print_summary(fills: list[FillRecord], cal: dict):
if not fills:
print("No fills recorded.")
return
# Filter actual fills (not skips/cancels)
actual_fills = [f for f in fills if f.fill_qty > 0]
if not actual_fills:
print("No actual fills (only skips/cancels).")
return
total_filled = sum(f.fill_qty for f in actual_fills)
total_notional = sum(f.fill_price * f.fill_qty for f in actual_fills)
avg_price = total_notional / total_filled if total_filled > 0 else 0
arrival = actual_fills[0].mid_price
is_bps = (arrival - avg_price) / arrival * 10000 if arrival > 0 else 0
n_market = sum(1 for f in actual_fills if "MARKET" in f.action)
n_limit = sum(1 for f in actual_fills if "LIMIT" in f.action)
print(f"\n{'=' * 60}")
print(f" LIVE EXECUTION SUMMARY")
print(f"{'=' * 60}")
print(f" Scheduler: {actual_fills[0].scheduler}")
print(f" Tactic: {actual_fills[0].tactic}")
print(f" Arrival mid: ${arrival:,.2f}")
print(f" Avg fill: ${avg_price:,.2f}")
print(f" IS: {is_bps:+.2f} bps")
print(f" Total filled: {total_filled:.3f} BTC")
print(f" Completion: {total_filled / cal['total_qty'] * 100:.1f}%")
print(f" Market fills: {n_market}")
print(f" Limit fills: {n_limit}")
# Latency stats
if len(actual_fills) > 1:
intervals = [
actual_fills[i + 1].timestamp - actual_fills[i].timestamp
for i in range(len(actual_fills) - 1)
]
print(f" Avg interval: {np.mean(intervals):.2f}s")
print(f"{'=' * 60}")
def print_comparison(fills: list[FillRecord], backtest_csv: str | None):
if backtest_csv is None or not Path(backtest_csv).exists():
print("\n No backtest baseline provided for comparison.")
print(" Run: python run_multiday.py --mode oos --total-qty <qty>")
print(" Then pass --backtest-csv results/multiday/oos/multiday_results.csv")
return
import pandas as pd
bt = pd.read_csv(backtest_csv)
actual_fills = [f for f in fills if f.fill_qty > 0]
if not actual_fills:
return
sched = actual_fills[0].scheduler
tactic = actual_fills[0].tactic
# Map tactic names to backtest strategy names
tactic_map = {"MARKET": "MktOrder", "GLFT": "GLFT", "PEG": "PegBest"}
bt_strategy = f"{sched}+{tactic_map.get(tactic, tactic)}"
bt_match = bt[bt["strategy"] == bt_strategy]
if bt_match.empty:
print(f"\n No backtest match for {bt_strategy}")
return
total_filled = sum(f.fill_qty for f in actual_fills)
total_notional = sum(f.fill_price * f.fill_qty for f in actual_fills)
avg_price = total_notional / total_filled
arrival = actual_fills[0].mid_price
live_is = (arrival - avg_price) / arrival * 10000
bt_is = bt_match["is_bps"].mean()
print(f"\n{'=' * 60}")
print(f" LIVE vs BACKTEST COMPARISON -- {bt_strategy}")
print(f"{'=' * 60}")
print(f" Live IS: {live_is:+.2f} bps")
print(f" Backtest IS: {bt_is:+.2f} bps (avg across {len(bt_match)} days)")
print(f" Difference: {live_is - bt_is:+.2f} bps")
print(f"{'=' * 60}")
print(f"\n Note: testnet fills are at fake prices.")
print(f" The decision log (delta*, order prices) is the")
print(f" meaningful comparison, not the fill prices.")
async def run_live(args):
"""Main async entry point."""
# 1. Calibrate
logger.info("Starting calibration...")
cal = calibrate_live(
total_qty=args.qty,
kline_lookback_hours=args.kline_hours,
trade_lookback_minutes=args.trade_minutes,
n_depth_snapshots=args.depth_snapshots,
)
if args.dry_run:
logger.info("Dry run: calibration complete, skipping execution.")
return
# 2. Connect to testnet
api_key = os.environ.get("BINANCE_TESTNET_KEY", "")
api_secret = os.environ.get("BINANCE_TESTNET_SECRET", "")
if not api_key or not api_secret:
print("\nERROR: Set BINANCE_TESTNET_KEY and BINANCE_TESTNET_SECRET")
print("Get them from: https://testnet.binancefuture.com")
sys.exit(1)
client = await AsyncClient.create(
api_key=api_key,
api_secret=api_secret,
testnet=True,
)
# Set leverage to max (reduces margin requirement)
try:
await client.futures_change_leverage(symbol="BTCUSDT", leverage=125)
logger.info("Set BTCUSDT leverage to 20x")
except Exception as e:
logger.warning(f"Could not set leverage: {e}")
try:
await client.futures_change_margin_type(symbol="BTCUSDT", marginType="CROSSED")
logger.info("Set margin type to CROSSED")
except Exception as e:
logger.warning(f"Margin type: {e}") # already CROSSED is fine
try:
# Check testnet balance
account = await client.futures_account()
balance = float(account.get("totalWalletBalance", 0))
logger.info(f"Testnet balance: ${balance:,.2f}")
# Configure margin
try:
await client.futures_change_margin_type(symbol="BTCUSDT", marginType="CROSSED")
logger.info("Set margin type to CROSSED")
except Exception as e:
logger.warning(f"Margin type: {e}")
try:
await client.futures_change_leverage(symbol="BTCUSDT", leverage=20)
logger.info("Set BTCUSDT leverage to 20x")
except Exception as e:
logger.warning(f"Could not set leverage: {e}")
# 3. Run execution
executor = LiveExecutor(
client=client,
cal=cal,
scheduler_name=args.scheduler,
tactic_name=args.tactic,
check_interval=args.interval,
order_size=0.01,
)
logger.info(
f"Starting execution: {args.scheduler}+{args.tactic}, "
f"{args.qty} BTC, {cal['horizon_sec']:.0f}s horizon"
)
await executor.run()
# 4. Write results
date_str = datetime.now().strftime("%Y%m%d_%H%M%S")
output_dir = Path("results/live")
output_dir.mkdir(parents=True, exist_ok=True)
csv_path = output_dir / f"execution_log_{date_str}.csv"
write_csv(executor.fills, csv_path)
# 5. Print summary
print_summary(executor.fills, cal)
# 6. Compare with backtest if available
print_comparison(executor.fills, args.backtest_csv)
finally:
await client.close_connection()
def main():
parser = argparse.ArgumentParser(
description="Live execution on Binance Futures Testnet"
)
parser.add_argument("--scheduler", type=str, default="AC",
choices=["AC", "TWAP"])
parser.add_argument("--tactic", type=str, default="GLFT",
choices=["GLFT", "MARKET", "PEG"])
parser.add_argument("--qty", type=float, default=1.0,
help="Total BTC to liquidate")
parser.add_argument("--interval", type=float, default=1.0,
help="Control loop interval in seconds")
parser.add_argument("--dry-run", action="store_true",
help="Calibrate only, don't execute")
parser.add_argument("--backtest-csv", type=str, default=None,
help="Path to backtest results CSV for comparison")
parser.add_argument("--kline-hours", type=int, default=24,
help="Hours of kline data for sigma calibration")
parser.add_argument("--trade-minutes", type=int, default=10,
help="Minutes of trade data for A,k calibration")
parser.add_argument("--depth-snapshots", type=int, default=10,
help="Number of book snapshots for eta calibration")
args = parser.parse_args()
asyncio.run(run_live(args))
if __name__ == "__main__":
main()