xmaker: prune expired orders

This commit is contained in:
c9s 2024-10-17 13:02:44 +08:00
parent bfe8ce9f2c
commit 5fbb06639d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 39 additions and 0 deletions

View File

@ -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 {

View File

@ -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)