diff --git a/pkg/core/orderstore.go b/pkg/core/orderstore.go index b639e8ce1..05b1995a1 100644 --- a/pkg/core/orderstore.go +++ b/pkg/core/orderstore.go @@ -2,6 +2,7 @@ package core import ( "sync" + "time" "github.com/c9s/bbgo/pkg/types" ) @@ -138,6 +139,24 @@ func (s *OrderStore) BindStream(stream types.Stream) { }) } +func (s *OrderStore) Prune(expiryDuration time.Duration) { + cutOffTime := time.Now().Add(-expiryDuration) + orders := make(map[uint64]types.Order, len(s.orders)) + + s.mu.Lock() + defer s.mu.Unlock() + + for idx, o := range s.orders { + if o.UpdateTime.Time().Before(cutOffTime) { + continue + } + + orders[idx] = o + } + + s.orders = orders +} + func (s *OrderStore) HandleOrderUpdate(order types.Order) { switch order.Status { diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 4d4c6cae0..86cf55ec4 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1475,6 +1475,25 @@ func (s *Strategy) accountUpdater(ctx context.Context) { } } +func (s *Strategy) houseCleanWorker(ctx context.Context) { + expiryDuration := 3 * time.Hour + ticker := time.NewTicker(time.Hour) + + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + + case <-ticker.C: + s.orderStore.Prune(expiryDuration) + + } + + } + +} + func (s *Strategy) hedgeWorker(ctx context.Context) { ticker := time.NewTicker(util.MillisecondsJitter(s.HedgeInterval.Duration(), 200)) defer ticker.Stop() @@ -1809,6 +1828,7 @@ func (s *Strategy) CrossRun( go s.accountUpdater(ctx) go s.hedgeWorker(ctx) go s.quoteWorker(ctx) + go s.houseCleanWorker(ctx) if s.RecoverTrade { go s.tradeRecover(ctx)