stock-ai-agent/scripts/test_futures_data.py
2026-02-20 22:02:17 +08:00

81 lines
3.0 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
测试合约市场数据获取
"""
import sys
import os
# 确保路径正确
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
backend_dir = os.path.join(project_root, 'backend')
sys.path.insert(0, backend_dir)
from app.services.binance_service import binance_service
def main():
print("=" * 60)
print("📊 测试 Binance 合约市场数据获取")
print("=" * 60)
symbols = ['BTCUSDT', 'ETHUSDT']
for symbol in symbols:
print(f"\n{'='*60}")
print(f"📈 {symbol} 合约市场数据")
print(f"{'='*60}")
# 1. 获取资金费率
print(f"\n🔍 获取资金费率...")
funding_rate = binance_service.get_funding_rate(symbol)
if funding_rate:
print(f"✅ 资金费率: {funding_rate['funding_rate_percent']:.4f}%")
print(f" 市场情绪: {funding_rate['sentiment']}")
print(f" 标记价格: ${funding_rate['mark_price']:,.2f}")
print(f" 指数价格: ${funding_rate['index_price']:,.2f}")
# 2. 获取持仓量
print(f"\n🔍 获取持仓量...")
open_interest = binance_service.get_open_interest(symbol)
if open_interest:
print(f"✅ 持仓量: {open_interest['open_interest']:,.0f}")
print(f" 持仓金额: ${open_interest['open_interest_value']:,.0f}")
# 3. 获取历史持仓量
print(f"\n🔍 获取历史持仓量计算24h变化...")
hist_oi = binance_service.get_open_interest_hist(symbol, period='1h', limit=24)
if hist_oi and len(hist_oi) >= 2:
oi_now = float(hist_oi[0].get('sumOpenInterest', 0))
oi_24h = float(hist_oi[-1].get('sumOpenInterest', 0))
oi_change = oi_now - oi_24h
oi_change_pct = (oi_change / oi_24h * 100) if oi_24h > 0 else 0
print(f"✅ 24h前: {oi_24h:,.0f}")
print(f" 当前: {oi_now:,.0f}")
print(f" 变化: {oi_change:+,.0f} 张 ({oi_change_pct:+.2f}%)")
# 4. 获取综合数据
print(f"\n🔍 获取综合合约数据...")
market_data = binance_service.get_futures_market_data(symbol)
if market_data:
print(f"✅ 综合数据获取成功")
print(f" 资金费率: {market_data['funding_rate']['funding_rate_percent']:.4f}%")
print(f" 持仓24h变化: {market_data['oi_change_percent_24h']:+.2f}%")
print(f" 溢价率: {market_data['premium_rate']:+.2f}%")
print(f" 市场情绪: {market_data['market_sentiment']}")
# 5. 格式化给 LLM 的数据
print(f"\n🤖 格式化给 LLM 的数据:")
print(f"{''*60}")
if market_data:
formatted = binance_service.format_futures_data_for_llm(symbol, market_data)
print(formatted)
print("\n" + "=" * 60)
print("✅ 测试完成")
print("=" * 60)
if __name__ == "__main__":
main()