github.com/frankrap/okex-api@v1.0.4/spot_rest_api.go (about)

     1  package okex
     2  
     3  import "strings"
     4  
     5  /*
     6  币币账户信息
     7  获取币币账户资产列表(仅展示拥有资金的币对),查询各币种的余额、冻结和可用等信息。
     8  
     9  限速规则:20次/2s
    10  HTTP请求
    11  GET /api/spot/v3/accounts
    12  
    13  */
    14  func (client *Client) GetSpotAccounts() (GetSpotAccountsResult, error) { //*[]map[string]interface{}
    15  	//r := []map[string]interface{}{}
    16  	var r GetSpotAccountsResult
    17  
    18  	if _, _, err := client.Request(GET, SPOT_ACCOUNTS, nil, &r); err != nil {
    19  		return nil, err
    20  	}
    21  	return r, nil
    22  }
    23  
    24  /*
    25  单一币种账户信息
    26  获取币币账户单个币种的余额、冻结和可用等信息。
    27  
    28  限速规则:20次/2s
    29  HTTP请求
    30  GET /api/spot/v3/accounts/<currency>
    31  */
    32  func (client *Client) GetSpotAccountsCurrency(currency string) (GetSpotAccountsCurrencyResult, error) {
    33  	var r GetSpotAccountsCurrencyResult
    34  	uri := GetCurrencyUri(SPOT_ACCOUNTS_CURRENCY, currency)
    35  	_, _, err := client.Request(GET, uri, nil, &r)
    36  	return r, err
    37  }
    38  
    39  /*
    40  账单流水查询
    41  列出账户资产流水。账户资产流水是指导致账户余额增加或减少的行为。流水会分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他记录。
    42  
    43  限速规则:20次/2s
    44  HTTP请求
    45  GET /api/spot/v3/accounts/<currency>/ledger
    46  */
    47  func (client *Client) GetSpotAccountsCurrencyLeger(currency string, optionalParams *map[string]string) (*[]map[string]interface{}, error) {
    48  	r := []map[string]interface{}{}
    49  
    50  	baseUri := GetCurrencyUri(SPOT_ACCOUNTS_CURRENCY_LEDGER, currency)
    51  	uri := baseUri
    52  	if optionalParams != nil && len(*optionalParams) > 0 {
    53  		uri = BuildParams(baseUri, *optionalParams)
    54  	}
    55  
    56  	if _, _, err := client.Request(GET, uri, nil, &r); err != nil {
    57  		return nil, err
    58  	}
    59  	return &r, nil
    60  }
    61  
    62  /*
    63  获取订单列表
    64  列出您当前所有的订单信息。这个请求支持分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他纪录。
    65  
    66  限速规则:20次/2s
    67  HTTP请求
    68  GET /api/spot/v3/orders
    69  */
    70  func (client *Client) GetSpotOrders(status, instrument_id string, options *map[string]string) (*[]map[string]interface{}, error) {
    71  	r := []map[string]interface{}{}
    72  
    73  	fullOptions := NewParams()
    74  	fullOptions["instrument_id"] = instrument_id
    75  	fullOptions["status"] = status
    76  	if options != nil && len(*options) > 0 {
    77  		fullOptions["from"] = (*options)["from"]
    78  		fullOptions["to"] = (*options)["to"]
    79  		fullOptions["limit"] = (*options)["limit"]
    80  	}
    81  
    82  	uri := BuildParams(SPOT_ORDERS, fullOptions)
    83  
    84  	if _, _, err := client.Request(GET, uri, nil, &r); err != nil {
    85  		return nil, err
    86  	}
    87  	return &r, nil
    88  }
    89  
    90  /*
    91  获取所有未成交订单
    92  列出您当前所有的订单信息。这个请求支持分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他纪录。
    93  
    94  限速规则:20次/2s
    95  HTTP请求
    96  GET /api/spot/v3/orders_pending
    97  */
    98  func (client *Client) GetSpotOrdersPending(options *map[string]string) (*[]map[string]interface{}, error) {
    99  	r := []map[string]interface{}{}
   100  
   101  	fullOptions := NewParams()
   102  	uri := SPOT_ORDERS_PENDING
   103  	if options != nil && len(*options) > 0 {
   104  		fullOptions["instrument_id"] = (*options)["instrument_id"]
   105  		fullOptions["from"] = (*options)["from"]
   106  		fullOptions["to"] = (*options)["to"]
   107  		fullOptions["limit"] = (*options)["limit"]
   108  		uri = BuildParams(SPOT_ORDERS_PENDING, fullOptions)
   109  	}
   110  
   111  	if _, _, err := client.Request(GET, uri, nil, &r); err != nil {
   112  		return nil, err
   113  	}
   114  	return &r, nil
   115  }
   116  
   117  /*
   118  获取订单信息
   119  通过订单ID获取单个订单信息。
   120  
   121  限速规则:20次/2s
   122  
   123  HTTP请求
   124  GET /api/spot/v3/orders/<order_id>
   125  或者
   126  GET /api/spot/v3/orders/<client_oid>
   127  */
   128  func (client *Client) GetSpotOrdersById(instrumentId, orderOrClientId string) (SpotGetOrderResult, error) {
   129  	var order SpotGetOrderResult
   130  	uri := strings.Replace(SPOT_ORDERS_BY_ID, "{order_client_id}", orderOrClientId, -1)
   131  	options := NewParams()
   132  	options["instrument_id"] = instrumentId
   133  	uri = BuildParams(uri, options)
   134  
   135  	_, _, err := client.Request(GET, uri, nil, &order)
   136  	return order, err
   137  }
   138  
   139  /*
   140  获取成交明细
   141  获取最近的成交明细表。这个请求支持分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他记录。
   142  
   143  限速规则:20次/2s
   144  HTTP请求
   145  GET /api/spot/v3/fills
   146  */
   147  func (client *Client) GetSpotFills(order_id, instrument_id string, options *map[string]string) (*[]map[string]interface{}, error) {
   148  	r := []map[string]interface{}{}
   149  
   150  	fullOptions := NewParams()
   151  	fullOptions["instrument_id"] = instrument_id
   152  	fullOptions["order_id"] = order_id
   153  	if options != nil && len(*options) > 0 {
   154  		fullOptions["from"] = (*options)["from"]
   155  		fullOptions["to"] = (*options)["to"]
   156  		fullOptions["limit"] = (*options)["limit"]
   157  	}
   158  
   159  	uri := BuildParams(SPOT_FILLS, fullOptions)
   160  
   161  	if _, _, err := client.Request(GET, uri, nil, &r); err != nil {
   162  		return nil, err
   163  	}
   164  	return &r, nil
   165  }
   166  
   167  /*
   168  获取币对信息
   169  用于获取行情数据,这组公开接口提供了行情数据的快照,无需认证即可调用。
   170  
   171  获取交易币对的列表,查询各币对的交易限制和价格步长等信息。
   172  
   173  限速规则:20次/2s
   174  HTTP请求
   175  GET /api/spot/v3/instruments
   176  */
   177  func (client *Client) GetSpotInstruments() (*[]map[string]interface{}, error) {
   178  	r := []map[string]interface{}{}
   179  
   180  	if _, _, err := client.Request(GET, SPOT_INSTRUMENTS, nil, &r); err != nil {
   181  		return nil, err
   182  	}
   183  	return &r, nil
   184  }
   185  
   186  /*
   187  获取深度数据
   188  获取币对的深度列表。这个请求不支持分页,一个请求返回整个深度列表。
   189  
   190  限速规则:20次/2s
   191  HTTP请求
   192  GET /api/spot/v3/instruments/<instrument_id>/book
   193  */
   194  func (client *Client) GetSpotInstrumentBook(instrumentId string, optionalParams map[string]string) (SpotInstrumentBookResult, error) {
   195  	var book SpotInstrumentBookResult
   196  	uri := GetInstrumentIdUri(SPOT_INSTRUMENT_BOOK, instrumentId)
   197  	if optionalParams != nil && len(optionalParams) > 0 {
   198  		optionals := NewParams()
   199  		if size, ok := optionalParams["size"]; ok {
   200  			optionals["size"] = size
   201  		}
   202  		if depth, ok := optionalParams["depth"]; ok {
   203  			optionals["depth"] = depth
   204  		}
   205  		uri = BuildParams(uri, optionals)
   206  	}
   207  
   208  	_, _, err := client.Request(GET, uri, nil, &book)
   209  	return book, err
   210  }
   211  
   212  /*
   213  获取全部ticker信息
   214  获取平台全部币对的最新成交价、买一价、卖一价和24小时交易量的快照信息。
   215  
   216  限速规则:50次/2s
   217  HTTP请求
   218  GET /api/spot/v3/instruments/ticker
   219  */
   220  func (client *Client) GetSpotInstrumentsTicker() (*[]map[string]interface{}, error) {
   221  	r := []map[string]interface{}{}
   222  
   223  	if _, _, err := client.Request(GET, SPOT_INSTRUMENTS_TICKER, nil, &r); err != nil {
   224  		return nil, err
   225  	}
   226  	return &r, nil
   227  }
   228  
   229  /*
   230  获取某个ticker信息
   231  获取币对的最新成交价、买一价、卖一价和24小时交易量的快照信息。
   232  
   233  限速规则:20次/2s
   234  HTTP请求
   235  GET /api/spot/v3/instruments/<instrument-id>/ticker
   236  */
   237  func (client *Client) GetSpotInstrumentTicker(instrument_id string) (*map[string]interface{}, error) {
   238  	r := map[string]interface{}{}
   239  
   240  	uri := GetInstrumentIdUri(SPOT_INSTRUMENT_TICKER, instrument_id)
   241  	if _, _, err := client.Request(GET, uri, nil, &r); err != nil {
   242  		return nil, err
   243  	}
   244  	return &r, nil
   245  }
   246  
   247  /*
   248  获取成交数据
   249  获取币对最新的60条成交列表。这个请求支持分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他纪录。
   250  
   251  限速规则:20次/2s
   252  HTTP请求
   253  GET /api/spot/v3/instruments/<instrument_id>/trades
   254  */
   255  func (client *Client) GetSpotInstrumentTrade(instrument_id string, options *map[string]string) ([]byte, *[]map[string]interface{}, error) {
   256  	r := []map[string]interface{}{}
   257  
   258  	uri := GetInstrumentIdUri(SPOT_INSTRUMENT_TRADES, instrument_id)
   259  	fullOptions := NewParams()
   260  	if options != nil && len(*options) > 0 {
   261  		fullOptions["from"] = (*options)["from"]
   262  		fullOptions["to"] = (*options)["to"]
   263  		fullOptions["limit"] = (*options)["limit"]
   264  		uri = BuildParams(uri, fullOptions)
   265  	}
   266  
   267  	var respBody []byte
   268  	if respBody, _, err := client.Request(GET, uri, nil, &r); err != nil {
   269  		return respBody, nil, err
   270  	}
   271  	return respBody, &r, nil
   272  }
   273  
   274  /*
   275  获取成交数据
   276  获取币对最新的60条成交列表。这个请求支持分页,并且按时间倒序排序和存储,最新的排在最前面。请参阅分页部分以获取第一页之后的其他纪录。
   277  
   278  限速规则:20次/2s
   279  HTTP请求
   280  GET /api/spot/v3/instruments/<instrument_id>/candles
   281  */
   282  func (client *Client) GetSpotInstrumentCandles(instrumentID string, options *map[string]string) ([]byte, *[]interface{}, error) {
   283  	r := []interface{}{}
   284  
   285  	uri := GetInstrumentIdUri(SPOT_INSTRUMENT_CANDLES, instrumentID)
   286  	fullOptions := NewParams()
   287  	if options != nil && len(*options) > 0 {
   288  		fullOptions["start"] = (*options)["start"]
   289  		fullOptions["end"] = (*options)["end"]
   290  		fullOptions["granularity"] = (*options)["granularity"]
   291  		uri = BuildParams(uri, fullOptions)
   292  	}
   293  
   294  	var respBody []byte
   295  	if respBody, _, err := client.Request(GET, uri, nil, &r); err != nil {
   296  		return respBody, nil, err
   297  	}
   298  	return respBody, &r, nil
   299  }
   300  
   301  /*
   302  下单
   303  OKEx币币交易提供限价单和市价单两种下单模式(更多下单模式将会在后期支持)。只有当您的账户有足够的资金才能下单。
   304  
   305  一旦下单,您的账户资金将在订单生命周期内被冻结。被冻结的资金以及数量取决于订单指定的类型和参数。
   306  
   307  限速规则:100次/2s
   308  HTTP请求
   309  POST /api/spot/v3/orders
   310  */
   311  func (client *Client) PostSpotOrders(side, instrumentID string, optionalOrderInfo *map[string]string) (respBody []byte, result SpotNewOrderResult, err error) {
   312  	var r SpotNewOrderResult
   313  	postParams := NewParams()
   314  	postParams["side"] = side
   315  	postParams["instrument_id"] = instrumentID
   316  
   317  	if optionalOrderInfo != nil && len(*optionalOrderInfo) > 0 {
   318  		postParams["client_oid"] = (*optionalOrderInfo)["client_oid"]
   319  		postParams["type"] = (*optionalOrderInfo)["type"]
   320  		postParams["margin_trading"] = (*optionalOrderInfo)["margin_trading"]
   321  
   322  		if postParams["type"] == "limit" {
   323  			postParams["price"] = (*optionalOrderInfo)["price"]
   324  			postParams["size"] = (*optionalOrderInfo)["size"]
   325  		} else if postParams["type"] == "market" {
   326  			postParams["size"] = (*optionalOrderInfo)["size"]
   327  			postParams["notional"] = (*optionalOrderInfo)["notional"]
   328  		}
   329  	}
   330  
   331  	respBody, _, err = client.Request(POST, SPOT_ORDERS, postParams, &r)
   332  	return respBody, r, err
   333  }
   334  
   335  /*
   336  批量下单
   337  下指定币对的多个订单(每次只能下最多4个币对且每个币对可批量下10个单)。
   338  
   339  限速规则:50次/2s
   340  HTTP请求
   341  POST /api/spot/v3/batch_orders
   342  */
   343  func (client *Client) PostSpotBatchOrders(orderInfos *[]map[string]string) ([]byte, *map[string]interface{}, error) {
   344  	r := map[string]interface{}{}
   345  	var respBody []byte
   346  	var err error
   347  	if respBody, _, err = client.Request(POST, SPOT_BATCH_ORDERS, orderInfos, &r); err != nil {
   348  		return respBody, nil, err
   349  	}
   350  	return respBody, &r, nil
   351  }
   352  
   353  /*
   354  撤销指定订单
   355  撤销之前下的未完成订单。
   356  
   357  限速规则:100次/2s
   358  HTTP请求
   359  POST /api/spot/v3/cancel_orders/<order_id>
   360  或者
   361  POST /api/spot/v3/cancel_orders/<client_oid>
   362  */
   363  func (client *Client) PostSpotCancelOrders(instrumentId, orderOrClientId string) ([]byte, *map[string]interface{}, error) {
   364  	r := map[string]interface{}{}
   365  
   366  	uri := strings.Replace(SPOT_CANCEL_ORDERS_BY_ID, "{order_client_id}", orderOrClientId, -1)
   367  	options := NewParams()
   368  	options["instrument_id"] = instrumentId
   369  
   370  	var respBody []byte
   371  	if respBody, _, err := client.Request(POST, uri, options, &r); err != nil {
   372  		return respBody, nil, err
   373  	}
   374  	return respBody, &r, nil
   375  
   376  }
   377  
   378  /*
   379  批量撤销订单
   380  撤销指定的某一种或多种币对的所有未完成订单,每个币对可批量撤10个单。
   381  
   382  限速规则:50次/2s
   383  HTTP请求
   384  POST /api/spot/v3/cancel_batch_orders
   385  */
   386  func (client *Client) PostSpotCancelBatchOrders(orderInfos *[]map[string]interface{}) ([]byte, *map[string]interface{}, error) {
   387  	r := map[string]interface{}{}
   388  	var respBody []byte
   389  	if respBody, _, err := client.Request(POST, SPOT_CANCEL_BATCH_ORDERS, orderInfos, &r); err != nil {
   390  		return respBody, nil, err
   391  	}
   392  	return respBody, &r, nil
   393  }