bbgo_origin/pkg/strategy/pivotshort/trendema.go

52 lines
1.5 KiB
Go
Raw Normal View History

package pivotshort
import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
type TrendEMA struct {
types.IntervalWindow
// MaxGradient is the maximum gradient allowed for the entry.
MaxGradient float64 `json:"maxGradient"`
MinGradient float64 `json:"minGradient"`
trendEWMA *indicator.EWMA
trendEWMALast, trendEWMACurrent, trendGradient float64
}
func (s *TrendEMA) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor) {
symbol := orderExecutor.Position().Symbol
s.trendEWMA = session.StandardIndicatorSet(symbol).EWMA(s.IntervalWindow)
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
s.trendEWMALast = s.trendEWMACurrent
s.trendEWMACurrent = s.trendEWMA.Last()
}))
}
func (s *TrendEMA) GradientAllowed() bool {
if s.trendEWMALast > 0.0 && s.trendEWMACurrent > 0.0 {
s.trendGradient = s.trendEWMALast / s.trendEWMACurrent
}
if s.trendGradient == .0 {
return false
}
if s.MaxGradient > 0.0 && s.trendGradient < s.MaxGradient {
2022-07-29 08:13:57 +00:00
log.Infof("trendEMA %+v current=%f last=%f gradient=%f: allowed", s, s.trendEWMACurrent, s.trendEWMALast, s.trendGradient)
return true
}
if s.MinGradient > 0.0 && s.trendGradient > s.MinGradient {
2022-07-29 08:13:57 +00:00
log.Infof("trendEMA %+v current=%f last=%f gradient=%f: allowed", s, s.trendEWMACurrent, s.trendEWMALast, s.trendGradient)
return true
}
2022-07-29 08:13:57 +00:00
log.Infof("trendEMA %+v current=%f last=%f gradient=%f: disallowed", s, s.trendEWMACurrent, s.trendEWMALast, s.trendGradient)
return false
}