binance

package
v1.16.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2021 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const BNB = "BNB"

Variables

This section is empty.

Functions

func ConvertTrades

func ConvertTrades(remoteTrades []*binance.TradeV3) (trades []types.Trade, err error)

ConvertTrades converts the binance v3 trade into the global trade type

func ParseEvent

func ParseEvent(message string) (interface{}, error)

func ToGlobalOrder

func ToGlobalOrder(binanceOrder *binance.Order, isMargin bool) (*types.Order, error)

func ToGlobalOrders

func ToGlobalOrders(binanceOrders []*binance.Order) (orders []types.Order, err error)

func ToGlobalTrade

func ToGlobalTrade(t binance.TradeV3, isMargin bool) (*types.Trade, error)

Types

type Balance

type Balance struct {
	Asset  string `json:"a"`
	Free   string `json:"f"`
	Locked string `json:"l"`
}

outboundAccountInfo

{
  "e": "outboundAccountInfo",   // KLineEvent type
  "E": 1499405658849,           // KLineEvent time
  "m": 0,                       // Maker commission rate (bips)
  "t": 0,                       // Taker commission rate (bips)
  "b": 0,                       // Buyer commission rate (bips)
  "s": 0,                       // Seller commission rate (bips)
  "T": true,                    // Can trade?
  "W": true,                    // Can withdraw?
  "D": true,                    // Can deposit?
  "u": 1499405658848,           // Time of last account update
  "B": [                        // AccountBalances array
    {
      "a": "LTC",               // Asset
      "f": "17366.18538083",    // Free amount
      "l": "0.00000000"         // Locked amount
    },
    {
      "a": "BTC",
      "f": "10537.85314051",
      "l": "2.19464093"
    },
    {
      "a": "ETH",
      "f": "17902.35190619",
      "l": "0.00000000"
    },
    {
      "a": "BNC",
      "f": "1114503.29769312",
      "l": "0.00000000"
    },
    {
      "a": "NEO",
      "f": "0.00000000",
      "l": "0.00000000"
    }
  ],
  "P": [                       // Account Permissions
        "SPOT"
  ]
}

type BalanceUpdateEvent

type BalanceUpdateEvent struct {
	EventBase

	Asset     string `json:"a"`
	Delta     string `json:"d"`
	ClearTime int64  `json:"T"`
}

balanceUpdate

{
  "e": "balanceUpdate",         //KLineEvent Type
  "E": 1573200697110,           //KLineEvent Time
  "a": "BTC",                   //Asset
  "d": "100.00000000",          //Balance Delta
  "T": 1573200697068            //Clear Time
}

type DepthEntry

type DepthEntry struct {
	PriceLevel string
	Quantity   string
}

type DepthEvent

type DepthEvent struct {
	EventBase

	Symbol        string `json:"s"`
	FirstUpdateID int64  `json:"U"`
	FinalUpdateID int64  `json:"u"`

	Bids []DepthEntry
	Asks []DepthEntry
}

func (*DepthEvent) OrderBook

func (e *DepthEvent) OrderBook() (book types.SliceOrderBook, err error)

func (*DepthEvent) String

func (e *DepthEvent) String() (o string)

type DepthFrame

type DepthFrame struct {
	Symbol string
	// contains filtered or unexported fields
}

func (*DepthFrame) EmitPush

func (f *DepthFrame) EmitPush(e DepthEvent)

func (*DepthFrame) EmitReady

func (f *DepthFrame) EmitReady(snapshotDepth DepthEvent, bufEvents []DepthEvent)

func (*DepthFrame) OnPush

func (f *DepthFrame) OnPush(cb func(e DepthEvent))

func (*DepthFrame) OnReady

func (f *DepthFrame) OnReady(cb func(snapshotDepth DepthEvent, bufEvents []DepthEvent))

func (*DepthFrame) PushEvent

func (f *DepthFrame) PushEvent(e DepthEvent)

type EventBase

type EventBase struct {
	Event string `json:"e"` // event
	Time  int64  `json:"E"`
}

kline

