forked from henrylin99/quantitative_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_realtime_indicators.py
More file actions
308 lines (260 loc) · 10.8 KB
/
test_realtime_indicators.py
File metadata and controls
308 lines (260 loc) · 10.8 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
#!/usr/bin/env python3
"""
实时技术指标功能测试脚本
测试技术指标计算引擎和API接口
"""
import sys
import os
import requests
import json
from datetime import datetime, timedelta
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app, db
from app.models.realtime_indicator import RealtimeIndicator
from app.models.stock_minute_data import StockMinuteData
from app.services.realtime_indicator_engine import RealtimeIndicatorEngine
# 测试配置
BASE_URL = 'http://127.0.0.1:5001'
TEST_STOCK = '000001.SZ'
def test_indicator_model():
"""测试技术指标数据模型"""
print("\n🧪 测试技术指标数据模型...")
app = create_app()
with app.app_context():
try:
# 创建测试数据
test_indicator = RealtimeIndicator(
ts_code=TEST_STOCK,
datetime=datetime.now(),
period_type='5min',
indicator_name='MA',
value1=10.5,
value2=20.3,
metadata={'period': 20}
)
db.session.add(test_indicator)
db.session.commit()
# 查询测试
result = RealtimeIndicator.query.filter_by(
ts_code=TEST_STOCK,
indicator_name='MA'
).first()
if result:
print("✅ 技术指标数据模型测试通过")
print(f" - 股票代码: {result.ts_code}")
print(f" - 指标名称: {result.indicator_name}")
print(f" - 指标值: {result.value1}, {result.value2}")
# 清理测试数据
db.session.delete(result)
db.session.commit()
return True
else:
print("❌ 技术指标数据模型测试失败")
return False
except Exception as e:
print(f"❌ 技术指标数据模型测试异常: {str(e)}")
return False
def test_indicator_engine():
"""测试技术指标计算引擎"""
print("\n🧪 测试技术指标计算引擎...")
app = create_app()
with app.app_context():
try:
engine = RealtimeIndicatorEngine()
# 检查支持的指标
supported = engine.get_supported_indicators()
print(f"✅ 支持的指标数量: {len(supported)}")
for indicator in supported[:3]: # 显示前3个
print(f" - {indicator['name']} ({indicator['code']}): {indicator['description']}")
# 测试指标计算(需要有数据)
stock_data = StockMinuteData.query.filter_by(
ts_code=TEST_STOCK,
period_type='5min'
).limit(50).all()
if stock_data:
print(f"✅ 找到测试数据: {len(stock_data)} 条记录")
# 测试MA计算
result = engine.calculate_indicators(
ts_code=TEST_STOCK,
period_type='5min',
indicators=['MA'],
lookback_days=7
)
if result['success']:
print("✅ 技术指标计算引擎测试通过")
print(f" - 计算结果: {result['total_indicators']} 个指标")
print(f" - 数据点数: {result['data_points']}")
return True
else:
print(f"❌ 指标计算失败: {result['message']}")
return False
else:
print("⚠️ 没有找到测试数据,跳过指标计算测试")
return True
except Exception as e:
print(f"❌ 技术指标计算引擎测试异常: {str(e)}")
import traceback
traceback.print_exc()
return False
def test_api_endpoints():
"""测试API接口"""
print("\n🧪 测试技术指标API接口...")
test_results = []
# 测试支持的指标接口
try:
response = requests.get(f'{BASE_URL}/api/realtime-analysis/indicators/supported', timeout=10)
if response.status_code == 200:
data = response.json()
if data.get('success'):
print("✅ 支持的指标接口测试通过")
print(f" - 支持指标数: {len(data['data'])}")
test_results.append(True)
else:
print(f"❌ 支持的指标接口返回失败: {data.get('message')}")
test_results.append(False)
else:
print(f"❌ 支持的指标接口状态码: {response.status_code}")
test_results.append(False)
except Exception as e:
print(f"❌ 支持的指标接口测试异常: {str(e)}")
test_results.append(False)
# 测试统计信息接口
try:
response = requests.get(f'{BASE_URL}/api/realtime-analysis/indicators/stats', timeout=10)
if response.status_code == 200:
data = response.json()
if data.get('success'):
print("✅ 统计信息接口测试通过")
stats = data['data']
print(f" - 总记录数: {stats.get('total_records', 0)}")
print(f" - 股票数量: {stats.get('total_stocks', 0)}")
test_results.append(True)
else:
print(f"❌ 统计信息接口返回失败: {data.get('message')}")
test_results.append(False)
else:
print(f"❌ 统计信息接口状态码: {response.status_code}")
test_results.append(False)
except Exception as e:
print(f"❌ 统计信息接口测试异常: {str(e)}")
test_results.append(False)
# 测试指标计算接口(如果有数据)
try:
calc_data = {
'ts_code': TEST_STOCK,
'period_type': '5min',
'indicators': ['MA'],
'lookback_days': 7
}
response = requests.post(
f'{BASE_URL}/api/realtime-analysis/indicators/calculate',
json=calc_data,
timeout=30
)
if response.status_code == 200:
data = response.json()
if data.get('success'):
print("✅ 指标计算接口测试通过")
print(f" - 计算指标数: {data.get('total_indicators', 0)}")
print(f" - 存储记录数: {data.get('stored_records', 0)}")
test_results.append(True)
else:
print(f"⚠️ 指标计算接口返回: {data.get('message')}")
test_results.append(True) # 可能是没有数据,不算失败
else:
print(f"❌ 指标计算接口状态码: {response.status_code}")
test_results.append(False)
except Exception as e:
print(f"❌ 指标计算接口测试异常: {str(e)}")
test_results.append(False)
return all(test_results)
def test_frontend_access():
"""测试前端页面访问"""
print("\n🧪 测试前端页面访问...")
try:
response = requests.get(f'{BASE_URL}/realtime-analysis/indicators', timeout=10)
if response.status_code == 200:
content = response.text
if '实时技术指标分析' in content and 'indicatorTabs' in content:
print("✅ 技术指标前端页面访问正常")
print(" - 页面标题正确")
print(" - JavaScript组件加载正常")
return True
else:
print("❌ 技术指标前端页面内容异常")
return False
else:
print(f"❌ 技术指标前端页面状态码: {response.status_code}")
return False
except Exception as e:
print(f"❌ 技术指标前端页面访问异常: {str(e)}")
return False
def test_performance():
"""测试性能"""
print("\n🧪 测试性能...")
app = create_app()
with app.app_context():
try:
# 测试数据库查询性能
start_time = datetime.now()
# 查询最近的指标数据
indicators = RealtimeIndicator.query.filter(
RealtimeIndicator.datetime >= datetime.now() - timedelta(days=1)
).limit(1000).all()
query_time = (datetime.now() - start_time).total_seconds()
print(f"✅ 数据库查询性能测试")
print(f" - 查询记录数: {len(indicators)}")
print(f" - 查询耗时: {query_time:.3f} 秒")
# 性能评估
if query_time < 1.0:
print(" - 性能评级: 优秀 ⭐⭐⭐")
elif query_time < 3.0:
print(" - 性能评级: 良好 ⭐⭐")
else:
print(" - 性能评级: 需要优化 ⭐")
return True
except Exception as e:
print(f"❌ 性能测试异常: {str(e)}")
return False
def main():
"""主测试函数"""
print("🚀 开始实时技术指标功能测试")
print("=" * 50)
test_results = []
# 执行各项测试
test_results.append(test_indicator_model())
test_results.append(test_indicator_engine())
test_results.append(test_api_endpoints())
test_results.append(test_frontend_access())
test_results.append(test_performance())
# 汇总测试结果
print("\n" + "=" * 50)
print("📊 测试结果汇总")
print("=" * 50)
passed = sum(test_results)
total = len(test_results)
test_names = [
"技术指标数据模型",
"技术指标计算引擎",
"API接口功能",
"前端页面访问",
"性能测试"
]
for i, (name, result) in enumerate(zip(test_names, test_results)):
status = "✅ 通过" if result else "❌ 失败"
print(f"{i+1}. {name}: {status}")
print(f"\n🎯 总体结果: {passed}/{total} 项测试通过")
if passed == total:
print("🎉 所有测试通过!实时技术指标功能正常运行")
print("\n📝 下一步:")
print("1. 访问 http://127.0.0.1:5001/realtime-analysis/indicators")
print("2. 先同步股票数据,再进行指标计算")
print("3. 体验多周期分析和指标对比功能")
return True
else:
print("⚠️ 部分测试失败,请检查相关功能")
return False
if __name__ == '__main__':
success = main()
sys.exit(0 if success else 1)