feat: upgrade Binance to Algo Order API and improve trading flow

- Upgrade go-binance to v2.8.9 with new Algo Order API
- Migrate SetStopLoss/SetTakeProfit to use AlgoOrderTypeStopMarket/TakeProfitMarket
- Update cancel functions to handle both legacy and Algo orders
- Fix Lighter stop orders using correct order types (type=2/4) with TriggerPrice
- Add CancelAllOrders before opening positions for Bybit and Lighter
- Fix decision limit selector in API handler
- Add stop_loss/take_profit/confidence fields to DecisionAction
- Store decisions array in database with proper serialization
- Redesign DecisionCard with beautiful entry/SL/TP display
This commit is contained in:
tinkle-community
2025-12-15 21:22:22 +08:00
parent aeede956e6
commit 3f084005e4
12 changed files with 699 additions and 270 deletions

View File

@@ -14,44 +14,46 @@ import (
)
// SetStopLoss Set stop-loss order (implements Trader interface)
// IMPORTANT: Uses StopLossOrder type (type=2) with TriggerPrice, NOT regular limit order
func (t *LighterTraderV2) SetStopLoss(symbol string, positionSide string, quantity, stopPrice float64) error {
if t.txClient == nil {
return fmt.Errorf("TxClient not initialized")
}
logger.Infof("🛑 LIGHTER Setting stop-loss: %s %s qty=%.4f, stop=%.2f", symbol, positionSide, quantity, stopPrice)
logger.Infof("🛑 LIGHTER Setting stop-loss: %s %s qty=%.4f, trigger=%.2f", symbol, positionSide, quantity, stopPrice)
// Determine order direction (short position uses buy order, long position uses sell order)
// Determine order direction (long position uses sell order, short position uses buy order)
isAsk := (positionSide == "LONG" || positionSide == "long")
// Create limit stop-loss order
_, err := t.CreateOrder(symbol, isAsk, quantity, stopPrice, "limit")
// Create stop-loss order with TriggerPrice (type=2: StopLossOrder)
_, err := t.CreateStopOrder(symbol, isAsk, quantity, stopPrice, "stop_loss")
if err != nil {
return fmt.Errorf("failed to set stop-loss: %w", err)
}
logger.Infof("✓ LIGHTER stop-loss set: %.2f", stopPrice)
logger.Infof("✓ LIGHTER stop-loss set: trigger=%.2f", stopPrice)
return nil
}
// SetTakeProfit Set take-profit order (implements Trader interface)
// IMPORTANT: Uses TakeProfitOrder type (type=4) with TriggerPrice, NOT regular limit order
func (t *LighterTraderV2) SetTakeProfit(symbol string, positionSide string, quantity, takeProfitPrice float64) error {
if t.txClient == nil {
return fmt.Errorf("TxClient not initialized")
}
logger.Infof("🎯 LIGHTER Setting take-profit: %s %s qty=%.4f, tp=%.2f", symbol, positionSide, quantity, takeProfitPrice)
logger.Infof("🎯 LIGHTER Setting take-profit: %s %s qty=%.4f, trigger=%.2f", symbol, positionSide, quantity, takeProfitPrice)
// Determine order direction (short position uses buy order, long position uses sell order)
// Determine order direction (long position uses sell order, short position uses buy order)
isAsk := (positionSide == "LONG" || positionSide == "long")
// Create limit take-profit order
_, err := t.CreateOrder(symbol, isAsk, quantity, takeProfitPrice, "limit")
// Create take-profit order with TriggerPrice (type=4: TakeProfitOrder)
_, err := t.CreateStopOrder(symbol, isAsk, quantity, takeProfitPrice, "take_profit")
if err != nil {
return fmt.Errorf("failed to set take-profit: %w", err)
}
logger.Infof("✓ LIGHTER take-profit set: %.2f", takeProfitPrice)
logger.Infof("✓ LIGHTER take-profit set: trigger=%.2f", takeProfitPrice)
return nil
}