Skip to content
forked from c9s/bbgo

Commit

Permalink
Merge pull request c9s#1664 from c9s/feature/max/get-trades-api
Browse files Browse the repository at this point in the history
FEATURE: update max api to latest version
  • Loading branch information
kbearXD authored Jul 1, 2024
2 parents 1fe0ddb + e63158f commit a6aef35
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
41 changes: 18 additions & 23 deletions pkg/exchange/max/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,15 +985,10 @@ func (e *Exchange) QueryDepositHistory(

// QueryTrades
// For MAX API spec
// start_time and end_time need to be within 3 days
// without any parameters -> return trades within 24 hours
// give start_time or end_time -> ignore parameter from_id
// give start_time or from_id -> order by time asc
// give end_time -> order by time desc
// give from_id -> query trades from this id and order by asc
// give timestamp and order is asc -> query trades after timestamp and order by asc
// give timestamp and order is desc -> query trades before timestamp and order by desc
// limit should b1 1~1000
// For this QueryTrades spec (to be compatible with batch.TradeBatchQuery)
// give LastTradeID -> ignore start_time (but still can filter the end_time)
// without any parameters -> return trades within 24 hours
func (e *Exchange) QueryTrades(
ctx context.Context, symbol string, options *types.TradeQueryOptions,
) (trades []types.Trade, err error) {
Expand All @@ -1020,23 +1015,15 @@ func (e *Exchange) QueryTrades(
// However, we want to use from_id as main parameter for batch.TradeBatchQuery
if options.LastTradeID > 0 {
// MAX uses inclusive last trade ID
req.From(options.LastTradeID)
req.FromID(options.LastTradeID)
req.Order("asc")
} else {
// option's start_time and end_time need to be within 3 days
// so if the start_time and end_time is over 3 days, we make end_time down to start_time + 3 days
if options.StartTime != nil && options.EndTime != nil {
endTime := *options.EndTime
startTime := *options.StartTime
if endTime.Sub(startTime) > 72*time.Hour {
startTime := *options.StartTime
endTime = startTime.Add(72 * time.Hour)
}
req.StartTime(startTime)
req.EndTime(endTime)
} else if options.StartTime != nil {
req.StartTime(*options.StartTime)
if options.StartTime != nil {
req.Timestamp(*options.StartTime)
req.Order("asc")
} else if options.EndTime != nil {
req.EndTime(*options.EndTime)
req.Timestamp(*options.EndTime)
req.Order("desc")
}
}

Expand All @@ -1046,6 +1033,14 @@ func (e *Exchange) QueryTrades(
}

for _, t := range maxTrades {
if options.StartTime != nil && options.StartTime.After(t.CreatedAt.Time()) {
continue
}

if options.EndTime != nil && options.EndTime.Before(t.CreatedAt.Time()) {
continue
}

localTrades, err := toGlobalTradeV3(t)
if err != nil {
log.WithError(err).Errorf("can not convert trade: %+v", t)
Expand Down
8 changes: 4 additions & 4 deletions pkg/exchange/max/maxapi/v3/get_wallet_trades_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ func (s *Client) NewGetWalletTradesRequest(walletType WalletType) *GetWalletTrad
return &GetWalletTradesRequest{client: s.Client, walletType: walletType}
}

//go:generate GetRequest -url "/api/v3/wallet/:walletType/trades" -type GetWalletTradesRequest -responseType []Trade
//go:generate GetRequest -url "/api/v3/wallet/:walletType/new/trades" -type GetWalletTradesRequest -responseType []Trade
type GetWalletTradesRequest struct {
client requestgen.AuthenticatedAPIClient

walletType WalletType `param:"walletType,slug,required"`

market string `param:"market,required"`
from *uint64 `param:"from_id"`
startTime *time.Time `param:"start_time,milliseconds"`
endTime *time.Time `param:"end_time,milliseconds"`
timestamp *time.Time `param:"timestamp,milliseconds,omitempty"`
fromID *uint64 `param:"from_id,omitempty"`
order *string `param:"order,omitempty" validValues:"asc,desc"`
limit *uint64 `param:"limit"`
}
60 changes: 35 additions & 25 deletions pkg/exchange/max/maxapi/v3/get_wallet_trades_request_requestgen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a6aef35

Please sign in to comment.