2023-07-04 13:42:24 +00:00
|
|
|
package core
|
2021-05-28 16:25:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-12-05 15:54:20 +00:00
|
|
|
"time"
|
2021-05-28 16:25:23 +00:00
|
|
|
|
2023-12-12 10:18:34 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2021-05-28 16:25:23 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
2022-12-05 15:54:20 +00:00
|
|
|
const TradeExpiryTime = 24 * time.Hour
|
2022-12-05 16:15:09 +00:00
|
|
|
const PruneTriggerNumOfTrades = 10_000
|
2022-12-05 15:54:20 +00:00
|
|
|
|
2021-05-28 16:25:23 +00:00
|
|
|
type TradeStore struct {
|
|
|
|
// any created trades for tracking trades
|
2021-06-24 11:29:21 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
2022-12-05 16:28:38 +00:00
|
|
|
EnablePrune bool
|
|
|
|
|
2022-12-05 16:15:09 +00:00
|
|
|
trades map[uint64]types.Trade
|
|
|
|
lastTradeTime time.Time
|
2021-05-28 16:25:23 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 05:57:12 +00:00
|
|
|
func NewTradeStore() *TradeStore {
|
2021-05-28 16:25:23 +00:00
|
|
|
return &TradeStore{
|
2021-12-23 05:15:27 +00:00
|
|
|
trades: make(map[uint64]types.Trade),
|
2021-05-28 16:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TradeStore) Num() (num int) {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
2021-05-28 16:25:23 +00:00
|
|
|
num = len(s.trades)
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Unlock()
|
2021-05-28 16:25:23 +00:00
|
|
|
return num
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TradeStore) Trades() (trades []types.Trade) {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2021-05-28 16:25:23 +00:00
|
|
|
|
|
|
|
for _, o := range s.trades {
|
|
|
|
trades = append(trades, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades
|
|
|
|
}
|
|
|
|
|
2021-12-23 05:15:27 +00:00
|
|
|
func (s *TradeStore) Exists(oID uint64) (ok bool) {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2021-05-28 16:25:23 +00:00
|
|
|
|
|
|
|
_, ok = s.trades[oID]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TradeStore) Clear() {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
2021-12-23 05:15:27 +00:00
|
|
|
s.trades = make(map[uint64]types.Trade)
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
type TradeFilter func(trade types.Trade) bool
|
|
|
|
|
2023-07-19 09:33:12 +00:00
|
|
|
// Filter filters the trades by a given TradeFilter function
|
2021-06-24 11:29:21 +00:00
|
|
|
func (s *TradeStore) Filter(filter TradeFilter) {
|
|
|
|
s.Lock()
|
2021-12-23 05:15:27 +00:00
|
|
|
var trades = make(map[uint64]types.Trade)
|
2021-06-24 11:29:21 +00:00
|
|
|
for _, trade := range s.trades {
|
2021-10-05 13:29:45 +00:00
|
|
|
if !filter(trade) {
|
2021-06-24 11:29:21 +00:00
|
|
|
trades[trade.ID] = trade
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.trades = trades
|
|
|
|
s.Unlock()
|
2021-05-28 16:25:23 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 09:33:12 +00:00
|
|
|
// GetOrderTrades finds the trades match order id matches to the given order
|
2022-12-05 11:00:39 +00:00
|
|
|
func (s *TradeStore) GetOrderTrades(o types.Order) (trades []types.Trade) {
|
|
|
|
s.Lock()
|
|
|
|
for _, t := range s.trades {
|
|
|
|
if t.OrderID == o.OrderID {
|
|
|
|
trades = append(trades, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
return trades
|
|
|
|
}
|
|
|
|
|
2021-05-30 17:02:35 +00:00
|
|
|
func (s *TradeStore) GetAndClear() (trades []types.Trade) {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
2022-12-05 11:00:39 +00:00
|
|
|
for _, t := range s.trades {
|
|
|
|
trades = append(trades, t)
|
2021-05-30 17:02:35 +00:00
|
|
|
}
|
2021-12-23 05:15:27 +00:00
|
|
|
s.trades = make(map[uint64]types.Trade)
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Unlock()
|
2021-05-30 17:02:35 +00:00
|
|
|
|
|
|
|
return trades
|
|
|
|
}
|
|
|
|
|
2021-05-28 16:25:23 +00:00
|
|
|
func (s *TradeStore) Add(trades ...types.Trade) {
|
2021-06-24 11:29:21 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2021-05-28 16:25:23 +00:00
|
|
|
|
|
|
|
for _, trade := range trades {
|
|
|
|
s.trades[trade.ID] = trade
|
2022-12-05 16:15:09 +00:00
|
|
|
s.touchLastTradeTime(trade)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TradeStore) touchLastTradeTime(trade types.Trade) {
|
|
|
|
if trade.Time.Time().After(s.lastTradeTime) {
|
|
|
|
s.lastTradeTime = trade.Time.Time()
|
2021-05-28 16:25:23 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-05 11:23:12 +00:00
|
|
|
|
2023-12-12 10:18:34 +00:00
|
|
|
// Prune prunes trades that are older than the expiry time
|
|
|
|
// see TradeExpiryTime (24 hours)
|
|
|
|
func (s *TradeStore) Prune(curTime time.Time) {
|
2022-12-05 15:54:20 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
|
|
|
var trades = make(map[uint64]types.Trade)
|
|
|
|
var cutOffTime = curTime.Add(-TradeExpiryTime)
|
2023-12-12 10:18:34 +00:00
|
|
|
|
|
|
|
log.Infof("pruning expired trades, cutoff time = %s", cutOffTime.String())
|
2022-12-05 15:54:20 +00:00
|
|
|
for _, trade := range s.trades {
|
|
|
|
if trade.Time.Before(cutOffTime) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
trades[trade.ID] = trade
|
|
|
|
}
|
|
|
|
|
|
|
|
s.trades = trades
|
|
|
|
|
2023-12-12 10:18:34 +00:00
|
|
|
log.Infof("trade pruning done, size: %d", len(trades))
|
2022-12-05 15:54:20 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 16:15:09 +00:00
|
|
|
func (s *TradeStore) isCoolTrade(trade types.Trade) bool {
|
2023-12-12 10:18:34 +00:00
|
|
|
// if the duration between the current trade and the last trade is over 1 hour, we call it "cool trade"
|
|
|
|
return !s.lastTradeTime.IsZero() && time.Time(trade.Time).Sub(s.lastTradeTime) > time.Hour
|
2022-12-05 16:15:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 11:23:12 +00:00
|
|
|
func (s *TradeStore) BindStream(stream types.Stream) {
|
|
|
|
stream.OnTradeUpdate(func(trade types.Trade) {
|
|
|
|
s.Add(trade)
|
|
|
|
})
|
2022-12-05 16:28:38 +00:00
|
|
|
|
|
|
|
if s.EnablePrune {
|
|
|
|
stream.OnTradeUpdate(func(trade types.Trade) {
|
|
|
|
if s.isCoolTrade(trade) {
|
|
|
|
s.Prune(time.Time(trade.Time))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-12-05 11:23:12 +00:00
|
|
|
}
|