From e55676abab888a859da0e30a2244bfa6847f874c Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 16 Oct 2024 15:40:50 +0800 Subject: [PATCH] xmaker: add delayHedgeCounterMetrics counter --- pkg/strategy/xmaker/metrics.go | 7 +++++++ pkg/strategy/xmaker/strategy.go | 22 +++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkg/strategy/xmaker/metrics.go b/pkg/strategy/xmaker/metrics.go index c3a7fde47..bd898e184 100644 --- a/pkg/strategy/xmaker/metrics.go +++ b/pkg/strategy/xmaker/metrics.go @@ -2,6 +2,12 @@ package xmaker import "github.com/prometheus/client_golang/prometheus" +var delayHedgeCounterMetrics = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "xmaker_delay_hedge_counter", + Help: "delay hedge counter", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + var cancelOrderDurationMetrics = prometheus.NewHistogramVec( prometheus.HistogramOpts{ Name: "xmaker_cancel_order_duration_milliseconds", @@ -97,5 +103,6 @@ func init() { configMaxExposureMetrics, configBidMarginMetrics, configAskMarginMetrics, + delayHedgeCounterMetrics, ) } diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index eaf536801..8afce56ac 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1174,14 +1174,22 @@ func (s *Strategy) canDelayHedge(side types.SideType, pos fixedpoint.Value) bool signal := s.lastAggregatedSignal.Get() + if math.Abs(signal) < s.DelayHedgeSignalThreshold { + return false + } + // if the signal is strong enough, we can delay the hedge and wait for the next tick - if math.Abs(signal) > s.DelayHedgeSignalThreshold { - period, ok := s.getPositionHoldingPeriod(time.Now()) - if ok && (signal > 0 && side == types.SideTypeSell) || (signal < 0 && side == types.SideTypeBuy) { - if period < s.MaxDelayHedgeDuration.Duration() { - s.logger.Infof("delay hedge enabled, signal %f is strong enough, waiting for the next tick to hedge %s quantity (max period %s)", signal, pos, s.MaxDelayHedgeDuration.Duration().String()) - return true - } + period, ok := s.getPositionHoldingPeriod(time.Now()) + if !ok { + return false + } + + if (signal > 0 && side == types.SideTypeSell) || (signal < 0 && side == types.SideTypeBuy) { + if period < s.MaxDelayHedgeDuration.Duration() { + s.logger.Infof("delay hedge enabled, signal %f is strong enough, waiting for the next tick to hedge %s quantity (max period %s)", signal, pos, s.MaxDelayHedgeDuration.Duration().String()) + + delayHedgeCounterMetrics.With(s.metricsLabels).Inc() + return true } }