improve trade parsing error

This commit is contained in:
c9s 2020-11-30 18:29:22 +08:00
parent d226ec2e01
commit 4cf5929cac

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/adshao/go-binance" "github.com/adshao/go-binance"
"github.com/pkg/errors"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
@ -29,7 +30,7 @@ func toLocalOrderType(orderType types.OrderType) (binance.OrderType, error) {
return "", fmt.Errorf("order type %s not supported", orderType) return "", fmt.Errorf("order type %s not supported", orderType)
} }
func toGlobalOrder(binanceOrder *binance.Order) (*types.Order, error) { func ToGlobalOrder(binanceOrder *binance.Order) (*types.Order, error) {
return &types.Order{ return &types.Order{
SubmitOrder: types.SubmitOrder{ SubmitOrder: types.SubmitOrder{
ClientOrderID: binanceOrder.ClientOrderID, ClientOrderID: binanceOrder.ClientOrderID,
@ -54,7 +55,7 @@ func millisecondTime(t int64) time.Time {
return time.Unix(0, t*int64(time.Millisecond)) return time.Unix(0, t*int64(time.Millisecond))
} }
func toGlobalTrade(t binance.TradeV3) (*types.Trade, error) { func ToGlobalTrade(t binance.TradeV3) (*types.Trade, error) {
// skip trade ID that is the same. however this should not happen // skip trade ID that is the same. however this should not happen
var side types.SideType var side types.SideType
if t.IsBuyer { if t.IsBuyer {
@ -65,22 +66,25 @@ func toGlobalTrade(t binance.TradeV3) (*types.Trade, error) {
price, err := strconv.ParseFloat(t.Price, 64) price, err := strconv.ParseFloat(t.Price, 64)
if err != nil { if err != nil {
return nil, err return nil, errors.Wrapf(err, "price parse error, price: %+v", t.Price)
} }
quantity, err := strconv.ParseFloat(t.Quantity, 64) quantity, err := strconv.ParseFloat(t.Quantity, 64)
if err != nil { if err != nil {
return nil, err return nil, errors.Wrapf(err, "quantity parse error, quantity: %+v", t.Quantity)
} }
quoteQuantity, err := strconv.ParseFloat(t.QuoteQuantity, 64) var quoteQuantity = 0.0
if err != nil { if len(t.QuoteQuantity) > 0 {
return nil, err quoteQuantity, err = strconv.ParseFloat(t.QuoteQuantity, 64)
if err != nil {
return nil, errors.Wrapf(err, "quote quantity parse error, quoteQuantity: %+v", t.QuoteQuantity)
}
} }
fee, err := strconv.ParseFloat(t.Commission, 64) fee, err := strconv.ParseFloat(t.Commission, 64)
if err != nil { if err != nil {
return nil, err return nil, errors.Wrapf(err, "commission parse error, commission: %+v", t.Commission)
} }
return &types.Trade{ return &types.Trade{
@ -155,3 +159,17 @@ func toGlobalOrderStatus(orderStatus binance.OrderStatusType) types.OrderStatus
return types.OrderStatus(orderStatus) return types.OrderStatus(orderStatus)
} }
// ConvertTrades converts the binance v3 trade into the global trade type
func ConvertTrades(remoteTrades []*binance.TradeV3) (trades []types.Trade, err error) {
for _, t := range remoteTrades {
trade, err := ToGlobalTrade(*t)
if err != nil {
return nil, errors.Wrapf(err, "binance v3 trade parse error, trade: %+v", *t)
}
trades = append(trades, *trade)
}
return trades, err
}