Skip to content

feat(bitget): support preset TP/SL in single API call#1370

Open
Fumeng24 wants to merge 1 commit intoNoFxAiOS:devfrom
Fumeng24:feat/bitget-preset-tpsl
Open

feat(bitget): support preset TP/SL in single API call#1370
Fumeng24 wants to merge 1 commit intoNoFxAiOS:devfrom
Fumeng24:feat/bitget-preset-tpsl

Conversation

@Fumeng24
Copy link

@Fumeng24 Fumeng24 commented Feb 1, 2026

📝 Description

实现 Bitget 交易所的预设止损止盈功能,通过在下单 API 中使用 presetStopLossPricepresetStopSurplusPrice 参数,将原来的 3 次 API 调用优化为单次调用,提高订单设置的可靠性和执行效率。


🎯 Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📝 Documentation update
  • 🎨 Code style update
  • ♻️ Refactoring
  • ⚡ Performance improvement
  • ✅ Test update
  • 🔧 Build/config change
  • 🔒 Security fix

🔗 Related Issues

  • Closes # (Bitget 止损止盈不生效问题)

📋 Changes Made

  • trader.go: 新增 OpenLongWithTPSL()OpenShortWithTPSL() 方法,支持在单次 API 调用中设置预设止损止盈
  • trader.go: 新增 FormatPrice() 方法处理价格精度(Bitget 要求价格为合约精度的倍数,如 BTC 需要 0.1 的倍数)
  • trader.go: 重构 openLongInternal()openShortInternal() 内部方法,支持可选的 TP/SL 参数
  • auto_trader.go: 在 executeOpenLongWithRecord()executeOpenShortWithRecord() 中使用类型断言检测 trader 是否支持新的 TPSL 方法
  • auto_trader.go: 支持新方法时使用单次调用,否则回退到原有的 3 次调用方式(兼容其他交易所)
  • trader/bitget/trader_test.go: 新增测试文件,包含 TestOpenLongWithTPSLTestGetBalanceTestGetPositions 测试用例

🧪 Testing

  • Tested locally
  • Tests pass
  • Verified no existing functionality broke

测试详情:

  • 使用真实 Bitget API 测试 OpenLongWithTPSL 方法
  • 验证订单成功创建并包含预设止损止盈
  • 确认价格精度格式化正确(通过 FormatPrice 方法)

✅ Checklist

Code Quality

  • Code follows project style
  • Self-review completed
  • Comments added for complex logic

Documentation

  • Updated relevant documentation

Git

  • Commits follow conventional format
  • Rebased on latest dev branch
  • No merge conflicts

📚 Additional Notes

技术实现细节

  1. 单次 API 调用:使用 Bitget API V2 的 /api/v2/mix/order/place-order 端点,在请求体中添加 presetStopLossPricepresetStopSurplusPrice 参数

  2. 价格精度处理:Bitget 对价格精度有严格要求(错误码 45115),通过 FormatPrice() 方法根据合约的 PricePlace 字段格式化价格

  3. 向后兼容:通过 Go 的类型断言模式,只有 Bitget trader 使用新方法,其他交易所继续使用原有的 3 次调用方式

// 类型断言检测
type TPSLOpener interface {
    OpenLongWithTPSL(symbol string, amount, stopLoss, takeProfit float64) (string, error)
}

if opener, ok := at.trader.(TPSLOpener); ok {
    // 使用优化的单次调用
    return opener.OpenLongWithTPSL(symbol, amount, stopLoss, takeProfit)
}
// 回退到原有方式

By submitting this PR, I confirm:

  • I have read the Contributing Guidelines
  • I agree to the Code of Conduct
  • My contribution is licensed under AGPL-3.0

🌟 Thank you for your contribution!

- Add OpenLongWithTPSL/OpenShortWithTPSL methods to Bitget trader
- Use presetStopLossPrice and presetStopSurplusPrice parameters
- Add FormatPrice method for correct price precision
- Auto-detect and use optimized method in auto_trader.go
- Fallback to legacy 3-call method for other exchanges
- Add trader_test.go for testing Bitget orders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant