implement get fills request

This commit is contained in:
c9s 2021-12-21 23:25:19 +08:00
parent bace225470
commit 62fa6dd274
5 changed files with 281 additions and 82 deletions

View File

@ -0,0 +1 @@
package kucoin

View File

@ -23,61 +23,6 @@ const defaultHTTPTimeout = time.Second * 15
const RestBaseURL = "https://api.kucoin.com/api"
const SandboxRestBaseURL = "https://openapi-sandbox.kucoin.com/api"
type TradeType string
const (
TradeTypeSpot TradeType = "TRADE"
TradeTypeMargin TradeType = "MARGIN"
)
type SideType string
const (
SideTypeBuy SideType = "buy"
SideTypeSell SideType = "sell"
)
type TimeInForceType string
const (
// GTC Good Till Canceled orders remain open on the book until canceled. This is the default behavior if no policy is specified.
TimeInForceGTC TimeInForceType = "GTC"
// GTT Good Till Time orders remain open on the book until canceled or the allotted cancelAfter is depleted on the matching engine. GTT orders are guaranteed to cancel before any other order is processed after the cancelAfter seconds placed in order book.
TimeInForceGTT TimeInForceType = "GTT"
// FOK Fill Or Kill orders are rejected if the entire size cannot be matched.
TimeInForceFOK TimeInForceType = "FOK"
// IOC Immediate Or Cancel orders instantly cancel the remaining size of the limit order instead of opening it on the book.
TimeInForceIOC TimeInForceType = "IOC"
)
type OrderType string
const (
OrderTypeMarket OrderType = "market"
OrderTypeLimit OrderType = "limit"
)
type InstrumentType string
const (
InstrumentTypeSpot InstrumentType = "SPOT"
InstrumentTypeSwap InstrumentType = "SWAP"
InstrumentTypeFutures InstrumentType = "FUTURES"
InstrumentTypeOption InstrumentType = "OPTION"
)
type OrderState string
const (
OrderStateCanceled OrderState = "canceled"
OrderStateLive OrderState = "live"
OrderStatePartiallyFilled OrderState = "partially_filled"
OrderStateFilled OrderState = "filled"
)
type RestClient struct {
BaseURL *url.URL
@ -356,33 +301,6 @@ func (c *RestClient) MarketTicker(instId string) (*MarketTicker, error) {
return &tickerResponse.Data[0], nil
}
func (c *RestClient) MarketTickers(instType InstrumentType) ([]MarketTicker, error) {
// SPOT, SWAP, FUTURES, OPTION
var params = url.Values{}
params.Add("instType", string(instType))
req, err := c.newRequest("GET", "/api/v5/market/tickers", params, nil)
if err != nil {
return nil, err
}
response, err := c.sendRequest(req)
if err != nil {
return nil, err
}
var tickerResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data []MarketTicker `json:"data"`
}
if err := response.DecodeJSON(&tickerResponse); err != nil {
return nil, err
}
return tickerResponse.Data, nil
}
func sign(secret, payload string) string {
var sig = hmac.New(sha256.New, []byte(secret))
_, err := sig.Write([]byte(payload))

View File

@ -0,0 +1,139 @@
// Code generated by "requestgen -type GetFillsRequest"; DO NOT EDIT.
package kucoinapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
)
func (r *GetFillsRequest) OrderID(orderID string) *GetFillsRequest {
r.orderID = &orderID
return r
}
func (r *GetFillsRequest) Symbol(symbol string) *GetFillsRequest {
r.symbol = &symbol
return r
}
func (r *GetFillsRequest) Side(side string) *GetFillsRequest {
r.side = &side
return r
}
func (r *GetFillsRequest) OrderType(orderType string) *GetFillsRequest {
r.orderType = &orderType
return r
}
func (r *GetFillsRequest) StartAt(startAt time.Time) *GetFillsRequest {
r.startAt = &startAt
return r
}
func (r *GetFillsRequest) EndAt(endAt time.Time) *GetFillsRequest {
r.endAt = &endAt
return r
}
func (r *GetFillsRequest) GetParameters() (map[string]interface{}, error) {
var params = map[string]interface{}{}
// check orderID field -> json key orderId
if r.orderID != nil {
orderID := *r.orderID
// assign parameter of orderID
params["orderId"] = orderID
}
// check symbol field -> json key symbol
if r.symbol != nil {
symbol := *r.symbol
// assign parameter of symbol
params["symbol"] = symbol
}
// check side field -> json key side
if r.side != nil {
side := *r.side
switch side {
case "buy", "sell":
params["side"] = side
default:
return params, fmt.Errorf("side value %v is invalid", side)
}
// assign parameter of side
params["side"] = side
}
// check orderType field -> json key type
if r.orderType != nil {
orderType := *r.orderType
switch orderType {
case "limit", "market", "limit_stop", "market_stop":
params["type"] = orderType
default:
return params, fmt.Errorf("type value %v is invalid", orderType)
}
// assign parameter of orderType
params["type"] = orderType
}
// check startAt field -> json key startAt
if r.startAt != nil {
startAt := *r.startAt
// assign parameter of startAt
// convert time.Time to milliseconds time
params["startAt"] = strconv.FormatInt(startAt.UnixNano()/int64(time.Millisecond), 10)
}
// check endAt field -> json key endAt
if r.endAt != nil {
endAt := *r.endAt
// assign parameter of endAt
// convert time.Time to milliseconds time
params["endAt"] = strconv.FormatInt(endAt.UnixNano()/int64(time.Millisecond), 10)
}
return params, nil
}
func (r *GetFillsRequest) GetParametersQuery() (url.Values, error) {
query := url.Values{}
params, err := r.GetParameters()
if err != nil {
return query, err
}
for k, v := range params {
query.Add(k, fmt.Sprintf("%v", v))
}
return query, nil
}
func (r *GetFillsRequest) GetParametersJSON() ([]byte, error) {
params, err := r.GetParameters()
if err != nil {
return nil, err
}
return json.Marshal(params)
}

