pivotshort: add trendEMA protection

This commit is contained in:
c9s 2022-07-13 11:09:57 +08:00
parent f5f6fabe07
commit ee163eb441
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 34 additions and 9 deletions

View File

@ -101,12 +101,12 @@ exchangeStrategies:
minQuoteVolume: 200_000_000
- trailingStop:
callbackRate: 2%
callbackRate: 3%
# activationRatio is relative to the average cost,
# when side is buy, 1% means lower 1% than the average cost.
# when side is sell, 1% means higher 1% than the average cost.
activationRatio: 10%
activationRatio: 40%
# minProfit uses the position ROI to calculate the profit ratio
# minProfit: 1%

View File

@ -31,9 +31,13 @@ type BreakLow struct {
TrendEMA *types.IntervalWindow `json:"trendEMA"`
lastLow fixedpoint.Value
pivot *indicator.Pivot
stopEWMA *indicator.EWMA
lastLow fixedpoint.Value
pivot *indicator.Pivot
stopEWMA *indicator.EWMA
trendEWMA *indicator.EWMA
trendEWMALast, trendEWMACurrent float64
pivotLowPrices []fixedpoint.Value
orderExecutor *bbgo.GeneralOrderExecutor
@ -72,6 +76,15 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
s.stopEWMA = standardIndicator.EWMA(*s.StopEMA)
}
if s.TrendEMA != nil {
s.trendEWMA = standardIndicator.EWMA(*s.TrendEMA)
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.TrendEMA.Interval, func(kline types.KLine) {
s.trendEWMALast = s.trendEWMACurrent
s.trendEWMACurrent = s.trendEWMA.Last()
}))
}
// update pivot low data
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
lastLow := fixedpoint.NewFromFloat(s.pivot.LastLow())
@ -88,10 +101,6 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
}))
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, types.Interval1m, func(kline types.KLine) {
if position.IsOpened(kline.Close) {
return
}
if len(s.pivotLowPrices) == 0 {
log.Infof("currently there is no pivot low prices, can not check break low...")
return
@ -124,6 +133,22 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
log.Infof("%s breakLow signal detected, closed price %f < breakPrice %f", kline.Symbol, closePrice.Float64(), breakPrice.Float64())
if position.IsOpened(kline.Close) {
log.Infof("position is already opened, skip short")
return
}
// trend EMA protection
if s.trendEWMALast > 0.0 && s.trendEWMACurrent > 0.0 {
slope := s.trendEWMALast / s.trendEWMACurrent
if slope > 1.0 {
log.Infof("trendEMA %+v current=%f last=%f slope=%f: skip short", s.TrendEMA, s.trendEWMACurrent, s.trendEWMALast, slope)
return
}
log.Infof("trendEMA %+v current=%f last=%f slope=%f: short is enabled", s.TrendEMA, s.trendEWMACurrent, s.trendEWMALast, slope)
}
// stop EMA protection
if s.stopEWMA != nil {
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last())