binance: fix futures/margin order sync issue

fixes: #887
This commit is contained in:
c9s 2022-08-19 15:28:03 +08:00
parent bcd524361d
commit 8827fc3ec6
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 16 additions and 17 deletions

View File

@ -141,9 +141,9 @@ func toLocalOrderType(orderType types.OrderType) (binance.OrderType, error) {
return "", fmt.Errorf("can not convert to local order, order type %s not supported", orderType)
}
func toGlobalOrders(binanceOrders []*binance.Order) (orders []types.Order, err error) {
func toGlobalOrders(binanceOrders []*binance.Order, isMargin bool) (orders []types.Order, err error) {
for _, binanceOrder := range binanceOrders {
order, err := toGlobalOrder(binanceOrder, false)
order, err := toGlobalOrder(binanceOrder, isMargin)
if err != nil {
return orders, err
}

View File

@ -99,9 +99,9 @@ func toLocalFuturesOrderType(orderType types.OrderType) (futures.OrderType, erro
return "", fmt.Errorf("can not convert to local order, order type %s not supported", orderType)
}
func toGlobalFuturesOrders(futuresOrders []*futures.Order) (orders []types.Order, err error) {
func toGlobalFuturesOrders(futuresOrders []*futures.Order, isIsolated bool) (orders []types.Order, err error) {
for _, futuresOrder := range futuresOrders {
order, err := toGlobalFuturesOrder(futuresOrder, false)
order, err := toGlobalFuturesOrder(futuresOrder, isIsolated)
if err != nil {
return orders, err
}
@ -112,7 +112,7 @@ func toGlobalFuturesOrders(futuresOrders []*futures.Order) (orders []types.Order
return orders, err
}
func toGlobalFuturesOrder(futuresOrder *futures.Order, isMargin bool) (*types.Order, error) {
func toGlobalFuturesOrder(futuresOrder *futures.Order, isIsolated bool) (*types.Order, error) {
return &types.Order{
SubmitOrder: types.SubmitOrder{
ClientOrderID: futuresOrder.ClientOrderID,
@ -131,7 +131,7 @@ func toGlobalFuturesOrder(futuresOrder *futures.Order, isMargin bool) (*types.Or
ExecutedQuantity: fixedpoint.MustNewFromString(futuresOrder.ExecutedQuantity),
CreationTime: types.Time(millisecondTime(futuresOrder.Time)),
UpdateTime: types.Time(millisecondTime(futuresOrder.UpdateTime)),
IsMargin: isMargin,
IsFutures: true,
}, nil
}

View File

@ -3,13 +3,14 @@ package binance
import (
"context"
"fmt"
"github.com/adshao/go-binance/v2"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/adshao/go-binance/v2"
"github.com/adshao/go-binance/v2/futures"
"github.com/spf13/viper"
@ -696,7 +697,7 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
return orders, err
}
return toGlobalOrders(binanceOrders)
return toGlobalOrders(binanceOrders, false)
}
if e.IsFutures {
@ -707,7 +708,7 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
return orders, err
}
return toGlobalFuturesOrders(binanceOrders)
return toGlobalFuturesOrders(binanceOrders, false)
}
binanceOrders, err := e.client.NewListOpenOrdersService().Symbol(symbol).Do(ctx)
@ -715,7 +716,7 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
return orders, err
}
return toGlobalOrders(binanceOrders)
return toGlobalOrders(binanceOrders, false)
}
func (e *Exchange) QueryOrderTrades(ctx context.Context, q types.OrderQuery) ([]types.Trade, error) {
@ -802,7 +803,7 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
return orders, err
}
return toGlobalOrders(binanceOrders)
return toGlobalOrders(binanceOrders, e.IsMargin)
}
if e.IsFutures {
@ -821,7 +822,7 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
if err != nil {
return orders, err
}
return toGlobalFuturesOrders(binanceOrders)
return toGlobalFuturesOrders(binanceOrders, false)
}
// If orderId is set, it will get orders >= that orderId. Otherwise most recent orders are returned.
@ -847,7 +848,7 @@ func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since,
return orders, err
}
return toGlobalOrders(binanceOrders)
return toGlobalOrders(binanceOrders, e.IsMargin)
}
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err error) {
@ -1102,7 +1103,7 @@ func (e *Exchange) submitFuturesOrder(ctx context.Context, order types.SubmitOrd
Type: response.Type,
Side: response.Side,
ReduceOnly: response.ReduceOnly,
}, true)
}, false)
return createdOrder, err
}

View File

@ -121,7 +121,6 @@ func (e *ExecutionReportEvent) Order() (*types.Order, error) {
Price: e.OrderPrice,
StopPrice: e.StopPrice,
TimeInForce: types.TimeInForce(e.TimeInForce),
IsFutures: false,
ReduceOnly: false,
ClosePosition: false,
},

View File

@ -132,8 +132,6 @@ type SubmitOrder struct {
MarginSideEffect MarginOrderSideEffectType `json:"marginSideEffect,omitempty"` // AUTO_REPAY = repay, MARGIN_BUY = borrow, defaults to NO_SIDE_EFFECT
// futures order fields
IsFutures bool `json:"is_futures" db:"is_futures"`
ReduceOnly bool `json:"reduceOnly" db:"reduce_only"`
ClosePosition bool `json:"closePosition" db:"close_position"`
@ -253,6 +251,7 @@ type Order struct {
CreationTime Time `json:"creationTime" db:"created_at"`
UpdateTime Time `json:"updateTime" db:"updated_at"`
IsFutures bool `json:"isFutures" db:"is_futures"`
IsMargin bool `json:"isMargin" db:"is_margin"`
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
}