forked from henrylin99/quantitative_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_fix_system.py
More file actions
623 lines (511 loc) · 23.6 KB
/
Copy pathquick_fix_system.py
File metadata and controls
623 lines (511 loc) · 23.6 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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
多因子模型系统快速修复脚本
解决当前系统的主要问题:数据缺失、模型训练失败、API接口问题
"""
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
import warnings
import json
warnings.filterwarnings('ignore')
from app import create_app
from app.extensions import db
from app.models import (
StockDailyHistory, StockDailyBasic, StockFactor, StockMoneyflow,
StockCyqPerf, FactorDefinition, FactorValues, MLModelDefinition,
MLPredictions, StockBasic
)
class QuickSystemFix:
"""快速系统修复"""
def __init__(self):
self.app = create_app()
def run_complete_fix(self):
"""运行完整修复流程"""
print("🔧 开始多因子模型系统快速修复")
print("="*60)
with self.app.app_context():
try:
# 1. 检查和修复数据库表
self.fix_database_tables()
# 2. 补充因子定义
self.fix_factor_definitions()
# 3. 计算缺失的因子数据
self.fix_factor_data()
# 4. 修复模型定义
self.fix_model_definitions()
# 5. 创建工作演示模型
self.create_working_demo()
# 6. 生成修复报告
self.generate_fix_report()
print("✅ 系统修复完成!")
return True
except Exception as e:
print(f"❌ 系统修复失败: {e}")
return False
def fix_database_tables(self):
"""修复数据库表"""
print("📊 检查和修复数据库表...")
try:
# 创建所有表
db.create_all()
# 检查表状态
tables = [
('stock_basic', StockBasic),
('stock_daily_history', StockDailyHistory),
('factor_definition', FactorDefinition),
('factor_values', FactorValues),
('ml_model_definition', MLModelDefinition),
('ml_predictions', MLPredictions)
]
for table_name, model_class in tables:
count = model_class.query.count()
print(f" ✅ {table_name}: {count:,} 条记录")
print("✅ 数据库表检查完成")
except Exception as e:
print(f"❌ 数据库表修复失败: {e}")
raise
def fix_factor_definitions(self):
"""修复因子定义"""
print("📝 修复因子定义...")
try:
# 内置因子定义
builtin_factors = [
{
'factor_id': 'momentum_5d',
'factor_name': '5日动量',
'factor_formula': '(close - close_5d_ago) / close_5d_ago',
'factor_type': 'technical',
'description': '5日价格动量,反映短期趋势'
},
{
'factor_id': 'momentum_20d',
'factor_name': '20日动量',
'factor_formula': '(close - close_20d_ago) / close_20d_ago',
'factor_type': 'technical',
'description': '20日价格动量,反映中期趋势'
},
{
'factor_id': 'volatility_20d',
'factor_name': '20日波动率',
'factor_formula': 'std(pct_change, 20)',
'factor_type': 'technical',
'description': '20日收益率标准差'
},
{
'factor_id': 'volume_ratio_20d',
'factor_name': '20日量比',
'factor_formula': 'volume / mean(volume, 20)',
'factor_type': 'technical',
'description': '当日成交量与20日均量的比值'
},
{
'factor_id': 'price_to_ma20',
'factor_name': '价格相对20日均线',
'factor_formula': 'close / mean(close, 20) - 1',
'factor_type': 'technical',
'description': '收盘价相对20日均线的偏离度'
},
{
'factor_id': 'money_flow_strength',
'factor_name': '资金流向强度',
'factor_formula': 'net_mf_amount / total_mv',
'factor_type': 'money_flow',
'description': '净流入金额相对市值的比例'
},
{
'factor_id': 'chip_concentration',
'factor_name': '筹码集中度',
'factor_formula': 'cost_5pct / cost_95pct',
'factor_type': 'chip',
'description': '筹码分布集中度指标'
}
]
added_count = 0
for factor_def in builtin_factors:
existing = FactorDefinition.query.filter_by(factor_id=factor_def['factor_id']).first()
if not existing:
factor = FactorDefinition(
factor_id=factor_def['factor_id'],
factor_name=factor_def['factor_name'],
factor_formula=factor_def['factor_formula'],
factor_type=factor_def['factor_type'],
description=factor_def['description'],
params={},
is_active=True
)
db.session.add(factor)
added_count += 1
print(f" ➕ 添加因子: {factor_def['factor_id']}")
db.session.commit()
print(f"✅ 因子定义修复完成,新增 {added_count} 个因子")
except Exception as e:
db.session.rollback()
print(f"❌ 因子定义修复失败: {e}")
raise
def fix_factor_data(self):
"""修复因子数据"""
print("🔢 修复因子数据...")
try:
# 检查现有因子数据
existing_factors = db.session.execute("""
SELECT factor_id, COUNT(*) as count
FROM factor_values
GROUP BY factor_id
""").fetchall()
print(" 📊 现有因子数据:")
for factor_id, count in existing_factors:
print(f" {factor_id}: {count:,} 条记录")
# 如果数据不足,计算补充数据
total_factor_records = sum(count for _, count in existing_factors)
if total_factor_records < 50000: # 如果因子数据少于5万条
print(" 🔄 因子数据不足,开始补充计算...")
self._calculate_missing_factors()
else:
print(" ✅ 因子数据充足")
except Exception as e:
print(f"❌ 因子数据修复失败: {e}")
def _calculate_missing_factors(self):
"""计算缺失的因子数据"""
try:
# 获取最近的交易日期
latest_dates = db.session.execute("""
SELECT DISTINCT trade_date
FROM stock_daily_history
ORDER BY trade_date DESC
LIMIT 30
""").fetchall()
if not latest_dates:
print(" ⚠️ 没有历史价格数据,无法计算技术因子")
return
dates_to_process = [row[0] for row in latest_dates]
print(f" 📅 处理 {len(dates_to_process)} 个交易日")
calculated_count = 0
for trade_date in dates_to_process:
daily_count = self._calculate_factors_for_date(trade_date)
calculated_count += daily_count
if daily_count > 0:
print(f" {trade_date}: 计算了 {daily_count} 个因子值")
print(f" ✅ 总共计算了 {calculated_count} 个因子值")
except Exception as e:
print(f" ❌ 计算因子数据失败: {e}")
def _calculate_factors_for_date(self, trade_date):
"""计算指定日期的因子数据"""
try:
# 获取该日期的股票数据
stocks_data = db.session.execute("""
SELECT ts_code, close, vol, pct_chg
FROM stock_daily_history
WHERE trade_date = :trade_date
LIMIT 100
""", {'trade_date': trade_date}).fetchall()
if not stocks_data:
return 0
calculated_count = 0
for stock_data in stocks_data:
ts_code = stock_data[0]
# 计算简单的技术因子
factors = self._calculate_simple_factors(ts_code, trade_date)
# 保存因子值
for factor_id, factor_value in factors.items():
if factor_value is not None and not np.isnan(factor_value):
existing = FactorValues.query.filter_by(
ts_code=ts_code,
trade_date=trade_date,
factor_id=factor_id
).first()
if not existing:
factor_val = FactorValues(
ts_code=ts_code,
trade_date=trade_date,
factor_id=factor_id,
factor_value=float(factor_value)
)
db.session.add(factor_val)
calculated_count += 1
if calculated_count > 0:
db.session.commit()
return calculated_count
except Exception as e:
db.session.rollback()
return 0
def _calculate_simple_factors(self, ts_code, trade_date):
"""计算简单的技术因子"""
factors = {}
try:
# 获取历史数据
hist_data = db.session.execute("""
SELECT trade_date, close, vol, pct_chg
FROM stock_daily_history
WHERE ts_code = :ts_code
AND trade_date <= :trade_date
ORDER BY trade_date DESC
LIMIT 30
""", {'ts_code': ts_code, 'trade_date': trade_date}).fetchall()
if len(hist_data) < 5:
return factors
# 转换为列表
closes = [float(row[1]) for row in hist_data]
volumes = [float(row[2]) if row[2] else 0 for row in hist_data]
pct_changes = [float(row[3]) if row[3] else 0 for row in hist_data]
# 计算动量因子
if len(closes) >= 6:
factors['momentum_5d'] = (closes[0] - closes[5]) / closes[5] if closes[5] != 0 else 0
if len(closes) >= 21:
factors['momentum_20d'] = (closes[0] - closes[20]) / closes[20] if closes[20] != 0 else 0
# 计算波动率
if len(pct_changes) >= 20:
returns = np.array(pct_changes[:20]) / 100.0
factors['volatility_20d'] = np.std(returns)
# 计算量比
if len(volumes) >= 20:
current_volume = volumes[0]
avg_volume = np.mean(volumes[1:21])
if avg_volume > 0:
factors['volume_ratio_20d'] = current_volume / avg_volume
# 计算价格相对均线
if len(closes) >= 20:
current_price = closes[0]
ma20 = np.mean(closes[:20])
factors['price_to_ma20'] = (current_price / ma20) - 1
except Exception as e:
pass
return factors
def fix_model_definitions(self):
"""修复模型定义"""
print("🤖 修复模型定义...")
try:
# 获取可用的因子
available_factors = db.session.execute("""
SELECT DISTINCT factor_id
FROM factor_values
WHERE factor_id IN ('momentum_5d', 'momentum_20d', 'volatility_20d',
'volume_ratio_20d', 'price_to_ma20', 'money_flow_strength',
'chip_concentration')
""").fetchall()
available_factor_list = [row[0] for row in available_factors]
print(f" 📊 可用因子: {available_factor_list}")
if len(available_factor_list) < 2:
print(" ⚠️ 可用因子不足,创建模拟因子数据")
self._create_simulated_factor_data()
available_factor_list = ['money_flow_strength', 'chip_concentration']
# 创建模型定义
model_configs = [
{
'model_id': 'fixed_demo_model',
'model_name': '修复演示模型',
'model_type': 'random_forest',
'factor_list': available_factor_list[:3], # 使用前3个因子
'target_type': 'simulated_return'
},
{
'model_id': 'lightweight_model',
'model_name': '轻量级模型',
'model_type': 'random_forest',
'factor_list': available_factor_list[:2], # 使用前2个因子
'target_type': 'simulated_return'
}
]
created_count = 0
for config in model_configs:
existing = MLModelDefinition.query.filter_by(model_id=config['model_id']).first()
if existing:
db.session.delete(existing)
model_def = MLModelDefinition(
model_id=config['model_id'],
model_name=config['model_name'],
model_type=config['model_type'],
factor_list=config['factor_list'],
target_type=config['target_type'],
model_params={},
training_config={},
is_active=True
)
db.session.add(model_def)
created_count += 1
print(f" ➕ 创建模型: {config['model_id']}")
db.session.commit()
print(f"✅ 模型定义修复完成,创建了 {created_count} 个模型")
except Exception as e:
db.session.rollback()
print(f"❌ 模型定义修复失败: {e}")
def _create_simulated_factor_data(self):
"""创建模拟因子数据"""
try:
print(" 🎯 创建模拟因子数据...")
# 获取股票列表
stocks = db.session.execute("""
SELECT DISTINCT ts_code
FROM stock_daily_history
LIMIT 100
""").fetchall()
if not stocks:
print(" ❌ 没有股票数据")
return
# 创建模拟因子数据
trade_date = datetime.now().date()
created_count = 0
for stock_row in stocks:
ts_code = stock_row[0]
# 创建模拟因子值
simulated_factors = {
'money_flow_strength': np.random.normal(0, 0.01),
'chip_concentration': np.random.uniform(0.5, 2.0)
}
for factor_id, factor_value in simulated_factors.items():
existing = FactorValues.query.filter_by(
ts_code=ts_code,
trade_date=trade_date,
factor_id=factor_id
).first()
if not existing:
factor_val = FactorValues(
ts_code=ts_code,
trade_date=trade_date,
factor_id=factor_id,
factor_value=float(factor_value)
)
db.session.add(factor_val)
created_count += 1
db.session.commit()
print(f" ✅ 创建了 {created_count} 个模拟因子值")
except Exception as e:
db.session.rollback()
print(f" ❌ 创建模拟因子数据失败: {e}")
def create_working_demo(self):
"""创建工作演示"""
print("🎯 创建工作演示...")
try:
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import r2_score
# 获取演示模型
demo_model = MLModelDefinition.query.filter_by(model_id='fixed_demo_model').first()
if not demo_model:
print(" ❌ 未找到演示模型定义")
return
# 准备训练数据
factor_data = db.session.execute("""
SELECT ts_code, factor_id, factor_value
FROM factor_values
WHERE factor_id IN :factor_list
""", {'factor_list': tuple(demo_model.factor_list)}).fetchall()
if not factor_data:
print(" ❌ 没有因子数据")
return
# 转换为DataFrame
df = pd.DataFrame(factor_data, columns=['ts_code', 'factor_id', 'factor_value'])
pivot_df = df.pivot_table(
index='ts_code',
columns='factor_id',
values='factor_value',
aggfunc='first'
).dropna()
if len(pivot_df) < 20:
print(f" ⚠️ 数据量不足: {len(pivot_df)} 样本")
return
# 创建特征和目标变量
X = pivot_df[demo_model.factor_list]
# 创建模拟目标变量
np.random.seed(42)
scaler = RobustScaler()
X_scaled = scaler.fit_transform(X)
weights = np.random.random(len(demo_model.factor_list))
weights = weights / weights.sum()
signal = np.dot(X_scaled, weights)
noise = np.random.normal(0, 0.02, len(signal))
y = signal * 0.05 + noise
# 训练模型
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = RandomForestRegressor(n_estimators=50, max_depth=8, random_state=42)
scaler_final = RobustScaler()
X_train_scaled = scaler_final.fit_transform(X_train)
X_test_scaled = scaler_final.transform(X_test)
model.fit(X_train_scaled, y_train)
# 评估模型
train_pred = model.predict(X_train_scaled)
test_pred = model.predict(X_test_scaled)
train_r2 = r2_score(y_train, train_pred)
test_r2 = r2_score(y_test, test_pred)
print(f" 📊 模型性能: 训练R²={train_r2:.4f}, 测试R²={test_r2:.4f}")
# 生成预测结果
predictions = model.predict(X_test_scaled)
# 保存预测结果
trade_date = datetime.now().date()
saved_count = 0
for i, (ts_code, pred_return) in enumerate(zip(X_test.index, predictions)):
existing = MLPredictions.query.filter_by(
ts_code=ts_code,
trade_date=trade_date,
model_id='fixed_demo_model'
).first()
if not existing:
prediction = MLPredictions(
ts_code=ts_code,
trade_date=trade_date,
model_id='fixed_demo_model',
predicted_return=float(pred_return),
probability_score=abs(float(pred_return)),
rank_score=i + 1
)
db.session.add(prediction)
saved_count += 1
db.session.commit()
print(f" ✅ 保存了 {saved_count} 个预测结果")
print("✅ 工作演示创建完成")
except Exception as e:
db.session.rollback()
print(f"❌ 创建工作演示失败: {e}")
def generate_fix_report(self):
"""生成修复报告"""
print("📋 生成修复报告...")
try:
# 收集统计信息
stats = {
'timestamp': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'stock_count': StockBasic.query.count(),
'history_records': StockDailyHistory.query.count(),
'factor_definitions': FactorDefinition.query.count(),
'factor_records': FactorValues.query.count(),
'model_definitions': MLModelDefinition.query.count(),
'prediction_records': MLPredictions.query.count()
}
# 打印报告
print("\n" + "="*60)
print("📊 系统修复报告")
print("="*60)
print(f"修复时间: {stats['timestamp']}")
print(f"股票数量: {stats['stock_count']:,}")
print(f"历史记录: {stats['history_records']:,}")
print(f"因子定义: {stats['factor_definitions']}")
print(f"因子记录: {stats['factor_records']:,}")
print(f"模型定义: {stats['model_definitions']}")
print(f"预测记录: {stats['prediction_records']:,}")
# 保存报告
report_file = f"system_fix_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
with open(report_file, 'w', encoding='utf-8') as f:
json.dump(stats, f, ensure_ascii=False, indent=2)
print(f"\n✅ 修复报告已保存: {report_file}")
print("="*60)
except Exception as e:
print(f"❌ 生成修复报告失败: {e}")
def main():
"""主函数"""
print("🔧 多因子模型系统快速修复工具")
print("="*60)
# 创建修复实例
fixer = QuickSystemFix()
# 运行修复
success = fixer.run_complete_fix()
if success:
print("\n🎉 系统修复成功!")
print("现在可以运行以下命令启动系统:")
print(" python complete_system_launcher.py")
print(" 或者")
print(" python web_interface_v2.py")
else:
print("\n❌ 系统修复失败,请检查错误信息")
if __name__ == "__main__":
main()