mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #253 from narumiruna/refactor/float64slice
This commit is contained in:
commit
5323b273bc
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package indicator
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -10,64 +9,12 @@ import (
|
|||
"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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
55
pkg/types/float_slice.go
Normal file
55
pkg/types/float_slice.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user