forked from henrylin99/quantitative_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_ml_factor_system.py
More file actions
275 lines (251 loc) · 9.97 KB
/
init_ml_factor_system.py
File metadata and controls
275 lines (251 loc) · 9.97 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
多因子模型系统初始化脚本
"""
from app import create_app
from app.extensions import db
from app.models import (
FactorDefinition, FactorValues, MLModelDefinition, MLPredictions,
StockBasic
)
from datetime import datetime
import json
def init_database():
"""初始化数据库表"""
app = create_app()
with app.app_context():
try:
# 创建表
db.create_all()
print("✅ 数据库表创建成功")
# 检查现有因子定义
existing_count = FactorDefinition.query.count()
print(f"📊 现有因子定义数量: {existing_count}")
if existing_count == 0:
init_builtin_factors()
return True
except Exception as e:
print(f"❌ 数据库初始化失败: {e}")
return False
def init_builtin_factors():
"""初始化内置因子定义"""
builtin_factors = [
# 技术面因子
{
'factor_id': 'momentum_1d',
'factor_name': '1日动量',
'factor_formula': '(close - pre_close) / pre_close',
'factor_type': 'technical',
'description': '1日价格动量,反映短期价格变化',
'params': {'period': 1}
},
{
'factor_id': 'momentum_5d',
'factor_name': '5日动量',
'factor_formula': '(close - close_5d_ago) / close_5d_ago',
'factor_type': 'technical',
'description': '5日价格动量,反映短期趋势',
'params': {'period': 5}
},
{
'factor_id': 'momentum_20d',
'factor_name': '20日动量',
'factor_formula': '(close - close_20d_ago) / close_20d_ago',
'factor_type': 'technical',
'description': '20日价格动量,反映中期趋势',
'params': {'period': 20}
},
{
'factor_id': 'volatility_20d',
'factor_name': '20日波动率',
'factor_formula': 'std(pct_change, 20)',
'factor_type': 'technical',
'description': '20日收益率标准差,反映价格波动性',
'params': {'period': 20}
},
{
'factor_id': 'volume_ratio_20d',
'factor_name': '20日量比',
'factor_formula': 'volume / mean(volume, 20)',
'factor_type': 'technical',
'description': '当日成交量与20日均量的比值',
'params': {'period': 20}
},
{
'factor_id': 'price_to_ma20',
'factor_name': '价格相对20日均线',
'factor_formula': '(close - ma20) / ma20',
'factor_type': 'technical',
'description': '当前价格相对20日移动平均线的偏离度',
'params': {'period': 20}
},
# 基本面因子
{
'factor_id': 'pe_percentile',
'factor_name': 'PE历史分位数',
'factor_formula': 'percentile_rank(pe, 252)',
'factor_type': 'fundamental',
'description': 'PE在过去一年中的历史分位数',
'params': {'period': 252}
},
{
'factor_id': 'pb_percentile',
'factor_name': 'PB历史分位数',
'factor_formula': 'percentile_rank(pb, 252)',
'factor_type': 'fundamental',
'description': 'PB在过去一年中的历史分位数',
'params': {'period': 252}
},
{
'factor_id': 'ps_percentile',
'factor_name': 'PS历史分位数',
'factor_formula': 'percentile_rank(ps, 252)',
'factor_type': 'fundamental',
'description': 'PS在过去一年中的历史分位数',
'params': {'period': 252}
},
{
'factor_id': 'roe_ttm',
'factor_name': 'ROE(TTM)',
'factor_formula': 'net_profit_ttm / total_equity',
'factor_type': 'fundamental',
'description': '净资产收益率(滚动12个月)',
'params': {}
},
{
'factor_id': 'roa_ttm',
'factor_name': 'ROA(TTM)',
'factor_formula': 'net_profit_ttm / total_assets',
'factor_type': 'fundamental',
'description': '总资产收益率(滚动12个月)',
'params': {}
},
{
'factor_id': 'revenue_growth',
'factor_name': '营收增长率',
'factor_formula': '(revenue_ttm - revenue_ttm_1y_ago) / revenue_ttm_1y_ago',
'factor_type': 'fundamental',
'description': '营业收入同比增长率',
'params': {}
},
{
'factor_id': 'profit_growth',
'factor_name': '利润增长率',
'factor_formula': '(net_profit_ttm - net_profit_ttm_1y_ago) / net_profit_ttm_1y_ago',
'factor_type': 'fundamental',
'description': '净利润同比增长率',
'params': {}
},
# 资金面因子
{
'factor_id': 'money_flow_strength',
'factor_name': '资金流向强度',
'factor_formula': 'net_mf_amount / total_mv',
'factor_type': 'money_flow',
'description': '净流入金额相对市值的比例',
'params': {}
},
{
'factor_id': 'big_order_ratio',
'factor_name': '大单占比',
'factor_formula': '(buy_lg_amount + sell_lg_amount) / (buy_lg_amount + buy_md_amount + buy_sm_amount + sell_lg_amount + sell_md_amount + sell_sm_amount)',
'factor_type': 'money_flow',
'description': '大单交易金额占总交易金额的比例',
'params': {}
},
{
'factor_id': 'money_flow_momentum',
'factor_name': '资金流向动量',
'factor_formula': 'mean(net_mf_amount, 5) / std(net_mf_amount, 20)',
'factor_type': 'money_flow',
'description': '5日平均净流入相对20日波动的比值',
'params': {'short_period': 5, 'long_period': 20}
},
# 筹码面因子
{
'factor_id': 'chip_concentration',
'factor_name': '筹码集中度',
'factor_formula': '(cost_95pct - cost_5pct) / cost_50pct',
'factor_type': 'chip',
'description': '筹码分布的集中程度',
'params': {}
},
{
'factor_id': 'winner_rate_change',
'factor_name': '胜率变化',
'factor_formula': 'winner_rate - winner_rate_5d_ago',
'factor_type': 'chip',
'description': '当前胜率相对5日前的变化',
'params': {'period': 5}
}
]
try:
for factor_data in builtin_factors:
# 检查是否已存在
existing = FactorDefinition.query.filter_by(factor_id=factor_data['factor_id']).first()
if not existing:
factor = FactorDefinition(
factor_id=factor_data['factor_id'],
factor_name=factor_data['factor_name'],
factor_formula=factor_data['factor_formula'],
factor_type=factor_data['factor_type'],
description=factor_data['description'],
params=factor_data['params'],
is_active=True
)
db.session.add(factor)
db.session.commit()
print(f"✅ 成功初始化 {len(builtin_factors)} 个内置因子定义")
except Exception as e:
db.session.rollback()
print(f"❌ 初始化内置因子失败: {e}")
def check_data_availability():
"""检查数据可用性"""
app = create_app()
with app.app_context():
try:
# 检查股票基础数据
stock_count = StockBasic.query.count()
print(f"📈 股票基础数据: {stock_count} 只股票")
# 检查各类数据表的数据量
from app.models import (
StockDailyHistory, StockDailyBasic, StockFactor,
StockMoneyflow, StockCyqPerf
)
history_count = StockDailyHistory.query.count()
basic_count = StockDailyBasic.query.count()
factor_count = StockFactor.query.count()
money_count = StockMoneyflow.query.count()
cyq_count = StockCyqPerf.query.count()
print(f"📊 日线行情数据: {history_count:,} 条")
print(f"📊 基本面数据: {basic_count:,} 条")
print(f"📊 技术因子数据: {factor_count:,} 条")
print(f"📊 资金流向数据: {money_count:,} 条")
print(f"📊 筹码分布数据: {cyq_count:,} 条")
# 检查最新数据日期
if history_count > 0:
latest_date = db.session.query(db.func.max(StockDailyHistory.trade_date)).scalar()
print(f"📅 最新交易日期: {latest_date}")
return True
except Exception as e:
print(f"❌ 检查数据可用性失败: {e}")
return False
def main():
"""主函数"""
print("🚀 开始初始化多因子模型系统...")
# 1. 初始化数据库
if not init_database():
return
# 2. 检查数据可用性
if not check_data_availability():
return
print("\n✅ 多因子模型系统初始化完成!")
print("\n📋 下一步操作:")
print("1. 启动应用: python run.py")
print("2. 访问因子管理: http://127.0.0.1:5001/ml-factor")
print("3. 计算因子值: 在因子管理页面点击'计算因子'")
print("4. 创建ML模型: 访问模型管理页面")
print("5. 进行股票评分: 访问股票评分页面")
if __name__ == '__main__':
main()