bbgo_origin/pkg/exchange/max/maxapi/trade.go

240 lines
5.3 KiB
Go
Raw Normal View History

2020-10-02 02:10:59 +00:00
package max
import (
2020-10-06 10:44:56 +00:00
"context"
2020-10-02 02:10:59 +00:00
"net/url"
"strconv"
2021-02-22 10:45:44 +00:00
"github.com/pkg/errors"
2020-10-02 02:10:59 +00:00
)
2020-10-06 10:44:56 +00:00
type MarkerInfo struct {
2020-10-09 05:21:42 +00:00
Fee string `json:"fee"`
2020-10-06 10:44:56 +00:00
FeeCurrency string `json:"fee_currency"`
2020-10-09 05:21:42 +00:00
OrderID int `json:"order_id"`
2020-10-06 10:44:56 +00:00
}
type TradeInfo struct {
// Maker tells you the maker trade side
2020-10-09 05:21:42 +00:00
Maker string `json:"maker,omitempty"`
Bid *MarkerInfo `json:"bid,omitempty"`
Ask *MarkerInfo `json:"ask,omitempty"`
2020-10-06 10:44:56 +00:00
}
2020-10-02 02:10:59 +00:00
// Trade represents one returned trade on the max platform.
type Trade struct {
ID uint64 `json:"id" db:"exchange_id"`
Price string `json:"price" db:"price"`
Volume string `json:"volume" db:"volume"`
Funds string `json:"funds"`
Market string `json:"market" db:"market"`
MarketName string `json:"market_name"`
CreatedAt int64 `json:"created_at"`
CreatedAtMilliSeconds int64 `json:"created_at_in_ms"`
Side string `json:"side" db:"side"`
2020-10-06 10:44:56 +00:00
OrderID uint64 `json:"order_id"`
2020-10-02 02:10:59 +00:00
Fee string `json:"fee" db:"fee"` // float number as string
FeeCurrency string `json:"fee_currency" db:"fee_currency"`
2020-10-06 10:44:56 +00:00
Info TradeInfo `json:"info,omitempty"`
}
func (t Trade) IsBuyer() bool {
return t.Side == "bid" || t.Side == "buy"
2020-10-06 10:44:56 +00:00
}
func (t Trade) IsMaker() bool {
return t.Info.Maker == t.Side
2020-10-02 02:10:59 +00:00
}
type QueryTradeOptions struct {
Market string `json:"market"`
Timestamp int64 `json:"timestamp,omitempty"`
From int64 `json:"from,omitempty"`
To int64 `json:"to,omitempty"`
OrderBy string `json:"order_by,omitempty"`
Page int `json:"page,omitempty"`
Offset int `json:"offset,omitempty"`
Limit int64 `json:"limit,omitempty"`
}
type TradeService struct {
client *RestClient
}
func (options *QueryTradeOptions) Map() map[string]interface{} {
var data = map[string]interface{}{}
data["market"] = options.Market
if options.Limit > 0 {
data["limit"] = options.Limit
}
if options.Timestamp > 0 {
data["timestamp"] = options.Timestamp
}
if options.From >= 0 {
data["from"] = options.From
}
if options.To > options.From {
data["to"] = options.To
}
if len(options.OrderBy) > 0 {
// could be "asc" or "desc"
data["order_by"] = options.OrderBy
}
return data
}
func (options *QueryTradeOptions) Params() url.Values {
var params = url.Values{}
params.Add("market", options.Market)
if options.Limit > 0 {
params.Add("limit", strconv.FormatInt(options.Limit, 10))
}
if options.Timestamp > 0 {
params.Add("timestamp", strconv.FormatInt(options.Timestamp, 10))
}
if options.From >= 0 {
params.Add("from", strconv.FormatInt(options.From, 10))
}
if options.To > options.From {
params.Add("to", strconv.FormatInt(options.To, 10))
}
if len(options.OrderBy) > 0 {
// could be "asc" or "desc"
params.Add("order_by", options.OrderBy)
}
return params
}
2020-10-06 10:44:56 +00:00
func (s *TradeService) NewPrivateTradeRequest() *PrivateTradeRequest {
return &PrivateTradeRequest{client: s.client}
}
type PrivateRequestParams struct {
Nonce int64 `json:"nonce"`
Path string `json:"path"`
}
2021-02-22 10:45:44 +00:00
type PrivateTradeRequest struct {
client *RestClient
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
market *string
2020-10-06 10:44:56 +00:00
// Timestamp is the seconds elapsed since Unix epoch, set to return trades executed before the time only
2021-02-22 10:45:44 +00:00
timestamp *int64
2020-10-06 10:44:56 +00:00
// From field is a trade id, set ot return trades created after the trade
2021-02-22 10:45:44 +00:00
from *int64
2020-10-06 10:44:56 +00:00
// To field trade id, set to return trades created before the trade
2021-02-22 10:45:44 +00:00
to *int64
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
orderBy *string
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
pagination *bool
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
limit *int64
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
offset *int64
2020-10-06 10:44:56 +00:00
}
func (r *PrivateTradeRequest) Market(market string) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.market = &market
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) From(from int64) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.from = &from
2020-10-06 10:44:56 +00:00
return r
}
2021-02-16 09:11:15 +00:00
func (r *PrivateTradeRequest) Timestamp(t int64) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.timestamp = &t
2021-02-16 09:11:15 +00:00
return r
}
2020-10-06 10:44:56 +00:00
func (r *PrivateTradeRequest) To(to int64) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.to = &to
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) Limit(limit int64) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.limit = &limit
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) Offset(offset int64) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.offset = &offset
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) Pagination(p bool) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.pagination = &p
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) OrderBy(orderBy string) *PrivateTradeRequest {
2021-02-22 10:45:44 +00:00
r.orderBy = &orderBy
2020-10-06 10:44:56 +00:00
return r
}
func (r *PrivateTradeRequest) Do(ctx context.Context) (trades []Trade, err error) {
2021-02-22 10:45:44 +00:00
if r.market == nil {
return nil, errors.New("parameter market is mandatory")
2020-10-06 10:44:56 +00:00
}
2021-02-22 10:45:44 +00:00
payload := map[string]interface{}{
"market": r.market,
2020-10-06 10:44:56 +00:00
}
2021-02-22 10:45:44 +00:00
if r.timestamp != nil {
payload["timestamp"] = r.timestamp
2020-10-06 10:44:56 +00:00
}
2021-02-22 10:45:44 +00:00
if r.from != nil {
payload["from"] = r.from
}
2020-10-06 10:44:56 +00:00
2021-02-22 10:45:44 +00:00
if r.to != nil {
payload["to"] = r.to
}
2020-10-02 02:10:59 +00:00
2021-02-22 10:45:44 +00:00
if r.orderBy != nil {
payload["order_by"] = r.orderBy
2021-02-22 10:45:44 +00:00
}
if r.pagination != nil {
payload["pagination"] = r.pagination
}
if r.limit != nil {
payload["limit"] = r.limit
}
if r.offset != nil {
payload["offset"] = r.offset
}
2022-04-19 05:52:10 +00:00
req, err := r.client.newAuthenticatedRequest(nil, "GET", "v2/trades/my", nil, payload, nil)
2020-10-02 02:10:59 +00:00
if err != nil {
2021-02-22 10:45:44 +00:00
return trades, err
2020-10-02 02:10:59 +00:00
}
2022-04-19 04:10:15 +00:00
response, err := r.client.SendRequest(req)
2020-10-02 02:10:59 +00:00
if err != nil {
2021-02-22 10:45:44 +00:00
return trades, err
2020-10-02 02:10:59 +00:00
}
2021-02-22 10:45:44 +00:00
if err := response.DecodeJSON(&trades); err != nil {
return trades, err
2020-10-02 02:10:59 +00:00
}
2021-02-22 10:45:44 +00:00
return trades, err
2020-10-02 02:10:59 +00:00
}
2021-02-22 10:45:44 +00:00