mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-14 02:53:50 +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
|
//go:generate callbackgen -type AD
|
||||||
type AD struct {
|
type AD struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
PrePrice float64
|
PrePrice float64
|
||||||
|
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
|
|
|
@ -29,10 +29,10 @@ type BOLL struct {
|
||||||
// times of Std, generally it's 2
|
// times of Std, generally it's 2
|
||||||
K float64
|
K float64
|
||||||
|
|
||||||
SMA Float64Slice
|
SMA types.Float64Slice
|
||||||
StdDev Float64Slice
|
StdDev types.Float64Slice
|
||||||
UpBand Float64Slice
|
UpBand types.Float64Slice
|
||||||
DownBand Float64Slice
|
DownBand types.Float64Slice
|
||||||
|
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
//go:generate callbackgen -type EWMA
|
//go:generate callbackgen -type EWMA
|
||||||
type EWMA struct {
|
type EWMA struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
LastOpenTime time.Time
|
LastOpenTime time.Time
|
||||||
|
|
||||||
UpdateCallbacks []func(value float64)
|
UpdateCallbacks []func(value float64)
|
||||||
|
|
|
@ -18,11 +18,11 @@ type MACD struct {
|
||||||
types.IntervalWindow // 9
|
types.IntervalWindow // 9
|
||||||
ShortPeriod int // 12
|
ShortPeriod int // 12
|
||||||
LongPeriod int // 26
|
LongPeriod int // 26
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
FastEWMA EWMA
|
FastEWMA EWMA
|
||||||
SlowEWMA EWMA
|
SlowEWMA EWMA
|
||||||
SignalLine EWMA
|
SignalLine EWMA
|
||||||
Histogram Float64Slice
|
Histogram types.Float64Slice
|
||||||
|
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ On-Balance Volume (OBV) Definition
|
||||||
//go:generate callbackgen -type OBV
|
//go:generate callbackgen -type OBV
|
||||||
type OBV struct {
|
type OBV struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
PrePrice float64
|
PrePrice float64
|
||||||
|
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
|
|
|
@ -19,19 +19,19 @@ func Test_calculateOBV(t *testing.T) {
|
||||||
name string
|
name string
|
||||||
kLines []types.KLine
|
kLines []types.KLine
|
||||||
window int
|
window int
|
||||||
want Float64Slice
|
want types.Float64Slice
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "trivial_case",
|
name: "trivial_case",
|
||||||
kLines: buildKLines([]float64{0}, []float64{1}),
|
kLines: buildKLines([]float64{0}, []float64{1}),
|
||||||
window: 0,
|
window: 0,
|
||||||
want: Float64Slice{1.0},
|
want: types.Float64Slice{1.0},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "easy_case",
|
name: "easy_case",
|
||||||
kLines: buildKLines([]float64{3, 2, 1, 4}, []float64{3, 2, 2, 6}),
|
kLines: buildKLines([]float64{3, 2, 1, 4}, []float64{3, 2, 2, 6}),
|
||||||
window: 0,
|
window: 0,
|
||||||
want: Float64Slice{3, 1, -1, 5},
|
want: types.Float64Slice{3, 1, -1, 5},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package indicator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -10,64 +9,12 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"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
|
var zeroTime time.Time
|
||||||
|
|
||||||
//go:generate callbackgen -type SMA
|
//go:generate callbackgen -type SMA
|
||||||
type SMA struct {
|
type SMA struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
EndTime time.Time
|
EndTime time.Time
|
||||||
|
|
||||||
UpdateCallbacks []func(value float64)
|
UpdateCallbacks []func(value float64)
|
||||||
|
|
|
@ -17,8 +17,8 @@ Stochastic Oscillator
|
||||||
//go:generate callbackgen -type STOCH
|
//go:generate callbackgen -type STOCH
|
||||||
type STOCH struct {
|
type STOCH struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
K Float64Slice
|
K types.Float64Slice
|
||||||
D Float64Slice
|
D types.Float64Slice
|
||||||
|
|
||||||
KLineWindow types.KLineWindow
|
KLineWindow types.KLineWindow
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ Volume-Weighted Average Price (VWAP) Explained
|
||||||
//go:generate callbackgen -type VWAP
|
//go:generate callbackgen -type VWAP
|
||||||
type VWAP struct {
|
type VWAP struct {
|
||||||
types.IntervalWindow
|
types.IntervalWindow
|
||||||
Values Float64Slice
|
Values types.Float64Slice
|
||||||
WeightedSum float64
|
WeightedSum float64
|
||||||
VolumeSum float64
|
VolumeSum float64
|
||||||
EndTime time.Time
|
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