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

155 lines
4.2 KiB
Go
Raw Normal View History

2020-10-02 02:10:59 +00:00
package max
2022-04-21 06:52:44 +00:00
//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
2020-10-02 02:10:59 +00:00
import (
"net/url"
"strconv"
2022-04-21 06:52:44 +00:00
"time"
"github.com/c9s/requestgen"
2021-02-22 10:45:44 +00:00
2022-05-27 11:20:45 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-04-21 06:52:44 +00:00
"github.com/c9s/bbgo/pkg/types"
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
}
2022-05-27 11:20:45 +00:00
type Liquidity string
2020-10-02 02:10:59 +00:00
// Trade represents one returned trade on the max platform.
type Trade struct {
2022-05-27 11:20:45 +00:00
ID uint64 `json:"id" db:"exchange_id"`
WalletType WalletType `json:"wallet_type,omitempty"`
Price fixedpoint.Value `json:"price"`
Volume fixedpoint.Value `json:"volume"`
Funds fixedpoint.Value `json:"funds"`
Market string `json:"market"`
MarketName string `json:"market_name"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
Side string `json:"side"`
OrderID uint64 `json:"order_id"`
Fee fixedpoint.Value `json:"fee"` // float number as string
FeeCurrency string `json:"fee_currency"`
Liquidity Liquidity `json:"liquidity"`
Info TradeInfo `json:"info,omitempty"`
2020-10-06 10:44:56 +00:00
}
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 requestgen.AuthenticatedAPIClient
2020-10-02 02:10:59 +00:00
}
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
}
func (s *TradeService) NewGetPrivateTradeRequest() *GetPrivateTradesRequest {
return &GetPrivateTradesRequest{client: s.client}
2020-10-06 10:44:56 +00:00
}
type PrivateRequestParams struct {
Nonce int64 `json:"nonce"`
Path string `json:"path"`
}
//go:generate GetRequest -url "v2/trades/my" -type GetPrivateTradesRequest -responseType []Trade
type GetPrivateTradesRequest struct {
2022-04-21 06:52:44 +00:00
client requestgen.AuthenticatedAPIClient
2020-10-06 10:44:56 +00:00
2022-06-17 11:19:51 +00:00
market string `param:"market"` // nolint:golint,structcheck
2020-10-06 10:44:56 +00:00
2022-04-21 06:52:44 +00:00
// timestamp is the seconds elapsed since Unix epoch, set to return trades executed before the time only
2022-06-17 11:19:51 +00:00
timestamp *time.Time `param:"timestamp,seconds"` // nolint:golint,structcheck
2020-10-06 10:44:56 +00:00
// From field is a trade id, set ot return trades created after the trade
2022-06-17 11:19:51 +00:00
from *int64 `param:"from"` // nolint:golint,structcheck
2020-10-06 10:44:56 +00:00
// To field trade id, set to return trades created before the trade
2022-06-17 11:19:51 +00:00
to *int64 `param:"to"` // nolint:golint,structcheck
2020-10-06 10:44:56 +00:00
2022-04-21 06:52:44 +00:00
orderBy *string `param:"order_by"`
2020-10-06 10:44:56 +00:00
2022-04-21 06:52:44 +00:00
pagination *bool `param:"pagination"`
2021-02-16 09:11:15 +00:00
2022-04-21 06:52:44 +00:00
limit *int64 `param:"limit"`
2020-10-02 02:10:59 +00:00
2022-04-21 06:52:44 +00:00
offset *int64 `param:"offset"`
2020-10-02 02:10:59 +00:00
}