2022-08-26 09:51:43 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"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"`
|
|
|
|
|
|
|
|
ewma *indicator.EWMA
|
|
|
|
|
|
|
|
last, current float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrendEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
|
|
|
symbol := orderExecutor.Position().Symbol
|
|
|
|
s.ewma = session.StandardIndicatorSet(symbol).EWMA(s.IntervalWindow)
|
|
|
|
|
|
|
|
session.MarketDataStream.OnStart(func() {
|
|
|
|
if s.ewma.Length() < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.last = s.ewma.Values[s.ewma.Length()-2]
|
2023-05-31 11:35:44 +00:00
|
|
|
s.current = s.ewma.Last(0)
|
2022-08-26 09:51:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
|
|
|
|
s.last = s.current
|
2023-05-31 11:35:44 +00:00
|
|
|
s.current = s.ewma.Last(0)
|
2022-08-26 09:51:43 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrendEMA) Gradient() float64 {
|
|
|
|
if s.last > 0.0 && s.current > 0.0 {
|
2022-09-11 05:56:36 +00:00
|
|
|
return s.current / s.last
|
2022-08-26 09:51:43 +00:00
|
|
|
}
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TrendEMA) GradientAllowed() bool {
|
|
|
|
gradient := s.Gradient()
|
|
|
|
|
|
|
|
logrus.Infof("trendEMA %+v current=%f last=%f gradient=%f", s, s.current, s.last, gradient)
|
|
|
|
|
|
|
|
if gradient == .0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-09-15 17:20:48 +00:00
|
|
|
if s.MaxGradient > 0.0 && gradient > s.MaxGradient {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.MinGradient > 0.0 && gradient < s.MinGradient {
|
|
|
|
return false
|
2022-08-26 09:51:43 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 17:20:48 +00:00
|
|
|
return true
|
2022-08-26 09:51:43 +00:00
|
|
|
}
|