mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
bbgo: truncate trade buffer if it gets too large
This commit is contained in:
parent
c248b2a323
commit
eac0195815
|
@ -329,6 +329,8 @@ type ServiceConfig struct {
|
||||||
type EnvironmentConfig struct {
|
type EnvironmentConfig struct {
|
||||||
DisableDefaultKLineSubscription bool `json:"disableDefaultKLineSubscription"`
|
DisableDefaultKLineSubscription bool `json:"disableDefaultKLineSubscription"`
|
||||||
DisableHistoryKLinePreload bool `json:"disableHistoryKLinePreload"`
|
DisableHistoryKLinePreload bool `json:"disableHistoryKLinePreload"`
|
||||||
|
DisableSessionTradeBuffer bool `json:"disableSessionTradeBuffer"`
|
||||||
|
MaxSessionTradeBufferSize int `json:"maxSessionTradeBufferSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
|
|
@ -396,14 +396,27 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
|
||||||
return fmt.Errorf("market %s is not defined", symbol)
|
return fmt.Errorf("market %s is not defined", symbol)
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Trades[symbol] = &types.TradeSlice{Trades: nil}
|
disableSessionTradeBuffer := environ.environmentConfig != nil && environ.environmentConfig.DisableSessionTradeBuffer
|
||||||
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
|
maxSessionTradeBufferSize := 0
|
||||||
if trade.Symbol != symbol {
|
if environ.environmentConfig != nil && environ.environmentConfig.MaxSessionTradeBufferSize > 0 {
|
||||||
return
|
maxSessionTradeBufferSize = environ.environmentConfig.MaxSessionTradeBufferSize
|
||||||
}
|
}
|
||||||
|
|
||||||
session.Trades[symbol].Append(trade)
|
session.Trades[symbol] = &types.TradeSlice{Trades: nil}
|
||||||
})
|
|
||||||
|
if !disableSessionTradeBuffer {
|
||||||
|
session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
|
||||||
|
if trade.Symbol != symbol {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
session.Trades[symbol].Append(trade)
|
||||||
|
|
||||||
|
if maxSessionTradeBufferSize > 0 {
|
||||||
|
session.Trades[symbol].Truncate(maxSessionTradeBufferSize)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// session wide position
|
// session wide position
|
||||||
position := &types.Position{
|
position := &types.Position{
|
||||||
|
@ -416,7 +429,6 @@ func (session *ExchangeSession) initSymbol(ctx context.Context, environ *Environ
|
||||||
|
|
||||||
orderStore := core.NewOrderStore(symbol)
|
orderStore := core.NewOrderStore(symbol)
|
||||||
orderStore.AddOrderUpdate = true
|
orderStore.AddOrderUpdate = true
|
||||||
|
|
||||||
orderStore.BindStream(session.UserDataStream)
|
orderStore.BindStream(session.UserDataStream)
|
||||||
session.orderStores[symbol] = orderStore
|
session.orderStores[symbol] = orderStore
|
||||||
|
|
||||||
|
|
|
@ -47,6 +47,16 @@ func (s *TradeSlice) Append(t Trade) {
|
||||||
s.mu.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *TradeSlice) Truncate(size int) {
|
||||||
|
s.mu.Lock()
|
||||||
|
|
||||||
|
if len(s.Trades) > size {
|
||||||
|
s.Trades = s.Trades[len(s.Trades)-1-size:]
|
||||||
|
}
|
||||||
|
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
type Trade struct {
|
type Trade struct {
|
||||||
// GID is the global ID
|
// GID is the global ID
|
||||||
GID int64 `json:"gid" db:"gid"`
|
GID int64 `json:"gid" db:"gid"`
|
||||||
|
|
Loading…
Reference in New Issue
Block a user