pivotshort: refactor pivot low collector

This commit is contained in:
c9s 2022-07-27 01:57:28 +08:00
parent b746f801f7
commit ac496e8488
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -42,16 +42,15 @@ type BreakLow struct {
TrendEMA *TrendEMA `json:"trendEMA"`
lastLow fixedpoint.Value
pivotLow *indicator.PivotLow
pivotLowPrices []fixedpoint.Value
lastLow fixedpoint.Value
pivot *indicator.PivotLow
stopEWMA *indicator.EWMA
trendEWMA *indicator.EWMA
trendEWMALast, trendEWMACurrent float64
pivotLowPrices []fixedpoint.Value
orderExecutor *bbgo.GeneralOrderExecutor
session *bbgo.ExchangeSession
}
@ -79,7 +78,7 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
s.lastLow = fixedpoint.Zero
s.pivot = standardIndicator.PivotLow(s.IntervalWindow)
s.pivotLow = standardIndicator.PivotLow(s.IntervalWindow)
if s.StopEMA != nil {
s.stopEWMA = standardIndicator.EWMA(s.StopEMA.IntervalWindow)
@ -96,55 +95,22 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
// update pivot low data
session.MarketDataStream.OnStart(func() {
lastLow := fixedpoint.NewFromFloat(s.pivot.Last())
if lastLow.IsZero() {
return
if s.updatePivotLow() {
bbgo.Notify("%s new pivot low: %f", s.Symbol, s.pivotLow.Last())
}
if lastLow.Compare(s.lastLow) != 0 {
bbgo.Notify("%s found new pivot low: %f", s.Symbol, s.pivot.Last())
}
s.lastLow = lastLow
s.pivotLowPrices = append(s.pivotLowPrices, s.lastLow)
log.Infof("pilot calculation for max position: last low = %f, quantity = %f, leverage = %f",
s.lastLow.Float64(),
s.Quantity.Float64(),
s.Leverage.Float64())
quantity, err := risk.CalculateBaseQuantity(s.session, s.Market, s.lastLow, s.Quantity, s.Leverage)
if err != nil {
log.WithError(err).Errorf("quantity calculation error")
}
if quantity.IsZero() {
log.WithError(err).Errorf("quantity is zero, can not submit order")
return
}
bbgo.Notify("%s %f quantity will be used for shorting", s.Symbol, quantity.Float64())
s.pilotQuantityCalculation()
})
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, s.Interval, func(kline types.KLine) {
lastLow := fixedpoint.NewFromFloat(s.pivot.Last())
if lastLow.IsZero() {
return
if s.updatePivotLow() {
// when position is opened, do not send pivot low notify
if position.IsOpened(kline.Close) {
return
}
bbgo.Notify("%s new pivot low: %f", s.Symbol, s.pivotLow.Last())
}
if lastLow.Compare(s.lastLow) == 0 {
return
}
s.lastLow = lastLow
s.pivotLowPrices = append(s.pivotLowPrices, s.lastLow)
// when position is opened, do not send pivot low notify
if position.IsOpened(kline.Close) {
return
}
bbgo.Notify("%s new pivot low: %f", s.Symbol, s.pivot.Last())
}))
session.MarketDataStream.OnKLineClosed(types.KLineWith(symbol, types.Interval1m, func(kline types.KLine) {
@ -251,3 +217,33 @@ func (s *BreakLow) Bind(session *bbgo.ExchangeSession, orderExecutor *bbgo.Gener
}
}))
}
func (s *BreakLow) pilotQuantityCalculation() {
log.Infof("pilot calculation for max position: last low = %f, quantity = %f, leverage = %f",
s.lastLow.Float64(),
s.Quantity.Float64(),
s.Leverage.Float64())
quantity, err := risk.CalculateBaseQuantity(s.session, s.Market, s.lastLow, s.Quantity, s.Leverage)
if err != nil {
log.WithError(err).Errorf("quantity calculation error")
}
if quantity.IsZero() {
log.WithError(err).Errorf("quantity is zero, can not submit order")
return
}
bbgo.Notify("%s %f quantity will be used for shorting", s.Symbol, quantity.Float64())
}
func (s *BreakLow) updatePivotLow() bool {
lastLow := fixedpoint.NewFromFloat(s.pivotLow.Last())
if lastLow.IsZero() || lastLow.Compare(s.lastLow) == 0 {
return false
}
s.lastLow = lastLow
s.pivotLowPrices = append(s.pivotLowPrices, lastLow)
return true
}