core: log trades pruning

This commit is contained in:
c9s 2023-12-12 18:18:34 +08:00
parent b2895a0434
commit 8025d05eac
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -4,6 +4,8 @@ import (
"sync" "sync"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -112,14 +114,16 @@ func (s *TradeStore) touchLastTradeTime(trade types.Trade) {
} }
} }
// pruneExpiredTrades prunes trades that are older than the expiry time // Prune prunes trades that are older than the expiry time
// see TradeExpiryTime // see TradeExpiryTime (24 hours)
func (s *TradeStore) pruneExpiredTrades(curTime time.Time) { func (s *TradeStore) Prune(curTime time.Time) {
s.Lock() s.Lock()
defer s.Unlock() defer s.Unlock()
var trades = make(map[uint64]types.Trade) var trades = make(map[uint64]types.Trade)
var cutOffTime = curTime.Add(-TradeExpiryTime) var cutOffTime = curTime.Add(-TradeExpiryTime)
log.Infof("pruning expired trades, cutoff time = %s", cutOffTime.String())
for _, trade := range s.trades { for _, trade := range s.trades {
if trade.Time.Before(cutOffTime) { if trade.Time.Before(cutOffTime) {
continue continue
@ -129,15 +133,13 @@ func (s *TradeStore) pruneExpiredTrades(curTime time.Time) {
} }
s.trades = trades s.trades = trades
}
func (s *TradeStore) Prune(curTime time.Time) { log.Infof("trade pruning done, size: %d", len(trades))
s.pruneExpiredTrades(curTime)
} }
func (s *TradeStore) isCoolTrade(trade types.Trade) bool { func (s *TradeStore) isCoolTrade(trade types.Trade) bool {
// if the time of last trade is over 1 hour, we call it's cool trade // if the duration between the current trade and the last trade is over 1 hour, we call it "cool trade"
return s.lastTradeTime != (time.Time{}) && time.Time(trade.Time).Sub(s.lastTradeTime) > time.Hour return !s.lastTradeTime.IsZero() && time.Time(trade.Time).Sub(s.lastTradeTime) > time.Hour
} }
func (s *TradeStore) BindStream(stream types.Stream) { func (s *TradeStore) BindStream(stream types.Stream) {