-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset_performance.py
More file actions
276 lines (244 loc) · 10 KB
/
Copy pathreset_performance.py
File metadata and controls
276 lines (244 loc) · 10 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
270
271
272
273
274
275
276
import os
import shutil
import logging
import sqlite3
from datetime import datetime
import yaml
import glob
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_PATH = os.path.join(BASE_DIR, 'config.yaml')
def reset_config_previous_return_pct(strategy="core"):
"""Reset previous_return_pct to 0.0 in config.yaml for the specified strategy section."""
if not os.path.exists(CONFIG_PATH):
logger.error("config.yaml not found!")
return False
try:
with open(CONFIG_PATH, 'r') as f:
config = yaml.safe_load(f) or {}
updated = False
if strategy in {"core", "all"}:
if 'trading' in config and isinstance(config['trading'], dict):
if config['trading'].get('previous_return_pct') != 0.0:
config['trading']['previous_return_pct'] = 0.0
updated = True
if strategy in {"ai", "all"}:
if 'ai_trading' in config and isinstance(config['ai_trading'], dict):
if config['ai_trading'].get('previous_return_pct') != 0.0:
config['ai_trading']['previous_return_pct'] = 0.0
updated = True
if updated:
with open(CONFIG_PATH, 'w') as f:
yaml.safe_dump(config, f, sort_keys=False)
logger.info(f"Reset previous_return_pct to 0.0 in config.yaml for {strategy}.")
return True
except Exception as exc:
logger.error(f"Failed to update config.yaml: {exc}")
return False
def reset_database():
"""Archive the current database and start fresh"""
reset_config_previous_return_pct()
if not os.path.exists(CONFIG_PATH):
logger.error("config.yaml not found!")
return
with open(CONFIG_PATH, 'r') as f:
config = yaml.safe_load(f)
# Resolve DB Path
db_rel_path = config['data']['cache_path']
if not os.path.isabs(db_rel_path):
db_path = os.path.join(BASE_DIR, db_rel_path)
else:
db_path = db_rel_path
if not os.path.exists(db_path):
logger.info("No database to reset.")
return
# Archive
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
archive_name = f"trading_bot_archive_{timestamp}.db"
archive_path = os.path.join(os.path.dirname(db_path), archive_name)
try:
shutil.move(db_path, archive_path)
logger.info(f"Database Reset Complete.")
logger.info(f"Old DB archived at: {archive_path}")
logger.info("The bot will create a fresh, empty database on the next run.")
except Exception as e:
logger.error(f"Failed to reset database: {e}")
def _resolve_db_path(config):
db_rel_path = config['data']['cache_path']
if not os.path.isabs(db_rel_path):
return os.path.join(BASE_DIR, db_rel_path)
return db_rel_path
def _get_latest_price(conn, symbol):
row = conn.execute(
"SELECT date, close FROM prices WHERE symbol=? ORDER BY date DESC LIMIT 1",
(symbol,),
).fetchone()
if not row:
return None, None
return row[0], float(row[1])
def reset_pnl_keep_positions(tables=("positions", "positions_ai")):
"""
Reset BOTH realized + unrealized P&L while keeping open positions:
- Closed trades are archived (status set to ARCHIVED) so totals go back to zero.
- Open trades have their cost basis reset to the latest close (unrealized -> 0),
and TP is recalculated off the new basis (side-aware).
"""
reset_config_previous_return_pct()
if not os.path.exists(CONFIG_PATH):
logger.error("config.yaml not found!")
return False
with open(CONFIG_PATH, 'r') as f:
config = yaml.safe_load(f)
db_path = _resolve_db_path(config)
if not os.path.exists(db_path):
logger.error("Database not found: %s", db_path)
return False
tp_pct = float(((config.get("trading") or {}).get("take_profit_pct", 0.03)) or 0.03)
conn = sqlite3.connect(db_path)
try:
for table in tables:
# Archive closed positions so realized totals reset to zero.
conn.execute(f"UPDATE {table} SET status='ARCHIVED' WHERE status='CLOSED'")
# Reset open positions cost basis to latest close so unrealized resets to ~0.
rows = conn.execute(
f"SELECT id, symbol, side FROM {table} WHERE status='OPEN'"
).fetchall()
for pid, sym, side in rows:
side_u = str(side or "LONG").upper()
date, close = _get_latest_price(conn, sym)
if close is None:
continue
if side_u == "SHORT":
target = close * (1 - tp_pct)
else:
target = close * (1 + tp_pct)
conn.execute(
f"""
UPDATE {table}
SET entry_date=?, entry_price=?, target_price=?
WHERE id=?
""",
(date, close, target, pid),
)
conn.commit()
finally:
conn.close()
logger.info("P&L reset complete for tables: %s", ", ".join(tables))
return True
def reset_all_state(
tables=None,
strategy="core",
reset_positions=True,
reset_meta_learner=True,
reset_results_reports=True,
vacuum_sqlite=False,
):
"""
Full "fresh start" without deleting price history:
- Deletes rows in positions tables (OPEN + CLOSED) so P&L and open positions reset to zero.
- Resets meta_learner_state.json so penalties/insights don't repeat from past runs.
- Removes prior report artifacts in ./results (daily_report/unrealized/trades) but keeps email markers
so the scheduler doesn't resend historical emails.
"""
if tables is None:
if strategy == "core":
tables = ("positions",)
elif strategy == "ai":
tables = ("positions_ai",)
else:
tables = ("positions", "positions_ai")
reset_config_previous_return_pct(strategy=strategy)
if not os.path.exists(CONFIG_PATH):
logger.error("config.yaml not found!")
return False
with open(CONFIG_PATH, "r") as f:
config = yaml.safe_load(f) or {}
db_path = _resolve_db_path(config)
if not os.path.exists(db_path):
logger.error("Database not found: %s", db_path)
return False
conn = sqlite3.connect(db_path)
try:
if reset_positions:
for table in tables:
try:
conn.execute(f"DELETE FROM {table}")
logger.info("Cleared table: %s", table)
except Exception as exc:
logger.warning("Failed clearing table %s: %s", table, exc)
if vacuum_sqlite:
try:
conn.commit()
conn.execute("VACUUM")
logger.info("SQLite VACUUM complete.")
except Exception as exc:
logger.warning("SQLite VACUUM failed: %s", exc)
conn.commit()
finally:
conn.close()
base_dir = os.path.dirname(os.path.abspath(__file__))
if reset_meta_learner and strategy in {"core", "all"}:
meta_path = os.path.join(base_dir, "meta_learner_state.json")
try:
if os.path.exists(meta_path):
os.remove(meta_path)
logger.info("Meta-learner state reset: %s", meta_path)
except Exception as exc:
logger.warning("Failed to reset meta-learner state: %s", exc)
if reset_results_reports:
results_dir = os.path.join(base_dir, "results")
if os.path.isdir(results_dir):
if strategy == "core":
patterns = [
os.path.join(results_dir, "daily_report_[0-9]*.csv"),
os.path.join(results_dir, "unrealized_[0-9]*.csv"),
os.path.join(results_dir, "trades_[0-9]*.csv"),
]
elif strategy == "ai":
patterns = [
os.path.join(results_dir, "daily_report_ai_*.csv"),
os.path.join(results_dir, "unrealized_ai_*.csv"),
]
else:
patterns = [
os.path.join(results_dir, "daily_report_*.csv"),
os.path.join(results_dir, "unrealized_*.csv"),
os.path.join(results_dir, "trades_*.csv"),
]
removed = 0
for pat in patterns:
for path in glob.glob(pat):
try:
os.remove(path)
removed += 1
except Exception:
pass
logger.info("Removed %s report artifact files from results/ (kept email_sent_*.ok).", removed)
logger.info("Full state reset complete (positions + reports + meta).")
return True
if __name__ == "__main__":
import sys
mode = (sys.argv[1] if len(sys.argv) > 1 else "").strip().lower()
if mode in {"pnl", "pnl_keep_positions", "keep_positions"}:
confirm = input("Reset realized+unrealized P&L for BOTH accounts but KEEP positions open? (yes/no): ")
if confirm.lower() == "yes":
ok = reset_pnl_keep_positions()
print("P&L reset complete." if ok else "P&L reset failed.")
else:
print("Reset cancelled.")
elif mode in {"reset_state", "state", "fresh"}:
confirm = input("RESET positions + report artifacts + meta-learner state (keeps price history)? (yes/no): ")
if confirm.lower() == "yes":
ok = reset_all_state()
print("State reset complete." if ok else "State reset failed.")
else:
print("Reset cancelled.")
else:
confirm = input("Are you sure you want to RESET all performance history (archive DB)? (yes/no): ")
if confirm.lower() == 'yes':
reset_database()
print("Database reset complete.")
else:
print("Reset cancelled.")