2022-06-07 11:46:31 +00:00
|
|
|
package indicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Refer: https://tradingview.com/script/aDymGrFx-Drift-Study-Inspired-by-Monte-Carlo-Simulations-with-BM-KL/
|
|
|
|
// Brownian Motion's drift factor
|
|
|
|
// could be used in Monte Carlo Simulations
|
|
|
|
//go:generate callbackgen -type Drift
|
|
|
|
type Drift struct {
|
2022-06-29 12:49:02 +00:00
|
|
|
types.SeriesBase
|
2022-06-07 11:46:31 +00:00
|
|
|
types.IntervalWindow
|
2022-06-08 02:04:54 +00:00
|
|
|
chng *types.Queue
|
|
|
|
Values types.Float64Slice
|
|
|
|
SMA *SMA
|
2022-06-07 11:46:31 +00:00
|
|
|
LastValue float64
|
|
|
|
|
|
|
|
UpdateCallbacks []func(value float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) Update(value float64) {
|
|
|
|
if inc.chng == nil {
|
2022-06-29 12:49:02 +00:00
|
|
|
inc.SeriesBase.Series = inc
|
2022-06-08 02:04:54 +00:00
|
|
|
inc.SMA = &SMA{IntervalWindow: types.IntervalWindow{Interval: inc.Interval, Window: inc.Window}}
|
2022-06-07 11:46:31 +00:00
|
|
|
inc.chng = types.NewQueue(inc.Window)
|
2022-06-08 02:04:54 +00:00
|
|
|
inc.LastValue = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var chng float64
|
|
|
|
if value == 0 {
|
|
|
|
chng = 0
|
|
|
|
} else {
|
|
|
|
chng = math.Log(value / inc.LastValue)
|
|
|
|
inc.LastValue = value
|
2022-06-07 11:46:31 +00:00
|
|
|
}
|
|
|
|
inc.SMA.Update(chng)
|
|
|
|
inc.chng.Update(chng)
|
2022-06-08 02:04:54 +00:00
|
|
|
if inc.chng.Length() >= inc.Window {
|
|
|
|
stdev := types.Stdev(inc.chng, inc.Window)
|
|
|
|
drift := inc.SMA.Last() - stdev*stdev*0.5
|
|
|
|
inc.Values.Push(drift)
|
|
|
|
}
|
2022-06-07 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) Index(i int) float64 {
|
|
|
|
if inc.Values == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.Values.Index(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) Last() float64 {
|
|
|
|
if inc.Values.Length() == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.Values.Last()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) Length() int {
|
|
|
|
if inc.Values == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return inc.Values.Length()
|
|
|
|
}
|
|
|
|
|
2022-06-29 12:49:02 +00:00
|
|
|
var _ types.SeriesExtend = &Drift{}
|
2022-06-07 11:46:31 +00:00
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
func (inc *Drift) PushK(k types.KLine) {
|
|
|
|
inc.Update(k.Close.Float64())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) CalculateAndUpdate(allKLines []types.KLine) {
|
2022-06-07 11:46:31 +00:00
|
|
|
if inc.chng == nil {
|
|
|
|
for _, k := range allKLines {
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.PushK(k)
|
2022-06-07 11:46:31 +00:00
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-13 17:12:36 +00:00
|
|
|
k := allKLines[len(allKLines)-1]
|
|
|
|
inc.PushK(k)
|
2022-06-07 11:46:31 +00:00
|
|
|
inc.EmitUpdate(inc.Last())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
|
|
if inc.Interval != interval {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 17:12:36 +00:00
|
|
|
inc.CalculateAndUpdate(window)
|
2022-06-07 11:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (inc *Drift) Bind(updater KLineWindowUpdater) {
|
|
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
|
|
}
|