grid2: add a simple order poller

This commit is contained in:
c9s 2023-05-15 20:04:34 +08:00
parent b7680a750b
commit 6fe4f7ca90
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 71 additions and 0 deletions

View File

@ -258,6 +258,7 @@ func (s *Strategy) buildFilledPinOrderMapFromTrades(ctx context.Context, history
continue continue
} }
// TODO: replace this with queryOrderUntilSuccessful
order, err := s.orderQueryService.QueryOrder(ctx, types.OrderQuery{ order, err := s.orderQueryService.QueryOrder(ctx, types.OrderQuery{
OrderID: strconv.FormatUint(trade.OrderID, 10), OrderID: strconv.FormatUint(trade.OrderID, 10),
}) })

View File

@ -1800,6 +1800,57 @@ func (s *Strategy) getWriteContext(fallbackCtxList ...context.Context) context.C
return context.Background() return context.Background()
} }
type orderPollerRequirement interface {
types.ExchangeOrderQueryService
types.ExchangeTradeHistoryService
}
type OrderPoller struct {
filledOrders *bbgo.OrderStore
lastSyncTime time.Time
service orderPollerRequirement
}
func newOrderPoller(symbol string, service orderPollerRequirement) *OrderPoller {
orderStore := bbgo.NewOrderStore(symbol)
orderStore.AddOrderUpdate = true
orderStore.RemoveFilled = false
return &OrderPoller{
filledOrders: orderStore,
}
}
func (p *OrderPoller) Bind(stream types.Stream) {
p.filledOrders.BindStream(stream)
}
func (p *OrderPoller) Run(ctx context.Context, interval time.Duration) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}
func (p *OrderPoller) Start(ctx context.Context) {
go p.Run(ctx, 5*time.Minute)
}
func (s *Strategy) bindOrderPoller(session *bbgo.ExchangeSession) {
service, support := session.Exchange.(orderPollerRequirement)
if !support {
return
}
poller := newOrderPoller(s.Symbol, service)
poller.Bind(session.UserDataStream)
}
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
instanceID := s.InstanceID() instanceID := s.InstanceID()
@ -2155,3 +2206,22 @@ func queryOpenOrdersUntilSuccessful(ctx context.Context, ex types.Exchange, symb
err = generalBackoff(ctx, op) err = generalBackoff(ctx, op)
return openOrders, err return openOrders, err
} }
func queryOrderUntilSuccessful(ctx context.Context, service types.ExchangeOrderQueryService, orderId uint64) (*types.Order, error) {
var retOrder *types.Order
id := strconv.FormatUint(orderId, 10)
var op = func() (err2 error) {
order, err2 := service.QueryOrder(ctx, types.OrderQuery{OrderID: id})
if err2 != nil {
return err2
} else if order != nil {
retOrder = order
}
return nil
}
err := generalBackoff(ctx, op)
return retOrder, err
}