mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
feature: add seriesExtend
This commit is contained in:
parent
cb1ad3a89b
commit
12757a0458
|
@ -63,6 +63,46 @@ type Series interface {
|
||||||
Length() int
|
Length() int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SeriesExtend interface {
|
||||||
|
Series
|
||||||
|
Sum(limit ...int) float64
|
||||||
|
Mean(limit ...int) float64
|
||||||
|
Abs() SeriesExtend
|
||||||
|
Predict(lookback int, offset ...int) float64
|
||||||
|
NextCross(b Series, lookback int) (int, float64, bool)
|
||||||
|
CrossOver(b Series) BoolSeries
|
||||||
|
CrossUnder(b Series) BoolSeries
|
||||||
|
Highest(lookback int) float64
|
||||||
|
Lowest(lookback int) float64
|
||||||
|
Add(b interface{}) SeriesExtend
|
||||||
|
Minus(b interface{}) SeriesExtend
|
||||||
|
Div(b interface{}) SeriesExtend
|
||||||
|
Mul(b interface{}) SeriesExtend
|
||||||
|
Dot(b interface{}, limit ...int) float64
|
||||||
|
ToArray(limit ...int) (result []float64)
|
||||||
|
ToReverseArray(limit ...int) (result Float64Slice)
|
||||||
|
Change(offset ...int) SeriesExtend
|
||||||
|
Stdev(length int) float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type IndexFuncType func(int) float64
|
||||||
|
type LastFuncType func() float64
|
||||||
|
type LengthFuncType func() int
|
||||||
|
|
||||||
|
type SeriesBase struct {
|
||||||
|
index IndexFuncType
|
||||||
|
last LastFuncType
|
||||||
|
length LengthFuncType
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSeries(a Series) SeriesExtend {
|
||||||
|
return &SeriesBase{
|
||||||
|
index: a.Index,
|
||||||
|
last: a.Last,
|
||||||
|
length: a.Length,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type UpdatableSeries interface {
|
type UpdatableSeries interface {
|
||||||
Series
|
Series
|
||||||
Update(float64)
|
Update(float64)
|
||||||
|
@ -125,8 +165,8 @@ func (a *AbsResult) Length() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return series that having all the elements positive
|
// Return series that having all the elements positive
|
||||||
func Abs(a Series) Series {
|
func Abs(a Series) SeriesExtend {
|
||||||
return &AbsResult{a}
|
return NewSeries(&AbsResult{a})
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Series = &AbsResult{}
|
var _ Series = &AbsResult{}
|
||||||
|
@ -291,7 +331,7 @@ type AddSeriesResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add two series, result[i] = a[i] + b[i]
|
// Add two series, result[i] = a[i] + b[i]
|
||||||
func Add(a interface{}, b interface{}) Series {
|
func Add(a interface{}, b interface{}) SeriesExtend {
|
||||||
var aa Series
|
var aa Series
|
||||||
var bb Series
|
var bb Series
|
||||||
|
|
||||||
|
@ -313,7 +353,7 @@ func Add(a interface{}, b interface{}) Series {
|
||||||
panic("input should be either *Series or float64")
|
panic("input should be either *Series or float64")
|
||||||
|
|
||||||
}
|
}
|
||||||
return &AddSeriesResult{aa, bb}
|
return NewSeries(&AddSeriesResult{aa, bb})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *AddSeriesResult) Last() float64 {
|
func (a *AddSeriesResult) Last() float64 {
|
||||||
|
@ -341,10 +381,10 @@ type MinusSeriesResult struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Minus two series, result[i] = a[i] - b[i]
|
// Minus two series, result[i] = a[i] - b[i]
|
||||||
func Minus(a interface{}, b interface{}) Series {
|
func Minus(a interface{}, b interface{}) SeriesExtend {
|
||||||
aa := switchIface(a)
|
aa := switchIface(a)
|
||||||
bb := switchIface(b)
|
bb := switchIface(b)
|
||||||
return &MinusSeriesResult{aa, bb}
|
return NewSeries(&MinusSeriesResult{aa, bb})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *MinusSeriesResult) Last() float64 {
|
func (a *MinusSeriesResult) Last() float64 {
|
||||||
|
@ -388,13 +428,13 @@ func switchIface(b interface{}) Series {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Divid two series, result[i] = a[i] / b[i]
|
// Divid two series, result[i] = a[i] / b[i]
|
||||||
func Div(a interface{}, b interface{}) Series {
|
func Div(a interface{}, b interface{}) SeriesExtend {
|
||||||
aa := switchIface(a)
|
aa := switchIface(a)
|
||||||
if 0 == b {
|
if 0 == b {
|
||||||
panic("Divid by zero exception")
|
panic("Divid by zero exception")
|
||||||
}
|
}
|
||||||
bb := switchIface(b)
|
bb := switchIface(b)
|
||||||
return &DivSeriesResult{aa, bb}
|
return NewSeries(&DivSeriesResult{aa, bb})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -423,7 +463,7 @@ func (a *DivSeriesResult) Length() int {
|
||||||
var _ Series = &DivSeriesResult{}
|
var _ Series = &DivSeriesResult{}
|
||||||
|
|
||||||
// Multiple two series, result[i] = a[i] * b[i]
|
// Multiple two series, result[i] = a[i] * b[i]
|
||||||
func Mul(a interface{}, b interface{}) Series {
|
func Mul(a interface{}, b interface{}) SeriesExtend {
|
||||||
var aa Series
|
var aa Series
|
||||||
var bb Series
|
var bb Series
|
||||||
|
|
||||||
|
@ -444,7 +484,7 @@ func Mul(a interface{}, b interface{}) Series {
|
||||||
panic("input should be either Series or float64")
|
panic("input should be either Series or float64")
|
||||||
|
|
||||||
}
|
}
|
||||||
return &MulSeriesResult{aa, bb}
|
return NewSeries(&MulSeriesResult{aa, bb})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -546,13 +586,13 @@ func (c *ChangeResult) Length() int {
|
||||||
|
|
||||||
// Difference between current value and previous, a - a[offset]
|
// Difference between current value and previous, a - a[offset]
|
||||||
// offset: if not given, offset is 1.
|
// offset: if not given, offset is 1.
|
||||||
func Change(a Series, offset ...int) Series {
|
func Change(a Series, offset ...int) SeriesExtend {
|
||||||
o := 1
|
o := 1
|
||||||
if len(offset) > 0 {
|
if len(offset) > 0 {
|
||||||
o = offset[0]
|
o = offset[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ChangeResult{a, o}
|
return NewSeries(&ChangeResult{a, o})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stdev(a Series, length int) float64 {
|
func Stdev(a Series, length int) float64 {
|
||||||
|
|
85
pkg/types/seriesbase_imp.go
Normal file
85
pkg/types/seriesbase_imp.go
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
func (s *SeriesBase) Index(i int) float64 {
|
||||||
|
return s.index(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Last() float64 {
|
||||||
|
return s.last()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Length() int {
|
||||||
|
return s.length()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Sum(limit ...int) float64 {
|
||||||
|
return Sum(s, limit...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Mean(limit ...int) float64 {
|
||||||
|
return Mean(s, limit...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Abs() SeriesExtend {
|
||||||
|
return Abs(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Predict(lookback int, offset ...int) float64 {
|
||||||
|
return Predict(s, lookback, offset...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) NextCross(b Series, lookback int) (int, float64, bool) {
|
||||||
|
return NextCross(s, b, lookback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) CrossOver(b Series) BoolSeries {
|
||||||
|
return CrossOver(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) CrossUnder(b Series) BoolSeries {
|
||||||
|
return CrossUnder(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Highest(lookback int) float64 {
|
||||||
|
return Highest(s, lookback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Lowest(lookback int) float64 {
|
||||||
|
return Lowest(s, lookback)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Add(b interface{}) SeriesExtend {
|
||||||
|
return Add(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Minus(b interface{}) SeriesExtend {
|
||||||
|
return Minus(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Div(b interface{}) SeriesExtend {
|
||||||
|
return Div(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Mul(b interface{}) SeriesExtend {
|
||||||
|
return Mul(s, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Dot(b interface{}, limit ...int) float64 {
|
||||||
|
return Dot(s, b, limit...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) ToArray(limit ...int) (result []float64) {
|
||||||
|
return ToArray(s, limit...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) ToReverseArray(limit ...int) (result Float64Slice) {
|
||||||
|
return ToReverseArray(s, limit...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Change(offset ...int) SeriesExtend {
|
||||||
|
return Change(s, offset...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SeriesBase) Stdev(length int) float64 {
|
||||||
|
return Stdev(s, length)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user