forked from MaoTouHU/Nof1OpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
564 lines (491 loc) · 20.2 KB
/
Copy pathdatabase.py
File metadata and controls
564 lines (491 loc) · 20.2 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
"""
Database management module
"""
import sqlite3
import json
from datetime import datetime
from typing import List, Dict, Optional
class Database:
def __init__(self, db_path: str = 'AITradeGame.db'):
self.db_path = db_path
def get_connection(self):
"""Get database connection"""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
return conn
def init_db(self):
"""Initialize database tables"""
conn = self.get_connection()
cursor = conn.cursor()
# Providers table (API提供方)
cursor.execute('''
CREATE TABLE IF NOT EXISTS providers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
api_url TEXT NOT NULL,
api_key TEXT NOT NULL,
models TEXT, -- JSON string or comma-separated list of models
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Models table
cursor.execute('''
CREATE TABLE IF NOT EXISTS models (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
provider_id INTEGER,
model_name TEXT NOT NULL,
initial_capital REAL DEFAULT 10000,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (provider_id) REFERENCES providers(id)
)
''')
# Portfolios table
cursor.execute('''
CREATE TABLE IF NOT EXISTS portfolios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_id INTEGER NOT NULL,
coin TEXT NOT NULL,
quantity REAL NOT NULL,
avg_price REAL NOT NULL,
leverage INTEGER DEFAULT 1,
side TEXT DEFAULT 'long',
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (model_id) REFERENCES models(id),
UNIQUE(model_id, coin, side)
)
''')
# Trades table
cursor.execute('''
CREATE TABLE IF NOT EXISTS trades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_id INTEGER NOT NULL,
coin TEXT NOT NULL,
signal TEXT NOT NULL,
quantity REAL NOT NULL,
price REAL NOT NULL,
leverage INTEGER DEFAULT 1,
side TEXT DEFAULT 'long',
pnl REAL DEFAULT 0,
fee REAL DEFAULT 0,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (model_id) REFERENCES models(id)
)
''')
# Conversations table
cursor.execute('''
CREATE TABLE IF NOT EXISTS conversations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_id INTEGER NOT NULL,
user_prompt TEXT NOT NULL,
ai_response TEXT NOT NULL,
cot_trace TEXT,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (model_id) REFERENCES models(id)
)
''')
# Account values history table
cursor.execute('''
CREATE TABLE IF NOT EXISTS account_values (
id INTEGER PRIMARY KEY AUTOINCREMENT,
model_id INTEGER NOT NULL,
total_value REAL NOT NULL,
cash REAL NOT NULL,
positions_value REAL NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (model_id) REFERENCES models(id)
)
''')
# Settings table
cursor.execute('''
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
trading_frequency_minutes INTEGER DEFAULT 60,
trading_fee_rate REAL DEFAULT 0.001,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Insert default settings if no settings exist
cursor.execute('SELECT COUNT(*) FROM settings')
if cursor.fetchone()[0] == 0:
cursor.execute('''
INSERT INTO settings (trading_frequency_minutes, trading_fee_rate)
VALUES (60, 0.001)
''')
conn.commit()
conn.close()
# ============ Model Management (Moved) ============
def delete_model(self, model_id: int):
"""Delete model and related data"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('DELETE FROM models WHERE id = ?', (model_id,))
cursor.execute('DELETE FROM portfolios WHERE model_id = ?', (model_id,))
cursor.execute('DELETE FROM trades WHERE model_id = ?', (model_id,))
cursor.execute('DELETE FROM conversations WHERE model_id = ?', (model_id,))
cursor.execute('DELETE FROM account_values WHERE model_id = ?', (model_id,))
conn.commit()
conn.close()
# ============ Portfolio Management ============
def update_position(self, model_id: int, coin: str, quantity: float,
avg_price: float, leverage: int = 1, side: str = 'long'):
"""Update position"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO portfolios (model_id, coin, quantity, avg_price, leverage, side, updated_at)
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
ON CONFLICT(model_id, coin, side) DO UPDATE SET
quantity = excluded.quantity,
avg_price = excluded.avg_price,
leverage = excluded.leverage,
updated_at = CURRENT_TIMESTAMP
''', (model_id, coin, quantity, avg_price, leverage, side))
conn.commit()
conn.close()
def get_portfolio(self, model_id: int, current_prices: Dict = None) -> Dict:
"""Get portfolio with positions and P&L
Args:
model_id: Model ID
current_prices: Current market prices {coin: price} for unrealized P&L calculation
"""
conn = self.get_connection()
cursor = conn.cursor()
# Get positions
cursor.execute('''
SELECT * FROM portfolios WHERE model_id = ? AND quantity > 0
''', (model_id,))
positions = [dict(row) for row in cursor.fetchall()]
# Get initial capital
cursor.execute('SELECT initial_capital FROM models WHERE id = ?', (model_id,))
initial_capital = cursor.fetchone()['initial_capital']
# Calculate realized P&L (sum of all trade P&L)
cursor.execute('''
SELECT COALESCE(SUM(pnl), 0) as total_pnl FROM trades WHERE model_id = ?
''', (model_id,))
realized_pnl = cursor.fetchone()['total_pnl']
# Calculate margin used
margin_used = sum([p['quantity'] * p['avg_price'] / p['leverage'] for p in positions])
# Calculate unrealized P&L (if prices provided)
unrealized_pnl = 0
if current_prices:
for pos in positions:
coin = pos['coin']
if coin in current_prices:
current_price = current_prices[coin]
entry_price = pos['avg_price']
quantity = pos['quantity']
# Add current price to position
pos['current_price'] = current_price
# Calculate position P&L
if pos['side'] == 'long':
pos_pnl = (current_price - entry_price) * quantity
else: # short
pos_pnl = (entry_price - current_price) * quantity
pos['pnl'] = pos_pnl
unrealized_pnl += pos_pnl
else:
pos['current_price'] = None
pos['pnl'] = 0
else:
for pos in positions:
pos['current_price'] = None
pos['pnl'] = 0
# Cash = initial capital + realized P&L - margin used
cash = initial_capital + realized_pnl - margin_used
# Position value = quantity * entry price (not margin!)
positions_value = sum([p['quantity'] * p['avg_price'] for p in positions])
# Total account value = initial capital + realized P&L + unrealized P&L
total_value = initial_capital + realized_pnl + unrealized_pnl
conn.close()
return {
'model_id': model_id,
'cash': cash,
'positions': positions,
'positions_value': positions_value,
'margin_used': margin_used,
'total_value': total_value,
'realized_pnl': realized_pnl,
'unrealized_pnl': unrealized_pnl
}
def close_position(self, model_id: int, coin: str, side: str = 'long'):
"""Close position"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
DELETE FROM portfolios WHERE model_id = ? AND coin = ? AND side = ?
''', (model_id, coin, side))
conn.commit()
conn.close()
# ============ Trade Records ============
def add_trade(self, model_id: int, coin: str, signal: str, quantity: float,
price: float, leverage: int = 1, side: str = 'long', pnl: float = 0, fee: float = 0): # 新增fee参数
"""Add trade record with fee"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO trades (model_id, coin, signal, quantity, price, leverage, side, pnl, fee)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) # 新增fee字段
''', (model_id, coin, signal, quantity, price, leverage, side, pnl, fee)) # 传入fee值
conn.commit()
conn.close()
def get_trades(self, model_id: int, limit: int = 50) -> List[Dict]:
"""Get trade history"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM trades WHERE model_id = ?
ORDER BY timestamp DESC LIMIT ?
''', (model_id, limit))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
# ============ Conversation History ============
def add_conversation(self, model_id: int, user_prompt: str,
ai_response: str, cot_trace: str = ''):
"""Add conversation record"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO conversations (model_id, user_prompt, ai_response, cot_trace)
VALUES (?, ?, ?, ?)
''', (model_id, user_prompt, ai_response, cot_trace))
conn.commit()
conn.close()
def get_conversations(self, model_id: int, limit: int = 20) -> List[Dict]:
"""Get conversation history"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM conversations WHERE model_id = ?
ORDER BY timestamp DESC LIMIT ?
''', (model_id, limit))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
# ============ Account Value History ============
def record_account_value(self, model_id: int, total_value: float,
cash: float, positions_value: float):
"""Record account value snapshot"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO account_values (model_id, total_value, cash, positions_value)
VALUES (?, ?, ?, ?)
''', (model_id, total_value, cash, positions_value))
conn.commit()
conn.close()
def get_account_value_history(self, model_id: int, limit: int = 100) -> List[Dict]:
"""Get account value history"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM account_values WHERE model_id = ?
ORDER BY timestamp DESC LIMIT ?
''', (model_id, limit))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def get_aggregated_account_value_history(self, limit: int = 100) -> List[Dict]:
"""Get aggregated account value history across all models"""
conn = self.get_connection()
cursor = conn.cursor()
# Get the most recent timestamp for each time point across all models
cursor.execute('''
SELECT timestamp,
SUM(total_value) as total_value,
SUM(cash) as cash,
SUM(positions_value) as positions_value,
COUNT(DISTINCT model_id) as model_count
FROM (
SELECT timestamp,
total_value,
cash,
positions_value,
model_id,
ROW_NUMBER() OVER (PARTITION BY model_id, DATE(timestamp) ORDER BY timestamp DESC) as rn
FROM account_values
) grouped
WHERE rn <= 10 -- Keep up to 10 records per model per day for aggregation
GROUP BY DATE(timestamp), HOUR(timestamp)
ORDER BY timestamp DESC
LIMIT ?
''', (limit,))
rows = cursor.fetchall()
conn.close()
result = []
for row in rows:
result.append({
'timestamp': row['timestamp'],
'total_value': row['total_value'],
'cash': row['cash'],
'positions_value': row['positions_value'],
'model_count': row['model_count']
})
return result
def get_multi_model_chart_data(self, limit: int = 100) -> List[Dict]:
"""Get chart data for all models to display in multi-line chart"""
conn = self.get_connection()
cursor = conn.cursor()
# Get all models
cursor.execute('SELECT id, name FROM models')
models = cursor.fetchall()
chart_data = []
for model in models:
model_id = model['id']
model_name = model['name']
# Get account value history for this model
cursor.execute('''
SELECT timestamp, total_value FROM account_values
WHERE model_id = ?
ORDER BY timestamp DESC
LIMIT ?
''', (model_id, limit))
history = cursor.fetchall()
if history:
# Convert to list of dicts with model info
model_data = {
'model_id': model_id,
'model_name': model_name,
'data': [
{
'timestamp': row['timestamp'],
'value': row['total_value']
} for row in history
]
}
chart_data.append(model_data)
conn.close()
return chart_data
# ============ Settings Management ============
def get_settings(self) -> Dict:
"""Get system settings"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT trading_frequency_minutes, trading_fee_rate
FROM settings
ORDER BY id DESC
LIMIT 1
''')
row = cursor.fetchone()
conn.close()
if row:
return {
'trading_frequency_minutes': row['trading_frequency_minutes'],
'trading_fee_rate': row['trading_fee_rate']
}
else:
# Return default settings if none exist
return {
'trading_frequency_minutes': 60,
'trading_fee_rate': 0.001
}
def update_settings(self, trading_frequency_minutes: int, trading_fee_rate: float) -> bool:
"""Update system settings"""
conn = self.get_connection()
cursor = conn.cursor()
try:
cursor.execute('''
UPDATE settings
SET trading_frequency_minutes = ?,
trading_fee_rate = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = (
SELECT id FROM settings ORDER BY id DESC LIMIT 1
)
''', (trading_frequency_minutes, trading_fee_rate))
conn.commit()
conn.close()
return True
except Exception as e:
print(f"Error updating settings: {e}")
conn.close()
return False
# ============ Provider Management ============
def add_provider(self, name: str, api_url: str, api_key: str, models: str = '') -> int:
"""Add new API provider"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO providers (name, api_url, api_key, models)
VALUES (?, ?, ?, ?)
''', (name, api_url, api_key, models))
provider_id = cursor.lastrowid
conn.commit()
conn.close()
return provider_id
def get_provider(self, provider_id: int) -> Optional[Dict]:
"""Get provider information"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('SELECT * FROM providers WHERE id = ?', (provider_id,))
row = cursor.fetchone()
conn.close()
return dict(row) if row else None
def get_all_providers(self) -> List[Dict]:
"""Get all API providers"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('SELECT * FROM providers ORDER BY created_at DESC')
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def delete_provider(self, provider_id: int):
"""Delete provider"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('DELETE FROM providers WHERE id = ?', (provider_id,))
conn.commit()
conn.close()
def update_provider(self, provider_id: int, name: str, api_url: str, api_key: str, models: str):
"""Update provider information"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
UPDATE providers
SET name = ?, api_url = ?, api_key = ?, models = ?
WHERE id = ?
''', (name, api_url, api_key, models, provider_id))
conn.commit()
conn.close()
# ============ Model Management (Updated) ============
def add_model(self, name: str, provider_id: int, model_name: str, initial_capital: float = 10000) -> int:
"""Add new trading model"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
INSERT INTO models (name, provider_id, model_name, initial_capital)
VALUES (?, ?, ?, ?)
''', (name, provider_id, model_name, initial_capital))
model_id = cursor.lastrowid
conn.commit()
conn.close()
return model_id
def get_model(self, model_id: int) -> Optional[Dict]:
"""Get model information"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT m.*, p.api_key, p.api_url
FROM models m
LEFT JOIN providers p ON m.provider_id = p.id
WHERE m.id = ?
''', (model_id,))
row = cursor.fetchone()
conn.close()
return dict(row) if row else None
def get_all_models(self) -> List[Dict]:
"""Get all trading models"""
conn = self.get_connection()
cursor = conn.cursor()
cursor.execute('''
SELECT m.*, p.name as provider_name
FROM models m
LEFT JOIN providers p ON m.provider_id = p.id
ORDER BY m.created_at DESC
''')
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]