xmaker: initialize bollinger band signal

This commit is contained in:
c9s 2024-08-30 17:15:12 +08:00
parent 9ebab4f4f7
commit b8abc065de
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 15 additions and 22 deletions

View File

@ -63,11 +63,6 @@ func (s *BollingerBandTrendSignal) CalculateSignal(ctx context.Context) (float64
lastDownBand := fixedpoint.NewFromFloat(s.indicator.DownBand.Last(0))
lastUpBand := fixedpoint.NewFromFloat(s.indicator.UpBand.Last(0))
log.Infof("bollinger band: up/down = %f/%f, close price = %f",
lastUpBand.Float64(),
lastDownBand.Float64(),
closePrice.Float64())
// if the price is inside the band, do not vote
if closePrice.Compare(lastDownBand) > 0 && closePrice.Compare(lastUpBand) < 0 {
return 0.0, nil
@ -82,6 +77,10 @@ func (s *BollingerBandTrendSignal) CalculateSignal(ctx context.Context) (float64
signal = closePrice.Sub(lastUpBand).Float64() / maxBandWidth * 2.0
}
log.Infof("bollinger signal: %f", signal)
log.Infof("[BollingerBandTrendSignal] %f up/down = %f/%f, close price = %f",
signal,
lastUpBand.Float64(),
lastDownBand.Float64(),
closePrice.Float64())
return signal, nil
}

View File

@ -49,8 +49,6 @@ func (s *OrderBookBestPriceVolumeSignal) CalculateSignal(ctx context.Context) (f
return 0.0, nil
}
log.Infof("OrderBookBestPriceVolumeSignal: bid/ask = %f/%f", bid.Volume.Float64(), ask.Volume.Float64())
// TODO: may use scale to define this
sumVol := bid.Volume.Add(ask.Volume)
bidRatio := bid.Volume.Div(sumVol)
@ -65,6 +63,8 @@ func (s *OrderBookBestPriceVolumeSignal) CalculateSignal(ctx context.Context) (f
signal = -numerator.Div(denominator).Float64()
}
log.Infof("[OrderBookBestPriceVolumeSignal] %f bid/ask = %f/%f", signal, bid.Volume.Float64(), ask.Volume.Float64())
orderBookSignalMetrics.WithLabelValues(s.symbol).Set(signal)
return signal, nil
}

View File

@ -1247,20 +1247,14 @@ func (s *Strategy) CrossRun(
s.book.BindStream(s.sourceSession.MarketDataStream)
for _, signalConfig := range s.SignalConfigList {
var sigAny any
switch {
case signalConfig.OrderBookBestPriceSignal != nil:
sig := signalConfig.OrderBookBestPriceSignal
sig.book = s.book
sigAny = sig
case signalConfig.BollingerBandTrendSignal != nil:
}
if sigAny != nil {
if binder, ok := sigAny.(SessionBinder); ok {
binder.Bind(ctx, s.sourceSession, s.Symbol)
if signalConfig.OrderBookBestPriceSignal != nil {
signalConfig.OrderBookBestPriceSignal.book = s.book
if err := signalConfig.OrderBookBestPriceSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil {
return err
}
} else if signalConfig.BollingerBandTrendSignal != nil {
if err := signalConfig.BollingerBandTrendSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil {
return err
}
}
}