Skip to content

Commit

Permalink
all: move float slice/map to a single package
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Aug 25, 2022
1 parent de4f372 commit 5953fe4
Show file tree
Hide file tree
Showing 68 changed files with 458 additions and 362 deletions.
42 changes: 42 additions & 0 deletions pkg/datatype/floats/map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package floats

type Map map[string]float64

func (m Map) Sum() float64 {
sum := 0.0
for _, v := range m {
sum += v
}
return sum
}

func (m Map) MulScalar(x float64) Map {
o := Map{}
for k, v := range m {
o[k] = v * x
}

return o
}
func (m Map) DivScalar(x float64) Map {
o := Map{}
for k, v := range m {
o[k] = v / x
}

return o
}

func (m Map) Normalize() Map {
sum := m.Sum()
if sum == 0 {
panic("zero sum")
}

o := Map{}
for k, v := range m {
o[k] = v / sum
}

return o
}
151 changes: 151 additions & 0 deletions pkg/datatype/floats/slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package floats

import (
"math"

"gonum.org/v1/gonum/floats"
)

type Slice []float64

func New(a ...float64) Slice {
return Slice(a)
}

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

func (s *Slice) Update(v float64) {
*s = append(*s, v)
}

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

func (s Slice) Max() float64 {
return floats.Max(s)
}

func (s Slice) Min() float64 {
return floats.Min(s)
}

func (s Slice) Sum() (sum float64) {
return floats.Sum(s)
}

func (s Slice) Mean() (mean float64) {
length := len(s)
if length == 0 {
panic("zero length slice")
}
return s.Sum() / float64(length)
}

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

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

func (s Slice) Diff() (values Slice) {
for i, v := range s {
if i == 0 {
values.Push(0)
continue
}
values.Push(v - s[i-1])
}
return values
}

func (s Slice) PositiveValuesOrZero() (values Slice) {
for _, v := range s {
values.Push(math.Max(v, 0))
}
return values
}

func (s Slice) NegativeValuesOrZero() (values Slice) {
for _, v := range s {
values.Push(math.Min(v, 0))
}
return values
}

func (s Slice) Abs() (values Slice) {
for _, v := range s {
values.Push(math.Abs(v))
}
return values
}

func (s Slice) MulScalar(x float64) (values Slice) {
for _, v := range s {
values.Push(v * x)
}
return values
}

func (s Slice) DivScalar(x float64) (values Slice) {
for _, v := range s {
values.Push(v / x)
}
return values
}

func (s Slice) Mul(other Slice) (values Slice) {
if len(s) != len(other) {
panic("slice lengths do not match")
}

for i, v := range s {
values.Push(v * other[i])
}

return values
}

func (s Slice) Dot(other Slice) float64 {
return floats.Dot(s, other)
}

func (s Slice) Normalize() Slice {
return s.DivScalar(s.Sum())
}

func (s *Slice) Last() float64 {
length := len(*s)
if length > 0 {
return (*s)[length-1]
}
return 0.0
}

func (s *Slice) Index(i int) float64 {
length := len(*s)
if length-i <= 0 || i < 0 {
return 0.0
}
return (*s)[length-i-1]
}

func (s *Slice) Length() int {
return len(*s)
}

func (s Slice) Addr() *Slice {
return &s
}

3 changes: 2 additions & 1 deletion pkg/indicator/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"time"

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

Expand All @@ -16,7 +17,7 @@ Accumulation/Distribution Indicator (A/D)
type AD struct {
types.SeriesBase
types.IntervalWindow
Values types.Float64Slice
Values floats.Slice
PrePrice float64

EndTime time.Time
Expand Down
7 changes: 4 additions & 3 deletions pkg/indicator/alma.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"math"

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

Expand All @@ -19,9 +20,9 @@ type ALMA struct {
Sigma int // required: recommend to be 5
weight []float64
sum float64
input []float64
Values types.Float64Slice
UpdateCallbacks []func(value float64)
input []float64
Values floats.Slice
UpdateCallbacks []func(value float64)
}

const MaxNumOfALMA = 5_000
Expand Down
3 changes: 2 additions & 1 deletion pkg/indicator/atr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"math"
"time"

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

//go:generate callbackgen -type ATR
type ATR struct {
types.SeriesBase
types.IntervalWindow
PercentageVolatility types.Float64Slice
PercentageVolatility floats.Slice

PreviousClose float64
RMA *RMA
Expand Down
3 changes: 2 additions & 1 deletion pkg/indicator/atrp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"math"
"time"

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

Expand All @@ -18,7 +19,7 @@ import (
type ATRP struct {
types.SeriesBase
types.IntervalWindow
PercentageVolatility types.Float64Slice
PercentageVolatility floats.Slice

PreviousClose float64
RMA *RMA
Expand Down
5 changes: 3 additions & 2 deletions pkg/indicator/boll.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"time"

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

Expand All @@ -29,8 +30,8 @@ type BOLL struct {
SMA *SMA
StdDev *StdDev

UpBand types.Float64Slice
DownBand types.Float64Slice
UpBand floats.Slice
DownBand floats.Slice

EndTime time.Time

Expand Down
9 changes: 5 additions & 4 deletions pkg/indicator/cci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"math"

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

Expand All @@ -13,10 +14,10 @@ import (
type CCI struct {
types.SeriesBase
types.IntervalWindow
Input types.Float64Slice
TypicalPrice types.Float64Slice
MA types.Float64Slice
Values types.Float64Slice
Input floats.Slice
TypicalPrice floats.Slice
MA floats.Slice
Values floats.Slice

UpdateCallbacks []func(value float64)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/indicator/cma.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package indicator

import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)

Expand All @@ -9,9 +10,9 @@ import (
//go:generate callbackgen -type CA
type CA struct {
types.SeriesBase
Interval types.Interval
Values types.Float64Slice
length float64
Interval types.Interval
Values floats.Slice
length float64
UpdateCallbacks []func(value float64)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/indicator/dema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package indicator

import (
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/types"
)

Expand All @@ -11,7 +12,7 @@ import (
type DEMA struct {
types.IntervalWindow
types.SeriesBase
Values types.Float64Slice
Values floats.Slice
a1 *EWMA
a2 *EWMA

Expand Down
7 changes: 4 additions & 3 deletions pkg/indicator/drift.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"math"

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

Expand All @@ -13,9 +14,9 @@ import (
type Drift struct {
types.SeriesBase
types.IntervalWindow
chng *types.Queue
Values types.Float64Slice
MA types.UpdatableSeriesExtend
chng *types.Queue
Values floats.Slice
MA types.UpdatableSeriesExtend
LastValue float64

UpdateCallbacks []func(value float64)
Expand Down
3 changes: 2 additions & 1 deletion pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indicator
import (
"time"

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

Expand All @@ -15,7 +16,7 @@ type EWMA struct {
types.IntervalWindow
types.SeriesBase

Values types.Float64Slice
Values floats.Slice
EndTime time.Time

updateCallbacks []func(value float64)
Expand Down
Loading

0 comments on commit 5953fe4

Please sign in to comment.