mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-14 02:53:50 +00:00
pkg/exchange: add execution list request to bybit
This commit is contained in:
parent
f776914e8c
commit
aac833d135
67
pkg/exchange/bybit/bybitapi/get_execution_list_request.go
Normal file
67
pkg/exchange/bybit/bybitapi/get_execution_list_request.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package bybitapi
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/c9s/requestgen"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Result
|
||||
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Result
|
||||
|
||||
type TradesResponse struct {
|
||||
NextPageCursor string `json:"nextPageCursor"`
|
||||
Category Category `json:"category"`
|
||||
List []Trade `json:"list"`
|
||||
}
|
||||
|
||||
type Trade struct {
|
||||
Symbol string `json:"symbol"`
|
||||
OrderId string `json:"orderId"`
|
||||
Side Side `json:"side"`
|
||||
OrderType OrderType `json:"orderType"`
|
||||
// ExecFee is supported on restful API v5, but not on websocket API.
|
||||
ExecFee fixedpoint.Value `json:"execFee"`
|
||||
ExecId string `json:"execId"`
|
||||
ExecPrice fixedpoint.Value `json:"execPrice"`
|
||||
ExecQty fixedpoint.Value `json:"execQty"`
|
||||
ExecTime types.MillisecondTimestamp `json:"execTime"`
|
||||
IsMaker bool `json:"isMaker"`
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/v5/execution/list" -type GetExecutionListRequest -responseDataType .TradesResponse -rateLimiter 5+15/1s
|
||||
type GetExecutionListRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
category Category `param:"category,query" validValues:"spot"`
|
||||
|
||||
symbol *string `param:"symbol,query"`
|
||||
orderId *string `param:"orderId,query"`
|
||||
|
||||
// startTime the start timestamp (ms)
|
||||
// startTime and endTime are not passed, return 7 days by default;
|
||||
// Only startTime is passed, return range between startTime and startTime+7 days
|
||||
// Only endTime is passed, return range between endTime-7 days and endTime
|
||||
// If both are passed, the rule is endTime - startTime <= 7 days
|
||||
startTime *time.Time `param:"startTime,query,milliseconds"`
|
||||
|
||||
// endTime the end timestamp (ms)
|
||||
endTime *time.Time `param:"endTime,query,milliseconds"`
|
||||
|
||||
// limit for data size per page. [1, 100]. Default: 50
|
||||
limit *uint64 `param:"limit,query"`
|
||||
// cursor. Use the nextPageCursor token from the response to retrieve the next page of the result set
|
||||
cursor *string `param:"cursor,query"`
|
||||
}
|
||||
|
||||
// NewGetExecutionListRequest query users' execution records, sorted by execTime in descending order. However,
|
||||
// for Classic spot, they are sorted by execId in descending order.
|
||||
func (c *RestClient) NewGetExecutionListRequest() *GetExecutionListRequest {
|
||||
return &GetExecutionListRequest{
|
||||
client: c,
|
||||
category: CategorySpot,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,284 @@
|
|||
// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /v5/execution/list -type GetExecutionListRequest -responseDataType .TradesResponse -rateLimiter 5+15/1s"; DO NOT EDIT.
|
||||
|
||||
package bybitapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"golang.org/x/time/rate"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var GetExecutionListRequestLimiter = rate.NewLimiter(15.000000150000002, 5)
|
||||
|
||||
func (g *GetExecutionListRequest) Category(category Category) *GetExecutionListRequest {
|
||||
g.category = category
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Symbol(symbol string) *GetExecutionListRequest {
|
||||
g.symbol = &symbol
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) OrderId(orderId string) *GetExecutionListRequest {
|
||||
g.orderId = &orderId
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) StartTime(startTime time.Time) *GetExecutionListRequest {
|
||||
g.startTime = &startTime
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) EndTime(endTime time.Time) *GetExecutionListRequest {
|
||||
g.endTime = &endTime
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Limit(limit uint64) *GetExecutionListRequest {
|
||||
g.limit = &limit
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) Cursor(cursor string) *GetExecutionListRequest {
|
||||
g.cursor = &cursor
|
||||
return g
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (g *GetExecutionListRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check category field -> json key category
|
||||
category := g.category
|
||||
|
||||
// TEMPLATE check-valid-values
|
||||
switch category {
|
||||
case "spot":
|
||||
params["category"] = category
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("category value %v is invalid", category)
|
||||
|
||||
}
|
||||
// END TEMPLATE check-valid-values
|
||||
|
||||
// assign parameter of category
|
||||
params["category"] = category
|
||||
// check symbol field -> json key symbol
|
||||
if g.symbol != nil {
|
||||
symbol := *g.symbol
|
||||
|
||||
// assign parameter of symbol
|
||||
params["symbol"] = symbol
|
||||
} else {
|
||||
}
|
||||
// check orderId field -> json key orderId
|
||||
if g.orderId != nil {
|
||||
orderId := *g.orderId
|
||||
|
||||
// assign parameter of orderId
|
||||
params["orderId"] = orderId
|
||||
} else {
|
||||
}
|
||||
// check startTime field -> json key startTime
|
||||
if g.startTime != nil {
|
||||
startTime := *g.startTime
|
||||
|
||||
// assign parameter of startTime
|
||||
// convert time.Time to milliseconds time stamp
|
||||
params["startTime"] = strconv.FormatInt(startTime.UnixNano()/int64(time.Millisecond), 10)
|
||||
} else {
|
||||
}
|
||||
// check endTime field -> json key endTime
|
||||
if g.endTime != nil {
|
||||
endTime := *g.endTime
|
||||
|
||||
// assign parameter of endTime
|
||||
// convert time.Time to milliseconds time stamp
|
||||
params["endTime"] = strconv.FormatInt(endTime.UnixNano()/int64(time.Millisecond), 10)
|
||||
} else {
|
||||
}
|
||||
// check limit field -> json key limit
|
||||
if g.limit != nil {
|
||||
limit := *g.limit
|
||||
|
||||
// assign parameter of limit
|
||||
params["limit"] = limit
|
||||
} else {
|
||||
}
|
||||
// check cursor field -> json key cursor
|
||||
if g.cursor != nil {
|
||||
cursor := *g.cursor
|
||||
|
||||
// assign parameter of cursor
|
||||
params["cursor"] = cursor
|
||||
} else {
|
||||
}
|
||||
|
||||
query := url.Values{}
|
||||
for _k, _v := range params {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParameters builds and checks the parameters and return the result in a map object
|
||||
func (g *GetExecutionListRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (g *GetExecutionListRequest) GetParametersQuery() (url.Values, error) {
|
||||
query := url.Values{}
|
||||
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return query, err
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
if g.isVarSlice(_v) {
|
||||
g.iterateSlice(_v, func(it interface{}) {
|
||||
query.Add(_k+"[]", fmt.Sprintf("%v", it))
|
||||
})
|
||||
} else {
|
||||
query.Add(_k, fmt.Sprintf("%v", _v))
|
||||
}
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
|
||||
// GetParametersJSON converts the parameters from GetParameters into the JSON format
|
||||
func (g *GetExecutionListRequest) GetParametersJSON() ([]byte, error) {
|
||||
params, err := g.GetParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return json.Marshal(params)
|
||||
}
|
||||
|
||||
// GetSlugParameters builds and checks the slug parameters and return the result in a map object
|
||||
func (g *GetExecutionListRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) applySlugsToUrl(url string, slugs map[string]string) string {
|
||||
for _k, _v := range slugs {
|
||||
needleRE := regexp.MustCompile(":" + _k + "\\b")
|
||||
url = needleRE.ReplaceAllString(url, _v)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) iterateSlice(slice interface{}, _f func(it interface{})) {
|
||||
sliceValue := reflect.ValueOf(slice)
|
||||
for _i := 0; _i < sliceValue.Len(); _i++ {
|
||||
it := sliceValue.Index(_i).Interface()
|
||||
_f(it)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *GetExecutionListRequest) GetSlugsMap() (map[string]string, error) {
|
||||
slugs := map[string]string{}
|
||||
params, err := g.GetSlugParameters()
|
||||
if err != nil {
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
for _k, _v := range params {
|
||||
slugs[_k] = fmt.Sprintf("%v", _v)
|
||||
}
|
||||
|
||||
return slugs, nil
|
||||
}
|
||||
|
||||
// GetPath returns the request path of the API
|
||||
func (g *GetExecutionListRequest) GetPath() string {
|
||||
return "/v5/execution/list"
|
||||
}
|
||||
|
||||
// Do generates the request object and send the request object to the API endpoint
|
||||
func (g *GetExecutionListRequest) Do(ctx context.Context) (*TradesResponse, error) {
|
||||
if err := GetExecutionListRequestLimiter.Wait(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// no body params
|
||||
var params interface{}
|
||||
query, err := g.GetQueryParameters()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiURL string
|
||||
|
||||
apiURL = g.GetPath()
|
||||
|
||||
req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
response, err := g.client.SendRequest(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var apiResponse APIResponse
|
||||
|
||||
type responseUnmarshaler interface {
|
||||
Unmarshal(data []byte) error
|
||||
}
|
||||
|
||||
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
|
||||
if err := unmarshaler.Unmarshal(response.Body); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
// The line below checks the content type, however, some API server might not send the correct content type header,
|
||||
// Hence, this is commented for backward compatibility
|
||||
// response.IsJSON()
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
type responseValidator interface {
|
||||
Validate() error
|
||||
}
|
||||
|
||||
if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
|
||||
if err := validator.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
var data TradesResponse
|
||||
if err := json.Unmarshal(apiResponse.Result, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &data, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user