From cfd68fdd1d0996c9bb7e8c05d06e2c3efa6c3d54 Mon Sep 17 00:00:00 2001 From: c9s Date: Thu, 23 Dec 2021 13:15:27 +0800 Subject: [PATCH] all: change trade id to uint64 --- pkg/backtest/matching.go | 2 +- pkg/bbgo/trade_store.go | 12 ++++++------ pkg/exchange/binance/convert.go | 4 ++-- pkg/exchange/binance/exchange.go | 9 +++++---- pkg/exchange/binance/parse.go | 9 +++++---- pkg/exchange/ftx/exchange.go | 2 +- pkg/exchange/ftx/rest_responses.go | 4 ++-- pkg/exchange/max/convert.go | 2 +- pkg/exchange/max/exchange.go | 2 +- pkg/exchange/max/stream.go | 2 +- pkg/exchange/okex/convert.go | 2 +- pkg/service/trade.go | 2 +- pkg/types/exchange.go | 2 +- pkg/types/trade.go | 4 ++-- 14 files changed, 30 insertions(+), 28 deletions(-) diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 889ede0ca..5df54fb39 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -226,7 +226,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool) var id = incTradeID() return types.Trade{ - ID: int64(id), + ID: id, OrderID: order.OrderID, Exchange: "backtest", Price: order.Price, diff --git a/pkg/bbgo/trade_store.go b/pkg/bbgo/trade_store.go index 7a51ff396..eab6cca31 100644 --- a/pkg/bbgo/trade_store.go +++ b/pkg/bbgo/trade_store.go @@ -10,7 +10,7 @@ type TradeStore struct { // any created trades for tracking trades sync.Mutex - trades map[int64]types.Trade + trades map[uint64]types.Trade Symbol string RemoveCancelled bool @@ -21,7 +21,7 @@ type TradeStore struct { func NewTradeStore(symbol string) *TradeStore { return &TradeStore{ Symbol: symbol, - trades: make(map[int64]types.Trade), + trades: make(map[uint64]types.Trade), } } @@ -43,7 +43,7 @@ func (s *TradeStore) Trades() (trades []types.Trade) { return trades } -func (s *TradeStore) Exists(oID int64) (ok bool) { +func (s *TradeStore) Exists(oID uint64) (ok bool) { s.Lock() defer s.Unlock() @@ -53,7 +53,7 @@ func (s *TradeStore) Exists(oID int64) (ok bool) { func (s *TradeStore) Clear() { s.Lock() - s.trades = make(map[int64]types.Trade) + s.trades = make(map[uint64]types.Trade) s.Unlock() } @@ -61,7 +61,7 @@ type TradeFilter func(trade types.Trade) bool func (s *TradeStore) Filter(filter TradeFilter) { s.Lock() - var trades = make(map[int64]types.Trade) + var trades = make(map[uint64]types.Trade) for _, trade := range s.trades { if !filter(trade) { trades[trade.ID] = trade @@ -76,7 +76,7 @@ func (s *TradeStore) GetAndClear() (trades []types.Trade) { for _, o := range s.trades { trades = append(trades, o) } - s.trades = make(map[int64]types.Trade) + s.trades = make(map[uint64]types.Trade) s.Unlock() return trades diff --git a/pkg/exchange/binance/convert.go b/pkg/exchange/binance/convert.go index b9c44a38a..114cda456 100644 --- a/pkg/exchange/binance/convert.go +++ b/pkg/exchange/binance/convert.go @@ -294,7 +294,7 @@ func toGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error) { } return &types.Trade{ - ID: t.ID, + ID: uint64(t.ID), OrderID: uint64(t.OrderID), Price: price, Symbol: t.Symbol, @@ -347,7 +347,7 @@ func toGlobalFuturesTrade(t futures.AccountTrade) (*types.Trade, error) { } return &types.Trade{ - ID: t.ID, + ID: uint64(t.ID), OrderID: uint64(t.OrderID), Price: price, Symbol: t.Symbol, diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index e0b6e0802..bf92d3be2 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -3,13 +3,14 @@ package binance import ( "context" "fmt" - "github.com/adshao/go-binance/v2/futures" "net/http" "os" "strconv" "strings" "time" + "github.com/adshao/go-binance/v2/futures" + "go.uber.org/multierr" "golang.org/x/time/rate" @@ -961,7 +962,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type // BINANCE uses inclusive last trade ID if options.LastTradeID > 0 { - req.FromID(options.LastTradeID) + req.FromID(int64(options.LastTradeID)) } remoteTrades, err = req.Do(ctx) @@ -992,7 +993,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type // BINANCE uses inclusive last trade ID if options.LastTradeID > 0 { - req.FromID(options.LastTradeID) + req.FromID(int64(options.LastTradeID)) } remoteTrades, err = req.Do(ctx) @@ -1030,7 +1031,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type // BINANCE uses inclusive last trade ID if options.LastTradeID > 0 { - req.FromID(options.LastTradeID) + req.FromID(int64(options.LastTradeID)) } remoteTrades, err = req.Do(ctx) diff --git a/pkg/exchange/binance/parse.go b/pkg/exchange/binance/parse.go index 76aa64be2..3bc104f90 100644 --- a/pkg/exchange/binance/parse.go +++ b/pkg/exchange/binance/parse.go @@ -4,9 +4,10 @@ import ( "encoding/json" "errors" "fmt" - "github.com/adshao/go-binance/v2/futures" "time" + "github.com/adshao/go-binance/v2/futures" + "github.com/adshao/go-binance/v2" "github.com/valyala/fastjson" @@ -87,10 +88,10 @@ type ExecutionReportEvent struct { CurrentOrderStatus string `json:"X"` OrderID int64 `json:"i"` - Ignored int64 `json:"I"` + Ignored int64 `json:"I"` TradeID int64 `json:"t"` - TransactionTime int64 `json:"T"` + TransactionTime int64 `json:"T"` LastExecutedQuantity string `json:"l"` LastExecutedPrice string `json:"L"` @@ -137,7 +138,7 @@ func (e *ExecutionReportEvent) Trade() (*types.Trade, error) { tt := time.Unix(0, e.TransactionTime*int64(time.Millisecond)) return &types.Trade{ - ID: e.TradeID, + ID: uint64(e.TradeID), Exchange: types.ExchangeBinance, Symbol: e.Symbol, OrderID: uint64(e.OrderID), diff --git a/pkg/exchange/ftx/exchange.go b/pkg/exchange/ftx/exchange.go index 0081aaa0c..522e859d7 100644 --- a/pkg/exchange/ftx/exchange.go +++ b/pkg/exchange/ftx/exchange.go @@ -363,7 +363,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type limit = 200 } - tradeIDs := make(map[int64]struct{}) + tradeIDs := make(map[uint64]struct{}) lastTradeID := options.LastTradeID var trades []types.Trade diff --git a/pkg/exchange/ftx/rest_responses.go b/pkg/exchange/ftx/rest_responses.go index bd0395a38..a08612260 100644 --- a/pkg/exchange/ftx/rest_responses.go +++ b/pkg/exchange/ftx/rest_responses.go @@ -8,7 +8,7 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -//ex: 2019-03-05T09:56:55.728933+00:00 +// ex: 2019-03-05T09:56:55.728933+00:00 const timeLayout = "2006-01-02T15:04:05.999999Z07:00" type datetime struct { @@ -364,7 +364,7 @@ type fill struct { Size float64 `json:"size"` OrderId uint64 `json:"orderId"` Time datetime `json:"time"` - TradeId int64 `json:"tradeId"` + TradeId uint64 `json:"tradeId"` FeeRate float64 `json:"feeRate"` Fee float64 `json:"fee"` FeeCurrency string `json:"feeCurrency"` diff --git a/pkg/exchange/max/convert.go b/pkg/exchange/max/convert.go index 3c9f6a474..dd3656b93 100644 --- a/pkg/exchange/max/convert.go +++ b/pkg/exchange/max/convert.go @@ -233,7 +233,7 @@ func toGlobalTrade(t max.Trade) (*types.Trade, error) { } return &types.Trade{ - ID: int64(t.ID), + ID: t.ID, OrderID: t.OrderID, Price: price, Symbol: toGlobalSymbol(t.Market), diff --git a/pkg/exchange/max/exchange.go b/pkg/exchange/max/exchange.go index a397f16ee..265772d03 100644 --- a/pkg/exchange/max/exchange.go +++ b/pkg/exchange/max/exchange.go @@ -744,7 +744,7 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type // MAX uses exclusive last trade ID if options.LastTradeID > 0 { - req.From(options.LastTradeID) + req.From(int64(options.LastTradeID)) } // make it compatible with binance, we need the last trade id for the next page. diff --git a/pkg/exchange/max/stream.go b/pkg/exchange/max/stream.go index 0845a5083..c2a36ee7f 100644 --- a/pkg/exchange/max/stream.go +++ b/pkg/exchange/max/stream.go @@ -213,7 +213,7 @@ func convertWebSocketTrade(t max.TradeUpdate) (*types.Trade, error) { } return &types.Trade{ - ID: int64(t.ID), + ID: t.ID, OrderID: t.OrderID, Symbol: toGlobalSymbol(t.Market), Exchange: types.ExchangeMax, diff --git a/pkg/exchange/okex/convert.go b/pkg/exchange/okex/convert.go index d6f76abae..35b530fe6 100644 --- a/pkg/exchange/okex/convert.go +++ b/pkg/exchange/okex/convert.go @@ -136,7 +136,7 @@ func toGlobalTrades(orderDetails []okexapi.OrderDetails) ([]types.Trade, error) side := types.SideType(strings.ToUpper(string(orderDetail.Side))) trades = append(trades, types.Trade{ - ID: tradeID, + ID: uint64(tradeID), OrderID: uint64(orderID), Exchange: types.ExchangeOKEx, Price: orderDetail.LastFilledPrice.Float64(), diff --git a/pkg/service/trade.go b/pkg/service/trade.go index 45ad9049c..9268d940b 100644 --- a/pkg/service/trade.go +++ b/pkg/service/trade.go @@ -81,7 +81,7 @@ func (s *TradeService) Sync(ctx context.Context, exchange types.Exchange, symbol } var tradeKeys = map[types.TradeKey]struct{}{} - var lastTradeID int64 = 1 + var lastTradeID uint64 = 1 if len(records) > 0 { for _, record := range records { tradeKeys[record.Key()] = struct{}{} diff --git a/pkg/types/exchange.go b/pkg/types/exchange.go index 33eb709b5..acd03f756 100644 --- a/pkg/types/exchange.go +++ b/pkg/types/exchange.go @@ -126,5 +126,5 @@ type TradeQueryOptions struct { StartTime *time.Time EndTime *time.Time Limit int64 - LastTradeID int64 + LastTradeID uint64 } diff --git a/pkg/types/trade.go b/pkg/types/trade.go index e86a83fdb..7f423570e 100644 --- a/pkg/types/trade.go +++ b/pkg/types/trade.go @@ -50,7 +50,7 @@ type Trade struct { GID int64 `json:"gid" db:"gid"` // ID is the source trade ID - ID int64 `json:"id" db:"id"` + ID uint64 `json:"id" db:"id"` OrderID uint64 `json:"orderID" db:"order_id"` Exchange ExchangeName `json:"exchange" db:"exchange"` Price float64 `json:"price" db:"price"` @@ -143,6 +143,6 @@ func (trade Trade) Key() TradeKey { } type TradeKey struct { - ID int64 + ID uint64 Side SideType }