From 2c92d96b7ef3b1419ae3babec6ad79e6ae13c47d Mon Sep 17 00:00:00 2001 From: cpustejovsky Date: Sat, 26 Nov 2022 20:56:30 -0500 Subject: [PATCH] feat: rename candlestick functions to represent use-case; add test to make sure trades slice is not ordered --- candlestick/candlestick.go | 12 +++++++++--- candlestick/candlestick_test.go | 9 +++++---- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/candlestick/candlestick.go b/candlestick/candlestick.go index 9487c33..6050eb2 100644 --- a/candlestick/candlestick.go +++ b/candlestick/candlestick.go @@ -1,7 +1,7 @@ package candlestick import ( - csgo "github.com/cpustejovsky/customsortgo/sort" + csgo "github.com/cpustejovsky/customsortgo/pure" "sort" ) @@ -12,7 +12,10 @@ type TradeAggregate struct { Low float64 } -func AggregateTrades(trades []float64) TradeAggregate { +// NewTradeAggregate returns a TradeAggregate based on a copy of the income trades slice +// This is nearly 4x slower than AggregateTrades +// Use if you do not want to mutate your data +func NewTradeAggregate(trades []float64) TradeAggregate { sortedTrades := csgo.NewSortedFloats(trades) return TradeAggregate{ Open: trades[0], @@ -22,7 +25,10 @@ func AggregateTrades(trades []float64) TradeAggregate { } } -func AggregateTradesSideEffects(trades []float64) TradeAggregate { +// AggregateTrades returns a TradeAggregate based on the incoming trades slice, mutating the slice +// This is nearly 4x faster than NewTradeAggregate +// Use if the original order of incoming slice does not need to be retained +func AggregateTrades(trades []float64) TradeAggregate { //Mark first and last items in list of trades ta := TradeAggregate{ Open: trades[0], diff --git a/candlestick/candlestick_test.go b/candlestick/candlestick_test.go index af1b9f4..df41cd7 100644 --- a/candlestick/candlestick_test.go +++ b/candlestick/candlestick_test.go @@ -7,25 +7,26 @@ import ( ) func TestAggregateTrades(t *testing.T) { - trades := []float64{14, 14, 15, 15, 16, 15, 13, 12, 13} + trades := []float64{14.0, 14.0, 15.0, 15.0, 16.0, 15.0, 13.0, 12.0, 13.0} want := candlestick.TradeAggregate{Open: 14, Close: 13, High: 16, Low: 12} - got := candlestick.AggregateTrades(trades) + got := candlestick.NewTradeAggregate(trades) assert.Equal(t, want.Open, got.Open, "Open") assert.Equal(t, want.Close, got.Close, "Close") assert.Equal(t, want.High, got.High, "High") assert.Equal(t, want.Low, got.Low, "Low") + assert.Equal(t, 14.0, trades[0]) } func BenchmarkAggregateTrades(b *testing.B) { trades := []float64{14, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 13} for n := 0; n < b.N; n++ { - candlestick.AggregateTrades(trades) + candlestick.NewTradeAggregate(trades) } } func BenchmarkAggregateTradesSideEffects(b *testing.B) { trades := []float64{14, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 14, 15, 15, 16, 15, 13, 12, 13} for n := 0; n < b.N; n++ { - candlestick.AggregateTradesSideEffects(trades) + candlestick.AggregateTrades(trades) } }