Skip to content

Commit

Permalink
improve types.ShrinkSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 16, 2025
1 parent 4f9844e commit 1bdd388
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions pkg/bbgo/marketdatastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func (store *MarketDataStore) AddKLine(k types.KLine) {
store.EmitKLineWindowUpdate(k.Interval, *window)
}

func truncateKLineWindowIfNeeded(window *types.KLineWindow) int {
return types.ShrinkSlice(window, KLineWindowShrinkThreshold, KLineWindowShrinkSize)
func truncateKLineWindowIfNeeded(window *types.KLineWindow) {
*window = types.ShrinkSlice(*window, KLineWindowShrinkThreshold, KLineWindowShrinkSize)
}
4 changes: 2 additions & 2 deletions pkg/indicator/v2/cross.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func Cross(a, b types.Float64Source) *CrossStream {
}

func (s *CrossStream) Truncate() {
types.ShrinkSlice(&s.a, MaxSliceSize, TruncateSize)
types.ShrinkSlice(&s.b, MaxSliceSize, TruncateSize)
s.a = types.ShrinkSlice(s.a, MaxSliceSize, TruncateSize)
s.b = types.ShrinkSlice(s.b, MaxSliceSize, TruncateSize)
}

func (s *CrossStream) calculate() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/v2/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func EWMA2(source types.Float64Source, window int) *EWMAStream {
}

func (s *EWMAStream) Truncate() {
types.ShrinkSlice(&s.Slice, s.window*5, s.window*2)
s.Slice = types.ShrinkSlice(s.Slice, s.window*5, s.window*2)
}

func (s *EWMAStream) Calculate(v float64) float64 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/v2/klines.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func KLines(source types.Stream, symbol string, interval types.Interval) *KLineS
s.kLines = append(s.kLines, k)
s.EmitUpdate(k)

types.ShrinkSlice(&s.kLines, MaxNumOfKLines, MaxNumOfKLines/5)
s.kLines = types.ShrinkSlice(s.kLines, MaxNumOfKLines, MaxNumOfKLines/5)
}))

return s
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/v2/price.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *PriceStream) AddSubscriber(f func(v float64)) {
}

func (s *PriceStream) Truncate() {
types.ShrinkSlice(&s.Slice, 5000, 2000)
s.Slice = types.ShrinkSlice(s.Slice, 5000, 2000)
}

func (s *PriceStream) PushAndEmit(v float64) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/v2/rma.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *RMAStream) Calculate(x float64) float64 {
}

func (s *RMAStream) Truncate() {
types.ShrinkSlice(&s.Slice, MaxSliceSize, TruncateSize)
s.Slice = types.ShrinkSlice(s.Slice, MaxSliceSize, TruncateSize)
}

func checkWindow(window int) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/v2/tr.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TR2(source KLineSubscription) *TRStream {
}

func (s *TRStream) Truncate() {
types.ShrinkSlice(&s.Slice, MaxSliceSize, TruncateSize)
s.Slice = types.ShrinkSlice(s.Slice, MaxSliceSize, TruncateSize)
}

func (s *TRStream) calculateAndPush(high, low, cls float64) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/indicator/v2/truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ const MaxSliceSize = 5000
const TruncateSize = 1000

func generalTruncate(slice []float64) []float64 {
types.ShrinkSlice(&slice, MaxSliceSize, TruncateSize)
return slice
return types.ShrinkSlice(slice, MaxSliceSize, TruncateSize)
}
4 changes: 2 additions & 2 deletions pkg/strategy/xmaker/signal_trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

const tradeSliceCapacityLimit = 20000
const tradeSliceCapacityLimit = 10000
const tradeSliceShrinkThreshold = tradeSliceCapacityLimit * 4 / 5
const tradeSliceShrinkSize = tradeSliceCapacityLimit * 1 / 5

Expand All @@ -39,7 +39,7 @@ type TradeVolumeWindowSignal struct {
func (s *TradeVolumeWindowSignal) handleTrade(trade types.Trade) {
s.mu.Lock()
s.trades = append(s.trades, trade)
types.ShrinkSlice(&s.trades, tradeSliceShrinkThreshold, tradeSliceShrinkSize)
s.trades = types.ShrinkSlice(s.trades, tradeSliceShrinkThreshold, tradeSliceShrinkSize)
s.mu.Unlock()
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/types/kline.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,21 +697,21 @@ func KLineHighPriceMapper(k KLine) float64 {
}

// ShrinkSlice shrinks the slice to the new size by removing the old klines
func ShrinkSlice[S ~[]E, E comparable](slice *S, thresholdLen, newSize int) int {
curLen := len(*slice)
curCap := cap(*slice)
func ShrinkSlice[S ~[]E, E comparable](slice S, thresholdLen, newSize int) S {
curLen := len(slice)
curCap := cap(slice)

// newSize can't be larger than half of the current capacity
if newSize > curCap/2 {
newSize = curCap / 2
}

if curLen < thresholdLen {
return 0
if curLen < thresholdLen || curLen <= newSize {
return slice
}

start := curLen - newSize
copy(*slice, (*slice)[start:])
*slice = (*slice)[:newSize]
return newSize
newSlice := make(S, newSize)
copy(newSlice, slice[start:])
return newSlice
}
11 changes: 11 additions & 0 deletions pkg/types/kline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ func TestKLineWindow_Truncate(t *testing.T) {
assert.Len(t, win, 1)
assert.Equal(t, 11603.0, win.Last().Open.Float64())
}

func TestShrinkSlice(t *testing.T) {
var slice = make([]float64, 0, 100)
for i := 0; i < 50000; i++ {
slice = append(slice, float64(i))
}

slice2 := ShrinkSlice(slice, 40000, 10000)
assert.NotEqual(t, &slice2, &slice)
assert.Equal(t, 10000, len(slice2))
}

0 comments on commit 1bdd388

Please sign in to comment.