mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
FEATURE: use new max v3 api to query closed orders by timestamp
This commit is contained in:
parent
579e8b0ae5
commit
592cdede66
|
@ -268,6 +268,10 @@ func (e *Exchange) QueryOpenOrders(ctx context.Context, symbol string) (orders [
|
|||
// lastOrderID is not supported on MAX
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -311,6 +315,47 @@ func (e *Exchange) queryClosedOrdersByLastOrderID(ctx context.Context, symbol st
|
|||
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).Limit(1000)
|
||||
|
||||
if !until.IsZero() {
|
||||
req.Timestamp(until)
|
||||
}
|
||||
|
||||
maxOrders, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
return orders, err
|
||||
}
|
||||
|
||||
for _, maxOrder := range maxOrders {
|
||||
if maxOrder.CreatedAt.Time().Before(since) {
|
||||
break
|
||||
}
|
||||
order, err2 := toGlobalOrder(maxOrder)
|
||||
if err2 != nil {
|
||||
err = multierr.Append(err, err2)
|
||||
continue
|
||||
}
|
||||
|
||||
orders = append(orders, *order)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return types.SortOrdersAscending(orders), nil
|
||||
}
|
||||
|
||||
func (e *Exchange) CancelAllOrders(ctx context.Context) ([]types.Order, error) {
|
||||
walletType := maxapi.WalletTypeSpot
|
||||
if e.MarginSettings.IsMargin {
|
||||
|
|
|
@ -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 *string `param:"order_by,omitempty" validValues:"asc,desc,asc_updated_at,desc_updated_at"`
|
||||
limit *uint `param:"limit,omitempty"`
|
||||
}
|
|
@ -0,0 +1,230 @@
|
|||
// 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 string) *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
|
||||
|
||||
// TEMPLATE check-valid-values
|
||||
switch orderBy {
|
||||
case "asc", "desc":
|
||||
params["order_by"] = orderBy
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("order_by value %v is invalid", orderBy)
|
||||
|
||||
}
|
||||
// END TEMPLATE check-valid-values
|
||||
|
||||
// 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user