bbgo_origin/pkg/exchange/batch/trade.go

56 lines
1.2 KiB
Go
Raw Permalink 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
}
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
startTime := *options.StartTime
endTime := *options.EndTime
query := &AsyncTimeRangedBatchQuery{
Type: types.Trade{},
2022-05-30 16:59:33 +00:00
Q: func(startTime, endTime time.Time) (interface{}, error) {
options.StartTime = &startTime
options.EndTime = &endTime
2022-05-30 16:59:33 +00:00
return e.ExchangeTradeHistoryService.QueryTrades(ctx, symbol, options)
},
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
}
2022-05-30 16:59:33 +00:00
return trade.Key().String()
},
JumpIfEmpty: 24 * time.Hour,
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)
errC = query.Query(ctx, c, startTime, endTime)
2021-12-31 05:52:16 +00:00
return c, errC
}