mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
package fmaker
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/c9s/bbgo/pkg/datatype/floats"
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
)
|
|
|
|
//go:generate callbackgen -type S0
|
|
type S0 struct {
|
|
types.IntervalWindow
|
|
|
|
// Values
|
|
Values floats.Slice
|
|
|
|
EndTime time.Time
|
|
|
|
UpdateCallbacks []func(val float64)
|
|
}
|
|
|
|
func (inc *S0) Last(int) float64 {
|
|
if len(inc.Values) == 0 {
|
|
return 0.0
|
|
}
|
|
return inc.Values[len(inc.Values)-1]
|
|
}
|
|
|
|
func (inc *S0) CalculateAndUpdate(klines []types.KLine) {
|
|
if len(klines) < inc.Window {
|
|
return
|
|
}
|
|
|
|
var end = len(klines) - 1
|
|
var lastKLine = klines[end]
|
|
|
|
if inc.EndTime != zeroTime && lastKLine.GetEndTime().Before(inc.EndTime) {
|
|
return
|
|
}
|
|
|
|
var recentT = klines[end-(inc.Window-1) : end+1]
|
|
|
|
val, err := calculateS0(recentT, types.KLineClosePriceMapper)
|
|
if err != nil {
|
|
log.WithError(err).Error("can not calculate")
|
|
return
|
|
}
|
|
inc.Values.Push(val)
|
|
|
|
if len(inc.Values) > indicator.MaxNumOfVOL {
|
|
inc.Values = inc.Values[indicator.MaxNumOfVOLTruncateSize-1:]
|
|
}
|
|
|
|
inc.EndTime = klines[end].GetEndTime().Time()
|
|
|
|
inc.EmitUpdate(val)
|
|
|
|
}
|
|
|
|
func (inc *S0) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
|
|
if inc.Interval != interval {
|
|
return
|
|
}
|
|
|
|
inc.CalculateAndUpdate(window)
|
|
}
|
|
|
|
func (inc *S0) Bind(updater indicator.KLineWindowUpdater) {
|
|
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
|
|
}
|
|
|
|
func calculateS0(klines []types.KLine, valClose KLineValueMapper) (float64, error) {
|
|
window := 20
|
|
length := len(klines)
|
|
if length == 0 || length < window {
|
|
return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window)
|
|
}
|
|
var closes floats.Slice
|
|
|
|
for _, k := range klines {
|
|
closes.Push(valClose(k))
|
|
}
|
|
|
|
sma := floats.Slice.Sum(closes[len(closes)-window:len(closes)-1]) / float64(window)
|
|
alpha := sma / closes.Last(0)
|
|
|
|
return alpha, nil
|
|
}
|