mirror of
https://github.com/laoxong/nofx.git
synced 2026-06-04 01:48:22 +08:00
f4ece051e7
* refactor: 简化交易动作,移除 update_stop_loss/update_take_profit/partial_close - 移除 Decision 结构体中的 NewStopLoss, NewTakeProfit, ClosePercentage 字段 - 删除 executeUpdateStopLossWithRecord, executeUpdateTakeProfitWithRecord, executePartialCloseWithRecord 函数 - 简化 logger 中的 partial_close 聚合逻辑 - 更新 AI prompt 和验证逻辑,只保留 6 个核心动作 - 清理相关测试代码 保留的交易动作: open_long, open_short, close_long, close_short, hold, wait * refactor: 移除 AI学习与反思 模块 - 删除前端 AILearning.tsx 组件和相关引用 - 删除后端 /performance API 接口 - 删除 logger 中 AnalyzePerformance、calculateSharpeRatio 等函数 - 删除 PerformanceAnalysis、TradeOutcome、SymbolPerformance 等结构体 - 删除 Context 中的 Performance 字段 - 移除 AI prompt 中夏普比率自我进化相关内容 - 清理 i18n 翻译文件中的相关条目 该模块基于磁盘存储计算,经常出错,做减法移除 * refactor: 将数据库操作统一迁移到 store 包 - 新增 store/ 包,统一管理所有数据库操作 - store.go: 主 Store 结构,懒加载各子模块 - user.go, ai_model.go, exchange.go, trader.go 等子模块 - 支持加密/解密函数注入 (SetCryptoFuncs) - 更新 main.go 使用 store.New() 替代 config.NewDatabase() - 更新 api/server.go 使用 *store.Store 替代 *config.Database - 更新 manager/trader_manager.go: - 新增 LoadTradersFromStore, LoadUserTradersFromStore 方法 - 删除旧版 LoadUserTraders, LoadTraderByID, loadSingleTrader 等方法 - 移除 nofx/config 依赖 - 删除 config/database.go 和 config/database_test.go - 更新 api/server_test.go 使用 store.Trader 类型 - 清理 logger/ 包中未使用的 telegram 相关代码 * refactor: unify encryption key management via .env - Remove redundant EncryptionManager and SecureStorage - Simplify CryptoService to load keys from environment variables only - RSA_PRIVATE_KEY: RSA private key for client-server encryption - DATA_ENCRYPTION_KEY: AES-256 key for database encryption - JWT_SECRET: JWT signing key for authentication - Update start.sh to auto-generate missing keys on first run - Remove secrets/ directory and file-based key storage - Delete obsolete encryption setup scripts - Update .env.example with all required keys * refactor: unify logger usage across mcp package - Add MCPLogger adapter in logger package to implement mcp.Logger interface - Update mcp/config.go to use global logger by default - Remove redundant defaultLogger from mcp/logger.go - Keep noopLogger for testing purposes * chore: remove leftover test RSA key file * chore: remove unused bootstrap package * refactor: unify logging to use logger package instead of fmt/log - Replace all fmt.Print/log.Print calls with logger package - Add auto-initialization in logger package init() for test compatibility - Update main.go to initialize logger at startup - Migrate all packages: api, backtest, config, decision, manager, market, store, trader * refactor: rename database file from config.db to data.db - Update main.go, start.sh, docker-compose.yml - Update migration script and documentation - Update .gitignore and translations * fix: add RSA_PRIVATE_KEY to docker-compose environment * fix: add registration_enabled to /api/config response * fix: Fix navigation between login and register pages Use window.location.href instead of react-router's navigate() to fix the issue where URL changes but the page doesn't reload due to App.tsx using custom route state management. * fix: Switch SQLite from WAL to DELETE mode for Docker compatibility WAL mode causes data sync issues with Docker bind mounts on macOS due to incompatible file locking mechanisms between the container and host. DELETE mode (traditional journaling) ensures data is written directly to the main database file. * refactor: Remove default user from database initialization The default user was a legacy placeholder that is no longer needed now that proper user registration is in place. * feat: Add order tracking system with centralized status sync - Add trader_orders table for tracking all order lifecycle - Implement GetOrderStatus interface for all exchanges (Binance, Bybit, Hyperliquid, Aster, Lighter) - Create OrderSyncManager for centralized order status polling - Add trading statistics (Sharpe ratio, win rate, profit factor) to AI context - Include recent completed orders in AI decision input - Remove per-order goroutine polling in favor of global sync manager * feat: Add TradingView K-line chart to dashboard - Create TradingViewChart component with exchange/symbol selectors - Support Binance, Bybit, OKX, Coinbase, Kraken, KuCoin exchanges - Add popular symbols quick selection - Support multiple timeframes (1m to 1W) - Add fullscreen mode - Integrate with Dashboard page below equity chart - Add i18n translations for zh/en * refactor: Replace separate charts with tabbed ChartTabs component - Create ChartTabs component with tab switching between equity curve and K-line - Add embedded mode support for EquityChart and TradingViewChart - User can now switch between account equity and market chart in same area * fix: Use ChartTabs in App.tsx and fix embedded mode in EquityChart - Replace EquityChart with ChartTabs in App.tsx (the actual dashboard renderer) - Fix EquityChart embedded mode for error and empty data states - Rename interval state to timeInterval to avoid shadowing window.setInterval - Add debug logging to ChartTabs component * feat: Add position tracking system for accurate trade history - Add trader_positions table to track complete open/close trades - Add PositionSyncManager to detect manual closes via polling - Record position on open, update on close with PnL calculation - Use positions table for trading stats and recent trades (replacing orders table) - Fix TradingView chart symbol format (add .P suffix for futures) - Fix DecisionCard wait/hold action color (gray instead of red) - Auto-append USDT suffix for custom symbol input * update ---------
723 lines
19 KiB
Go
723 lines
19 KiB
Go
package trader
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"nofx/logger"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
bybit "github.com/bybit-exchange/bybit.go.api"
|
|
)
|
|
|
|
// BybitTrader Bybit USDT 永續合約交易器
|
|
type BybitTrader struct {
|
|
client *bybit.Client
|
|
|
|
// 余额缓存
|
|
cachedBalance map[string]interface{}
|
|
balanceCacheTime time.Time
|
|
balanceCacheMutex sync.RWMutex
|
|
|
|
// 持仓缓存
|
|
cachedPositions []map[string]interface{}
|
|
positionsCacheTime time.Time
|
|
positionsCacheMutex sync.RWMutex
|
|
|
|
// 缓存有效期(15秒)
|
|
cacheDuration time.Duration
|
|
}
|
|
|
|
// NewBybitTrader 创建 Bybit 交易器
|
|
func NewBybitTrader(apiKey, secretKey string) *BybitTrader {
|
|
const src = "Up000938"
|
|
|
|
client := bybit.NewBybitHttpClient(apiKey, secretKey, bybit.WithBaseURL(bybit.MAINNET))
|
|
|
|
// 设置 HTTP 传输
|
|
if client != nil && client.HTTPClient != nil {
|
|
defaultTransport := client.HTTPClient.Transport
|
|
if defaultTransport == nil {
|
|
defaultTransport = http.DefaultTransport
|
|
}
|
|
|
|
client.HTTPClient.Transport = &headerRoundTripper{
|
|
base: defaultTransport,
|
|
refererID: src,
|
|
}
|
|
}
|
|
|
|
trader := &BybitTrader{
|
|
client: client,
|
|
cacheDuration: 15 * time.Second,
|
|
}
|
|
|
|
logger.Infof("🔵 [Bybit] 交易器已初始化")
|
|
|
|
return trader
|
|
}
|
|
|
|
// headerRoundTripper 用于添加自定义 header 的 HTTP RoundTripper
|
|
type headerRoundTripper struct {
|
|
base http.RoundTripper
|
|
refererID string
|
|
}
|
|
|
|
func (h *headerRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
req.Header.Set("Referer", h.refererID)
|
|
return h.base.RoundTrip(req)
|
|
}
|
|
|
|
// GetBalance 获取账户余额
|
|
func (t *BybitTrader) GetBalance() (map[string]interface{}, error) {
|
|
// 检查缓存
|
|
t.balanceCacheMutex.RLock()
|
|
if t.cachedBalance != nil && time.Since(t.balanceCacheTime) < t.cacheDuration {
|
|
balance := t.cachedBalance
|
|
t.balanceCacheMutex.RUnlock()
|
|
return balance, nil
|
|
}
|
|
t.balanceCacheMutex.RUnlock()
|
|
|
|
// 调用 API
|
|
params := map[string]interface{}{
|
|
"accountType": "UNIFIED",
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).GetAccountWallet(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取 Bybit 余额失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return nil, fmt.Errorf("Bybit API 错误: %s", result.RetMsg)
|
|
}
|
|
|
|
// 提取余额信息
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return nil, fmt.Errorf("Bybit 余额返回格式错误")
|
|
}
|
|
|
|
list, _ := resultData["list"].([]interface{})
|
|
|
|
var totalEquity, availableBalance float64 = 0, 0
|
|
|
|
if len(list) > 0 {
|
|
account, _ := list[0].(map[string]interface{})
|
|
if equityStr, ok := account["totalEquity"].(string); ok {
|
|
totalEquity, _ = strconv.ParseFloat(equityStr, 64)
|
|
}
|
|
if availStr, ok := account["totalAvailableBalance"].(string); ok {
|
|
availableBalance, _ = strconv.ParseFloat(availStr, 64)
|
|
}
|
|
}
|
|
|
|
balance := map[string]interface{}{
|
|
"totalEquity": totalEquity,
|
|
"availableBalance": availableBalance,
|
|
"balance": totalEquity, // 兼容其他交易所格式
|
|
}
|
|
|
|
// 更新缓存
|
|
t.balanceCacheMutex.Lock()
|
|
t.cachedBalance = balance
|
|
t.balanceCacheTime = time.Now()
|
|
t.balanceCacheMutex.Unlock()
|
|
|
|
return balance, nil
|
|
}
|
|
|
|
// GetPositions 获取所有持仓
|
|
func (t *BybitTrader) GetPositions() ([]map[string]interface{}, error) {
|
|
// 检查缓存
|
|
t.positionsCacheMutex.RLock()
|
|
if t.cachedPositions != nil && time.Since(t.positionsCacheTime) < t.cacheDuration {
|
|
positions := t.cachedPositions
|
|
t.positionsCacheMutex.RUnlock()
|
|
return positions, nil
|
|
}
|
|
t.positionsCacheMutex.RUnlock()
|
|
|
|
// 调用 API
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"settleCoin": "USDT",
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).GetPositionList(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取 Bybit 持仓失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return nil, fmt.Errorf("Bybit API 错误: %s", result.RetMsg)
|
|
}
|
|
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return nil, fmt.Errorf("Bybit 持仓返回格式错误")
|
|
}
|
|
|
|
list, _ := resultData["list"].([]interface{})
|
|
|
|
var positions []map[string]interface{}
|
|
|
|
for _, item := range list {
|
|
pos, ok := item.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
sizeStr, _ := pos["size"].(string)
|
|
size, _ := strconv.ParseFloat(sizeStr, 64)
|
|
|
|
// 跳过空仓位
|
|
if size == 0 {
|
|
continue
|
|
}
|
|
|
|
entryPriceStr, _ := pos["avgPrice"].(string)
|
|
entryPrice, _ := strconv.ParseFloat(entryPriceStr, 64)
|
|
|
|
unrealisedPnlStr, _ := pos["unrealisedPnl"].(string)
|
|
unrealisedPnl, _ := strconv.ParseFloat(unrealisedPnlStr, 64)
|
|
|
|
leverageStr, _ := pos["leverage"].(string)
|
|
leverage, _ := strconv.ParseFloat(leverageStr, 64)
|
|
|
|
positionSide, _ := pos["side"].(string) // Buy = LONG, Sell = SHORT
|
|
|
|
// 转换为统一格式
|
|
side := "LONG"
|
|
positionAmt := size
|
|
if positionSide == "Sell" {
|
|
side = "SHORT"
|
|
positionAmt = -size
|
|
}
|
|
|
|
position := map[string]interface{}{
|
|
"symbol": pos["symbol"],
|
|
"side": side,
|
|
"positionAmt": positionAmt,
|
|
"entryPrice": entryPrice,
|
|
"unrealizedPnL": unrealisedPnl,
|
|
"leverage": int(leverage),
|
|
}
|
|
|
|
positions = append(positions, position)
|
|
}
|
|
|
|
// 更新缓存
|
|
t.positionsCacheMutex.Lock()
|
|
t.cachedPositions = positions
|
|
t.positionsCacheTime = time.Now()
|
|
t.positionsCacheMutex.Unlock()
|
|
|
|
return positions, nil
|
|
}
|
|
|
|
// OpenLong 开多仓
|
|
func (t *BybitTrader) OpenLong(symbol string, quantity float64, leverage int) (map[string]interface{}, error) {
|
|
// 先设置杠杆
|
|
if err := t.SetLeverage(symbol, leverage); err != nil {
|
|
logger.Infof("⚠️ [Bybit] 设置杠杆失败: %v", err)
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": "Buy",
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"positionIdx": 0, // 单向持仓模式
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Bybit 开多失败: %w", err)
|
|
}
|
|
|
|
// 清除缓存
|
|
t.clearCache()
|
|
|
|
return t.parseOrderResult(result)
|
|
}
|
|
|
|
// OpenShort 开空仓
|
|
func (t *BybitTrader) OpenShort(symbol string, quantity float64, leverage int) (map[string]interface{}, error) {
|
|
// 先设置杠杆
|
|
if err := t.SetLeverage(symbol, leverage); err != nil {
|
|
logger.Infof("⚠️ [Bybit] 设置杠杆失败: %v", err)
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": "Sell",
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"positionIdx": 0, // 单向持仓模式
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Bybit 开空失败: %w", err)
|
|
}
|
|
|
|
// 清除缓存
|
|
t.clearCache()
|
|
|
|
return t.parseOrderResult(result)
|
|
}
|
|
|
|
// CloseLong 平多仓
|
|
func (t *BybitTrader) CloseLong(symbol string, quantity float64) (map[string]interface{}, error) {
|
|
// 如果 quantity = 0,获取当前持仓数量
|
|
if quantity == 0 {
|
|
positions, err := t.GetPositions()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, pos := range positions {
|
|
if pos["symbol"] == symbol && pos["side"] == "LONG" {
|
|
quantity = pos["positionAmt"].(float64)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if quantity <= 0 {
|
|
return nil, fmt.Errorf("没有多仓可平")
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": "Sell", // 平多用 Sell
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"positionIdx": 0,
|
|
"reduceOnly": true,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Bybit 平多失败: %w", err)
|
|
}
|
|
|
|
// 清除缓存
|
|
t.clearCache()
|
|
|
|
return t.parseOrderResult(result)
|
|
}
|
|
|
|
// CloseShort 平空仓
|
|
func (t *BybitTrader) CloseShort(symbol string, quantity float64) (map[string]interface{}, error) {
|
|
// 如果 quantity = 0,获取当前持仓数量
|
|
if quantity == 0 {
|
|
positions, err := t.GetPositions()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, pos := range positions {
|
|
if pos["symbol"] == symbol && pos["side"] == "SHORT" {
|
|
quantity = -pos["positionAmt"].(float64) // 空仓是负数
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if quantity <= 0 {
|
|
return nil, fmt.Errorf("没有空仓可平")
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": "Buy", // 平空用 Buy
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"positionIdx": 0,
|
|
"reduceOnly": true,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Bybit 平空失败: %w", err)
|
|
}
|
|
|
|
// 清除缓存
|
|
t.clearCache()
|
|
|
|
return t.parseOrderResult(result)
|
|
}
|
|
|
|
// SetLeverage 设置杠杆
|
|
func (t *BybitTrader) SetLeverage(symbol string, leverage int) error {
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"buyLeverage": fmt.Sprintf("%d", leverage),
|
|
"sellLeverage": fmt.Sprintf("%d", leverage),
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).SetPositionLeverage(context.Background())
|
|
if err != nil {
|
|
// 如果杠杆已经是目标值,Bybit 会返回错误,忽略这种情况
|
|
if strings.Contains(err.Error(), "leverage not modified") {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("设置杠杆失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 && result.RetCode != 110043 { // 110043 = leverage not modified
|
|
return fmt.Errorf("设置杠杆失败: %s", result.RetMsg)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetMarginMode 设置仓位模式
|
|
func (t *BybitTrader) SetMarginMode(symbol string, isCrossMargin bool) error {
|
|
tradeMode := 1 // 逐仓
|
|
if isCrossMargin {
|
|
tradeMode = 0 // 全仓
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"tradeMode": tradeMode,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).SwitchPositionMargin(context.Background())
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "Cross/isolated margin mode is not modified") {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("设置保证金模式失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 && result.RetCode != 110026 { // already in target mode
|
|
return fmt.Errorf("设置保证金模式失败: %s", result.RetMsg)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetMarketPrice 获取市场价格
|
|
func (t *BybitTrader) GetMarketPrice(symbol string) (float64, error) {
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).GetMarketTickers(context.Background())
|
|
if err != nil {
|
|
return 0, fmt.Errorf("获取市场价格失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return 0, fmt.Errorf("API 错误: %s", result.RetMsg)
|
|
}
|
|
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return 0, fmt.Errorf("返回格式错误")
|
|
}
|
|
|
|
list, _ := resultData["list"].([]interface{})
|
|
|
|
if len(list) == 0 {
|
|
return 0, fmt.Errorf("未找到 %s 的价格数据", symbol)
|
|
}
|
|
|
|
ticker, _ := list[0].(map[string]interface{})
|
|
lastPriceStr, _ := ticker["lastPrice"].(string)
|
|
lastPrice, err := strconv.ParseFloat(lastPriceStr, 64)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("解析价格失败: %w", err)
|
|
}
|
|
|
|
return lastPrice, nil
|
|
}
|
|
|
|
// SetStopLoss 设置止损单
|
|
func (t *BybitTrader) SetStopLoss(symbol string, positionSide string, quantity, stopPrice float64) error {
|
|
side := "Sell" // LONG 止损用 Sell
|
|
if positionSide == "SHORT" {
|
|
side = "Buy" // SHORT 止损用 Buy
|
|
}
|
|
|
|
// 获取当前价格来确定 triggerDirection
|
|
currentPrice, err := t.GetMarketPrice(symbol)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
triggerDirection := 2 // 价格下跌触发(默认多单止损)
|
|
if stopPrice > currentPrice {
|
|
triggerDirection = 1 // 价格上涨触发(空单止损)
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": side,
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"triggerPrice": fmt.Sprintf("%v", stopPrice),
|
|
"triggerDirection": triggerDirection,
|
|
"triggerBy": "LastPrice",
|
|
"reduceOnly": true,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("设置止损失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return fmt.Errorf("设置止损失败: %s", result.RetMsg)
|
|
}
|
|
|
|
logger.Infof(" ✓ [Bybit] 止损单已设置: %s @ %.2f", symbol, stopPrice)
|
|
return nil
|
|
}
|
|
|
|
// SetTakeProfit 设置止盈单
|
|
func (t *BybitTrader) SetTakeProfit(symbol string, positionSide string, quantity, takeProfitPrice float64) error {
|
|
side := "Sell" // LONG 止盈用 Sell
|
|
if positionSide == "SHORT" {
|
|
side = "Buy" // SHORT 止盈用 Buy
|
|
}
|
|
|
|
// 获取当前价格来确定 triggerDirection
|
|
currentPrice, err := t.GetMarketPrice(symbol)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
triggerDirection := 1 // 价格上涨触发(默认多单止盈)
|
|
if takeProfitPrice < currentPrice {
|
|
triggerDirection = 2 // 价格下跌触发(空单止盈)
|
|
}
|
|
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"side": side,
|
|
"orderType": "Market",
|
|
"qty": fmt.Sprintf("%v", quantity),
|
|
"triggerPrice": fmt.Sprintf("%v", takeProfitPrice),
|
|
"triggerDirection": triggerDirection,
|
|
"triggerBy": "LastPrice",
|
|
"reduceOnly": true,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).PlaceOrder(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("设置止盈失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return fmt.Errorf("设置止盈失败: %s", result.RetMsg)
|
|
}
|
|
|
|
logger.Infof(" ✓ [Bybit] 止盈单已设置: %s @ %.2f", symbol, takeProfitPrice)
|
|
return nil
|
|
}
|
|
|
|
// CancelStopLossOrders 取消止损单
|
|
func (t *BybitTrader) CancelStopLossOrders(symbol string) error {
|
|
return t.cancelConditionalOrders(symbol, "StopLoss")
|
|
}
|
|
|
|
// CancelTakeProfitOrders 取消止盈单
|
|
func (t *BybitTrader) CancelTakeProfitOrders(symbol string) error {
|
|
return t.cancelConditionalOrders(symbol, "TakeProfit")
|
|
}
|
|
|
|
// CancelAllOrders 取消所有挂单
|
|
func (t *BybitTrader) CancelAllOrders(symbol string) error {
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
}
|
|
|
|
_, err := t.client.NewUtaBybitServiceWithParams(params).CancelAllOrders(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("取消所有订单失败: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CancelStopOrders 取消所有止盈止损单
|
|
func (t *BybitTrader) CancelStopOrders(symbol string) error {
|
|
if err := t.CancelStopLossOrders(symbol); err != nil {
|
|
logger.Infof("⚠️ [Bybit] 取消止损单失败: %v", err)
|
|
}
|
|
if err := t.CancelTakeProfitOrders(symbol); err != nil {
|
|
logger.Infof("⚠️ [Bybit] 取消止盈单失败: %v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// FormatQuantity 格式化数量
|
|
func (t *BybitTrader) FormatQuantity(symbol string, quantity float64) (string, error) {
|
|
// Bybit 通常使用 3 位小数
|
|
return fmt.Sprintf("%.3f", quantity), nil
|
|
}
|
|
|
|
// 辅助方法
|
|
|
|
func (t *BybitTrader) clearCache() {
|
|
t.balanceCacheMutex.Lock()
|
|
t.cachedBalance = nil
|
|
t.balanceCacheMutex.Unlock()
|
|
|
|
t.positionsCacheMutex.Lock()
|
|
t.cachedPositions = nil
|
|
t.positionsCacheMutex.Unlock()
|
|
}
|
|
|
|
func (t *BybitTrader) parseOrderResult(result *bybit.ServerResponse) (map[string]interface{}, error) {
|
|
if result.RetCode != 0 {
|
|
return nil, fmt.Errorf("下单失败: %s", result.RetMsg)
|
|
}
|
|
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return nil, fmt.Errorf("返回格式错误")
|
|
}
|
|
|
|
orderId, _ := resultData["orderId"].(string)
|
|
|
|
return map[string]interface{}{
|
|
"orderId": orderId,
|
|
"status": "NEW",
|
|
}, nil
|
|
}
|
|
|
|
// GetOrderStatus 获取订单状态
|
|
func (t *BybitTrader) GetOrderStatus(symbol string, orderID string) (map[string]interface{}, error) {
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"orderId": orderID,
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).GetOrderHistory(context.Background())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取订单状态失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return nil, fmt.Errorf("API 错误: %s", result.RetMsg)
|
|
}
|
|
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return nil, fmt.Errorf("返回格式错误")
|
|
}
|
|
|
|
list, _ := resultData["list"].([]interface{})
|
|
if len(list) == 0 {
|
|
return nil, fmt.Errorf("未找到订单 %s", orderID)
|
|
}
|
|
|
|
order, _ := list[0].(map[string]interface{})
|
|
|
|
// 解析订单数据
|
|
status, _ := order["orderStatus"].(string)
|
|
avgPriceStr, _ := order["avgPrice"].(string)
|
|
cumExecQtyStr, _ := order["cumExecQty"].(string)
|
|
cumExecFeeStr, _ := order["cumExecFee"].(string)
|
|
|
|
avgPrice, _ := strconv.ParseFloat(avgPriceStr, 64)
|
|
executedQty, _ := strconv.ParseFloat(cumExecQtyStr, 64)
|
|
commission, _ := strconv.ParseFloat(cumExecFeeStr, 64)
|
|
|
|
// 转换状态为统一格式
|
|
unifiedStatus := status
|
|
switch status {
|
|
case "Filled":
|
|
unifiedStatus = "FILLED"
|
|
case "New", "Created":
|
|
unifiedStatus = "NEW"
|
|
case "Cancelled", "Rejected":
|
|
unifiedStatus = "CANCELED"
|
|
case "PartiallyFilled":
|
|
unifiedStatus = "PARTIALLY_FILLED"
|
|
}
|
|
|
|
return map[string]interface{}{
|
|
"orderId": orderID,
|
|
"status": unifiedStatus,
|
|
"avgPrice": avgPrice,
|
|
"executedQty": executedQty,
|
|
"commission": commission,
|
|
}, nil
|
|
}
|
|
|
|
func (t *BybitTrader) cancelConditionalOrders(symbol string, orderType string) error {
|
|
// 先获取所有条件单
|
|
params := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"orderFilter": "StopOrder", // 条件单
|
|
}
|
|
|
|
result, err := t.client.NewUtaBybitServiceWithParams(params).GetOpenOrders(context.Background())
|
|
if err != nil {
|
|
return fmt.Errorf("获取条件单失败: %w", err)
|
|
}
|
|
|
|
if result.RetCode != 0 {
|
|
return nil // 没有订单
|
|
}
|
|
|
|
resultData, ok := result.Result.(map[string]interface{})
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
list, _ := resultData["list"].([]interface{})
|
|
|
|
// 取消匹配的订单
|
|
for _, item := range list {
|
|
order, ok := item.(map[string]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
orderId, _ := order["orderId"].(string)
|
|
stopOrderType, _ := order["stopOrderType"].(string)
|
|
|
|
// 根据类型筛选
|
|
shouldCancel := false
|
|
if orderType == "StopLoss" && (stopOrderType == "StopLoss" || stopOrderType == "Stop") {
|
|
shouldCancel = true
|
|
}
|
|
if orderType == "TakeProfit" && (stopOrderType == "TakeProfit" || stopOrderType == "PartialTakeProfit") {
|
|
shouldCancel = true
|
|
}
|
|
|
|
if shouldCancel && orderId != "" {
|
|
cancelParams := map[string]interface{}{
|
|
"category": "linear",
|
|
"symbol": symbol,
|
|
"orderId": orderId,
|
|
}
|
|
t.client.NewUtaBybitServiceWithParams(cancelParams).CancelOrder(context.Background())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|