Skip to content

Commit

Permalink
feat: rename candlestick functions to represent use-case; add test to…
Browse files Browse the repository at this point in the history
… make sure trades slice is not ordered
  • Loading branch information
cpustejovsky committed Nov 27, 2022
1 parent 07016d1 commit 2c92d96
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 9 additions & 3 deletions candlestick/candlestick.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package candlestick

import (
csgo "github.com/cpustejovsky/customsortgo/sort"
csgo "github.com/cpustejovsky/customsortgo/pure"
"sort"
)

Expand All @@ -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],
Expand All @@ -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],
Expand Down
9 changes: 5 additions & 4 deletions candlestick/candlestick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 2c92d96

Please sign in to comment.