diff --git a/pkg/exchange/binance/convert.go b/pkg/exchange/binance/convert.go index 01dbee100..4468630ad 100644 --- a/pkg/exchange/binance/convert.go +++ b/pkg/exchange/binance/convert.go @@ -13,6 +13,19 @@ import ( "github.com/c9s/bbgo/pkg/util" ) +func toGlobalTicker(stats *binance.PriceChangeStats) types.Ticker { + return types.Ticker{ + Volume: util.MustParseFloat(stats.Volume), + Last: util.MustParseFloat(stats.LastPrice), + Open: util.MustParseFloat(stats.OpenPrice), + High: util.MustParseFloat(stats.HighPrice), + Low: util.MustParseFloat(stats.LowPrice), + Buy: util.MustParseFloat(stats.BidPrice), + Sell: util.MustParseFloat(stats.AskPrice), + Time: time.Unix(0, stats.CloseTime*int64(time.Millisecond)), + } +} + func toLocalOrderType(orderType types.OrderType) (binance.OrderType, error) { switch orderType { case types.OrderTypeLimit: diff --git a/pkg/exchange/binance/exchange.go b/pkg/exchange/binance/exchange.go index 69c56eaac..c5c51d2ff 100644 --- a/pkg/exchange/binance/exchange.go +++ b/pkg/exchange/binance/exchange.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "strings" "time" "github.com/adshao/go-binance/v2" @@ -48,6 +49,18 @@ func (e *Exchange) Name() types.ExchangeName { return types.ExchangeBinance } +func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error) { + req := e.Client.NewListPriceChangeStatsService() + req.Symbol(strings.ToUpper(symbol)) + stats, err := req.Do(ctx) + if err != nil { + return nil, err + } + + ticker := toGlobalTicker(stats[0]) + return &ticker, nil +} + func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) { var ret = make(map[string]types.Ticker) listPriceChangeStatsService := e.Client.NewListPriceChangeStatsService() @@ -533,12 +546,25 @@ func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) NewClientOrderID(clientOrderID). Type(orderType) - req.Quantity(order.QuantityString) - - if len(order.PriceString) > 0 { - req.Price(order.PriceString) + if len(order.QuantityString) > 0 { + req.Quantity(order.QuantityString) + } else if order.Market.Symbol != "" { + req.Quantity(order.Market.FormatQuantity(order.Quantity)) + } else { + req.Quantity(strconv.FormatFloat(order.Quantity, 'f', 8, 64)) } + // set price field for limit orders + switch order.Type { + case types.OrderTypeStopLimit, types.OrderTypeLimit: + if len(order.PriceString) > 0 { + req.Price(order.PriceString) + } else if order.Market.Symbol != "" { + req.Price(order.Market.FormatPrice(order.Price)) + } + } + + switch order.Type { case types.OrderTypeStopLimit, types.OrderTypeStopMarket: if len(order.StopPriceString) == 0 {