mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge branch 'strategy/swing' into main
This commit is contained in:
commit
607656ff55
|
@ -6,10 +6,13 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
//go:generate callbackgen -type EWMA
|
||||
type EWMA struct {
|
||||
types.IntervalWindow
|
||||
Values Float64Slice
|
||||
EndTime time.Time
|
||||
Values Float64Slice
|
||||
EndTime time.Time
|
||||
|
||||
UpdateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *EWMA) Last() float64 {
|
||||
|
@ -32,18 +35,20 @@ func (inc *EWMA) calculateAndUpdate(kLines []types.KLine) {
|
|||
return
|
||||
}
|
||||
|
||||
inc.EndTime = kLines[index].EndTime
|
||||
|
||||
var recentK = kLines[index-(inc.Window-1) : index+1]
|
||||
if len(inc.Values) > 0 {
|
||||
var previousEWMA = inc.Values[len(inc.Values)-1]
|
||||
var ewma = lastK.Close*multiplier + previousEWMA*(1-multiplier)
|
||||
inc.Values.Push(ewma)
|
||||
inc.EmitUpdate(ewma)
|
||||
} else {
|
||||
// The first EWMA is actually SMA
|
||||
var sma = calculateSMA(recentK)
|
||||
inc.Values.Push(sma)
|
||||
inc.EmitUpdate(sma)
|
||||
}
|
||||
|
||||
inc.EndTime = kLines[index].EndTime
|
||||
}
|
||||
|
||||
type KLineWindowUpdater interface {
|
15
pkg/indicator/ewma_callbacks.go
Normal file
15
pkg/indicator/ewma_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type EWMA"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *EWMA) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *EWMA) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
|
@ -14,10 +14,14 @@ func (s *Float64Slice) Push(v float64) {
|
|||
|
||||
var zeroTime time.Time
|
||||
|
||||
|
||||
//go:generate callbackgen -type SMA
|
||||
type SMA struct {
|
||||
types.IntervalWindow
|
||||
Values Float64Slice
|
||||
EndTime time.Time
|
||||
|
||||
UpdateCallbacks []func(value float64)
|
||||
}
|
||||
|
||||
func (inc *SMA) Last() float64 {
|
||||
|
@ -40,6 +44,8 @@ func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {
|
|||
var sma = calculateSMA(recentK)
|
||||
inc.Values.Push(sma)
|
||||
inc.EndTime = kLines[index].EndTime
|
||||
|
||||
inc.EmitUpdate(sma)
|
||||
}
|
||||
|
||||
func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
||||
|
|
15
pkg/indicator/sma_callbacks.go
Normal file
15
pkg/indicator/sma_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type SMA"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *SMA) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *SMA) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user