forked from MaoTouHU/Nof1OpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
584 lines (492 loc) · 20.3 KB
/
Copy pathapp.py
File metadata and controls
584 lines (492 loc) · 20.3 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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
from flask import Flask, render_template, request, jsonify
from flask_cors import CORS
import time
import threading
import json
import re
from datetime import datetime
from trading_engine import TradingEngine
from market_data import MarketDataFetcher
from ai_trader import AITrader
from database import Database
from version import __version__, __github_owner__, __repo__, GITHUB_REPO_URL, LATEST_RELEASE_URL
app = Flask(__name__)
CORS(app)
db = Database('AITradeGame.db')
market_fetcher = MarketDataFetcher()
trading_engines = {}
auto_trading = True
TRADE_FEE_RATE = 0.001 # 默认交易费率
@app.route('/')
def index():
return render_template('index.html')
# ============ Provider API Endpoints ============
@app.route('/api/providers', methods=['GET'])
def get_providers():
"""Get all API providers"""
providers = db.get_all_providers()
return jsonify(providers)
@app.route('/api/providers', methods=['POST'])
def add_provider():
"""Add new API provider"""
data = request.json
try:
provider_id = db.add_provider(
name=data['name'],
api_url=data['api_url'],
api_key=data['api_key'],
models=data.get('models', '')
)
return jsonify({'id': provider_id, 'message': 'Provider added successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/providers/<int:provider_id>', methods=['DELETE'])
def delete_provider(provider_id):
"""Delete API provider"""
try:
db.delete_provider(provider_id)
return jsonify({'message': 'Provider deleted successfully'})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/providers/models', methods=['POST'])
def fetch_provider_models():
"""Fetch available models from provider's API"""
data = request.json
api_url = data.get('api_url')
api_key = data.get('api_key')
if not api_url or not api_key:
return jsonify({'error': 'API URL and key are required'}), 400
try:
# This is a placeholder - implement actual API call based on provider
# For now, return empty list or common models
models = []
# Try to detect provider type and call appropriate API
if 'openai.com' in api_url.lower():
# OpenAI API call
import requests
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.get(f'{api_url}/models', headers=headers, timeout=10)
if response.status_code == 200:
result = response.json()
models = [m['id'] for m in result.get('data', []) if 'gpt' in m['id'].lower()]
elif 'deepseek' in api_url.lower():
# DeepSeek API
import requests
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.get(f'{api_url}/models', headers=headers, timeout=10)
if response.status_code == 200:
result = response.json()
models = [m['id'] for m in result.get('data', [])]
else:
# Default: return common model names
models = ['gpt-3.5-turbo', 'gpt-4', 'gpt-4-turbo']
return jsonify({'models': models})
except Exception as e:
print(f"[ERROR] Fetch models failed: {e}")
return jsonify({'error': f'Failed to fetch models: {str(e)}'}), 500
# ============ Model API Endpoints ============
@app.route('/api/models', methods=['GET'])
def get_models():
models = db.get_all_models()
return jsonify(models)
@app.route('/api/models', methods=['POST'])
def add_model():
data = request.json
try:
# Get provider info
provider = db.get_provider(data['provider_id'])
if not provider:
return jsonify({'error': 'Provider not found'}), 404
model_id = db.add_model(
name=data['name'],
provider_id=data['provider_id'],
model_name=data['model_name'],
initial_capital=float(data.get('initial_capital', 100000))
)
model = db.get_model(model_id)
trading_engines[model_id] = TradingEngine(
model_id=model_id,
db=db,
market_fetcher=market_fetcher,
ai_trader=AITrader(
api_key=model['api_key'],
api_url=model['api_url'],
model_name=model['model_name']
),
trade_fee_rate=TRADE_FEE_RATE # 新增:传入费率
)
print(f"[INFO] Model {model_id} ({data['name']}) initialized")
return jsonify({'id': model_id, 'message': 'Model added successfully'})
except Exception as e:
print(f"[ERROR] Failed to add model: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/api/models/<int:model_id>', methods=['DELETE'])
def delete_model(model_id):
try:
model = db.get_model(model_id)
model_name = model['name'] if model else f"ID-{model_id}"
db.delete_model(model_id)
if model_id in trading_engines:
del trading_engines[model_id]
print(f"[INFO] Model {model_id} ({model_name}) deleted")
return jsonify({'message': 'Model deleted successfully'})
except Exception as e:
print(f"[ERROR] Delete model {model_id} failed: {e}")
return jsonify({'error': str(e)}), 500
@app.route('/api/models/<int:model_id>/portfolio', methods=['GET'])
def get_portfolio(model_id):
prices_data = market_fetcher.get_current_prices(['BTC', 'ETH', 'SOL', 'BNB', 'XRP', 'DOGE'])
current_prices = {coin: prices_data[coin]['price'] for coin in prices_data}
portfolio = db.get_portfolio(model_id, current_prices)
account_value = db.get_account_value_history(model_id, limit=100)
return jsonify({
'portfolio': portfolio,
'account_value_history': account_value
})
@app.route('/api/models/<int:model_id>/trades', methods=['GET'])
def get_trades(model_id):
limit = request.args.get('limit', 50, type=int)
trades = db.get_trades(model_id, limit=limit)
return jsonify(trades)
@app.route('/api/models/<int:model_id>/conversations', methods=['GET'])
def get_conversations(model_id):
limit = request.args.get('limit', 20, type=int)
conversations = db.get_conversations(model_id, limit=limit)
return jsonify(conversations)
@app.route('/api/aggregated/portfolio', methods=['GET'])
def get_aggregated_portfolio():
"""Get aggregated portfolio data across all models"""
prices_data = market_fetcher.get_current_prices(['BTC', 'ETH', 'SOL', 'BNB', 'XRP', 'DOGE'])
current_prices = {coin: prices_data[coin]['price'] for coin in prices_data}
# Get aggregated data
models = db.get_all_models()
total_portfolio = {
'total_value': 0,
'cash': 0,
'positions_value': 0,
'realized_pnl': 0,
'unrealized_pnl': 0,
'initial_capital': 0,
'positions': []
}
all_positions = {}
for model in models:
portfolio = db.get_portfolio(model['id'], current_prices)
if portfolio:
total_portfolio['total_value'] += portfolio.get('total_value', 0)
total_portfolio['cash'] += portfolio.get('cash', 0)
total_portfolio['positions_value'] += portfolio.get('positions_value', 0)
total_portfolio['realized_pnl'] += portfolio.get('realized_pnl', 0)
total_portfolio['unrealized_pnl'] += portfolio.get('unrealized_pnl', 0)
total_portfolio['initial_capital'] += portfolio.get('initial_capital', 0)
# Aggregate positions by coin and side
for pos in portfolio.get('positions', []):
key = f"{pos['coin']}_{pos['side']}"
if key not in all_positions:
all_positions[key] = {
'coin': pos['coin'],
'side': pos['side'],
'quantity': 0,
'avg_price': 0,
'total_cost': 0,
'leverage': pos['leverage'],
'current_price': pos['current_price'],
'pnl': 0
}
# Weighted average calculation
current_pos = all_positions[key]
current_cost = current_pos['quantity'] * current_pos['avg_price']
new_cost = pos['quantity'] * pos['avg_price']
total_quantity = current_pos['quantity'] + pos['quantity']
if total_quantity > 0:
current_pos['avg_price'] = (current_cost + new_cost) / total_quantity
current_pos['quantity'] = total_quantity
current_pos['total_cost'] = current_cost + new_cost
current_pos['pnl'] = (pos['current_price'] - current_pos['avg_price']) * total_quantity
total_portfolio['positions'] = list(all_positions.values())
# Get multi-model chart data
chart_data = db.get_multi_model_chart_data(limit=100)
return jsonify({
'portfolio': total_portfolio,
'chart_data': chart_data,
'model_count': len(models)
})
@app.route('/api/models/chart-data', methods=['GET'])
def get_models_chart_data():
"""Get chart data for all models"""
limit = request.args.get('limit', 100, type=int)
chart_data = db.get_multi_model_chart_data(limit=limit)
return jsonify(chart_data)
@app.route('/api/market/prices', methods=['GET'])
def get_market_prices():
coins = ['BTC', 'ETH', 'SOL', 'BNB', 'XRP', 'DOGE']
prices = market_fetcher.get_current_prices(coins)
return jsonify(prices)
@app.route('/api/models/<int:model_id>/execute', methods=['POST'])
def execute_trading(model_id):
if model_id not in trading_engines:
model = db.get_model(model_id)
if not model:
return jsonify({'error': 'Model not found'}), 404
# Get provider info
provider = db.get_provider(model['provider_id'])
if not provider:
return jsonify({'error': 'Provider not found'}), 404
trading_engines[model_id] = TradingEngine(
model_id=model_id,
db=db,
market_fetcher=market_fetcher,
ai_trader=AITrader(
api_key=provider['api_key'],
api_url=provider['api_url'],
model_name=model['model_name']
),
trade_fee_rate=TRADE_FEE_RATE # 新增:传入费率
)
try:
result = trading_engines[model_id].execute_trading_cycle()
return jsonify(result)
except Exception as e:
return jsonify({'error': str(e)}), 500
def trading_loop():
print("[INFO] Trading loop started")
while auto_trading:
try:
if not trading_engines:
time.sleep(30)
continue
print(f"\n{'='*60}")
print(f"[CYCLE] {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print(f"[INFO] Active models: {len(trading_engines)}")
print(f"{'='*60}")
for model_id, engine in list(trading_engines.items()):
try:
print(f"\n[EXEC] Model {model_id}")
result = engine.execute_trading_cycle()
if result.get('success'):
print(f"[OK] Model {model_id} completed")
if result.get('executions'):
for exec_result in result['executions']:
signal = exec_result.get('signal', 'unknown')
coin = exec_result.get('coin', 'unknown')
msg = exec_result.get('message', '')
if signal != 'hold':
print(f" [TRADE] {coin}: {msg}")
else:
error = result.get('error', 'Unknown error')
print(f"[WARN] Model {model_id} failed: {error}")
except Exception as e:
print(f"[ERROR] Model {model_id} exception: {e}")
import traceback
print(traceback.format_exc())
continue
print(f"\n{'='*60}")
print(f"[SLEEP] Waiting 3 minutes for next cycle")
print(f"{'='*60}\n")
time.sleep(180)
except Exception as e:
print(f"\n[CRITICAL] Trading loop error: {e}")
import traceback
print(traceback.format_exc())
print("[RETRY] Retrying in 60 seconds\n")
time.sleep(60)
print("[INFO] Trading loop stopped")
@app.route('/api/leaderboard', methods=['GET'])
def get_leaderboard():
models = db.get_all_models()
leaderboard = []
prices_data = market_fetcher.get_current_prices(['BTC', 'ETH', 'SOL', 'BNB', 'XRP', 'DOGE'])
current_prices = {coin: prices_data[coin]['price'] for coin in prices_data}
for model in models:
portfolio = db.get_portfolio(model['id'], current_prices)
account_value = portfolio.get('total_value', model['initial_capital'])
returns = ((account_value - model['initial_capital']) / model['initial_capital']) * 100
leaderboard.append({
'model_id': model['id'],
'model_name': model['name'],
'account_value': account_value,
'returns': returns,
'initial_capital': model['initial_capital']
})
leaderboard.sort(key=lambda x: x['returns'], reverse=True)
return jsonify(leaderboard)
@app.route('/api/settings', methods=['GET'])
def get_settings():
"""Get system settings"""
try:
settings = db.get_settings()
return jsonify(settings)
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/settings', methods=['PUT'])
def update_settings():
"""Update system settings"""
try:
data = request.json
trading_frequency_minutes = int(data.get('trading_frequency_minutes', 60))
trading_fee_rate = float(data.get('trading_fee_rate', 0.001))
success = db.update_settings(trading_frequency_minutes, trading_fee_rate)
if success:
return jsonify({'success': True, 'message': 'Settings updated successfully'})
else:
return jsonify({'success': False, 'error': 'Failed to update settings'}), 500
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/version', methods=['GET'])
def get_version():
"""Get current version information"""
return jsonify({
'current_version': __version__,
'github_repo': GITHUB_REPO_URL,
'latest_release_url': LATEST_RELEASE_URL
})
@app.route('/api/check-update', methods=['GET'])
def check_update():
"""Check for GitHub updates"""
try:
import requests
# Get latest release from GitHub
headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'AITradeGame/1.0'
}
# Try to get latest release
try:
response = requests.get(
f"https://api.github.com/repos/{__github_owner__}/{__repo__}/releases/latest",
headers=headers,
timeout=5
)
if response.status_code == 200:
release_data = response.json()
latest_version = release_data.get('tag_name', '').lstrip('v')
release_url = release_data.get('html_url', '')
release_notes = release_data.get('body', '')
# Compare versions
is_update_available = compare_versions(latest_version, __version__) > 0
return jsonify({
'update_available': is_update_available,
'current_version': __version__,
'latest_version': latest_version,
'release_url': release_url,
'release_notes': release_notes,
'repo_url': GITHUB_REPO_URL
})
else:
# If API fails, still return current version info
return jsonify({
'update_available': False,
'current_version': __version__,
'error': 'Could not check for updates'
})
except Exception as e:
print(f"[WARN] GitHub API error: {e}")
return jsonify({
'update_available': False,
'current_version': __version__,
'error': 'Network error checking updates'
})
except Exception as e:
print(f"[ERROR] Check update failed: {e}")
return jsonify({
'update_available': False,
'current_version': __version__,
'error': str(e)
}), 500
def compare_versions(version1, version2):
"""Compare two version strings.
Returns:
1 if version1 > version2
0 if version1 == version2
-1 if version1 < version2
"""
def normalize(v):
# Extract numeric parts from version string
parts = re.findall(r'\d+', v)
# Pad with zeros to make them comparable
return [int(p) for p in parts]
v1_parts = normalize(version1)
v2_parts = normalize(version2)
# Pad shorter version with zeros
max_len = max(len(v1_parts), len(v2_parts))
v1_parts.extend([0] * (max_len - len(v1_parts)))
v2_parts.extend([0] * (max_len - len(v2_parts)))
# Compare
if v1_parts > v2_parts:
return 1
elif v1_parts < v2_parts:
return -1
else:
return 0
def init_trading_engines():
try:
models = db.get_all_models()
if not models:
print("[WARN] No trading models found")
return
print(f"\n[INIT] Initializing trading engines...")
for model in models:
model_id = model['id']
model_name = model['name']
try:
# Get provider info
provider = db.get_provider(model['provider_id'])
if not provider:
print(f" [WARN] Model {model_id} ({model_name}): Provider not found")
continue
trading_engines[model_id] = TradingEngine(
model_id=model_id,
db=db,
market_fetcher=market_fetcher,
ai_trader=AITrader(
api_key=provider['api_key'],
api_url=provider['api_url'],
model_name=model['model_name']
),
trade_fee_rate=TRADE_FEE_RATE
)
print(f" [OK] Model {model_id} ({model_name})")
except Exception as e:
print(f" [ERROR] Model {model_id} ({model_name}): {e}")
continue
print(f"[INFO] Initialized {len(trading_engines)} engine(s)\n")
except Exception as e:
print(f"[ERROR] Init engines failed: {e}\n")
if __name__ == '__main__':
import webbrowser
import os
print("\n" + "=" * 60)
print("AITradeGame - Starting...")
print("=" * 60)
print("[INFO] Initializing database...")
db.init_db()
print("[INFO] Database initialized")
print("[INFO] Initializing trading engines...")
init_trading_engines()
if auto_trading:
trading_thread = threading.Thread(target=trading_loop, daemon=True)
trading_thread.start()
print("[INFO] Auto-trading enabled")
print("\n" + "=" * 60)
print("AITradeGame is running!")
print("Server: http://localhost:5000")
print("Press Ctrl+C to stop")
print("=" * 60 + "\n")
# 自动打开浏览器
def open_browser():
time.sleep(1.5) # 等待服务器启动
url = "http://localhost:5000"
try:
webbrowser.open(url)
print(f"[INFO] Browser opened: {url}")
except Exception as e:
print(f"[WARN] Could not open browser: {e}")
browser_thread = threading.Thread(target=open_browser, daemon=True)
browser_thread.start()
app.run(debug=False, host='0.0.0.0', port=5000, use_reloader=False)