From aa7aa942757d9f24606d6554bb87f5fa87a47222 Mon Sep 17 00:00:00 2001 From: tinkle-community Date: Tue, 13 Jan 2026 13:38:09 +0800 Subject: [PATCH] fix: address code review issues for GetOpenOrders - Add error logging for OKX/Bitget API failures (was silently swallowed) - Fix Lighter position side logic to handle reduce-only orders - Change verbose debug logs from Infof to Debugf level --- trader/bitget_trader.go | 10 ++++++++-- trader/lighter_trader_v2_orders.go | 4 ++-- trader/lighter_trader_v2_trading.go | 16 +++++++++++++--- trader/okx_trader.go | 10 ++++++++-- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/trader/bitget_trader.go b/trader/bitget_trader.go index bce15a00..8990df52 100644 --- a/trader/bitget_trader.go +++ b/trader/bitget_trader.go @@ -1109,7 +1109,10 @@ func (t *BitgetTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) { } data, err := t.doRequest("GET", bitgetPendingPath, params) - if err == nil { + if err != nil { + logger.Warnf("[Bitget] Failed to get pending orders: %v", err) + } + if err == nil && data != nil { var orders struct { EntrustedList []struct { OrderId string `json:"orderId"` @@ -1154,7 +1157,10 @@ func (t *BitgetTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) { } planData, err := t.doRequest("GET", "/api/v2/mix/order/orders-plan-pending", planParams) - if err == nil { + if err != nil { + logger.Warnf("[Bitget] Failed to get plan orders: %v", err) + } + if err == nil && planData != nil { var planOrders struct { EntrustedList []struct { OrderId string `json:"orderId"` diff --git a/trader/lighter_trader_v2_orders.go b/trader/lighter_trader_v2_orders.go index e89a99ac..629ffbd1 100644 --- a/trader/lighter_trader_v2_orders.go +++ b/trader/lighter_trader_v2_orders.go @@ -214,7 +214,7 @@ func (t *LighterTraderV2) GetActiveOrders(symbol string) ([]OrderResponse, error endpoint := fmt.Sprintf("%s/api/v1/accountActiveOrders?account_index=%d&market_id=%d", t.baseURL, t.accountIndex, marketIndex) - logger.Infof("📋 LIGHTER GetActiveOrders: endpoint=%s", endpoint) + logger.Debugf("📋 LIGHTER GetActiveOrders: endpoint=%s", endpoint) // Send GET request req, err := http.NewRequest("GET", endpoint, nil) @@ -237,7 +237,7 @@ func (t *LighterTraderV2) GetActiveOrders(symbol string) ([]OrderResponse, error return nil, fmt.Errorf("failed to read response: %w", err) } - logger.Infof("📋 LIGHTER GetActiveOrders raw response: %s", string(body)) + logger.Debugf("📋 LIGHTER GetActiveOrders raw response: %s", string(body)) // Parse response - Lighter API uses "orders" field, not "data" var apiResp struct { diff --git a/trader/lighter_trader_v2_trading.go b/trader/lighter_trader_v2_trading.go index 88d31b7e..c4e9b2be 100644 --- a/trader/lighter_trader_v2_trading.go +++ b/trader/lighter_trader_v2_trading.go @@ -713,10 +713,20 @@ func (t *LighterTraderV2) GetOpenOrders(symbol string) ([]OpenOrder, error) { orderType = "TAKE_PROFIT_MARKET" } - // Determine position side based on order direction + // Determine position side based on order direction and reduce-only flag positionSide := "LONG" - if side == "SELL" { - positionSide = "SHORT" + if order.ReduceOnly { + // For reduce-only orders, position side is opposite to order side + if side == "BUY" { + positionSide = "SHORT" // Buying to close short + } else { + positionSide = "LONG" // Selling to close long + } + } else { + // For opening orders + if side == "SELL" { + positionSide = "SHORT" + } } // Parse price and quantity from string fields diff --git a/trader/okx_trader.go b/trader/okx_trader.go index fda737a6..fc3fae69 100644 --- a/trader/okx_trader.go +++ b/trader/okx_trader.go @@ -1396,7 +1396,10 @@ func (t *OKXTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) { // 1. Get pending limit orders path := fmt.Sprintf("%s?instId=%s&instType=SWAP", okxPendingOrdersPath, instId) data, err := t.doRequest("GET", path, nil) - if err == nil { + if err != nil { + logger.Warnf("[OKX] Failed to get pending orders: %v", err) + } + if err == nil && data != nil { var orders []struct { OrdId string `json:"ordId"` InstId string `json:"instId"` @@ -1437,7 +1440,10 @@ func (t *OKXTrader) GetOpenOrders(symbol string) ([]OpenOrder, error) { // 2. Get pending algo orders (stop-loss/take-profit) algoPath := fmt.Sprintf("%s?instId=%s&instType=SWAP", okxAlgoPendingPath, instId) algoData, err := t.doRequest("GET", algoPath, nil) - if err == nil { + if err != nil { + logger.Warnf("[OKX] Failed to get algo orders: %v", err) + } + if err == nil && algoData != nil { var algoOrders []struct { AlgoId string `json:"algoId"` InstId string `json:"instId"`