Skip to content

Commit 78071dc

Browse files
authored
Merge pull request #1107 from c9s/feature/maxapi/support-time-range
FEATURE: make MAX QueryTrades support start_time, end_time
2 parents ee43884 + da48e0f commit 78071dc

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

pkg/exchange/batch/trade.go

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (e TradeBatchQuery) Query(ctx context.Context, symbol string, options *type
4040
}
4141
return trade.Key().String()
4242
},
43+
JumpIfEmpty: 24 * time.Hour,
4344
}
4445

4546
c = make(chan types.Trade, 100)

pkg/exchange/max/exchange.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,17 @@ func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since,
780780
return allDeposits, err
781781
}
782782

783+
// QueryTrades
784+
// For MAX API spec
785+
// start_time and end_time need to be within 3 days
786+
// without any parameters -> return trades within 24 hours
787+
// give start_time or end_time -> ignore parameter from_id
788+
// give start_time or from_id -> order by time asc
789+
// give end_time -> order by time desc
790+
// limit should b1 1~1000
791+
// For this QueryTrades spec (to be compatible with batch.TradeBatchQuery)
792+
// give LastTradeID -> ignore start_time (but still can filter the end_time)
793+
// without any parameters -> return trades within 24 hours
783794
func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *types.TradeQueryOptions) (trades []types.Trade, err error) {
784795
if err := tradeQueryLimiter.Wait(ctx); err != nil {
785796
return nil, err
@@ -800,10 +811,28 @@ func (e *Exchange) QueryTrades(ctx context.Context, symbol string, options *type
800811
req.Limit(1000)
801812
}
802813

803-
// MAX uses exclusive last trade ID
804-
// the timestamp parameter is used for reverse order, we can't use it.
814+
// If we use start_time as parameter, MAX will ignore from_id.
815+
// However, we want to use from_id as main parameter for batch.TradeBatchQuery
805816
if options.LastTradeID > 0 {
817+
// MAX uses inclusive last trade ID
806818
req.From(options.LastTradeID)
819+
} else {
820+
// option's start_time and end_time need to be within 3 days
821+
// so if the start_time and end_time is over 3 days, we make end_time down to start_time + 3 days
822+
if options.StartTime != nil && options.EndTime != nil {
823+
endTime := *options.EndTime
824+
startTime := *options.StartTime
825+
if endTime.Sub(startTime) > 72*time.Hour {
826+
startTime := *options.StartTime
827+
endTime = startTime.Add(72 * time.Hour)
828+
}
829+
req.StartTime(startTime)
830+
req.EndTime(endTime)
831+
} else if options.StartTime != nil {
832+
req.StartTime(*options.StartTime)
833+
} else if options.EndTime != nil {
834+
req.EndTime(*options.EndTime)
835+
}
807836
}
808837

809838
maxTrades, err := req.Do(ctx)

0 commit comments

Comments
 (0)