2021-05-28 16:25:23 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TradeStore struct {
|
|
|
|
// any created trades for tracking trades
|
2021-06-24 11:29:21 +00:00
|
|
|
sync.Mutex
|
|
|
|
|
2021-12-23 05:15:27 +00:00
|
|
|
trades map[uint64]types.Trade
|
2021-05-28 16:25:23 +00:00
|
|
|
|
|
|
|
Symbol string
|
|
|
|
RemoveCancelled bool
|
|
|
|
RemoveFilled bool
|
|
|
|
AddOrderUpdate bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTradeStore(symbol string) *TradeStore {
|
|
|
|
return &TradeStore{
|
|
|
|
Symbol: symbol,
|
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
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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()
|
2021-05-30 17:02:35 +00:00
|
|
|
for _, o := range s.trades {
|
|
|
|
trades = append(trades, o)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|