stock-ai-agent/scripts/test_modify_order.py
2026-02-23 21:53:03 +08:00

106 lines
2.8 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
"""
测试使用 modify-order 修改止损止盈
"""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'backend'))
import time
import uuid
from app.services.bitget_trading_api_sdk import get_bitget_trading_api
def print_section(title):
print("\n" + "=" * 60)
print(f" {title}")
print("=" * 60)
def main():
print("\n" + "🧪" * 30)
print(" 测试 modify-order 修改止损止盈")
print("🧪" * 30)
api = get_bitget_trading_api()
exchange = api.exchange
# 检查是否有 modify order 相关的方法
print("\n检查 modify order 方法:")
methods = [m for m in dir(exchange) if 'modify' in m.lower() and 'order' in m.lower()]
for m in methods:
print(f" {m}")
# 1. 开仓
print_section("1. 开仓")
order = api.place_order(
symbol="BTC/USDT:USDT",
side='buy',
order_type='market',
size=0.0001,
client_order_id=f"test_{uuid.uuid4().hex[:8]}"
)
if not order:
print("❌ 开仓失败")
return
order_id = order.get('id')
print(f"✅ 开仓成功! 订单ID: {order_id}")
time.sleep(2)
# 2. 查询持仓
print_section("2. 查询持仓")
positions = api.get_position("BTC/USDT:USDT")
position = None
for pos in positions:
if float(pos.get('contracts', 0)) > 0:
position = pos
break
if not position:
print("❌ 无持仓")
return
mark_price = float(position.get('markPrice'))
print(f"持仓: {position.get('contracts')} 张, 标记价: ${mark_price:,.2f}")
# 3. 尝试使用 edit_order 修改(添加止损止盈)
print_section("3. 使用 edit_order 添加止损止盈")
stop_loss = mark_price * 0.98
take_profit = mark_price * 1.03
print(f"目标止损: ${stop_loss:,.2f}")
print(f"目标止盈: ${take_profit:,.2f}")
try:
# 尝试使用 CCXT 的 edit_order 方法
# 注意可能需要传入订单ID和新参数
result = exchange.edit_order(
id=order_id,
symbol="BTC/USDT:USDT",
type='market', # 原订单类型
side='buy', # 原订单方向
amount=0.0001, # 原订单数量
params={
'stopLoss': str(stop_loss),
'takeProfit': str(take_profit),
'tdMode': 'cross',
'marginCoin': 'USDT',
}
)
print(f"✅ edit_order 成功: {result}")
except Exception as e:
print(f"❌ edit_order 失败: {e}")
# 4. 平仓
print_section("4. 平仓")
api.close_position("BTC/USDT:USDT")
print("✅ 已平仓")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"\n\n❌ 测试出错: {e}")
import traceback
traceback.print_exc()