mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
Merge pull request #1421 from c9s/feature/maxapi/get-closed-orders
FEATURE: use new max v3 api to query closed orders by timestamp
This commit is contained in:
commit
9722c3eb49
|
@ -275,6 +275,10 @@ func (e *Exchange) QueryClosedOrders(
|
|||
ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64,
|
||||
) ([]types.Order, error) {
|
||||
log.Warn("!!!MAX EXCHANGE API NOTICE!!! the since/until conditions will not be effected on closed orders query, max exchange does not support time-range-based query")
|
||||
if !since.IsZero() || !until.IsZero() {
|
||||
return e.queryClosedOrdersByTime(ctx, symbol, since, until)
|
||||
}
|
||||
|
||||
return e.queryClosedOrdersByLastOrderID(ctx, symbol, lastOrderID)
|
||||
}
|
||||
|
||||
|
@ -321,6 +325,47 @@ func (e *Exchange) queryClosedOrdersByLastOrderID(
|
|||
return types.SortOrdersAscending(orders), nil
|
||||
}
|
||||
|
||||
func (e *Exchange) queryClosedOrdersByTime(ctx context.Context, symbol string, since, until time.Time) (orders []types.Order, err error) {
|
||||
if err := e.closedOrderQueryLimiter.Wait(ctx); err != nil {
|
||||
return orders, err
|
||||
}
|
||||
|
||||
market := toLocalSymbol(symbol)
|
||||
walletType := maxapi.WalletTypeSpot
|
||||
if e.MarginSettings.IsMargin {
|
||||
walletType = maxapi.WalletTypeMargin
|
||||
}
|
||||
|
||||
req := e.v3client.NewGetWalletClosedOrdersRequest(walletType).
|
||||
Market(market).
|
||||
Timestamp(since).
|
||||
Limit(1000).
|
||||
OrderBy(maxapi.OrderByAsc)
|
||||
|
||||
maxOrders, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
|
||||
for _, maxOrder := range maxOrders {
|
||||
if maxOrder.CreatedAt.Time().After(until) {
|
||||
continue
|
||||
}
|
||||
order, err2 := toGlobalOrder(maxOrder)
|
||||
if err2 != nil {
|
||||
err = multierr.Append(err, err2)
|
||||
continue
|
||||
}
|
||||
|
||||
orders = append(orders, *order)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return orders, nil
|
||||
}
|
||||
|
||||
func (e *Exchange) CancelAllOrders(ctx context.Context) ([]types.Order, error) {
|
||||
walletType := maxapi.WalletTypeSpot
|
||||
if e.MarginSettings.IsMargin {
|
||||
|
|
|
@ -17,6 +17,15 @@ const (
|
|||
WalletTypeMargin WalletType = "m"
|
||||
)
|
||||
|
||||
type OrderByType string
|
||||
|
||||
const (
|
||||
OrderByAsc OrderByType = "asc"
|
||||
OrderByDesc OrderByType = "desc"
|
||||
OrderByAscUpdatedAt OrderByType = "asc_updated_at"
|
||||
OrderByDescUpdatedAt OrderByType = "desc_updated_at"
|
||||
)
|
||||
|
||||
type OrderStateToQuery int
|
||||
|
||||
const (
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package v3
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/c9s/requestgen"
|
||||
)
|
||||
|
||||
//go:generate -command GetRequest requestgen -method GET
|
||||
//go:generate -command PostRequest requestgen -method POST
|
||||
//go:generate -command DeleteRequest requestgen -method DELETE
|
||||
|
||||
func (s *Client) NewGetWalletClosedOrdersRequest(walletType WalletType) *GetWalletClosedOrdersRequest {
|
||||
return &GetWalletClosedOrdersRequest{client: s.Client, walletType: walletType}
|
||||
}
|
||||
|
||||
//go:generate GetRequest -url "/api/v3/wallet/:walletType/orders/closed" -type GetWalletClosedOrdersRequest -responseType []Order
|
||||
type GetWalletClosedOrdersRequest struct {
|
||||
client requestgen.AuthenticatedAPIClient
|
||||
|
||||
walletType WalletType `param:"walletType,slug,required"`
|
||||
|
||||
market string `param:"market,required"`
|
||||
timestamp *time.Time `param:"timestamp,milliseconds,omitempty"`
|
||||
orderBy *OrderByType `param:"order_by,omitempty"`
|
||||
limit *uint `param:"limit,omitempty"`
|
||||
}
|
|
@ -0,0 +1,219 @@
|
|||
// Code generated by "requestgen --method GET -url /api/v3/wallet/:walletType/orders/closed -type GetWalletClosedOrdersRequest -responseType []Order"; DO NOT EDIT.
|
||||
|
||||
package v3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/exchange/max/maxapi"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) Market(market string) *GetWalletClosedOrdersRequest {
|
||||
g.market = market
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) Timestamp(timestamp time.Time) *GetWalletClosedOrdersRequest {
|
||||
g.timestamp = ×tamp
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) OrderBy(orderBy max.OrderByType) *GetWalletClosedOrdersRequest {
|
||||
g.orderBy = &orderBy
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) Limit(limit uint) *GetWalletClosedOrdersRequest {
|
||||
g.limit = &limit
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) WalletType(walletType max.WalletType) *GetWalletClosedOrdersRequest {
|
||||
g.walletType = walletType
|
||||
return g
|
||||
}
|
||||
|
||||
// GetQueryParameters builds and checks the query parameters and returns url.Values
|
||||
func (g *GetWalletClosedOrdersRequest) GetQueryParameters() (url.Values, error) {
|
||||
var params = map[string]interface{}{}
|
||||
|
||||
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 *GetWalletClosedOrdersRequest) GetParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check market field -> json key market
|
||||
market := g.market
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(market) == 0 {
|
||||
return nil, fmt.Errorf("market is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of market
|
||||
params["market"] = market
|
||||
// check timestamp field -> json key timestamp
|
||||
if g.timestamp != nil {
|
||||
timestamp := *g.timestamp
|
||||
|
||||
// assign parameter of timestamp
|
||||
// convert time.Time to milliseconds time stamp
|
||||
params["timestamp"] = strconv.FormatInt(timestamp.UnixNano()/int64(time.Millisecond), 10)
|
||||
} else {
|
||||
}
|
||||
// check orderBy field -> json key order_by
|
||||
if g.orderBy != nil {
|
||||
orderBy := *g.orderBy
|
||||
|
||||
// assign parameter of orderBy
|
||||
params["order_by"] = orderBy
|
||||
} else {
|
||||
}
|
||||
// check limit field -> json key limit
|
||||
if g.limit != nil {
|
||||
limit := *g.limit
|
||||
|
||||
// assign parameter of limit
|
||||
params["limit"] = limit
|
||||
} else {
|
||||
}
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
// GetParametersQuery converts the parameters from GetParameters into the url.Values format
|
||||
func (g *GetWalletClosedOrdersRequest) 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 *GetWalletClosedOrdersRequest) 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 *GetWalletClosedOrdersRequest) GetSlugParameters() (map[string]interface{}, error) {
|
||||
var params = map[string]interface{}{}
|
||||
// check walletType field -> json key walletType
|
||||
walletType := g.walletType
|
||||
|
||||
// TEMPLATE check-required
|
||||
if len(walletType) == 0 {
|
||||
return nil, fmt.Errorf("walletType is required, empty string given")
|
||||
}
|
||||
// END TEMPLATE check-required
|
||||
|
||||
// assign parameter of walletType
|
||||
params["walletType"] = walletType
|
||||
|
||||
return params, nil
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) 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 *GetWalletClosedOrdersRequest) 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 *GetWalletClosedOrdersRequest) isVarSlice(_v interface{}) bool {
|
||||
rt := reflect.TypeOf(_v)
|
||||
switch rt.Kind() {
|
||||
case reflect.Slice:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) 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
|
||||
}
|
||||
|
||||
func (g *GetWalletClosedOrdersRequest) Do(ctx context.Context) ([]max.Order, error) {
|
||||
|
||||
// empty params for GET operation
|
||||
var params interface{}
|
||||
query, err := g.GetParametersQuery()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL := "/api/v3/wallet/:walletType/orders/closed"
|
||||
slugs, err := g.GetSlugsMap()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiURL = g.applySlugsToUrl(apiURL, slugs)
|
||||
|
||||
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 []max.Order
|
||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiResponse, nil
|
||||
}
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
// create type alias
|
||||
type WalletType = maxapi.WalletType
|
||||
type OrderByType = maxapi.OrderByType
|
||||
type OrderType = maxapi.OrderType
|
||||
|
||||
type Order = maxapi.Order
|
||||
|
|
Loading…
Reference in New Issue
Block a user