{
  "e": "kline",     // KLineEvent type
  "E": 123456789,   // KLineEvent time
  "s": "BNBBTC",    // Symbol
  "k": {
    "t": 123400000, // Kline start time
    "T": 123460000, // Kline close time
    "s": "BNBBTC",  // Symbol
    "i": "1m",      // Interval
    "f": 100,       // First trade ID
    "L": 200,       // Last trade ID
    "o": "0.0010",  // Open price
    "c": "0.0020",  // Close price
    "h": "0.0025",  // High price
    "l": "0.0015",  // Low price
    "v": "1000",    // Base asset volume
    "n": 100,       // Number of trades
    "x": false,     // Is this kline closed?
    "q": "1.0000",  // Quote asset volume
    "V": "500",     // Taker buy base asset volume
    "Q": "0.500",   // Taker buy quote asset volume
    "B": "123456"   // Ignore
  }
}

type Exchange

type Exchange struct {
	types.MarginSettings

	Client *binance.Client
}

func New

func New(key, secret string) *Exchange

func (*Exchange) BatchQueryKLines

func (e *Exchange) BatchQueryKLines(ctx context.Context, symbol string, interval types.Interval, startTime, endTime time.Time) ([]types.KLine, error)

func (*Exchange) CancelOrders

func (e *Exchange) CancelOrders(ctx context.Context, orders ...types.Order) (err2 error)

func (*Exchange) Name

func (e *Exchange) Name() types.ExchangeName

func (*Exchange) NewStream

func (e *Exchange) NewStream() types.Stream

func (*Exchange) PlatformFeeCurrency

func (e *Exchange) PlatformFeeCurrency() string

func (*Exchange) QueryAccount

func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error)

func (*Exchange) QueryAccountBalances

func (e *Exchange) QueryAccountBalances(ctx context.Context) (types.BalanceMap, error)

func (*Exchange) QueryAveragePrice

func (e *Exchange) QueryAveragePrice(ctx context.Context, symbol string) (float64, error)

func (*Exchange) QueryClosedOrders

func (e *Exchange) QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []types.Order, err error)

func (*Exchange) QueryDepositHistory

func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error)

func (*Exchange) QueryIsolatedMarginAccount

func (e *Exchange) QueryIsolatedMarginAccount(ctx context.Context, symbols ...string) (*types.IsolatedMarginAccount, error)

func (*Exchange) QueryKLines

func (e *Exchange) QueryKLines(ctx context.Context, symbol string, interval types.Interval, options types.KLineQueryOptions) ([]types.KLine, error)

QueryKLines queries the Kline/candlestick bars for a symbol. Klines are uniquely identified by their open time. 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

func (*Exchange) QueryMarginAccount

func (e *Exchange) QueryMarginAccount(ctx context.Context) (*types.MarginAccount, error)

func (*Exchange) QueryMarkets

func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error)

func (*Exchange) QueryOpenOrders

func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders []types.Order, err error)

func (*Exchange) QueryTicker

func (e *Exchange) QueryTicker(ctx context.Context, symbol string) (*types.Ticker, error)

func (*Exchange) QueryTickers

func (e *Exchange) QueryTickers(ctx context.Context, symbol ...string) (map[string]types.Ticker, error)

func (*Exchange) QueryTrades

func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error)

func (*Exchange) QueryWithdrawHistory

func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error)

func (*Exchange) SubmitOrders

func (e *Exchange) SubmitOrders(ctx context.Context, orders ...types.SubmitOrder) (createdOrders types.OrderSlice, err error)

func (*Exchange) Withdrawal

func (e *Exchange) Withdrawal(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *types.WithdrawalOptions) error

type ExecutionReportEvent

