Skip to content

Commit

Permalink
Move Float64Slice to types
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed May 22, 2021
1 parent 1531f2b commit 2052d05
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 69 deletions.
2 changes: 1 addition & 1 deletion pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Accumulation/Distribution Indicator (A/D)
//go:generate callbackgen -type AD
type AD struct {
types.IntervalWindow
Values Float64Slice
Values types.Float64Slice
PrePrice float64

EndTime time.Time
Expand Down
8 changes: 4 additions & 4 deletions pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type BOLL struct {
// times of Std, generally it's 2
K float64

SMA Float64Slice
StdDev Float64Slice
UpBand Float64Slice
DownBand Float64Slice
SMA types.Float64Slice
StdDev types.Float64Slice
UpBand types.Float64Slice
DownBand types.Float64Slice

EndTime time.Time

Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
//go:generate callbackgen -type EWMA
type EWMA struct {
types.IntervalWindow
Values Float64Slice
Values types.Float64Slice
LastOpenTime time.Time

UpdateCallbacks []func(value float64)
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/macd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ type MACD struct {
types.IntervalWindow // 9
ShortPeriod int // 12
LongPeriod int // 26
Values Float64Slice
Values types.Float64Slice
FastEWMA EWMA
SlowEWMA EWMA
SignalLine EWMA
Histogram Float64Slice
Histogram types.Float64Slice

EndTime time.Time

Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/obv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ On-Balance Volume (OBV) Definition
//go:generate callbackgen -type OBV
type OBV struct {
types.IntervalWindow
Values Float64Slice
Values types.Float64Slice
PrePrice float64

EndTime time.Time
Expand Down
6 changes: 3 additions & 3 deletions pkg/indicator/obv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ func Test_calculateOBV(t *testing.T) {
name string
kLines []types.KLine
window int
want Float64Slice
want types.Float64Slice
}{
{
name: "trivial_case",
kLines: buildKLines([]float64{0}, []float64{1}),
window: 0,
want: Float64Slice{1.0},
want: types.Float64Slice{1.0},
},
{
name: "easy_case",
kLines: buildKLines([]float64{3, 2, 1, 4}, []float64{3, 2, 2, 6}),
window: 0,
want: Float64Slice{3, 1, -1, 5},
want: types.Float64Slice{3, 1, -1, 5},
},
}

Expand Down
55 changes: 1 addition & 54 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,19 @@ package indicator

import (
"fmt"
"math"
"time"

log "github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/types"
)

type Float64Slice []float64

func (s *Float64Slice) Push(v float64) {
*s = append(*s, v)
}

func (s *Float64Slice) Pop(i int64) (v float64) {
v = (*s)[i]
*s = append((*s)[:i], (*s)[i+1:]...)
return v
}

func (s Float64Slice) Max() float64 {
m := -math.MaxFloat64
for _, v := range s {
m = math.Max(m, v)
}
return m
}

func (s Float64Slice) Min() float64 {
m := math.MaxFloat64
for _, v := range s {
m = math.Min(m, v)
}
return m
}

func (s Float64Slice) Sum() (sum float64) {
for _, v := range s {
sum += v
}
return sum
}

func (s Float64Slice) Mean() (mean float64) {
return s.Sum() / float64(len(s))
}

func (s Float64Slice) Tail(size int) Float64Slice {
length := len(s)
if length <= size {
win := make(Float64Slice, length)
copy(win, s)
return win
}

win := make(Float64Slice, size)
copy(win, s[length-size:])
return win
}

var zeroTime time.Time

//go:generate callbackgen -type SMA
type SMA struct {
types.IntervalWindow
Values Float64Slice
Values types.Float64Slice
EndTime time.Time

UpdateCallbacks []func(value float64)
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/stoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Stochastic Oscillator
//go:generate callbackgen -type STOCH
type STOCH struct {
types.IntervalWindow
K Float64Slice
D Float64Slice
K types.Float64Slice
D types.Float64Slice

KLineWindow types.KLineWindow

Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/vwap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Volume-Weighted Average Price (VWAP) Explained
//go:generate callbackgen -type VWAP
type VWAP struct {
types.IntervalWindow
Values Float64Slice
Values types.Float64Slice
WeightedSum float64
VolumeSum float64
EndTime time.Time
Expand Down
55 changes: 55 additions & 0 deletions pkg/types/float_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package types

import "math"

type Float64Slice []float64

func (s *Float64Slice) Push(v float64) {
*s = append(*s, v)
}

func (s *Float64Slice) Pop(i int64) (v float64) {
v = (*s)[i]
*s = append((*s)[:i], (*s)[i+1:]...)
return v
}

func (s Float64Slice) Max() float64 {
m := -math.MaxFloat64
for _, v := range s {
m = math.Max(m, v)
}
return m
}

func (s Float64Slice) Min() float64 {
m := math.MaxFloat64
for _, v := range s {
m = math.Min(m, v)
}
return m
}

func (s Float64Slice) Sum() (sum float64) {
for _, v := range s {
sum += v
}
return sum
}

func (s Float64Slice) Mean() (mean float64) {
return s.Sum() / float64(len(s))
}

func (s Float64Slice) Tail(size int) Float64Slice {
length := len(s)
if length <= size {
win := make(Float64Slice, length)
copy(win, s)
return win
}

win := make(Float64Slice, size)
copy(win, s[length-size:])
return win
}

0 comments on commit 2052d05

Please sign in to comment.