add binance single ticker query method and fix quantity formating

This commit is contained in:
c9s 2021-02-18 16:17:40 +08:00
parent e71a81684a
commit 29bbd03836
2 changed files with 43 additions and 4 deletions

View File

@ -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:

View File

@ -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 {