-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtrack_session.py
More file actions
184 lines (154 loc) · 5.92 KB
/
track_session.py
File metadata and controls
184 lines (154 loc) · 5.92 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
"""Track Oracle3 dashboard session — log snapshots every 10 minutes."""
import json
import time
import urllib.request
from datetime import datetime
from pathlib import Path
LOG_DIR = Path(__file__).parent / 'session_logs'
LOG_DIR.mkdir(exist_ok=True)
SESSION_ID = datetime.now().strftime('%Y%m%d_%H%M%S')
LOG_FILE = LOG_DIR / f'session_{SESSION_ID}.jsonl'
SUMMARY_FILE = LOG_DIR / f'session_{SESSION_ID}_summary.txt'
API_URL = 'http://localhost:3000/api/state'
INTERVAL = 600 # 10 minutes
def fetch_state():
try:
with urllib.request.urlopen(API_URL, timeout=10) as resp:
return json.loads(resp.read())
except Exception:
return None
def fmt_ts():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
def log_snapshot(s, elapsed_min):
p = s['portfolio']
equity = float(p['equity'])
cash = float(p['cash'])
total_pnl = float(p['total_pnl'])
perf = s.get('performance', {})
n_trades = perf.get('total_trades', 0)
win_rate = float(perf.get('win_rate', 0))
sharpe = float(perf.get('sharpe_ratio', 0))
max_dd = float(perf.get('max_drawdown', 0))
line = (
f'[{fmt_ts()}] '
f't={elapsed_min}min | '
f'Equity=${equity:,.2f} | '
f'P&L=${total_pnl:+,.2f} | '
f'Cash=${cash:,.2f} | '
f'Trades={n_trades} | '
f'WinRate={win_rate*100:.1f}% | '
f'Sharpe={sharpe:.2f} | '
f'MaxDD={max_dd*100:.2f}% | '
f'Events={s["event_count"]} | '
f'Positions={len(s["positions"])} | '
f'Decisions={len(s["decisions"])} | '
f'OBs={len(s["order_books"])}'
)
print(line)
# Append full state as JSONL
with open(LOG_FILE, 'a') as f:
snapshot = {
'timestamp': fmt_ts(),
'elapsed_min': elapsed_min,
'equity': equity,
'cash': cash,
'total_pnl': total_pnl,
'realized_pnl': float(p['realized_pnl']),
'unrealized_pnl': float(p['unrealized_pnl']),
'exposure_pct': p['exposure_pct'],
'n_trades': n_trades,
'n_positions': len(s['positions']),
'n_decisions': len(s['decisions']),
'n_order_books': len(s['order_books']),
'n_events': s['event_count'],
'win_rate': win_rate,
'sharpe': sharpe,
'max_dd': max_dd,
'positions': s['positions'],
'decisions': s['decisions'][-10:],
'trades': s['trades'][-10:],
'equity_curve': s.get('equity_curve', []),
'performance': perf,
}
f.write(json.dumps(snapshot) + '\n')
def write_summary():
"""Generate a text summary from the JSONL log."""
snapshots = []
with open(LOG_FILE) as f:
for line in f:
snapshots.append(json.loads(line))
if not snapshots:
return
first = snapshots[0]
last = snapshots[-1]
with open(SUMMARY_FILE, 'w') as f:
f.write('=' * 60 + '\n')
f.write(' Oracle3 Paper Trading Session Summary\n')
f.write('=' * 60 + '\n\n')
f.write(f'Session ID: {SESSION_ID}\n')
f.write(f'Start: {first["timestamp"]}\n')
f.write(f'End: {last["timestamp"]}\n')
f.write(f'Duration: {last["elapsed_min"]} minutes\n\n')
f.write('--- Portfolio ---\n')
f.write('Initial Capital: $10,000.00\n')
f.write(f'Final Equity: ${last["equity"]:,.2f}\n')
f.write(f'Total P&L: ${last["total_pnl"]:+,.2f}\n')
f.write(f'Return: {(last["equity"]-10000)/100:+.2f}%\n\n')
f.write('--- Performance ---\n')
f.write(f'Total Trades: {last["n_trades"]}\n')
f.write(f'Win Rate: {last["win_rate"]*100:.1f}%\n')
f.write(f'Sharpe Ratio: {last["sharpe"]:.4f}\n')
f.write(f'Max Drawdown: {last["max_dd"]*100:.2f}%\n\n')
f.write('--- Equity Timeline ---\n')
for snap in snapshots:
pnl = snap['total_pnl']
bar = '+' * max(0, int(pnl / 10)) if pnl >= 0 else '-' * max(0, int(-pnl / 10))
f.write(f' t={snap["elapsed_min"]:>4}min ${snap["equity"]:>10,.2f} P&L: ${pnl:>+8,.2f} {bar}\n')
f.write('\n--- Final Positions ---\n')
for pos in last.get('positions', []):
f.write(f' {pos["symbol"]}: {pos["qty"]}x @ ${float(pos["current_price"]):.4f} P&L: ${float(pos["unrealized_pnl"]):+.2f}\n')
if not last.get('positions'):
f.write(' (no positions)\n')
f.write('\n--- Recent Trades ---\n')
for t in last.get('trades', []):
f.write(f' {t["time"]} {t["side"]:4s} {t["name"][:30]:30s} @ ${float(t["price"]):.4f} x{t["qty"]} [{t["status"]}]\n')
if not last.get('trades'):
f.write(' (no trades)\n')
f.write('\n' + '=' * 60 + '\n')
print(f'\nSummary written to: {SUMMARY_FILE}')
if __name__ == '__main__':
print(f'Oracle3 Session Tracker — logging every {INTERVAL//60} min')
print(f'Log file: {LOG_FILE}')
print('Dashboard: http://localhost:3000')
print()
start = time.time()
# Initial snapshot
s = fetch_state()
if s:
log_snapshot(s, 0)
else:
print('ERROR: Cannot connect to dashboard. Is it running?')
exit(1)
try:
while True:
time.sleep(INTERVAL)
elapsed = int((time.time() - start) / 60)
s = fetch_state()
if s:
log_snapshot(s, elapsed)
write_summary()
else:
print(f'[{fmt_ts()}] WARNING: Dashboard not responding')
# Stop after 3.5 hours
if elapsed >= 210:
print('3.5 hour limit reached. Stopping tracker.')
break
except KeyboardInterrupt:
print('\nTracker stopped by user.')
finally:
s = fetch_state()
if s:
elapsed = int((time.time() - start) / 60)
log_snapshot(s, elapsed)
write_summary()
print('Done.')