2020-07-11 05:02:53 +00:00
|
|
|
package binance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-11 07:27:26 +00:00
|
|
|
"fmt"
|
2020-07-11 07:19:36 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
"github.com/adshao/go-binance"
|
2020-07-11 07:19:36 +00:00
|
|
|
|
2020-07-11 07:18:31 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/bbgo/types"
|
2020-07-11 05:08:50 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-07-11 05:02:53 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Exchange struct {
|
|
|
|
Client *binance.Client
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:08:50 +00:00
|
|
|
func NewExchange(key, secret string) *Exchange {
|
|
|
|
var client = binance.NewClient(key, secret)
|
|
|
|
return &Exchange{
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (float64, error) {
|
|
|
|
resp, err := e.Client.NewAveragePriceService().Symbol(symbol).Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:08:50 +00:00
|
|
|
return util.MustParseFloat(resp.Price), nil
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:20:09 +00:00
|
|
|
func (e *Exchange) NewPrivateStream() (*PrivateStream, error) {
|
2020-07-11 05:02:53 +00:00
|
|
|
return &PrivateStream{
|
2020-07-15 04:20:44 +00:00
|
|
|
Client: e.Client,
|
2020-07-11 05:02:53 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 04:28:40 +00:00
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (map[string]types.Balance, error) {
|
|
|
|
account, err := e.QueryAccount(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account.Balances, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
|
|
|
|
account, err := e.Client.NewGetAccountService().Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var balances = map[string]types.Balance{}
|
2020-07-15 04:20:44 +00:00
|
|
|
for _, b := range account.Balances {
|
2020-07-13 04:28:40 +00:00
|
|
|
balances[b.Asset] = types.Balance{
|
|
|
|
Currency: b.Asset,
|
|
|
|
Available: util.MustParseFloat(b.Free),
|
|
|
|
Locked: util.MustParseFloat(b.Locked),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.Account{
|
|
|
|
MakerCommission: account.MakerCommission,
|
|
|
|
TakerCommission: account.TakerCommission,
|
|
|
|
Balances: balances,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-11 07:19:36 +00:00
|
|
|
func (e *Exchange) SubmitOrder(ctx context.Context, order *types.Order) error {
|
2020-07-11 05:02:53 +00:00
|
|
|
/*
|
|
|
|
limit order example
|
|
|
|
|
|
|
|
order, err := Client.NewCreateOrderService().
|
|
|
|
Symbol(Symbol).
|
|
|
|
Side(side).
|
|
|
|
Type(binance.OrderTypeLimit).
|
|
|
|
TimeInForce(binance.TimeInForceTypeGTC).
|
|
|
|
Quantity(volumeString).
|
|
|
|
Price(priceString).
|
|
|
|
Do(ctx)
|
|
|
|
*/
|
|
|
|
|
2020-07-11 07:27:26 +00:00
|
|
|
orderType, err := toLocalOrderType(order.Type)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
req := e.Client.NewCreateOrderService().
|
|
|
|
Symbol(order.Symbol).
|
2020-07-11 07:18:31 +00:00
|
|
|
Side(binance.SideType(order.Side)).
|
2020-07-11 07:27:26 +00:00
|
|
|
Type(orderType).
|
2020-07-11 05:02:53 +00:00
|
|
|
Quantity(order.VolumeStr)
|
|
|
|
|
|
|
|
if len(order.PriceStr) > 0 {
|
|
|
|
req.Price(order.PriceStr)
|
|
|
|
}
|
|
|
|
if len(order.TimeInForce) > 0 {
|
|
|
|
req.TimeInForce(order.TimeInForce)
|
|
|
|
}
|
|
|
|
|
|
|
|
retOrder, err := req.Do(ctx)
|
|
|
|
logrus.Infof("[binance] order created: %+v", retOrder)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-11 07:27:26 +00:00
|
|
|
func toLocalOrderType(orderType types.OrderType) (binance.OrderType, error) {
|
|
|
|
switch orderType {
|
|
|
|
case types.OrderTypeLimit:
|
|
|
|
return binance.OrderTypeLimit, nil
|
|
|
|
|
|
|
|
case types.OrderTypeMarket:
|
|
|
|
return binance.OrderTypeMarket, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("order type %s not supported", orderType)
|
|
|
|
}
|
|
|
|
|
2020-07-15 04:20:44 +00:00
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol, interval string, options types.KLineQueryOptions) ([]types.KLine, error) {
|
|
|
|
var limit = 500
|
|
|
|
if options.Limit > 0 {
|
2020-07-11 12:40:19 +00:00
|
|
|
// default limit == 500
|
2020-07-15 04:20:44 +00:00
|
|
|
limit = options.Limit
|
2020-07-11 12:40:19 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 09:45:21 +00:00
|
|
|
logrus.Infof("[binance] querying kline %s %s %v", symbol, interval, options)
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2020-07-15 04:20:44 +00:00
|
|
|
req := e.Client.NewKlinesService().Symbol(symbol).
|
|
|
|
Interval(interval).
|
|
|
|
Limit(limit)
|
|
|
|
|
|
|
|
if options.StartTime != nil {
|
|
|
|
req.StartTime(options.StartTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.EndTime != nil {
|
|
|
|
req.EndTime(options.EndTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := req.Do(ctx)
|
2020-07-11 05:02:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var kLines []types.KLine
|
|
|
|
for _, kline := range resp {
|
|
|
|
kLines = append(kLines, types.KLine{
|
|
|
|
Symbol: symbol,
|
|
|
|
Interval: interval,
|
|
|
|
StartTime: kline.OpenTime,
|
|
|
|
EndTime: kline.CloseTime,
|
|
|
|
Open: kline.Open,
|
|
|
|
Close: kline.Close,
|
|
|
|
High: kline.High,
|
|
|
|
Low: kline.Low,
|
|
|
|
Volume: kline.Volume,
|
|
|
|
QuoteVolume: kline.QuoteAssetVolume,
|
|
|
|
NumberOfTrades: kline.TradeNum,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return kLines, nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 14:49:11 +00:00
|
|
|
func (e *Exchange) BatchQueryTrades(ctx context.Context, symbol string, startTime time.Time) (trades []types.Trade, err error) {
|
2020-07-11 05:02:53 +00:00
|
|
|
logrus.Infof("[binance] querying %s trades from %s", symbol, startTime)
|
|
|
|
|
|
|
|
var lastTradeID int64 = 0
|
|
|
|
for {
|
|
|
|
req := e.Client.NewListTradesService().
|
|
|
|
Limit(1000).
|
|
|
|
Symbol(symbol).
|
|
|
|
StartTime(startTime.UnixNano() / 1000000)
|
|
|
|
|
|
|
|
if lastTradeID > 0 {
|
|
|
|
req.FromID(lastTradeID)
|
|
|
|
}
|
|
|
|
|
|
|
|
bnTrades, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(bnTrades) <= 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range bnTrades {
|
|
|
|
// skip trade ID that is the same. however this should not happen
|
|
|
|
if t.ID == lastTradeID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var side string
|
|
|
|
if t.IsBuyer {
|
|
|
|
side = "BUY"
|
|
|
|
} else {
|
|
|
|
side = "SELL"
|
|
|
|
}
|
|
|
|
|
|
|
|
// trade time
|
|
|
|
tt := time.Unix(0, t.Time*1000000)
|
|
|
|
|
|
|
|
logrus.Infof("[binance] trade: %d %s % 4s price: % 13s volume: % 11s %6s % 5s %s", t.ID, t.Symbol, side, t.Price, t.Quantity, BuyerOrSellerLabel(t), MakerOrTakerLabel(t), tt)
|
|
|
|
|
|
|
|
price, err := strconv.ParseFloat(t.Price, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
quantity, err := strconv.ParseFloat(t.Quantity, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fee, err := strconv.ParseFloat(t.Commission, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-11 07:19:36 +00:00
|
|
|
trades = append(trades, types.Trade{
|
2020-07-11 05:02:53 +00:00
|
|
|
ID: t.ID,
|
|
|
|
Price: price,
|
|
|
|
Volume: quantity,
|
|
|
|
Side: side,
|
|
|
|
IsBuyer: t.IsBuyer,
|
|
|
|
IsMaker: t.IsMaker,
|
|
|
|
Fee: fee,
|
|
|
|
FeeCurrency: t.CommissionAsset,
|
|
|
|
Time: tt,
|
|
|
|
})
|
|
|
|
|
|
|
|
lastTradeID = t.ID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades, nil
|
|
|
|
}
|