pivotshort: improve last high/low invalidation

This commit is contained in:
c9s 2022-09-14 18:20:02 +08:00
parent 88696bc6d2
commit ebf4abf54d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 24 additions and 13 deletions

View File

@ -48,6 +48,8 @@ type BreakLow struct {
lastLow, lastFastLow fixedpoint.Value
lastLowInvalidated bool
// lastBreakLow is the low that the price just break
lastBreakLow fixedpoint.Value
@ -163,6 +165,11 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
return
}
if s.lastLowInvalidated {
log.Infof("the last low is invalidated, skip")
return
}
previousLow := s.lastLow
ratio := fixedpoint.One.Add(s.Ratio)
breakPrice := previousLow.Mul(ratio)
@ -266,19 +273,18 @@ func (s *BreakLow) updatePivotLow() bool {
return false
}
// if the last low is different
lastLowChanged := low.Compare(s.lastLow) != 0
if lastLowChanged {
if s.lastFastLow.IsZero() || low.Compare(s.lastFastLow) > 0 {
s.lastLow = low
s.pivotLowPrices = append(s.pivotLowPrices, low)
}
s.lastLow = low
s.lastLowInvalidated = false
s.pivotLowPrices = append(s.pivotLowPrices, low)
}
fastLow := fixedpoint.NewFromFloat(s.fastPivotLow.Last())
if !fastLow.IsZero() {
if fastLow.Compare(s.lastLow) < 0 {
// invalidate the last low
s.lastLow = fixedpoint.Zero
s.lastLowInvalidated = true
lastLowChanged = false
}
s.lastFastLow = fastLow

View File

@ -40,10 +40,11 @@ type FailedBreakHigh struct {
TrendEMA *bbgo.TrendEMA `json:"trendEMA"`
lastFailedBreakHigh, lastHigh, lastFastHigh fixedpoint.Value
lastHighInvalidated bool
pivotHighPrices []fixedpoint.Value
pivotHigh, fastPivotHigh *indicator.PivotHigh
vwma *indicator.VWMA
pivotHighPrices []fixedpoint.Value
orderExecutor *bbgo.GeneralOrderExecutor
session *bbgo.ExchangeSession
@ -174,7 +175,12 @@ func (s *FailedBreakHigh) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.BreakInterval, func(kline types.KLine) {
if len(s.pivotHighPrices) == 0 || s.lastHigh.IsZero() {
log.Infof("currently there is no pivot high prices, can not check failed break high...")
log.Infof("%s currently there is no pivot high prices, can not check failed break high...", s.Symbol)
return
}
if s.lastHighInvalidated {
log.Infof("%s last high %f is invalidated", s.Symbol, s.lastHigh.Float64())
return
}
@ -288,18 +294,17 @@ func (s *FailedBreakHigh) updatePivotHigh() bool {
lastHighChanged := high.Compare(s.lastHigh) != 0
if lastHighChanged {
if s.lastHigh.IsZero() || high.Compare(s.lastHigh) > 0 {
s.lastHigh = high
s.pivotHighPrices = append(s.pivotHighPrices, high)
}
s.lastHigh = high
s.lastHighInvalidated = false
s.pivotHighPrices = append(s.pivotHighPrices, high)
}
fastHigh := fixedpoint.NewFromFloat(s.fastPivotHigh.Last())
if !fastHigh.IsZero() {
if fastHigh.Compare(s.lastHigh) > 0 {
// invalidate the last low
s.lastHigh = fixedpoint.Zero
lastHighChanged = false
s.lastHighInvalidated = true
}
s.lastFastHigh = fastHigh
}