pivotshort: add maxGradient config to trendEMA

This commit is contained in:
c9s 2022-07-28 10:27:16 +08:00
parent 93593ffa06
commit d61047cd26
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 61 additions and 40 deletions

View File

@ -15,32 +15,6 @@ type StopEMA struct {
Range fixedpoint.Value `json:"range"`
}
type TrendEMA struct {
types.IntervalWindow
trendEWMA *indicator.EWMA
trendEWMALast, trendEWMACurrent 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) Gradient() (float64, bool) {
if s.trendEWMALast > 0.0 && s.trendEWMACurrent > 0.0 {
gradient := s.trendEWMALast / s.trendEWMACurrent
return gradient, true
}
return 0.0, false
}
type FakeBreakStop struct {
types.IntervalWindow
}
@ -120,6 +94,9 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
}
if s.TrendEMA != nil {
if s.TrendEMA.MaxGradient == 0.0 {
s.TrendEMA.MaxGradient = 1.0
}
s.TrendEMA.Bind(session, orderExecutor)
}
@ -213,13 +190,8 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
}
// trend EMA protection
if gradient, ok := s.TrendEMA.Gradient(); ok {
if gradient > 1.0 {
log.Debugf("trendEMA %+v current=%f last=%f slope=%f: skip short", s.TrendEMA, s.TrendEMA.trendEWMACurrent, s.TrendEMA.trendEWMALast, gradient)
return
}
log.Debugf("trendEMA %+v current=%f last=%f slope=%f: short is enabled", s.TrendEMA, s.TrendEMA.trendEWMACurrent, s.TrendEMA.trendEWMALast, gradient)
if !s.TrendEMA.GradientAllowed() {
return
}
// stop EMA protection

View File

@ -58,6 +58,9 @@ func (s *ResistanceShort) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
s.activeOrders.BindStream(session.UserDataStream)
if s.TrendEMA != nil {
if s.TrendEMA.MaxGradient == 0.0 {
s.TrendEMA.MaxGradient = 1.0
}
s.TrendEMA.Bind(session, orderExecutor)
}
@ -68,13 +71,8 @@ func (s *ResistanceShort) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
// trend EMA protection
if gradient, ok := s.TrendEMA.Gradient(); ok {
if gradient > 1.0 {
log.Debugf("trendEMA %+v current=%f last=%f gradient=%f: skip short", s.TrendEMA, s.TrendEMA.trendEWMACurrent, s.TrendEMA.trendEWMALast, gradient)
return
}
log.Debugf("trendEMA %+v current=%f last=%f gradient=%f: short is enabled", s.TrendEMA, s.TrendEMA.trendEWMACurrent, s.TrendEMA.trendEWMALast, gradient)
if !s.TrendEMA.GradientAllowed() {
return
}
position := s.orderExecutor.Position()

View File

@ -0,0 +1,51 @@
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 {
log.Debugf("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 {
log.Debugf("trendEMA %+v current=%f last=%f gradient=%f: allowed", s, s.trendEWMACurrent, s.trendEWMALast, s.trendGradient)
return true
}
log.Debugf("trendEMA %+v current=%f last=%f gradient=%f: disallowed", s, s.trendEWMACurrent, s.trendEWMALast, s.trendGradient)
return false
}