stock-ai-agent/backend/close_other_symbols.py
2026-02-08 14:14:15 +08:00

66 lines
1.9 KiB
Python
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
"""
关闭非 BTCUSDT/ETHUSDT 的订单
"""
import sys
import os
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app.services.paper_trading_service import get_paper_trading_service
from app.services.binance_service import binance_service
def main():
# 允许的交易对
allowed_symbols = {'BTCUSDT', 'ETHUSDT'}
service = get_paper_trading_service()
active_orders = service.get_active_orders()
print(f"当前活跃订单数: {len(active_orders)}")
# 找出需要平仓的订单
orders_to_close = [
order for order in active_orders
if order.get('symbol') not in allowed_symbols
]
if not orders_to_close:
print("没有需要平仓的订单")
return
print(f"\n需要平仓的订单数: {len(orders_to_close)}")
for order in orders_to_close:
symbol = order.get('symbol')
order_id = order.get('order_id')
side = order.get('side')
entry_price = order.get('entry_price', 0)
# 获取当前价格
current_price = binance_service.get_current_price(symbol)
if not current_price:
# 如果无法获取价格使用入场价平仓盈亏为0
print(f"无法获取 {symbol} 价格,使用入场价平仓")
current_price = entry_price
print(f"\n平仓订单: {order_id[:12]}...")
print(f" 交易对: {symbol}")
print(f" 方向: {side}")
print(f" 入场价: ${entry_price:,.2f}")
print(f" 平仓价: ${current_price:,.2f}")
result = service.close_order_manual(order_id, current_price)
if result:
print(f" 结果: 平仓成功,盈亏 {result.get('pnl_percent', 0):.2f}%")
else:
print(f" 结果: 平仓失败")
print("\n完成!")
if __name__ == "__main__":
main()