From 656112de454f5245ecda8a9856493e8ef9f46227 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 Sep 2024 16:07:28 +0800 Subject: [PATCH] xmaker: call signalConfig.TradeVolumeWindowSignal.Bind --- pkg/strategy/xmaker/signal_trade.go | 15 ++++++++++++--- pkg/strategy/xmaker/strategy.go | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/strategy/xmaker/signal_trade.go b/pkg/strategy/xmaker/signal_trade.go index 6e67d3595..bb3db5f9c 100644 --- a/pkg/strategy/xmaker/signal_trade.go +++ b/pkg/strategy/xmaker/signal_trade.go @@ -2,6 +2,7 @@ package xmaker import ( "context" + "sync" "time" "github.com/prometheus/client_golang/prometheus" @@ -27,10 +28,14 @@ type TradeVolumeWindowSignal struct { trades []types.Trade symbol string + + mu sync.Mutex } func (s *TradeVolumeWindowSignal) handleTrade(trade types.Trade) { + s.mu.Lock() s.trades = append(s.trades, trade) + s.mu.Unlock() } func (s *TradeVolumeWindowSignal) Bind(ctx context.Context, session *bbgo.ExchangeSession, symbol string) error { @@ -52,6 +57,9 @@ func (s *TradeVolumeWindowSignal) filterTrades(now time.Time) []types.Trade { startTime := now.Add(-time.Duration(s.Window)) startIdx := 0 + s.mu.Lock() + defer s.mu.Unlock() + for idx, td := range s.trades { // skip trades before the start time if td.Time.Before(startTime) { @@ -62,8 +70,9 @@ func (s *TradeVolumeWindowSignal) filterTrades(now time.Time) []types.Trade { break } - s.trades = s.trades[startIdx:] - return s.trades + trades := s.trades[startIdx:] + s.trades = trades + return trades } func (s *TradeVolumeWindowSignal) calculateTradeVolume(trades []types.Trade) (buyVolume, sellVolume float64) { @@ -95,7 +104,7 @@ func (s *TradeVolumeWindowSignal) CalculateSignal(ctx context.Context) (float64, sig = -(sellRatio - threshold) / 2.0 } - log.Infof("[TradeVolumeWindowSignal] sig: %f buy/sell = %f/%f", sig, buyVolume, sellVolume) + log.Infof("[TradeVolumeWindowSignal] %f buy/sell = %f/%f", sig, buyVolume, sellVolume) tradeVolumeWindowSignalMetrics.WithLabelValues(s.symbol).Set(sig) return sig, nil diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 943fd9185..de97f691d 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1373,6 +1373,10 @@ func (s *Strategy) CrossRun( if err := signalConfig.BollingerBandTrendSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil { return err } + } else if signalConfig.TradeVolumeWindowSignal != nil { + if err := signalConfig.TradeVolumeWindowSignal.Bind(ctx, s.sourceSession, s.Symbol); err != nil { + return err + } } }