You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""
amazingData SDK 数据获取测试
测试获取 600126 股票的日 K 线数据
"""
import sys
sys.path.insert(0, '/app/working/workspaces/developer/projects/20260330_kline_system/backend')
from datetime import datetime, timedelta
from app.services.amazing_data_service import amazing_data_service
from app.services.amazing_data_adapter import SecurityType, Period
print("=" * 70)
print("amazingData SDK 数据获取测试")
print("=" * 70)
print()
# 测试参数 - 使用正确的代码格式 (600126.SH)
symbol = "600126.SH" # 杭钢股份 (带市场后缀)
security_type = SecurityType.STOCK_A # 沪深 A 股
period = Period.DAILY # 日 K 线
end_date = (datetime.now()).strftime("%Y%m%d")
start_date = (datetime.now() - timedelta(days=30)).strftime("%Y%m%d")
print(f"📋 测试参数:")
print(f" 股票代码:{symbol}")
print(f" 证券类型:{security_type}")
print(f" 周期:{period}")
print(f" 开始日期:{start_date}")
print(f" 结束日期:{end_date}")
print()
# 连接数据源
print("🔌 正在连接 amazingData 数据源...")
success = amazing_data_service.connect()
if not success:
print("❌ 连接失败!")
sys.exit(1)
print("✅ 连接成功!")
print()
# 直接调用 adapter 获取数据
print(f"📊 正在获取 {symbol} 的日 K 线数据...")
try:
# 直接使用 adapter
adapter = amazing_data_service._adapter
# 获取 K 线数据
kline_dict = adapter.get_kline(
codes=[symbol],
start_date=start_date,
end_date=end_date,
period=period
)
if kline_dict and symbol in kline_dict:
df = kline_dict[symbol]
print(f"✅ 获取成功!共 {len(df)} 条数据")
print()
# 显示前 5 条数据
print("📈 前 5 条 K 线数据:")
print("-" * 70)
print(f"{'日期':<12} {'开盘':<10} {'最高':<10} {'最低':<10} {'收盘':<10} {'成交量':<12}")
print("-" * 70)
for i, (idx, row) in enumerate(df.head(5).iterrows()):
date = row.get('datetime', row.get('date', idx))
open_price = row.get('open', 0)
high = row.get('high', 0)
low = row.get('low', 0)
close = row.get('close', 0)
volume = row.get('volume', 0)
print(f"{str(date):<12} {open_price:<10.2f} {high:<10.2f} {low:<10.2f} {close:<10.2f} {volume:<12}")
print("-" * 70)
# 数据统计
if len(df) > 0:
print()
print("📊 数据统计:")
print(f" 最新日期:{df.index[-1] if hasattr(df, 'index') else df.iloc[-1].get('datetime', 'N/A')}")
print(f" 最新收盘价:{df.iloc[-1]['close'] if 'close' in df.columns else 'N/A'}")
print(f" 数据条数:{len(df)}")
print(f" 列名:{list(df.columns)}")
else:
print(f"⚠️ 未获取到数据kline_dict keys: {kline_dict.keys() if kline_dict else 'None'}")
except Exception as e:
print(f"❌ 获取数据失败:{e}")
import traceback
traceback.print_exc()
print()
print("🔌 正在断开连接...")
amazing_data_service.disconnect()
print("✅ 已断开连接")
print()
print("=" * 70)
print("测试完成!")
print("=" * 70)