bbgo_origin/pkg/strategy/fmaker/A18.go

93 lines
1.8 KiB
Go
Raw Permalink Normal View History

2022-05-29 13:27:58 +00:00
package fmaker
import (
"fmt"
"time"
"github.com/c9s/bbgo/pkg/datatype/floats"
2022-05-29 13:27:58 +00:00
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type A18
type A18 struct {
types.IntervalWindow
// Values
Values floats.Slice
2022-05-29 13:27:58 +00:00
EndTime time.Time
UpdateCallbacks []func(val float64)
}
func (inc *A18) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}
return inc.Values[len(inc.Values)-1]
}
func (inc *A18) CalculateAndUpdate(klines []types.KLine) {
2022-05-29 13:27:58 +00:00
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 := calculateA18(recentT, indicator.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 *A18) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.CalculateAndUpdate(window)
2022-05-29 13:27:58 +00:00
}
func (inc *A18) Bind(updater indicator.KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}
// CLOSE/DELAY(CLOSE,5)
func calculateA18(klines []types.KLine, valClose KLineValueMapper) (float64, error) {
window := 5
length := len(klines)
if length == 0 || length < window {
return 0., fmt.Errorf("insufficient elements for calculating with window = %d", window)
}
var closes floats.Slice
2022-05-29 13:27:58 +00:00
for _, k := range klines {
closes.Push(valClose(k))
}
delay5 := closes.Index(4)
curr := closes.Index(0)
alpha := curr / delay5
return alpha, nil
}