mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #1355 from c9s/refactor/grid2/rename-and-move
REFACTOR: rename file and variable
This commit is contained in:
commit
4b9d52004f
|
@ -23,21 +23,21 @@ type SyncActiveOrdersOpts struct {
|
||||||
exchange types.Exchange
|
exchange types.Exchange
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) initializeRecoverCh() bool {
|
func (s *Strategy) initializeRecoverC() bool {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
isInitialize := false
|
isInitialize := false
|
||||||
|
|
||||||
if s.activeOrdersRecoverC == nil {
|
if s.recoverC == nil {
|
||||||
s.logger.Info("initializing recover channel")
|
s.logger.Info("initializing recover channel")
|
||||||
s.activeOrdersRecoverC = make(chan struct{}, 1)
|
s.recoverC = make(chan struct{}, 1)
|
||||||
} else {
|
} else {
|
||||||
s.logger.Info("recover channel is already initialized, trigger active orders recover")
|
s.logger.Info("recover channel is already initialized, trigger active orders recover")
|
||||||
isInitialize = true
|
isInitialize = true
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case s.activeOrdersRecoverC <- struct{}{}:
|
case s.recoverC <- struct{}{}:
|
||||||
s.logger.Info("trigger active orders recover")
|
s.logger.Info("trigger active orders recover")
|
||||||
default:
|
default:
|
||||||
s.logger.Info("activeOrdersRecoverC is full")
|
s.logger.Info("activeOrdersRecoverC is full")
|
||||||
|
@ -49,7 +49,7 @@ func (s *Strategy) initializeRecoverCh() bool {
|
||||||
|
|
||||||
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||||
// every time we activeOrdersRecoverC receive signal, do active orders recover
|
// every time we activeOrdersRecoverC receive signal, do active orders recover
|
||||||
if isInitialize := s.initializeRecoverCh(); isInitialize {
|
if isInitialize := s.initializeRecoverC(); isInitialize {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||||
log.WithError(err).Errorf("unable to sync active orders")
|
log.WithError(err).Errorf("unable to sync active orders")
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-s.activeOrdersRecoverC:
|
case <-s.recoverC:
|
||||||
if err := syncActiveOrders(ctx, opts); err != nil {
|
if err := syncActiveOrders(ctx, opts); err != nil {
|
||||||
log.WithError(err).Errorf("unable to sync active orders")
|
log.WithError(err).Errorf("unable to sync active orders")
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,3 +174,77 @@ func TestSyncActiveOrders(t *testing.T) {
|
||||||
assert.Equal(types.OrderStatusNew, activeOrders[0].Status)
|
assert.Equal(types.OrderStatusNew, activeOrders[0].Status)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSyncActiveOrder(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
symbol := "ETHUSDT"
|
||||||
|
|
||||||
|
t.Run("sync filled order in active orderbook, active orderbook should remove this order", func(t *testing.T) {
|
||||||
|
mockOrderQueryService := mocks.NewMockExchangeOrderQueryService(mockCtrl)
|
||||||
|
activeOrderbook := bbgo.NewActiveOrderBook(symbol)
|
||||||
|
|
||||||
|
order := types.Order{
|
||||||
|
OrderID: 1,
|
||||||
|
Status: types.OrderStatusNew,
|
||||||
|
SubmitOrder: types.SubmitOrder{
|
||||||
|
Symbol: symbol,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
activeOrderbook.Add(order)
|
||||||
|
|
||||||
|
updatedOrder := order
|
||||||
|
updatedOrder.Status = types.OrderStatusFilled
|
||||||
|
|
||||||
|
mockOrderQueryService.EXPECT().QueryOrder(ctx, types.OrderQuery{
|
||||||
|
Symbol: symbol,
|
||||||
|
OrderID: strconv.FormatUint(order.OrderID, 10),
|
||||||
|
}).Return(&updatedOrder, nil)
|
||||||
|
|
||||||
|
if !assert.NoError(syncActiveOrder(ctx, activeOrderbook, mockOrderQueryService, order.OrderID)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify active orderbook
|
||||||
|
activeOrders := activeOrderbook.Orders()
|
||||||
|
assert.Equal(0, len(activeOrders))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("sync partial-filled order in active orderbook, active orderbook should still keep this order", func(t *testing.T) {
|
||||||
|
mockOrderQueryService := mocks.NewMockExchangeOrderQueryService(mockCtrl)
|
||||||
|
activeOrderbook := bbgo.NewActiveOrderBook(symbol)
|
||||||
|
|
||||||
|
order := types.Order{
|
||||||
|
OrderID: 1,
|
||||||
|
Status: types.OrderStatusNew,
|
||||||
|
SubmitOrder: types.SubmitOrder{
|
||||||
|
Symbol: symbol,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
activeOrderbook.Add(order)
|
||||||
|
|
||||||
|
updatedOrder := order
|
||||||
|
updatedOrder.Status = types.OrderStatusPartiallyFilled
|
||||||
|
|
||||||
|
mockOrderQueryService.EXPECT().QueryOrder(ctx, types.OrderQuery{
|
||||||
|
Symbol: symbol,
|
||||||
|
OrderID: strconv.FormatUint(order.OrderID, 10),
|
||||||
|
}).Return(&updatedOrder, nil)
|
||||||
|
|
||||||
|
if !assert.NoError(syncActiveOrder(ctx, activeOrderbook, mockOrderQueryService, order.OrderID)) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify active orderbook
|
||||||
|
activeOrders := activeOrderbook.Orders()
|
||||||
|
assert.Equal(1, len(activeOrders))
|
||||||
|
assert.Equal(order.OrderID, activeOrders[0].OrderID)
|
||||||
|
assert.Equal(updatedOrder.Status, activeOrders[0].Status)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -204,7 +204,7 @@ type Strategy struct {
|
||||||
tradingCtx, writeCtx context.Context
|
tradingCtx, writeCtx context.Context
|
||||||
cancelWrite context.CancelFunc
|
cancelWrite context.CancelFunc
|
||||||
|
|
||||||
activeOrdersRecoverC chan struct{}
|
recoverC chan struct{}
|
||||||
|
|
||||||
// this ensures that bbgo.Sync to lock the object
|
// this ensures that bbgo.Sync to lock the object
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
Loading…
Reference in New Issue
Block a user