feat: add AI grid trading and market regime classification

- Add GridTrader interface with PlaceLimitOrder, CancelOrder, GetOrderBook
- Implement GridTrader for all exchanges (Binance, Bybit, OKX, Bitget, Hyperliquid, Aster, Lighter)
- Add grid engine with ATR-based boundary calculation and fund distribution
- Add market regime classification documents (Chinese/English)
- Add GridConfigEditor component for frontend configuration
This commit is contained in:
tinkle-community
2026-01-13 10:33:02 +08:00
parent 705aa641b0
commit 5fb26c17dc
22 changed files with 3901 additions and 32 deletions

View File

@@ -112,3 +112,95 @@ type OpenOrder struct {
Quantity float64 `json:"quantity"`
Status string `json:"status"` // NEW
}
// LimitOrderRequest represents a limit order request for grid trading
type LimitOrderRequest struct {
Symbol string `json:"symbol"`
Side string `json:"side"` // BUY/SELL
PositionSide string `json:"position_side"` // LONG/SHORT (for hedge mode)
Price float64 `json:"price"` // Limit price
Quantity float64 `json:"quantity"`
Leverage int `json:"leverage"`
PostOnly bool `json:"post_only"` // Maker only order
ReduceOnly bool `json:"reduce_only"` // Reduce position only
ClientID string `json:"client_id"` // Client order ID for tracking
}
// LimitOrderResult represents the result of placing a limit order
type LimitOrderResult struct {
OrderID string `json:"order_id"`
ClientID string `json:"client_id"`
Symbol string `json:"symbol"`
Side string `json:"side"`
PositionSide string `json:"position_side"`
Price float64 `json:"price"`
Quantity float64 `json:"quantity"`
Status string `json:"status"` // NEW, PARTIALLY_FILLED, FILLED, CANCELED
}
// GridTrader extends Trader interface with limit order support for grid trading
// Exchanges that support grid trading should implement this interface
type GridTrader interface {
Trader
// PlaceLimitOrder places a limit order at specified price
// Returns order ID and status
PlaceLimitOrder(req *LimitOrderRequest) (*LimitOrderResult, error)
// CancelOrder cancels a specific order by ID
CancelOrder(symbol, orderID string) error
// GetOrderBook gets current order book (for price validation)
// Returns best bid/ask prices
GetOrderBook(symbol string, depth int) (bids, asks [][]float64, err error)
}
// GridTraderAdapter wraps a basic Trader to provide GridTrader interface
// Uses stop orders as a fallback when limit orders aren't directly available
type GridTraderAdapter struct {
Trader
}
// NewGridTraderAdapter creates an adapter for basic Trader
func NewGridTraderAdapter(t Trader) *GridTraderAdapter {
return &GridTraderAdapter{Trader: t}
}
// PlaceLimitOrder implements limit order using available methods
// For exchanges without native limit order support, this uses conditional orders
func (a *GridTraderAdapter) PlaceLimitOrder(req *LimitOrderRequest) (*LimitOrderResult, error) {
// Use SetStopLoss/SetTakeProfit as conditional limit orders
// For buy orders below current price, use stop-loss mechanism
// For sell orders above current price, use take-profit mechanism
var err error
if req.Side == "BUY" {
err = a.Trader.SetStopLoss(req.Symbol, "SHORT", req.Quantity, req.Price)
} else {
err = a.Trader.SetTakeProfit(req.Symbol, "LONG", req.Quantity, req.Price)
}
if err != nil {
return nil, err
}
return &LimitOrderResult{
OrderID: req.ClientID,
ClientID: req.ClientID,
Symbol: req.Symbol,
Side: req.Side,
PositionSide: req.PositionSide,
Price: req.Price,
Quantity: req.Quantity,
Status: "NEW",
}, nil
}
// CancelOrder cancels a specific order
func (a *GridTraderAdapter) CancelOrder(symbol, orderID string) error {
// Fallback: cancel all orders for the symbol
return a.Trader.CancelAllOrders(symbol)
}
// GetOrderBook returns empty order book (not supported in basic Trader)
func (a *GridTraderAdapter) GetOrderBook(symbol string, depth int) (bids, asks [][]float64, err error) {
// Not supported, return empty
return nil, nil, nil
}