bbgo_origin/pkg/exchange/batch/trade.go

60 lines
1.3 KiB
Go
Raw Normal View History

2021-12-31 05:52:16 +00:00
package batch
import (
"context"
"time"
"github.com/c9s/bbgo/pkg/types"
)
2022-05-30 16:59:33 +00:00
var closedErrChan = make(chan error)
func init() {
close(closedErrChan)
}
2021-12-31 05:52:16 +00:00
type TradeBatchQuery struct {
2022-05-30 16:59:33 +00:00
types.ExchangeTradeHistoryService
2021-12-31 05:52:16 +00:00
}
2024-06-19 09:35:38 +00:00
func (e TradeBatchQuery) Query(
ctx context.Context, symbol string, options *types.TradeQueryOptions, opts ...Option,
) (c chan types.Trade, errC chan error) {
2022-06-24 07:19:12 +00:00
if options.EndTime == nil {
now := time.Now()
options.EndTime = &now
}
2022-05-30 16:59:33 +00:00
query := &AsyncTimeRangedBatchQuery{
Type: types.Trade{},
2022-05-30 16:59:33 +00:00
Q: func(startTime, endTime time.Time) (interface{}, error) {
2024-06-19 09:35:38 +00:00
return e.ExchangeTradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
StartTime: &startTime,
EndTime: &endTime,
Limit: options.Limit,
2024-06-19 09:35:38 +00:00
LastTradeID: options.LastTradeID,
})
2022-05-30 16:59:33 +00:00
},
T: func(obj interface{}) time.Time {
return time.Time(obj.(types.Trade).Time)
},
ID: func(obj interface{}) string {
trade := obj.(types.Trade)
if trade.ID > options.LastTradeID {
options.LastTradeID = trade.ID
2021-12-31 05:52:16 +00:00
}
2024-06-19 09:35:38 +00:00
2022-05-30 16:59:33 +00:00
return trade.Key().String()
},
JumpIfEmpty: 24*time.Hour - 5*time.Minute, // exchange may not have trades in the last 24 hours
2022-05-30 16:59:33 +00:00
}
2021-12-31 05:52:16 +00:00
for _, opt := range opts {
opt(query)
}
2022-05-30 16:59:33 +00:00
c = make(chan types.Trade, 100)
2024-06-19 09:35:38 +00:00
errC = query.Query(ctx, c, *options.StartTime, *options.EndTime)
2021-12-31 05:52:16 +00:00
return c, errC
}