View File

@ -41,6 +41,92 @@ func (c *TradeService) NewCancelAllOrderRequest() *CancelAllOrderRequest {
}
}
func (c *TradeService) NewGetFillsRequest() *GetFillsRequest {
return &GetFillsRequest{client: c.client}
}
//go:generate requestgen -type GetFillsRequest
type GetFillsRequest struct {
client *RestClient
orderID *string `param:"orderId"`
symbol *string `param:"symbol"`
side *string `param:"side" validValues:"buy,sell"`
orderType *string `param:"type" validValues:"limit,market,limit_stop,market_stop"`
startAt *time.Time `param:"startAt,milliseconds"`
endAt *time.Time `param:"endAt,milliseconds"`
}
type FillListPage struct {
CurrentPage int `json:"currentPage"`
PageSize int `json:"pageSize"`
TotalNumber int `json:"totalNum"`
TotalPage int `json:"totalPage"`
Items []Fill `json:"items"`
}
type Fill struct {
Symbol string `json:"symbol"`
TradeId string `json:"tradeId"`
OrderId string `json:"orderId"`
CounterOrderId string `json:"counterOrderId"`
Side SideType `json:"side"`
Liquidity LiquidityType `json:"liquidity"`
ForceTaker bool `json:"forceTaker"`
Price fixedpoint.Value `json:"price"`
Size fixedpoint.Value `json:"size"`
Funds fixedpoint.Value `json:"funds"`
Fee fixedpoint.Value `json:"fee"`
FeeRate fixedpoint.Value `json:"feeRate"`
FeeCurrency string `json:"feeCurrency"`
Stop string `json:"stop"`
Type OrderType `json:"type"`
CreatedAt types.MillisecondTimestamp `json:"createdAt"`
TradeType TradeType `json:"tradeType"`
}
func (r *GetFillsRequest) Do(ctx context.Context) (*FillListPage, error) {
params, err := r.GetParametersQuery()
if err != nil {
return nil, err
}
if !params.Has("tradeType") {
params.Add("tradeType", "TRADE")
}
req, err := r.client.newAuthenticatedRequest("GET", "/api/v1/fills", params, nil)
if err != nil {
return nil, err
}
response, err := r.client.sendRequest(req)
if err != nil {
return nil, err
}
var orderResponse struct {
Code string `json:"code"`
Message string `json:"msg"`
Data *FillListPage `json:"data"`
}
if err := response.DecodeJSON(&orderResponse); err != nil {
return nil, err
}
if orderResponse.Data == nil {
return nil, errors.New("api error: [" + orderResponse.Code + "] " + orderResponse.Message)
}
return orderResponse.Data, nil
}
//go:generate requestgen -type ListOrdersRequest
type ListOrdersRequest struct {
client *RestClient

View File

@ -0,0 +1,55 @@
package kucoinapi
type TradeType string
const (
TradeTypeSpot TradeType = "TRADE"
TradeTypeMargin TradeType = "MARGIN"
)
type SideType string
const (
SideTypeBuy SideType = "buy"
SideTypeSell SideType = "sell"
)
type TimeInForceType string
const (
// GTC Good Till Canceled orders remain open on the book until canceled. This is the default behavior if no policy is specified.
TimeInForceGTC TimeInForceType = "GTC"
// GTT Good Till Time orders remain open on the book until canceled or the allotted cancelAfter is depleted on the matching engine. GTT orders are guaranteed to cancel before any other order is processed after the cancelAfter seconds placed in order book.
TimeInForceGTT TimeInForceType = "GTT"
// FOK Fill Or Kill orders are rejected if the entire size cannot be matched.
TimeInForceFOK TimeInForceType = "FOK"
// IOC Immediate Or Cancel orders instantly cancel the remaining size of the limit order instead of opening it on the book.
TimeInForceIOC TimeInForceType = "IOC"
)
type LiquidityType string
const (
LiquidityTypeMaker LiquidityType = "maker"
LiquidityTypeTaker LiquidityType = "taker"
)
type OrderType string
const (
OrderTypeMarket OrderType = "market"
OrderTypeLimit OrderType = "limit"
OrderTypeStopLimit OrderType = "stop_limit"
)
type OrderState string
const (
OrderStateCanceled OrderState = "canceled"
OrderStateLive OrderState = "live"
OrderStatePartiallyFilled OrderState = "partially_filled"
OrderStateFilled OrderState = "filled"
)