mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 23:16:53 +00:00
Port from idoall's codebase (#161)
* 修复火币Post REST API方法不正确的问题,同时增加火币海带丝交易所 * add vendor folder * 修改命名空间依赖 * 第一次提交分支 * 增加取消订单功能 * 修复binance.GetAccount方法 * 更新readme.md * 增加 Gateio 交易所的支持,支持获取K线、支持的交易对、交易市场参数 * 替换HuobiHadax的参数 * 买/卖订单、取消订单 * OKEX 币币交易:增加获取用户信息,下订单,取消订单 * 测试ok kline * 修复 Bitfinex 的 GetAccountInfo 方法 * 做一些不必要的删减 * 修复binfinex不返回错误的bug * 统一我修改交易所的Kline获取方式 * Bitfinex 增加获取最新价格 * update main.go * 更新GetSymbol方法 * 修改火币和海带丝的Kline编号ID类型 * 修改海带丝的默认配置大小写 * okex增加获取最新价格 * 调整okex的参数判断 * 调整比特儿的参数名称 * 修改火币、火币Hadax的参数全名 * 更新海带丝的配置名称 * 修改bintfinex的GetAccountInfo方法 * 去掉一行注释 * 支持zb交易所的部分功能 * 修复获取K线时没有设置参数的错误 * 增加 Binance 取消订单的方法,获取订单状态,获取所有打开的状态以及所有订单 * 修改获取深度和历史订单的数据 * 修改币安获取深度的参数 * 修改火币获取市场深度的参数 * 修改okex获取市场深度的参数 * 修改币安、OKex获取历史订单的参数 * 修复币安提交参数错误的问题 * merge upstrem * merge后,调整一部分命名空间 * 修改ZB时间参数的命名方式 * 继续替换命名空间 * 命名空间的替换 * 继续命名空间的替换 * 测试 * Port code from idoall's PR * Drop errors dep * Start amending PR * Fix commented code * Translate text from Chinese to English (except for ZB). The reasning behind this is that it's a Chinese exchange and the structs are self explanatory in English, but would for other developers in China * Translate Chinese text, basic formatting changes * Remove commented lines and address feedback on PR
This commit is contained in:
@@ -23,8 +23,8 @@ type Binance struct {
|
||||
WebsocketConn *websocket.Conn
|
||||
|
||||
// valid string list that a required by the exchange
|
||||
validLimits []string
|
||||
validIntervals []string
|
||||
validLimits []int
|
||||
validIntervals []TimeInterval
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -45,7 +45,10 @@ const (
|
||||
// Authenticated endpoints
|
||||
newOrderTest = "/api/v3/order/test"
|
||||
newOrder = "/api/v3/order"
|
||||
cancelOrder = "/api/v3/order"
|
||||
queryOrder = "/api/v3/order"
|
||||
openOrders = "/api/v3/openOrders"
|
||||
allOrders = "/api/v3/allOrders"
|
||||
|
||||
// binance authenticated and unauthenticated limit rates
|
||||
// to-do
|
||||
@@ -130,21 +133,22 @@ func (b *Binance) GetExchangeInfo() (ExchangeInfo, error) {
|
||||
|
||||
// GetOrderBook returns full orderbook information
|
||||
//
|
||||
// OrderBookDataRequestParams contains the following members
|
||||
// symbol: string of currency pair
|
||||
// limit: returned limit amount
|
||||
func (b *Binance) GetOrderBook(symbol string, limit int64) (OrderBook, error) {
|
||||
func (b *Binance) GetOrderBook(obd OrderBookDataRequestParams) (OrderBook, error) {
|
||||
orderbook, resp := OrderBook{}, OrderBookData{}
|
||||
|
||||
if err := b.CheckLimit(limit); err != nil {
|
||||
if err := b.CheckLimit(obd.Limit); err != nil {
|
||||
return orderbook, err
|
||||
}
|
||||
if err := b.CheckSymbol(symbol); err != nil {
|
||||
if err := b.CheckSymbol(obd.Symbol); err != nil {
|
||||
return orderbook, err
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("limit", strconv.FormatInt(limit, 10))
|
||||
params.Set("symbol", common.StringToUpper(obd.Symbol))
|
||||
params.Set("limit", fmt.Sprintf("%d", obd.Limit))
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, orderBookDepth, params.Encode())
|
||||
|
||||
@@ -189,22 +193,13 @@ func (b *Binance) GetOrderBook(symbol string, limit int64) (OrderBook, error) {
|
||||
}
|
||||
|
||||
// GetRecentTrades returns recent trade activity
|
||||
//
|
||||
// symbol: string of currency pair
|
||||
// limit: returned limit amount WARNING: MAX 500!
|
||||
func (b *Binance) GetRecentTrades(symbol string, limit int64) ([]RecentTrade, error) {
|
||||
// limit: Up to 500 results returned
|
||||
func (b *Binance) GetRecentTrades(rtr RecentTradeRequestParams) ([]RecentTrade, error) {
|
||||
resp := []RecentTrade{}
|
||||
|
||||
if err := b.CheckLimit(limit); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if err := b.CheckSymbol(symbol); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("limit", strconv.FormatInt(limit, 10))
|
||||
params.Set("symbol", common.StringToUpper(rtr.Symbol))
|
||||
params.Set("limit", fmt.Sprintf("%d", rtr.Limit))
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, recentTrades, params.Encode())
|
||||
|
||||
@@ -214,21 +209,18 @@ func (b *Binance) GetRecentTrades(symbol string, limit int64) ([]RecentTrade, er
|
||||
// GetHistoricalTrades returns historical trade activity
|
||||
//
|
||||
// symbol: string of currency pair
|
||||
// limit: returned limit amount WARNING: MAX 500! (NOT REQUIRED)
|
||||
// limit: Optional. Default 500; max 1000.
|
||||
// fromID:
|
||||
func (b *Binance) GetHistoricalTrades(symbol string, limit, fromID int64) ([]HistoricalTrade, error) {
|
||||
func (b *Binance) GetHistoricalTrades(symbol string, limit int, fromID int64) ([]HistoricalTrade, error) {
|
||||
resp := []HistoricalTrade{}
|
||||
|
||||
if err := b.CheckLimit(limit); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
if err := b.CheckSymbol(symbol); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("limit", strconv.FormatInt(limit, 10))
|
||||
params.Set("limit", strconv.Itoa(limit))
|
||||
params.Set("fromid", strconv.FormatInt(fromID, 10))
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, historicalTrades, params.Encode())
|
||||
@@ -239,8 +231,8 @@ func (b *Binance) GetHistoricalTrades(symbol string, limit, fromID int64) ([]His
|
||||
// GetAggregatedTrades returns aggregated trade activity
|
||||
//
|
||||
// symbol: string of currency pair
|
||||
// limit: returned limit amount WARNING: MAX 500!
|
||||
func (b *Binance) GetAggregatedTrades(symbol string, limit int64) ([]AggregatedTrade, error) {
|
||||
// limit: Optional. Default 500; max 1000.
|
||||
func (b *Binance) GetAggregatedTrades(symbol string, limit int) ([]AggregatedTrade, error) {
|
||||
resp := []AggregatedTrade{}
|
||||
|
||||
if err := b.CheckLimit(limit); err != nil {
|
||||
@@ -252,36 +244,37 @@ func (b *Binance) GetAggregatedTrades(symbol string, limit int64) ([]AggregatedT
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("limit", strconv.FormatInt(limit, 10))
|
||||
params.Set("limit", strconv.Itoa(limit))
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, aggregatedTrades, params.Encode())
|
||||
|
||||
return resp, b.SendHTTPRequest(path, &resp)
|
||||
}
|
||||
|
||||
// GetCandleStickData returns candle stick data
|
||||
// GetSpotKline returns kline data
|
||||
//
|
||||
// symbol:
|
||||
// limit:
|
||||
// interval
|
||||
func (b *Binance) GetCandleStickData(symbol, interval string, limit int64) ([]CandleStick, error) {
|
||||
// KlinesRequestParams supports 5 parameters
|
||||
// symbol: the symbol to get the kline data for
|
||||
// limit: optinal
|
||||
// interval: the interval time for the data
|
||||
// startTime: startTime filter for kline data
|
||||
// endTime: endTime filter for the kline data
|
||||
func (b *Binance) GetSpotKline(arg KlinesRequestParams) ([]CandleStick, error) {
|
||||
var resp interface{}
|
||||
var kline []CandleStick
|
||||
|
||||
if err := b.CheckLimit(limit); err != nil {
|
||||
return kline, err
|
||||
}
|
||||
if err := b.CheckSymbol(symbol); err != nil {
|
||||
return kline, err
|
||||
}
|
||||
if err := b.CheckIntervals(interval); err != nil {
|
||||
return kline, err
|
||||
}
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("limit", strconv.FormatInt(limit, 10))
|
||||
params.Set("interval", interval)
|
||||
params.Set("symbol", arg.Symbol)
|
||||
params.Set("interval", string(arg.Interval))
|
||||
if arg.Limit != 0 {
|
||||
params.Set("limit", strconv.Itoa(arg.Limit))
|
||||
}
|
||||
if arg.StartTime != 0 {
|
||||
params.Set("startTime", strconv.FormatInt(arg.StartTime, 10))
|
||||
}
|
||||
if arg.EndTime != 0 {
|
||||
params.Set("endTime", strconv.FormatInt(arg.EndTime, 10))
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s%s?%s", apiURL, candleStick, params.Encode())
|
||||
|
||||
@@ -402,19 +395,31 @@ func (b *Binance) NewOrderTest() (interface{}, error) {
|
||||
func (b *Binance) NewOrder(o NewOrderRequest) (NewOrderResponse, error) {
|
||||
var resp NewOrderResponse
|
||||
|
||||
path := fmt.Sprintf("%s%s", apiURL, newOrderTest)
|
||||
path := fmt.Sprintf("%s%s", apiURL, newOrder)
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", o.Symbol)
|
||||
params.Set("side", o.Side)
|
||||
params.Set("type", o.TradeType)
|
||||
params.Set("timeInForce", o.TimeInForce)
|
||||
params.Set("side", string(o.Side))
|
||||
params.Set("type", string(o.TradeType))
|
||||
params.Set("quantity", strconv.FormatFloat(o.Quantity, 'f', -1, 64))
|
||||
params.Set("price", strconv.FormatFloat(o.Price, 'f', -1, 64))
|
||||
params.Set("newClientOrderID", o.NewClientOrderID)
|
||||
params.Set("stopPrice", strconv.FormatFloat(o.StopPrice, 'f', -1, 64))
|
||||
params.Set("icebergQty", strconv.FormatFloat(o.IcebergQty, 'f', -1, 64))
|
||||
params.Set("newOrderRespType", o.NewOrderRespType)
|
||||
params.Set("timeInForce", string(o.TimeInForce))
|
||||
|
||||
if o.NewClientOrderID != "" {
|
||||
params.Set("newClientOrderID", o.NewClientOrderID)
|
||||
}
|
||||
|
||||
if o.StopPrice != 0 {
|
||||
params.Set("stopPrice", strconv.FormatFloat(o.StopPrice, 'f', -1, 64))
|
||||
}
|
||||
|
||||
if o.IcebergQty != 0 {
|
||||
params.Set("icebergQty", strconv.FormatFloat(o.IcebergQty, 'f', -1, 64))
|
||||
}
|
||||
|
||||
if o.NewOrderRespType != "" {
|
||||
params.Set("newOrderRespType", o.NewOrderRespType)
|
||||
}
|
||||
|
||||
if err := b.SendAuthHTTPRequest("POST", path, params, &resp); err != nil {
|
||||
return resp, err
|
||||
@@ -426,6 +431,71 @@ func (b *Binance) NewOrder(o NewOrderRequest) (NewOrderResponse, error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// CancelOrder sends a cancel order to Binance
|
||||
func (b *Binance) CancelOrder(symbol string, orderID int64, origClientOrderID string) (CancelOrderResponse, error) {
|
||||
|
||||
var resp CancelOrderResponse
|
||||
|
||||
path := fmt.Sprintf("%s%s", apiURL, cancelOrder)
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", symbol)
|
||||
|
||||
if orderID != 0 {
|
||||
params.Set("orderId", strconv.FormatInt(orderID, 10))
|
||||
}
|
||||
|
||||
if origClientOrderID != "" {
|
||||
params.Set("origClientOrderId", origClientOrderID)
|
||||
}
|
||||
|
||||
if err := b.SendAuthHTTPRequest("DELETE", path, params, &resp); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// OpenOrders Current open orders
|
||||
// Get all open orders on a symbol. Careful when accessing this with no symbol.
|
||||
func (b *Binance) OpenOrders(symbol string) ([]QueryOrderData, error) {
|
||||
var resp []QueryOrderData
|
||||
|
||||
path := fmt.Sprintf("%s%s", apiURL, openOrders)
|
||||
|
||||
params := url.Values{}
|
||||
if symbol != "" {
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
}
|
||||
if err := b.SendAuthHTTPRequest("GET", path, params, &resp); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// AllOrders Get all account orders; active, canceled, or filled.
|
||||
// orderId optional param
|
||||
// limit optional param, default 500; max 500
|
||||
func (b *Binance) AllOrders(symbol, orderID, limit string) ([]QueryOrderData, error) {
|
||||
var resp []QueryOrderData
|
||||
|
||||
path := fmt.Sprintf("%s%s", apiURL, allOrders)
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
if orderID != "" {
|
||||
params.Set("orderId", orderID)
|
||||
}
|
||||
if limit != "" {
|
||||
params.Set("limit", limit)
|
||||
}
|
||||
if err := b.SendAuthHTTPRequest("GET", path, params, &resp); err != nil {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// QueryOrder returns information on a past order
|
||||
func (b *Binance) QueryOrder(symbol, origClientOrderID string, orderID int64) (QueryOrderData, error) {
|
||||
var resp QueryOrderData
|
||||
@@ -434,8 +504,12 @@ func (b *Binance) QueryOrder(symbol, origClientOrderID string, orderID int64) (Q
|
||||
|
||||
params := url.Values{}
|
||||
params.Set("symbol", common.StringToUpper(symbol))
|
||||
params.Set("origClientOrderId", origClientOrderID)
|
||||
params.Set("orderId", strconv.FormatInt(orderID, 10))
|
||||
if origClientOrderID != "" {
|
||||
params.Set("origClientOrderId", origClientOrderID)
|
||||
}
|
||||
if orderID != 0 {
|
||||
params.Set("orderId", strconv.FormatInt(orderID, 10))
|
||||
}
|
||||
|
||||
if err := b.SendAuthHTTPRequest("GET", path, params, &resp); err != nil {
|
||||
return resp, err
|
||||
@@ -484,7 +558,7 @@ func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, re
|
||||
if params == nil {
|
||||
params = url.Values{}
|
||||
}
|
||||
params.Set("recvWindow", strconv.FormatInt(5000, 10))
|
||||
params.Set("recvWindow", strconv.FormatInt(common.RecvWindow(5*time.Second), 10))
|
||||
params.Set("timestamp", strconv.FormatInt(time.Now().Unix()*1000, 10))
|
||||
|
||||
signature := params.Encode()
|
||||
@@ -494,7 +568,6 @@ func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, re
|
||||
|
||||
headers := make(map[string]string)
|
||||
headers["X-MBX-APIKEY"] = b.APIKey
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
|
||||
if b.Verbose {
|
||||
log.Printf("sent path: \n%s\n", path)
|
||||
@@ -505,16 +578,18 @@ func (b *Binance) SendAuthHTTPRequest(method, path string, params url.Values, re
|
||||
}
|
||||
|
||||
// CheckLimit checks value against a variable list
|
||||
func (b *Binance) CheckLimit(limit int64) error {
|
||||
if !common.StringDataCompare(b.validLimits, strconv.FormatInt(limit, 10)) {
|
||||
return errors.New("Incorrect limit values - valid values are 5, 10, 20, 50, 100, 500, 1000")
|
||||
func (b *Binance) CheckLimit(limit int) error {
|
||||
for x := range b.validLimits {
|
||||
if b.validLimits[x] == limit {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return errors.New("Incorrect limit values - valid values are 5, 10, 20, 50, 100, 500, 1000")
|
||||
}
|
||||
|
||||
// CheckSymbol checks value against a variable list
|
||||
func (b *Binance) CheckSymbol(symbol string) error {
|
||||
enPairs := b.GetEnabledCurrencies()
|
||||
enPairs := b.GetAvailableCurrencies()
|
||||
for x := range enPairs {
|
||||
if exchange.FormatExchangeCurrency(b.Name, enPairs[x]).String() == symbol {
|
||||
return nil
|
||||
@@ -525,14 +600,32 @@ func (b *Binance) CheckSymbol(symbol string) error {
|
||||
|
||||
// CheckIntervals checks value against a variable list
|
||||
func (b *Binance) CheckIntervals(interval string) error {
|
||||
if !common.StringDataCompare(b.validIntervals, interval) {
|
||||
return errors.New(`Incorrect interval values - valid values are "1m","3m","5m","15m","30m","1h","2h","4h","6h","8h","12h","1d","3d","1w","1M"`)
|
||||
for x := range b.validIntervals {
|
||||
if TimeInterval(interval) == b.validIntervals[x] {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return errors.New(`Incorrect interval values - valid values are "1m","3m","5m","15m","30m","1h","2h","4h","6h","8h","12h","1d","3d","1w","1M"`)
|
||||
}
|
||||
|
||||
// SetValues sets the default valid values
|
||||
func (b *Binance) SetValues() {
|
||||
b.validLimits = []string{"5", "10", "20", "50", "100", "500", "1000"}
|
||||
b.validIntervals = []string{"1m", "3m", "5m", "15m", "30m", "1h", "2h", "4h", "6h", "8h", "12h", "1d", "3d", "1w", "1M"}
|
||||
b.validLimits = []int{5, 10, 20, 50, 100, 500, 1000}
|
||||
b.validIntervals = []TimeInterval{
|
||||
TimeIntervalMinute,
|
||||
TimeIntervalThreeMinutes,
|
||||
TimeIntervalFiveMinutes,
|
||||
TimeIntervalFifteenMinutes,
|
||||
TimeIntervalThirtyMinutes,
|
||||
TimeIntervalHour,
|
||||
TimeIntervalTwoHours,
|
||||
TimeIntervalFourHours,
|
||||
TimeIntervalSixHours,
|
||||
TimeIntervalEightHours,
|
||||
TimeIntervalTwelveHours,
|
||||
TimeIntervalDay,
|
||||
TimeIntervalThreeDays,
|
||||
TimeIntervalWeek,
|
||||
TimeIntervalMonth,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,11 @@ func TestGetExchangeValidCurrencyPairs(t *testing.T) {
|
||||
|
||||
func TestGetOrderBook(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := b.GetOrderBook("BTCUSDT", 5)
|
||||
_, err := b.GetOrderBook(OrderBookDataRequestParams{
|
||||
Symbol: "BTCUSDT",
|
||||
Limit: 10,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance GetOrderBook() error", err)
|
||||
}
|
||||
@@ -51,7 +55,12 @@ func TestGetOrderBook(t *testing.T) {
|
||||
|
||||
func TestGetRecentTrades(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := b.GetRecentTrades("BTCUSDT", 5)
|
||||
|
||||
_, err := b.GetRecentTrades(RecentTradeRequestParams{
|
||||
Symbol: "BTCUSDT",
|
||||
Limit: 15,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance GetRecentTrades() error", err)
|
||||
}
|
||||
@@ -73,11 +82,15 @@ func TestGetAggregatedTrades(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCandleStickData(t *testing.T) {
|
||||
func TestGetSpotKline(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := b.GetCandleStickData("BTCUSDT", "1d", 5)
|
||||
_, err := b.GetSpotKline(KlinesRequestParams{
|
||||
Symbol: "BTCUSDT",
|
||||
Interval: TimeIntervalFiveMinutes,
|
||||
Limit: 24,
|
||||
})
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance GetCandleStickData() error", err)
|
||||
t.Error("Test Failed - Binance GetSpotKline() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,16 +136,72 @@ func TestNewOrderTest(t *testing.T) {
|
||||
|
||||
func TestNewOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := b.NewOrder(NewOrderRequest{})
|
||||
|
||||
if testAPIKey == "" || testAPISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
_, err := b.NewOrder(NewOrderRequest{
|
||||
Symbol: "BTCUSDT",
|
||||
Side: BinanceRequestParamsSideSell,
|
||||
TradeType: BinanceRequestParamsOrderLimit,
|
||||
TimeInForce: BinanceRequestParamsTimeGTC,
|
||||
Quantity: 0.01,
|
||||
Price: 1536.1,
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Error("Test Failed - Binance NewOrder() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCancelOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if testAPIKey == "" || testAPISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
_, err := b.CancelOrder("BTCUSDT", 82584683, "")
|
||||
if err == nil {
|
||||
t.Error("Test Failed - Binance CancelOrder() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := b.QueryOrder("", "", 1337)
|
||||
if err == nil {
|
||||
|
||||
if testAPIKey == "" || testAPISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
_, err := b.QueryOrder("BTCUSDT", "", 1337)
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance QueryOrder() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if testAPIKey == "" || testAPISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
_, err := b.OpenOrders("BTCUSDT")
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance OpenOrders() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllOrders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if testAPIKey == "" || testAPISecret == "" {
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
_, err := b.AllOrders("BTCUSDT", "", "")
|
||||
if err != nil {
|
||||
t.Error("Test Failed - Binance AllOrders() error", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package binance
|
||||
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
)
|
||||
@@ -45,6 +44,12 @@ type ExchangeInfo struct {
|
||||
} `json:"symbols"`
|
||||
}
|
||||
|
||||
// OrderBookDataRequestParams represents Klines request data.
|
||||
type OrderBookDataRequestParams struct {
|
||||
Symbol string `json:"symbol"` // Required field; example LTCBTC,BTCUSDT
|
||||
Limit int `json:"limit"` // Default 100; max 1000. Valid limits:[5, 10, 20, 50, 100, 500, 1000]
|
||||
}
|
||||
|
||||
// OrderBookData is resp data from orderbook endpoint
|
||||
type OrderBookData struct {
|
||||
Code int `json:"code"`
|
||||
@@ -68,6 +73,12 @@ type OrderBook struct {
|
||||
}
|
||||
}
|
||||
|
||||
// RecentTradeRequestParams represents Klines request data.
|
||||
type RecentTradeRequestParams struct {
|
||||
Symbol string `json:"symbol"` // Required field. example LTCBTC, BTCUSDT
|
||||
Limit int `json:"limit"` // Default 500; max 500.
|
||||
}
|
||||
|
||||
// RecentTrade holds recent trade data
|
||||
type RecentTrade struct {
|
||||
Code int `json:"code"`
|
||||
@@ -75,16 +86,18 @@ type RecentTrade struct {
|
||||
ID int64 `json:"id"`
|
||||
Price float64 `json:"price,string"`
|
||||
Quantity float64 `json:"qty,string"`
|
||||
Time int64 `json:"time"`
|
||||
Time float64 `json:"time"`
|
||||
IsBuyerMaker bool `json:"isBuyerMaker"`
|
||||
IsBestMatch bool `json:"isBestMatch"`
|
||||
}
|
||||
|
||||
// MultiStreamData holds stream data
|
||||
type MultiStreamData struct {
|
||||
Stream string `json:"stream"`
|
||||
Data json.RawMessage `json:"data"`
|
||||
}
|
||||
|
||||
// TradeStream holds the trade stream data
|
||||
type TradeStream struct {
|
||||
EventType string `json:"e"`
|
||||
EventTime int64 `json:"E"`
|
||||
@@ -99,6 +112,7 @@ type TradeStream struct {
|
||||
BestMatchPrice bool `json:"M"`
|
||||
}
|
||||
|
||||
// KlineStream holds the kline stream data
|
||||
type KlineStream struct {
|
||||
EventType string `json:"e"`
|
||||
EventTime int64 `json:"E"`
|
||||
@@ -123,6 +137,7 @@ type KlineStream struct {
|
||||
} `json:"k"`
|
||||
}
|
||||
|
||||
// TickerStream holds the ticker stream data
|
||||
type TickerStream struct {
|
||||
EventType string `json:"e"`
|
||||
EventTime int64 `json:"E"`
|
||||
@@ -228,15 +243,21 @@ type BestPrice struct {
|
||||
|
||||
// NewOrderRequest request type
|
||||
type NewOrderRequest struct {
|
||||
Symbol string
|
||||
Side string
|
||||
TradeType string
|
||||
TimeInForce string
|
||||
// Symbol (currency pair to trade)
|
||||
Symbol string
|
||||
// Side Buy or Sell
|
||||
Side RequestParamsSideType
|
||||
// TradeType (market or limit order)
|
||||
TradeType RequestParamsOrderType
|
||||
// TimeInForce specifies how long the order remains in effect.
|
||||
// Examples are (Good Till Cancel (GTC), Immediate or Cancel (IOC) and Fill Or Kill (FOK))
|
||||
TimeInForce RequestParamsTimeForceType
|
||||
// Quantity
|
||||
Quantity float64
|
||||
Price float64
|
||||
NewClientOrderID string
|
||||
StopPrice float64
|
||||
IcebergQty float64
|
||||
StopPrice float64 //Used with STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, and TAKE_PROFIT_LIMIT orders.
|
||||
IcebergQty float64 //Used with LIMIT, STOP_LOSS_LIMIT, and TAKE_PROFIT_LIMIT to create an iceberg order.
|
||||
NewOrderRespType string
|
||||
}
|
||||
|
||||
@@ -263,6 +284,14 @@ type NewOrderResponse struct {
|
||||
} `json:"fills"`
|
||||
}
|
||||
|
||||
// CancelOrderResponse is the return structured response from the exchange
|
||||
type CancelOrderResponse struct {
|
||||
Symbol string `json:"symbol"`
|
||||
OrigClientOrderID string `json:"origClientOrderId"`
|
||||
OrderID int64 `json:"orderId"`
|
||||
ClientOrderID string `json:"clientOrderId"`
|
||||
}
|
||||
|
||||
// QueryOrderData holds query order data
|
||||
type QueryOrderData struct {
|
||||
Code int `json:"code"`
|
||||
@@ -279,7 +308,7 @@ type QueryOrderData struct {
|
||||
Side string `json:"side"`
|
||||
StopPrice float64 `json:"stopPrice,string"`
|
||||
IcebergQty float64 `json:"icebergQty,string"`
|
||||
Time int64 `json:"time"`
|
||||
Time float64 `json:"time"`
|
||||
IsWorking bool `json:"isWorking"`
|
||||
}
|
||||
|
||||
@@ -302,3 +331,85 @@ type Account struct {
|
||||
UpdateTime int64 `json:"updateTime"`
|
||||
Balances []Balance `json:"balances"`
|
||||
}
|
||||
|
||||
// RequestParamsSideType trade order side (buy or sell)
|
||||
type RequestParamsSideType string
|
||||
|
||||
var (
|
||||
// BinanceRequestParamsSideBuy buy order type
|
||||
BinanceRequestParamsSideBuy = RequestParamsSideType("BUY")
|
||||
|
||||
// BinanceRequestParamsSideSell sell order type
|
||||
BinanceRequestParamsSideSell = RequestParamsSideType("SELL")
|
||||
)
|
||||
|
||||
// RequestParamsTimeForceType Time in force
|
||||
type RequestParamsTimeForceType string
|
||||
|
||||
var (
|
||||
// BinanceRequestParamsTimeGTC GTC
|
||||
BinanceRequestParamsTimeGTC = RequestParamsTimeForceType("GTC")
|
||||
|
||||
// BinanceRequestParamsTimeIOC IOC
|
||||
BinanceRequestParamsTimeIOC = RequestParamsTimeForceType("IOC")
|
||||
|
||||
// BinanceRequestParamsTimeFOK FOK
|
||||
BinanceRequestParamsTimeFOK = RequestParamsTimeForceType("FOK")
|
||||
)
|
||||
|
||||
// RequestParamsOrderType trade order type
|
||||
type RequestParamsOrderType string
|
||||
|
||||
var (
|
||||
// BinanceRequestParamsOrderLimit Limit order
|
||||
BinanceRequestParamsOrderLimit = RequestParamsOrderType("LIMIT")
|
||||
|
||||
// BinanceRequestParamsOrderMarket Market order
|
||||
BinanceRequestParamsOrderMarket = RequestParamsOrderType("MARKET")
|
||||
|
||||
// BinanceRequestParamsOrderStopLoss STOP_LOSS
|
||||
BinanceRequestParamsOrderStopLoss = RequestParamsOrderType("STOP_LOSS")
|
||||
|
||||
// BinanceRequestParamsOrderStopLossLimit STOP_LOSS_LIMIT
|
||||
BinanceRequestParamsOrderStopLossLimit = RequestParamsOrderType("STOP_LOSS_LIMIT")
|
||||
|
||||
// BinanceRequestParamsOrderTakeProfit TAKE_PROFIT
|
||||
BinanceRequestParamsOrderTakeProfit = RequestParamsOrderType("TAKE_PROFIT")
|
||||
|
||||
// BinanceRequestParamsOrderTakeProfitLimit TAKE_PROFIT_LIMIT
|
||||
BinanceRequestParamsOrderTakeProfitLimit = RequestParamsOrderType("TAKE_PROFIT_LIMIT")
|
||||
|
||||
// BinanceRequestParamsOrderLimitMarker LIMIT_MAKER
|
||||
BinanceRequestParamsOrderLimitMarker = RequestParamsOrderType("LIMIT_MAKER")
|
||||
)
|
||||
|
||||
// KlinesRequestParams represents Klines request data.
|
||||
type KlinesRequestParams struct {
|
||||
Symbol string // Required field; example LTCBTC, BTCUSDT
|
||||
Interval TimeInterval // Time interval period
|
||||
Limit int // Default 500; max 500.
|
||||
StartTime int64
|
||||
EndTime int64
|
||||
}
|
||||
|
||||
// TimeInterval represents interval enum.
|
||||
type TimeInterval string
|
||||
|
||||
// Vars related to time intervals
|
||||
var (
|
||||
TimeIntervalMinute = TimeInterval("1m")
|
||||
TimeIntervalThreeMinutes = TimeInterval("3m")
|
||||
TimeIntervalFiveMinutes = TimeInterval("5m")
|
||||
TimeIntervalFifteenMinutes = TimeInterval("15m")
|
||||
TimeIntervalThirtyMinutes = TimeInterval("30m")
|
||||
TimeIntervalHour = TimeInterval("1h")
|
||||
TimeIntervalTwoHours = TimeInterval("2h")
|
||||
TimeIntervalFourHours = TimeInterval("4h")
|
||||
TimeIntervalSixHours = TimeInterval("6h")
|
||||
TimeIntervalEightHours = TimeInterval("8h")
|
||||
TimeIntervalTwelveHours = TimeInterval("12h")
|
||||
TimeIntervalDay = TimeInterval("1d")
|
||||
TimeIntervalThreeDays = TimeInterval("3d")
|
||||
TimeIntervalWeek = TimeInterval("1w")
|
||||
TimeIntervalMonth = TimeInterval("1M")
|
||||
)
|
||||
|
||||
@@ -15,6 +15,7 @@ const (
|
||||
binancePingPeriod = 20 * time.Second
|
||||
)
|
||||
|
||||
// WebsocketClient starts and handles the websocket client connection
|
||||
func (b *Binance) WebsocketClient() {
|
||||
for b.Enabled && b.Websocket {
|
||||
var Dialer websocket.Dialer
|
||||
|
||||
@@ -106,7 +106,7 @@ func (b *Binance) GetOrderbookEx(currency pair.CurrencyPair, assetType string) (
|
||||
// UpdateOrderbook updates and returns the orderbook for a currency pair
|
||||
func (b *Binance) UpdateOrderbook(p pair.CurrencyPair, assetType string) (orderbook.Base, error) {
|
||||
var orderBook orderbook.Base
|
||||
orderbookNew, err := b.GetOrderBook(exchange.FormatExchangeCurrency(b.Name, p).String(), 1000)
|
||||
orderbookNew, err := b.GetOrderBook(OrderBookDataRequestParams{Symbol: exchange.FormatExchangeCurrency(b.Name, p).String(), Limit: 1000})
|
||||
if err != nil {
|
||||
return orderBook, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user