diff --git a/pkg/bbgo/marketdatastore.go b/pkg/bbgo/marketdatastore.go index dd9eaf099..f51256111 100644 --- a/pkg/bbgo/marketdatastore.go +++ b/pkg/bbgo/marketdatastore.go @@ -5,6 +5,8 @@ import "github.com/c9s/bbgo/pkg/types" const MaxNumOfKLines = 5_000 const MaxNumOfKLinesTruncate = 100 +const CapacityOfKLineWindowLimit = 5_000 + // MarketDataStore receives and maintain the public market data of a single symbol //go:generate callbackgen -type MarketDataStore type MarketDataStore struct { @@ -57,8 +59,13 @@ func (store *MarketDataStore) AddKLine(k types.KLine) { } window.Add(k) - if len(*window) > MaxNumOfKLines { - *window = (*window)[MaxNumOfKLinesTruncate-1:] + lenOfWindow := len(*window) + capOfWindow := cap(*window) + + if lenOfWindow == capOfWindow && capOfWindow > CapacityOfKLineWindowLimit { + truncatedWindow := (*window)[(CapacityOfKLineWindowLimit / 2):] + copy(*window, truncatedWindow) + *window = (*window)[:len(truncatedWindow)] } store.EmitKLineClosed(k)