type ExecutionReportEvent struct {
	EventBase

	Symbol string `json:"s"`
	Side   string `json:"S"`

	ClientOrderID         string `json:"c"`
	OriginalClientOrderID string `json:"C"`

	OrderType         string `json:"o"`
	OrderCreationTime int64  `json:"O"`

	TimeInForce     string `json:"f"`
	IcebergQuantity string `json:"F"`

	OrderQuantity      string `json:"q"`
	QuoteOrderQuantity string `json:"Q"`

	OrderPrice string `json:"p"`
	StopPrice  string `json:"P"`

	IsOnBook bool `json:"w"`

	IsMaker bool `json:"m"`
	Ignore  bool `json:"M"`

	CommissionAmount string `json:"n"`
	CommissionAsset  string `json:"N"`

	CurrentExecutionType string `json:"x"`
	CurrentOrderStatus   string `json:"X"`

	OrderID int64 `json:"i"`
	Ignored int64 `json:"I"`

	TradeID         int64 `json:"t"`
	TransactionTime int64 `json:"T"`

	LastExecutedQuantity string `json:"l"`
	LastExecutedPrice    string `json:"L"`

	CumulativeFilledQuantity               string `json:"z"`
	CumulativeQuoteAssetTransactedQuantity string `json:"Z"`

	LastQuoteAssetTransactedQuantity string `json:"Y"`
}

executionReport

{
  "e": "executionReport",        // Event type
  "E": 1499405658658,            // Event time
  "s": "ETHBTC",                 // Symbol
  "c": "mUvoqJxFIILMdfAW5iGSOW", // Client order ID
  "S": "BUY",                    // Side
  "o": "LIMIT",                  // Order type
  "f": "GTC",                    // Time in force
  "q": "1.00000000",             // Order quantity
  "p": "0.10264410",             // Order price
  "P": "0.00000000",             // Stop price
  "F": "0.00000000",             // Iceberg quantity
  "g": -1,                       // OrderListId
  "C": null,                     // Original client order ID; This is the ID of the order being canceled
  "x": "NEW",                    // Current execution type
  "X": "NEW",                    // Current order status
  "r": "NONE",                   // Order reject reason; will be an error code.
  "i": 4293153,                  // Order ID
  "l": "0.00000000",             // Last executed quantity
  "z": "0.00000000",             // Cumulative filled quantity
  "L": "0.00000000",             // Last executed price
  "n": "0",                      // Commission amount
  "N": null,                     // Commission asset
  "T": 1499405658657,            // Transaction time
  "t": -1,                       // Trade ID
  "I": 8641984,                  // Ignore
  "w": true,                     // Is the order on the book?
  "m": false,                    // Is this trade the maker side?
  "M": false,                    // Ignore
  "O": 1499405658657,            // Order creation time
  "Z": "0.00000000",             // Cumulative quote asset transacted quantity
  "Y": "0.00000000",             // Last quote asset transacted quantity (i.e. lastPrice * lastQty)
  "Q": "0.00000000"              // Quote Order Qty
}

func (*ExecutionReportEvent) Order

func (e *ExecutionReportEvent) Order() (*types.Order, error)

func (*ExecutionReportEvent) Trade

func (e *ExecutionReportEvent) Trade() (*types.Trade, error)

type KLine

type KLine struct {
	StartTime int64 `json:"t"`
	EndTime   int64 `json:"T"`

	Symbol   string `json:"s"`
	Interval string `json:"i"`

	Open  string `json:"o"`
	Close string `json:"c"`
	High  string `json:"h"`
	Low   string `json:"l"`

	Volume      string `json:"v"` // base asset volume (like 10 BTC)
	QuoteVolume string `json:"q"` // quote asset volume

	TakerBaseVolume  string `json:"V"` // taker buy base asset volume (like 10 BTC)
	TakerQuoteVolume string `json:"Q"` // taker buy quote asset volume (like 1000USDT)

	LastTradeID    int   `json:"L"`
	NumberOfTrades int64 `json:"n"`
	Closed         bool  `json:"x"`
}

func (*KLine) KLine

func (k *KLine) KLine() types.KLine

type KLineEvent

type KLineEvent struct {
	EventBase
	Symbol string `json:"s"`
	KLine  KLine  `json:"k,omitempty"`
}

type OutboundAccountInfoEvent

