2020-07-11 05:02:53 +00:00
|
|
|
package binance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-11 07:27:26 +00:00
|
|
|
"fmt"
|
2021-10-19 07:54:16 +00:00
|
|
|
"github.com/adshao/go-binance/v2/futures"
|
2020-12-21 09:48:30 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-02-18 08:17:40 +00:00
|
|
|
"strings"
|
2020-07-11 07:19:36 +00:00
|
|
|
"time"
|
|
|
|
|
2021-01-11 05:36:49 +00:00
|
|
|
"github.com/adshao/go-binance/v2"
|
2020-10-25 10:26:10 +00:00
|
|
|
"github.com/google/uuid"
|
2020-10-25 11:18:03 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-07-11 07:19:36 +00:00
|
|
|
|
2020-08-13 02:11:27 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2020-11-10 06:19:33 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
2020-10-11 08:46:15 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
"github.com/c9s/bbgo/pkg/util"
|
2020-07-11 05:02:53 +00:00
|
|
|
)
|
|
|
|
|
2021-05-25 18:15:47 +00:00
|
|
|
const BNB = "BNB"
|
|
|
|
|
2020-08-11 00:36:36 +00:00
|
|
|
var log = logrus.WithFields(logrus.Fields{
|
|
|
|
"exchange": "binance",
|
|
|
|
})
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
func init() {
|
|
|
|
_ = types.Exchange(&Exchange{})
|
2021-01-19 18:09:12 +00:00
|
|
|
_ = types.MarginExchange(&Exchange{})
|
2020-12-21 09:48:30 +00:00
|
|
|
|
|
|
|
if ok, _ := strconv.ParseBool(os.Getenv("DEBUG_BINANCE_STREAM")); ok {
|
|
|
|
log.Level = logrus.DebugLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 05:02:53 +00:00
|
|
|
type Exchange struct {
|
2021-01-19 18:09:12 +00:00
|
|
|
types.MarginSettings
|
2021-10-07 13:29:52 +00:00
|
|
|
types.FuturesSettings
|
2020-12-02 14:19:31 +00:00
|
|
|
|
2021-05-31 19:15:19 +00:00
|
|
|
key, secret string
|
|
|
|
Client *binance.Client
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
func New(key, secret string) *Exchange {
|
2020-07-11 05:08:50 +00:00
|
|
|
var client = binance.NewClient(key, secret)
|
2021-05-16 07:03:31 +00:00
|
|
|
_, _ = client.NewSetServerTimeService().Do(context.Background())
|
2020-07-11 05:08:50 +00:00
|
|
|
return &Exchange{
|
2021-05-31 19:15:19 +00:00
|
|
|
key: key,
|
|
|
|
secret: secret,
|
|
|
|
|
2020-07-11 05:08:50 +00:00
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-11 12:08:54 +00:00
|
|
|
func (e *Exchange) Name() types.ExchangeName {
|
|
|
|
return types.ExchangeBinance
|
|
|
|
}
|
|
|
|
|
2021-02-18 08:17:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-06 17:35:23 +00:00
|
|
|
func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error) {
|
2021-02-18 09:37:49 +00:00
|
|
|
var tickers = make(map[string]types.Ticker)
|
2021-02-06 17:35:23 +00:00
|
|
|
|
|
|
|
if len(symbol) == 1 {
|
2021-02-18 09:37:49 +00:00
|
|
|
ticker, err := e.QueryTicker(ctx, symbol[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tickers[strings.ToUpper(symbol[0])] = *ticker
|
|
|
|
return tickers, nil
|
2021-02-06 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:37:49 +00:00
|
|
|
var req = e.Client.NewListPriceChangeStatsService()
|
|
|
|
changeStats, err := req.Do(ctx)
|
2021-02-06 17:35:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-07 21:58:30 +00:00
|
|
|
m := make(map[string]struct{})
|
|
|
|
exists := struct{}{}
|
2021-02-06 17:35:23 +00:00
|
|
|
|
|
|
|
for _, s := range symbol {
|
2021-02-07 21:58:30 +00:00
|
|
|
m[s] = exists
|
2021-02-06 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-07 21:58:30 +00:00
|
|
|
for _, stats := range changeStats {
|
|
|
|
if _, ok := m[stats.Symbol]; len(symbol) != 0 && !ok {
|
2021-02-06 17:35:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tick := types.Ticker{
|
2021-02-07 21:58:30 +00:00
|
|
|
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)),
|
2021-02-06 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:37:49 +00:00
|
|
|
tickers[stats.Symbol] = tick
|
2021-02-06 17:35:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 09:37:49 +00:00
|
|
|
return tickers, nil
|
2021-02-04 21:48:35 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:16:59 +00:00
|
|
|
func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
|
2020-10-16 02:14:36 +00:00
|
|
|
log.Info("querying market info...")
|
|
|
|
|
2020-10-14 02:16:59 +00:00
|
|
|
exchangeInfo, err := e.Client.NewExchangeInfoService().Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
markets := types.MarketMap{}
|
|
|
|
for _, symbol := range exchangeInfo.Symbols {
|
|
|
|
market := types.Market{
|
|
|
|
Symbol: symbol.Symbol,
|
2021-05-25 18:13:59 +00:00
|
|
|
LocalSymbol: symbol.Symbol,
|
2020-10-14 02:16:59 +00:00
|
|
|
PricePrecision: symbol.QuotePrecision,
|
|
|
|
VolumePrecision: symbol.BaseAssetPrecision,
|
|
|
|
QuoteCurrency: symbol.QuoteAsset,
|
|
|
|
BaseCurrency: symbol.BaseAsset,
|
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
if f := symbol.MinNotionalFilter(); f != nil {
|
2020-10-14 02:16:59 +00:00
|
|
|
market.MinNotional = util.MustParseFloat(f.MinNotional)
|
2020-10-14 02:39:14 +00:00
|
|
|
market.MinAmount = util.MustParseFloat(f.MinNotional)
|
2020-10-14 02:16:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 02:34:33 +00:00
|
|
|
// The LOT_SIZE filter defines the quantity (aka "lots" in auction terms) rules for a symbol.
|
|
|
|
// There are 3 parts:
|
|
|
|
// minQty defines the minimum quantity/icebergQty allowed.
|
|
|
|
// maxQty defines the maximum quantity/icebergQty allowed.
|
|
|
|
// stepSize defines the intervals that a quantity/icebergQty can be increased/decreased by.
|
2020-10-25 10:26:10 +00:00
|
|
|
if f := symbol.LotSizeFilter(); f != nil {
|
2020-10-14 02:16:59 +00:00
|
|
|
market.MinQuantity = util.MustParseFloat(f.MinQuantity)
|
|
|
|
market.MaxQuantity = util.MustParseFloat(f.MaxQuantity)
|
2021-02-10 16:21:56 +00:00
|
|
|
market.StepSize = util.MustParseFloat(f.StepSize)
|
2020-10-14 02:16:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
if f := symbol.PriceFilter(); f != nil {
|
2020-10-14 02:34:33 +00:00
|
|
|
market.MaxPrice = util.MustParseFloat(f.MaxPrice)
|
|
|
|
market.MinPrice = util.MustParseFloat(f.MinPrice)
|
|
|
|
market.TickSize = util.MustParseFloat(f.TickSize)
|
2020-10-14 02:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
markets[symbol.Symbol] = market
|
|
|
|
}
|
|
|
|
|
|
|
|
return markets, nil
|
|
|
|
}
|
|
|
|
|
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-10-03 12:09:22 +00:00
|
|
|
func (e *Exchange) NewStream() types.Stream {
|
2020-12-02 14:19:31 +00:00
|
|
|
stream := NewStream(e.Client)
|
2020-12-21 09:48:30 +00:00
|
|
|
stream.MarginSettings = e.MarginSettings
|
|
|
|
return stream
|
|
|
|
}
|
2020-12-02 14:19:31 +00:00
|
|
|
|
2021-02-28 06:58:43 +00:00
|
|
|
func (e *Exchange) QueryMarginAccount(ctx context.Context) (*types.MarginAccount, error) {
|
2021-02-28 06:52:14 +00:00
|
|
|
account, err := e.Client.NewGetMarginAccountService().Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-28 06:58:43 +00:00
|
|
|
return toGlobalMarginAccount(account), nil
|
2020-12-21 09:48:30 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 06:52:14 +00:00
|
|
|
func (e *Exchange) QueryIsolatedMarginAccount(ctx context.Context, symbols ...string) (*types.IsolatedMarginAccount, error) {
|
2020-12-21 09:48:30 +00:00
|
|
|
req := e.Client.NewGetIsolatedMarginAccountService()
|
|
|
|
if len(symbols) > 0 {
|
|
|
|
req.Symbols(symbols...)
|
2020-12-02 14:19:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 06:52:14 +00:00
|
|
|
account, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return toGlobalIsolatedMarginAccount(account), nil
|
2020-07-11 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-13 12:49:51 +00:00
|
|
|
func (e *Exchange) getLaunchDate() (time.Time, error) {
|
|
|
|
// binance launch date 12:00 July 14th, 2017
|
|
|
|
loc, err := time.LoadLocation("Asia/Shanghai")
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
2020-08-13 02:11:27 +00:00
|
|
|
|
2021-03-13 12:49:51 +00:00
|
|
|
return time.Date(2017, time.July, 14, 0, 0, 0, 0, loc), nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 15:24:05 +00:00
|
|
|
func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error {
|
2021-09-01 16:21:56 +00:00
|
|
|
req := e.Client.NewCreateWithdrawService()
|
|
|
|
req.Coin(asset)
|
|
|
|
req.Address(address)
|
|
|
|
req.Amount(fmt.Sprintf("%f", amount.Float64()))
|
2021-05-11 18:15:22 +00:00
|
|
|
|
2021-05-26 15:24:05 +00:00
|
|
|
if options != nil {
|
|
|
|
if options.Network != "" {
|
|
|
|
req.Network(options.Network)
|
|
|
|
}
|
|
|
|
if options.AddressTag != "" {
|
|
|
|
req.Network(options.AddressTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := req.Do(ctx)
|
2021-05-11 18:15:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("withdrawal request sent, response: %+v", response)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-13 12:49:51 +00:00
|
|
|
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
|
2020-08-13 02:11:27 +00:00
|
|
|
startTime := since
|
2021-03-13 12:49:51 +00:00
|
|
|
|
|
|
|
var emptyTime = time.Time{}
|
|
|
|
if startTime == emptyTime {
|
|
|
|
startTime, err = e.getLaunchDate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 02:11:27 +00:00
|
|
|
txIDs := map[string]struct{}{}
|
|
|
|
|
|
|
|
for startTime.Before(until) {
|
|
|
|
// startTime ~ endTime must be in 90 days
|
|
|
|
endTime := startTime.AddDate(0, 0, 60)
|
|
|
|
if endTime.After(until) {
|
|
|
|
endTime = until
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:15:33 +00:00
|
|
|
req := e.Client.NewListWithdrawsService()
|
|
|
|
if len(asset) > 0 {
|
2021-09-01 16:27:57 +00:00
|
|
|
req.Coin(asset)
|
2020-10-12 09:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
withdraws, err := req.
|
2020-08-13 02:11:27 +00:00
|
|
|
StartTime(startTime.UnixNano() / int64(time.Millisecond)).
|
|
|
|
EndTime(endTime.UnixNano() / int64(time.Millisecond)).
|
|
|
|
Do(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-10-11 12:08:54 +00:00
|
|
|
return allWithdraws, err
|
2020-08-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range withdraws {
|
|
|
|
if _, ok := txIDs[d.TxID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
status := ""
|
|
|
|
switch d.Status {
|
|
|
|
case 0:
|
|
|
|
status = "email_sent"
|
|
|
|
case 1:
|
|
|
|
status = "cancelled"
|
|
|
|
case 2:
|
|
|
|
status = "awaiting_approval"
|
|
|
|
case 3:
|
|
|
|
status = "rejected"
|
|
|
|
case 4:
|
|
|
|
status = "processing"
|
|
|
|
case 5:
|
|
|
|
status = "failure"
|
|
|
|
case 6:
|
|
|
|
status = "completed"
|
2020-10-11 09:35:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
status = fmt.Sprintf("unsupported code: %d", d.Status)
|
2020-08-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txIDs[d.TxID] = struct{}{}
|
2021-09-01 16:27:57 +00:00
|
|
|
|
2021-09-03 06:21:59 +00:00
|
|
|
// 2006-01-02 15:04:05
|
|
|
|
applyTime, err := time.Parse("2006-01-02 15:04:05", d.ApplyTime)
|
2021-09-01 16:27:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-11 12:08:54 +00:00
|
|
|
allWithdraws = append(allWithdraws, types.Withdraw{
|
2021-03-14 03:01:40 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
2021-09-01 16:27:57 +00:00
|
|
|
ApplyTime: types.Time(applyTime),
|
|
|
|
Asset: d.Coin,
|
|
|
|
Amount: util.MustParseFloat(d.Amount),
|
2020-08-31 04:32:51 +00:00
|
|
|
Address: d.Address,
|
|
|
|
TransactionID: d.TxID,
|
2021-09-01 16:27:57 +00:00
|
|
|
TransactionFee: util.MustParseFloat(d.TransactionFee),
|
2020-08-13 02:11:27 +00:00
|
|
|
WithdrawOrderID: d.WithdrawOrderID,
|
2020-08-31 04:32:51 +00:00
|
|
|
Network: d.Network,
|
|
|
|
Status: status,
|
2020-08-13 02:11:27 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime = endTime
|
|
|
|
}
|
|
|
|
|
|
|
|
return allWithdraws, nil
|
|
|
|
}
|
|
|
|
|
2020-10-11 09:35:59 +00:00
|
|
|
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
|
2020-08-13 02:11:27 +00:00
|
|
|
startTime := since
|
2021-04-21 07:06:53 +00:00
|
|
|
|
|
|
|
var emptyTime = time.Time{}
|
|
|
|
if startTime == emptyTime {
|
|
|
|
startTime, err = e.getLaunchDate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 02:11:27 +00:00
|
|
|
txIDs := map[string]struct{}{}
|
|
|
|
for startTime.Before(until) {
|
|
|
|
|
|
|
|
// startTime ~ endTime must be in 90 days
|
|
|
|
endTime := startTime.AddDate(0, 0, 60)
|
|
|
|
if endTime.After(until) {
|
|
|
|
endTime = until
|
|
|
|
}
|
|
|
|
|
2020-10-12 09:15:33 +00:00
|
|
|
req := e.Client.NewListDepositsService()
|
|
|
|
if len(asset) > 0 {
|
2021-09-01 16:27:57 +00:00
|
|
|
req.Coin(asset)
|
2020-10-12 09:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
deposits, err := req.
|
2020-08-13 02:11:27 +00:00
|
|
|
StartTime(startTime.UnixNano() / int64(time.Millisecond)).
|
|
|
|
EndTime(endTime.UnixNano() / int64(time.Millisecond)).
|
|
|
|
Do(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range deposits {
|
|
|
|
if _, ok := txIDs[d.TxID]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0(0:pending,6: credited but cannot withdraw, 1:success)
|
2020-10-11 09:35:59 +00:00
|
|
|
status := types.DepositStatus(fmt.Sprintf("code: %d", d.Status))
|
|
|
|
|
2020-08-13 02:11:27 +00:00
|
|
|
switch d.Status {
|
|
|
|
case 0:
|
2020-10-11 09:35:59 +00:00
|
|
|
status = types.DepositPending
|
2020-08-13 02:11:27 +00:00
|
|
|
case 6:
|
2020-10-11 09:35:59 +00:00
|
|
|
// https://www.binance.com/en/support/faq/115003736451
|
|
|
|
status = types.DepositCredited
|
2020-08-13 02:11:27 +00:00
|
|
|
case 1:
|
2020-10-11 09:35:59 +00:00
|
|
|
status = types.DepositSuccess
|
2020-08-13 02:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
txIDs[d.TxID] = struct{}{}
|
2020-10-11 09:35:59 +00:00
|
|
|
allDeposits = append(allDeposits, types.Deposit{
|
2021-03-14 03:01:40 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
2021-05-19 17:32:26 +00:00
|
|
|
Time: types.Time(time.Unix(0, d.InsertTime*int64(time.Millisecond))),
|
2021-09-01 16:27:57 +00:00
|
|
|
Asset: d.Coin,
|
|
|
|
Amount: util.MustParseFloat(d.Amount),
|
2020-08-13 02:11:27 +00:00
|
|
|
Address: d.Address,
|
|
|
|
AddressTag: d.AddressTag,
|
|
|
|
TransactionID: d.TxID,
|
|
|
|
Status: status,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime = endTime
|
|
|
|
}
|
|
|
|
|
|
|
|
return allDeposits, nil
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:32:41 +00:00
|
|
|
func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error) {
|
2020-07-13 04:28:40 +00:00
|
|
|
account, err := e.QueryAccount(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-10-18 03:30:37 +00:00
|
|
|
return account.Balances(), nil
|
2020-07-13 04:28:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:59:43 +00:00
|
|
|
func (e *Exchange) PlatformFeeCurrency() string {
|
2021-05-25 18:15:47 +00:00
|
|
|
return BNB
|
2020-08-03 12:06:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 04:28:40 +00:00
|
|
|
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,
|
2020-11-10 06:19:33 +00:00
|
|
|
Available: fixedpoint.Must(fixedpoint.NewFromString(b.Free)),
|
|
|
|
Locked: fixedpoint.Must(fixedpoint.NewFromString(b.Locked)),
|
2020-07-13 04:28:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 14:53:14 +00:00
|
|
|
// binance use 15 -> 0.15%, so we convert it to 0.0015
|
2020-10-18 03:30:37 +00:00
|
|
|
a := &types.Account{
|
2021-03-20 14:53:14 +00:00
|
|
|
MakerCommission: fixedpoint.NewFromFloat(float64(account.MakerCommission) * 0.0001),
|
|
|
|
TakerCommission: fixedpoint.NewFromFloat(float64(account.TakerCommission) * 0.0001),
|
2020-10-18 03:30:37 +00:00
|
|
|
}
|
|
|
|
a.UpdateBalances(balances)
|
|
|
|
return a, nil
|
2020-07-13 04:28:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 10:26:10 +00:00
|
|
|
func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error) {
|
2021-01-19 18:09:12 +00:00
|
|
|
if e.IsMargin {
|
2021-01-18 13:56:58 +00:00
|
|
|
req := e.Client.NewListMarginOpenOrdersService().Symbol(symbol)
|
2021-01-19 18:09:12 +00:00
|
|
|
req.IsIsolated(e.IsIsolatedMargin)
|
2021-01-18 13:56:58 +00:00
|
|
|
|
|
|
|
binanceOrders, err := req.Do(ctx)
|
2020-10-25 10:26:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
return toGlobalOrders(binanceOrders)
|
2021-01-18 11:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binanceOrders, err := e.Client.NewListOpenOrdersService().Symbol(symbol).Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
return toGlobalOrders(binanceOrders)
|
2020-10-25 10:26:10 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error) {
|
|
|
|
if until.Sub(since) >= 24*time.Hour {
|
|
|
|
until = since.Add(24*time.Hour - time.Millisecond)
|
|
|
|
}
|
2020-11-05 00:33:57 +00:00
|
|
|
|
2021-01-19 18:09:12 +00:00
|
|
|
log.Infof("querying closed orders %s from %s <=> %s ...", symbol, since, until)
|
2020-11-05 00:33:57 +00:00
|
|
|
|
2021-01-19 18:09:12 +00:00
|
|
|
if e.IsMargin {
|
2021-01-18 13:56:58 +00:00
|
|
|
req := e.Client.NewListMarginOrdersService().Symbol(symbol)
|
2021-01-19 18:09:12 +00:00
|
|
|
req.IsIsolated(e.IsIsolatedMargin)
|
2021-01-18 13:56:58 +00:00
|
|
|
|
|
|
|
if lastOrderID > 0 {
|
|
|
|
req.OrderID(int64(lastOrderID))
|
|
|
|
} else {
|
|
|
|
req.StartTime(since.UnixNano() / int64(time.Millisecond)).
|
|
|
|
EndTime(until.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
|
|
|
|
binanceOrders, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
return toGlobalOrders(binanceOrders)
|
2021-01-18 13:56:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
req := e.Client.NewListOrdersService().
|
|
|
|
Symbol(symbol)
|
2020-11-05 00:33:57 +00:00
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
if lastOrderID > 0 {
|
|
|
|
req.OrderID(int64(lastOrderID))
|
|
|
|
} else {
|
|
|
|
req.StartTime(since.UnixNano() / int64(time.Millisecond)).
|
|
|
|
EndTime(until.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
2020-11-05 00:33:57 +00:00
|
|
|
|
2020-11-05 03:00:51 +00:00
|
|
|
binanceOrders, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return orders, err
|
|
|
|
}
|
2020-11-05 00:33:57 +00:00
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
return toGlobalOrders(binanceOrders)
|
2020-11-05 00:33:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 16:26:17 +00:00
|
|
|
func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err2 error) {
|
|
|
|
for _, o := range orders {
|
|
|
|
var req = e.Client.NewCancelOrderService()
|
|
|
|
|
|
|
|
// Mandatory
|
|
|
|
req.Symbol(o.Symbol)
|
|
|
|
|
|
|
|
if o.OrderID > 0 {
|
|
|
|
req.OrderID(int64(o.OrderID))
|
|
|
|
} else if len(o.ClientOrderID) > 0 {
|
|
|
|
req.NewClientOrderID(o.ClientOrderID)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Errorf("order cancel error")
|
|
|
|
err2 = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
|
2021-01-15 07:46:54 +00:00
|
|
|
orderType, err := toLocalOrderType(order.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := e.Client.NewCreateMarginOrderService().
|
|
|
|
Symbol(order.Symbol).
|
|
|
|
Type(orderType).
|
2021-06-06 17:06:38 +00:00
|
|
|
Side(binance.SideType(order.Side))
|
|
|
|
|
|
|
|
clientOrderID := newSpotClientOrderID(order.ClientOrderID)
|
|
|
|
if len(clientOrderID) > 0 {
|
|
|
|
req.NewClientOrderID(clientOrderID)
|
|
|
|
}
|
2021-01-15 07:46:54 +00:00
|
|
|
|
|
|
|
// use response result format
|
|
|
|
req.NewOrderRespType(binance.NewOrderRespTypeRESULT)
|
|
|
|
|
2021-01-19 18:09:12 +00:00
|
|
|
if e.IsIsolatedMargin {
|
|
|
|
req.IsIsolated(e.IsIsolatedMargin)
|
2021-01-15 07:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(order.MarginSideEffect) > 0 {
|
|
|
|
req.SideEffectType(binance.SideEffectType(order.MarginSideEffect))
|
|
|
|
}
|
|
|
|
|
2021-01-15 23:37:42 +00:00
|
|
|
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))
|
|
|
|
}
|
2021-01-15 07:46:54 +00:00
|
|
|
|
2021-02-10 16:21:56 +00:00
|
|
|
// set price field for limit orders
|
|
|
|
switch order.Type {
|
2021-10-17 16:41:41 +00:00
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
2021-02-10 16:21:56 +00:00
|
|
|
if len(order.PriceString) > 0 {
|
|
|
|
req.Price(order.PriceString)
|
|
|
|
} else if order.Market.Symbol != "" {
|
|
|
|
req.Price(order.Market.FormatPrice(order.Price))
|
|
|
|
}
|
2021-01-15 07:46:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 16:21:56 +00:00
|
|
|
// set stop price
|
2021-01-15 07:46:54 +00:00
|
|
|
switch order.Type {
|
2021-02-10 16:21:56 +00:00
|
|
|
|
2021-01-15 07:46:54 +00:00
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeStopMarket:
|
|
|
|
if len(order.StopPriceString) == 0 {
|
|
|
|
return nil, fmt.Errorf("stop price string can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.StopPrice(order.StopPriceString)
|
|
|
|
}
|
|
|
|
|
2021-01-15 23:37:42 +00:00
|
|
|
// could be IOC or FOK
|
2021-01-15 07:46:54 +00:00
|
|
|
if len(order.TimeInForce) > 0 {
|
|
|
|
// TODO: check the TimeInForce value
|
|
|
|
req.TimeInForce(binance.TimeInForceType(order.TimeInForce))
|
2021-04-13 15:18:40 +00:00
|
|
|
} else {
|
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeLimit, types.OrderTypeStopLimit:
|
|
|
|
req.TimeInForce(binance.TimeInForceTypeGTC)
|
|
|
|
}
|
2021-01-15 07:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
response, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("margin order creation response: %+v", response)
|
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
createdOrder, err := toGlobalOrder(&binance.Order{
|
2021-01-15 07:46:54 +00:00
|
|
|
Symbol: response.Symbol,
|
|
|
|
OrderID: response.OrderID,
|
|
|
|
ClientOrderID: response.ClientOrderID,
|
|
|
|
Price: response.Price,
|
|
|
|
OrigQuantity: response.OrigQuantity,
|
|
|
|
ExecutedQuantity: response.ExecutedQuantity,
|
|
|
|
CummulativeQuoteQuantity: response.CummulativeQuoteQuantity,
|
|
|
|
Status: response.Status,
|
|
|
|
TimeInForce: response.TimeInForce,
|
|
|
|
Type: response.Type,
|
|
|
|
Side: response.Side,
|
|
|
|
UpdateTime: response.TransactTime,
|
|
|
|
Time: response.TransactTime,
|
2021-01-19 17:24:29 +00:00
|
|
|
IsIsolated: response.IsIsolated,
|
|
|
|
}, true)
|
2021-01-15 07:46:54 +00:00
|
|
|
|
|
|
|
return createdOrder, err
|
2021-01-15 07:11:38 +00:00
|
|
|
}
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-04-28 11:20:55 +00:00
|
|
|
// BBGO is a broker on Binance
|
|
|
|
const spotBrokerID = "NSUYEBKM"
|
|
|
|
|
|
|
|
func newSpotClientOrderID(originalID string) (clientOrderID string) {
|
2021-06-06 17:00:01 +00:00
|
|
|
if originalID == types.NoClientOrderID {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-04-28 11:20:55 +00:00
|
|
|
prefix := "x-" + spotBrokerID
|
|
|
|
prefixLen := len(prefix)
|
|
|
|
|
|
|
|
if originalID != "" {
|
|
|
|
// try to keep the whole original client order ID if user specifies it.
|
2021-05-11 18:15:22 +00:00
|
|
|
if prefixLen+len(originalID) > 32 {
|
2021-04-28 11:20:55 +00:00
|
|
|
return originalID
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOrderID = prefix + originalID
|
|
|
|
return clientOrderID
|
|
|
|
}
|
|
|
|
|
|
|
|
clientOrderID = uuid.New().String()
|
|
|
|
clientOrderID = prefix + clientOrderID
|
|
|
|
if len(clientOrderID) > 32 {
|
|
|
|
return clientOrderID[0:32]
|
|
|
|
}
|
|
|
|
|
|
|
|
return clientOrderID
|
|
|
|
}
|
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
func (e *Exchange) submitSpotOrder(ctx context.Context, order types.SubmitOrder) (*types.Order, error) {
|
|
|
|
orderType, err := toLocalOrderType(order.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-25 10:56:07 +00:00
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
req := e.Client.NewCreateOrderService().
|
|
|
|
Symbol(order.Symbol).
|
|
|
|
Side(binance.SideType(order.Side)).
|
|
|
|
Type(orderType)
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-06-06 17:06:38 +00:00
|
|
|
clientOrderID := newSpotClientOrderID(order.ClientOrderID)
|
|
|
|
if len(clientOrderID) > 0 {
|
|
|
|
req.NewClientOrderID(clientOrderID)
|
|
|
|
}
|
|
|
|
|
2021-02-18 08:17:40 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-02-18 08:17:40 +00:00
|
|
|
// set price field for limit orders
|
|
|
|
switch order.Type {
|
2021-10-17 16:41:41 +00:00
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeLimit, types.OrderTypeLimitMaker:
|
2021-02-18 08:17:40 +00:00
|
|
|
if len(order.PriceString) > 0 {
|
|
|
|
req.Price(order.PriceString)
|
|
|
|
} else if order.Market.Symbol != "" {
|
|
|
|
req.Price(order.Market.FormatPrice(order.Price))
|
|
|
|
}
|
2021-01-15 07:11:38 +00:00
|
|
|
}
|
2020-12-03 01:25:47 +00:00
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeStopLimit, types.OrderTypeStopMarket:
|
|
|
|
if len(order.StopPriceString) == 0 {
|
|
|
|
return nil, fmt.Errorf("stop price string can not be empty")
|
2020-12-03 01:25:47 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
req.StopPrice(order.StopPriceString)
|
|
|
|
}
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
if len(order.TimeInForce) > 0 {
|
|
|
|
// TODO: check the TimeInForce value
|
|
|
|
req.TimeInForce(binance.TimeInForceType(order.TimeInForce))
|
2021-04-13 15:18:40 +00:00
|
|
|
} else {
|
|
|
|
switch order.Type {
|
|
|
|
case types.OrderTypeLimit, types.OrderTypeStopLimit:
|
|
|
|
req.TimeInForce(binance.TimeInForceTypeGTC)
|
|
|
|
}
|
2021-01-15 07:11:38 +00:00
|
|
|
}
|
2020-07-11 07:27:26 +00:00
|
|
|
|
2021-10-17 16:41:41 +00:00
|
|
|
req.NewOrderRespType(binance.NewOrderRespTypeRESULT)
|
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
response, err := req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-29 09:26:22 +00:00
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
log.Infof("spot order creation response: %+v", response)
|
2021-01-15 07:11:38 +00:00
|
|
|
|
2021-05-28 16:26:39 +00:00
|
|
|
createdOrder, err := toGlobalOrder(&binance.Order{
|
2021-01-15 07:11:38 +00:00
|
|
|
Symbol: response.Symbol,
|
|
|
|
OrderID: response.OrderID,
|
|
|
|
ClientOrderID: response.ClientOrderID,
|
|
|
|
Price: response.Price,
|
|
|
|
OrigQuantity: response.OrigQuantity,
|
|
|
|
ExecutedQuantity: response.ExecutedQuantity,
|
|
|
|
CummulativeQuoteQuantity: response.CummulativeQuoteQuantity,
|
|
|
|
Status: response.Status,
|
|
|
|
TimeInForce: response.TimeInForce,
|
|
|
|
Type: response.Type,
|
|
|
|
Side: response.Side,
|
|
|
|
UpdateTime: response.TransactTime,
|
|
|
|
Time: response.TransactTime,
|
2021-01-19 17:24:29 +00:00
|
|
|
IsIsolated: response.IsIsolated,
|
2021-01-15 07:11:38 +00:00
|
|
|
// StopPrice:
|
|
|
|
// IcebergQuantity:
|
|
|
|
// UpdateTime:
|
|
|
|
// IsWorking: ,
|
2021-01-19 17:24:29 +00:00
|
|
|
}, false)
|
2021-01-15 07:11:38 +00:00
|
|
|
|
|
|
|
return createdOrder, err
|
|
|
|
}
|
2020-10-25 11:18:03 +00:00
|
|
|
|
2021-01-15 07:11:38 +00:00
|
|
|
func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error) {
|
|
|
|
for _, order := range orders {
|
2021-01-15 07:46:54 +00:00
|
|
|
var createdOrder *types.Order
|
|
|
|
|
2021-01-19 18:09:12 +00:00
|
|
|
if e.IsMargin {
|
2021-01-15 07:46:54 +00:00
|
|
|
createdOrder, err = e.submitMarginOrder(ctx, order)
|
|
|
|
} else {
|
|
|
|
createdOrder, err = e.submitSpotOrder(ctx, order)
|
|
|
|
}
|
|
|
|
|
2020-10-25 11:18:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return createdOrders, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if createdOrder == nil {
|
|
|
|
return createdOrders, errors.New("nil converted order")
|
|
|
|
}
|
|
|
|
|
|
|
|
createdOrders = append(createdOrders, *createdOrder)
|
2020-07-11 07:27:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 11:18:03 +00:00
|
|
|
return createdOrders, err
|
2020-07-11 07:27:26 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 16:49:17 +00:00
|
|
|
// QueryKLines queries the Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time.
|
2021-05-05 08:23:46 +00:00
|
|
|
// Binance uses inclusive start time query range, eg:
|
|
|
|
// https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1m&startTime=1620172860000
|
|
|
|
// the above query will return a kline with startTime = 1620172860000
|
|
|
|
// and,
|
|
|
|
// https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1m&startTime=1620172860000&endTime=1620172920000
|
|
|
|
// the above query will return a kline with startTime = 1620172860000, and a kline with endTime = 1620172860000
|
|
|
|
//
|
|
|
|
// the endTime of a binance kline, is the (startTime + interval time - 1 millisecond), e.g.,
|
|
|
|
// millisecond unix timestamp: 1620172860000 and 1620172919999
|
2020-11-06 13:40:48 +00:00
|
|
|
func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error) {
|
2020-10-10 04:02:06 +00:00
|
|
|
|
2021-05-02 09:46:08 +00:00
|
|
|
var limit = 1000
|
2020-07-15 04:20:44 +00:00
|
|
|
if options.Limit > 0 {
|
2021-05-02 09:46:08 +00:00
|
|
|
// default limit == 1000
|
2020-07-15 04:20:44 +00:00
|
|
|
limit = options.Limit
|
2020-07-11 12:40:19 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 00:36:36 +00:00
|
|
|
log.Infof("querying kline %s %s %v", symbol, interval, options)
|
2020-07-11 05:02:53 +00:00
|
|
|
|
2020-10-06 09:32:41 +00:00
|
|
|
req := e.Client.NewKlinesService().
|
|
|
|
Symbol(symbol).
|
2020-11-06 13:40:48 +00:00
|
|
|
Interval(string(interval)).
|
2020-07-15 04:20:44 +00:00
|
|
|
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
|
2020-09-16 04:28:15 +00:00
|
|
|
for _, k := range resp {
|
2020-07-11 05:02:53 +00:00
|
|
|
kLines = append(kLines, types.KLine{
|
2021-05-31 17:08:02 +00:00
|
|
|
Exchange: types.ExchangeBinance,
|
|
|
|
Symbol: symbol,
|
|
|
|
Interval: interval,
|
|
|
|
StartTime: time.Unix(0, k.OpenTime*int64(time.Millisecond)),
|
|
|
|
EndTime: time.Unix(0, k.CloseTime*int64(time.Millisecond)),
|
|
|
|
Open: util.MustParseFloat(k.Open),
|
|
|
|
Close: util.MustParseFloat(k.Close),
|
|
|
|
High: util.MustParseFloat(k.High),
|
|
|
|
Low: util.MustParseFloat(k.Low),
|
|
|
|
Volume: util.MustParseFloat(k.Volume),
|
|
|
|
QuoteVolume: util.MustParseFloat(k.QuoteAssetVolume),
|
|
|
|
TakerBuyBaseAssetVolume: util.MustParseFloat(k.TakerBuyBaseAssetVolume),
|
|
|
|
TakerBuyQuoteAssetVolume: util.MustParseFloat(k.TakerBuyQuoteAssetVolume),
|
|
|
|
LastTradeID: 0,
|
|
|
|
NumberOfTrades: uint64(k.TradeNum),
|
|
|
|
Closed: true,
|
2020-07-11 05:02:53 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return kLines, nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 10:15:45 +00:00
|
|
|
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
|
2021-01-19 17:27:27 +00:00
|
|
|
var remoteTrades []*binance.TradeV3
|
2020-07-22 04:26:27 +00:00
|
|
|
|
2021-01-19 18:09:12 +00:00
|
|
|
if e.IsMargin {
|
2021-01-19 17:27:27 +00:00
|
|
|
req := e.Client.NewListMarginTradesService().
|
2021-01-19 18:09:12 +00:00
|
|
|
IsIsolated(e.IsIsolatedMargin).
|
2021-01-19 17:27:27 +00:00
|
|
|
Symbol(symbol)
|
2020-07-22 04:26:27 +00:00
|
|
|
|
2021-01-19 17:27:27 +00:00
|
|
|
if options.Limit > 0 {
|
|
|
|
req.Limit(int(options.Limit))
|
2021-02-18 09:37:49 +00:00
|
|
|
} else {
|
|
|
|
req.Limit(1000)
|
2021-01-19 17:27:27 +00:00
|
|
|
}
|
2020-07-22 04:26:27 +00:00
|
|
|
|
2021-01-19 17:27:27 +00:00
|
|
|
if options.StartTime != nil {
|
|
|
|
req.StartTime(options.StartTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
2021-02-18 09:37:49 +00:00
|
|
|
|
2021-01-19 17:27:27 +00:00
|
|
|
if options.EndTime != nil {
|
|
|
|
req.EndTime(options.EndTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
2021-02-18 08:40:47 +00:00
|
|
|
|
2021-02-18 10:20:18 +00:00
|
|
|
// BINANCE uses inclusive last trade ID
|
2021-01-19 17:27:27 +00:00
|
|
|
if options.LastTradeID > 0 {
|
2021-02-18 10:20:18 +00:00
|
|
|
req.FromID(options.LastTradeID)
|
2021-01-19 17:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteTrades, err = req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
req := e.Client.NewListTradesService().
|
|
|
|
Symbol(symbol)
|
|
|
|
|
|
|
|
if options.Limit > 0 {
|
|
|
|
req.Limit(int(options.Limit))
|
2021-02-18 09:37:49 +00:00
|
|
|
} else {
|
|
|
|
req.Limit(1000)
|
2021-01-19 17:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.StartTime != nil {
|
|
|
|
req.StartTime(options.StartTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
|
|
|
if options.EndTime != nil {
|
|
|
|
req.EndTime(options.EndTime.UnixNano() / int64(time.Millisecond))
|
|
|
|
}
|
2021-02-18 09:37:49 +00:00
|
|
|
|
2021-02-18 10:20:18 +00:00
|
|
|
// BINANCE uses inclusive last trade ID
|
2021-01-19 17:27:27 +00:00
|
|
|
if options.LastTradeID > 0 {
|
2021-02-18 10:20:18 +00:00
|
|
|
req.FromID(options.LastTradeID)
|
2021-01-19 17:27:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
remoteTrades, err = req.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-22 04:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range remoteTrades {
|
2021-01-19 18:09:12 +00:00
|
|
|
localTrade, err := ToGlobalTrade(*t, e.IsMargin)
|
2020-07-22 04:26:27 +00:00
|
|
|
if err != nil {
|
2020-08-11 00:36:36 +00:00
|
|
|
log.WithError(err).Errorf("can not convert binance trade: %+v", t)
|
2020-07-26 16:54:49 +00:00
|
|
|
continue
|
2020-07-22 04:26:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 16:54:49 +00:00
|
|
|
trades = append(trades, *localTrade)
|
2020-07-22 04:26:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 16:54:49 +00:00
|
|
|
return trades, nil
|
2020-07-22 04:26:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 13:40:48 +00:00
|
|
|
func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol string, interval types.Interval, startTime, endTime time.Time) ([]types.KLine, error) {
|
2020-08-14 05:08:09 +00:00
|
|
|
var allKLines []types.KLine
|
|
|
|
|
|
|
|
for startTime.Before(endTime) {
|
|
|
|
klines, err := e.QueryKLines(ctx, symbol, interval, types.KLineQueryOptions{
|
|
|
|
StartTime: &startTime,
|
|
|
|
Limit: 1000,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kline := range klines {
|
2020-09-16 04:28:15 +00:00
|
|
|
if kline.EndTime.After(endTime) {
|
2020-08-14 05:08:09 +00:00
|
|
|
return allKLines, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
allKLines = append(allKLines, kline)
|
2020-09-16 04:28:15 +00:00
|
|
|
startTime = kline.EndTime
|
2020-08-14 05:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allKLines, nil
|
2020-08-14 05:47:55 +00:00
|
|
|
}
|
2021-05-31 19:15:19 +00:00
|
|
|
|
2021-10-14 15:01:10 +00:00
|
|
|
type FundingRate struct {
|
|
|
|
FundingRate fixedpoint.Value
|
|
|
|
FundingTime time.Time
|
|
|
|
Time time.Time
|
|
|
|
}
|
|
|
|
|
2021-10-19 07:54:16 +00:00
|
|
|
type PremiumIndex struct {
|
|
|
|
Symbol string `json:"symbol"`
|
|
|
|
MarkPrice fixedpoint.Value `json:"markPrice"`
|
|
|
|
LastFundingRate fixedpoint.Value `json:"lastFundingRate"`
|
|
|
|
NextFundingTime time.Time `json:"nextFundingTime"`
|
|
|
|
Time time.Time `json:"time"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertPremiumIndex(index *futures.PremiumIndex) (*PremiumIndex, error) {
|
|
|
|
markPrice, err := fixedpoint.NewFromString(index.MarkPrice)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lastFundingRate, err := fixedpoint.NewFromString(index.LastFundingRate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nextFundingTime := time.Unix(0, index.NextFundingTime*int64(time.Millisecond))
|
|
|
|
t := time.Unix(0, index.Time*int64(time.Millisecond))
|
|
|
|
|
|
|
|
return &PremiumIndex{
|
|
|
|
Symbol: index.Symbol,
|
|
|
|
MarkPrice: markPrice,
|
|
|
|
NextFundingTime: nextFundingTime,
|
|
|
|
LastFundingRate: lastFundingRate,
|
|
|
|
Time: t,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:01:19 +00:00
|
|
|
func (e *Exchange) QueryPremiumIndex(ctx context.Context, symbol string) (*PremiumIndex, error) {
|
2021-10-19 07:54:16 +00:00
|
|
|
futuresClient := binance.NewFuturesClient(e.key, e.secret)
|
2021-10-20 06:01:19 +00:00
|
|
|
|
|
|
|
// when symbol is set, only one index will be returned.
|
2021-10-19 07:54:16 +00:00
|
|
|
indexes, err := futuresClient.NewPremiumIndexService().Symbol(symbol).Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-10-20 06:01:19 +00:00
|
|
|
return convertPremiumIndex(indexes[0])
|
2021-10-19 07:54:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 07:29:55 +00:00
|
|
|
func (e *Exchange) QueryFundingRateHistory(ctx context.Context, symbol string) (*FundingRate, error) {
|
2021-05-31 19:15:19 +00:00
|
|
|
futuresClient := binance.NewFuturesClient(e.key, e.secret)
|
|
|
|
rates, err := futuresClient.NewFundingRateService().
|
|
|
|
Symbol(symbol).
|
|
|
|
Limit(1).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
2021-10-14 15:01:10 +00:00
|
|
|
return nil, err
|
2021-05-31 19:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(rates) == 0 {
|
2021-10-14 15:01:10 +00:00
|
|
|
return nil, errors.New("empty funding rate data")
|
|
|
|
}
|
|
|
|
|
|
|
|
rate := rates[0]
|
|
|
|
fundingRate, err := fixedpoint.NewFromString(rate.FundingRate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-05-31 19:15:19 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 15:01:10 +00:00
|
|
|
return &FundingRate{
|
|
|
|
FundingRate: fundingRate,
|
|
|
|
FundingTime: time.Unix(0, rate.FundingTime*int64(time.Millisecond)),
|
|
|
|
Time: time.Unix(0, rate.Time*int64(time.Millisecond)),
|
|
|
|
}, nil
|
2021-05-31 19:15:19 +00:00
|
|
|
}
|