type OutboundAccountInfoEvent struct {
	EventBase

	MakerCommissionRate  int `json:"m"`
	TakerCommissionRate  int `json:"t"`
	BuyerCommissionRate  int `json:"b"`
	SellerCommissionRate int `json:"s"`

	CanTrade    bool `json:"T"`
	CanWithdraw bool `json:"W"`
	CanDeposit  bool `json:"D"`

	LastAccountUpdateTime int `json:"u"`

	Balances    []Balance `json:"B,omitempty"`
	Permissions []string  `json:"P,omitempty"`
}

type OutboundAccountPositionEvent

type OutboundAccountPositionEvent struct {
	EventBase

	LastAccountUpdateTime int       `json:"u"`
	Balances              []Balance `json:"B,omitempty"`
}

type ResultEvent

type ResultEvent struct {
	Result interface{} `json:"result,omitempty"`
	ID     int         `json:"id"`
}

type Stream

type Stream struct {
	types.MarginSettings

	types.StandardStream

	Client    *binance.Client
	ListenKey string
	Conn      *websocket.Conn
	// contains filtered or unexported fields
}

func NewStream

func NewStream(client *binance.Client) *Stream

func (*Stream) Close

func (s *Stream) Close() error

func (*Stream) Connect

func (s *Stream) Connect(ctx context.Context) error

func (*Stream) EmitBalanceUpdateEvent

func (s *Stream) EmitBalanceUpdateEvent(event *BalanceUpdateEvent)

func (*Stream) EmitDepthEvent

func (s *Stream) EmitDepthEvent(e *DepthEvent)

func (*Stream) EmitExecutionReportEvent

func (s *Stream) EmitExecutionReportEvent(event *ExecutionReportEvent)

func (*Stream) EmitKLineClosedEvent

func (s *Stream) EmitKLineClosedEvent(e *KLineEvent)

func (*Stream) EmitKLineEvent

func (s *Stream) EmitKLineEvent(e *KLineEvent)

func (*Stream) EmitOutboundAccountInfoEvent

func (s *Stream) EmitOutboundAccountInfoEvent(event *OutboundAccountInfoEvent)

func (*Stream) EmitOutboundAccountPositionEvent

func (s *Stream) EmitOutboundAccountPositionEvent(event *OutboundAccountPositionEvent)

func (*Stream) OnBalanceUpdateEvent

func (s *Stream) OnBalanceUpdateEvent(cb func(event *BalanceUpdateEvent))

func (*Stream) OnDepthEvent

func (s *Stream) OnDepthEvent(cb func(e *DepthEvent))

func (*Stream) OnExecutionReportEvent

func (s *Stream) OnExecutionReportEvent(cb func(event *ExecutionReportEvent))

func (*Stream) OnKLineClosedEvent

func (s *Stream) OnKLineClosedEvent(cb func(e *KLineEvent))

func (*Stream) OnKLineEvent

func (s *Stream) OnKLineEvent(cb func(e *KLineEvent))

func (*Stream) OnOutboundAccountInfoEvent

func (s *Stream) OnOutboundAccountInfoEvent(cb func(event *OutboundAccountInfoEvent))

func (*Stream) OnOutboundAccountPositionEvent

func (s *Stream) OnOutboundAccountPositionEvent(cb func(event *OutboundAccountPositionEvent))

func (*Stream) SetPublicOnly

func (s *Stream) SetPublicOnly()

type StreamEventHub

type StreamEventHub interface {
	OnDepthEvent(cb func(e *DepthEvent))

	OnKLineEvent(cb func(e *KLineEvent))

	OnKLineClosedEvent(cb func(e *KLineEvent))

	OnBalanceUpdateEvent(cb func(event *BalanceUpdateEvent))

	OnOutboundAccountInfoEvent(cb func(event *OutboundAccountInfoEvent))

	OnOutboundAccountPositionEvent(cb func(event *OutboundAccountPositionEvent))

	OnExecutionReportEvent(cb func(event *ExecutionReportEvent))
}

type StreamRequest

type StreamRequest struct {
	// request ID is required
	ID     int      `json:"id"`
	Method string   `json:"method"`
	Params []string `json:"params"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL