From 31aea5753e7f05b6a2eca61207fc3c372bd3cea8 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 30 Oct 2024 14:45:50 +0800 Subject: [PATCH] liqmaker: add market metrics updater --- pkg/strategy/liquiditymaker/metrics.go | 47 +++++++++++++++++++------ pkg/strategy/liquiditymaker/strategy.go | 20 +++++++++++ 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/pkg/strategy/liquiditymaker/metrics.go b/pkg/strategy/liquiditymaker/metrics.go index afb9bf93b..863615fc4 100644 --- a/pkg/strategy/liquiditymaker/metrics.go +++ b/pkg/strategy/liquiditymaker/metrics.go @@ -2,71 +2,91 @@ package liquiditymaker import "github.com/prometheus/client_golang/prometheus" +var generalLabels = []string{"strategy_type", "strategy_id", "exchange", "symbol"} + +var spreadMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_spread", + Help: "", + }, generalLabels) + +var tickerBidMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_ticker_bid", + Help: "", + }, generalLabels) + +var tickerAskMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_ticker_ask", + Help: "", + }, generalLabels) + var openOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_open_order_bid_exposure_in_usd", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var openOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_open_order_ask_exposure_in_usd", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var askLiquidityAmountMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_ask_liquidity_amount", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var bidLiquidityAmountMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_bid_liquidity_amount", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var askLiquidityPriceHighMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_ask_liquidity_price_high", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var askLiquidityPriceLowMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_ask_liquidity_price_low", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var bidLiquidityPriceHighMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_bid_liquidity_price_high", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var bidLiquidityPriceLowMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_bid_liquidity_price_low", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var midPriceMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_mid_price", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) var orderPlacementStatusMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_order_placement_status", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol", "side"}) + }, generalLabels) var liquidityPriceRangeMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Name: "liqmaker_liquidity_price_range", Help: "", - }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + }, generalLabels) func init() { prometheus.MustRegister( @@ -86,5 +106,10 @@ func init() { bidLiquidityPriceLowMetrics, orderPlacementStatusMetrics, + + tickerBidMetrics, + tickerAskMetrics, + + spreadMetrics, ) } diff --git a/pkg/strategy/liquiditymaker/strategy.go b/pkg/strategy/liquiditymaker/strategy.go index 25ae7f9b0..5e61961f1 100644 --- a/pkg/strategy/liquiditymaker/strategy.go +++ b/pkg/strategy/liquiditymaker/strategy.go @@ -140,7 +140,24 @@ func (s *Strategy) Initialize() error { return nil } +func (s *Strategy) updateMarketMetrics(ctx context.Context) error { + ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol) + if err != nil { + return err + } + + currentSpread := ticker.Sell.Sub(ticker.Buy) + + tickerBidMetrics.With(s.metricsLabels).Set(ticker.Buy.Float64()) + tickerAskMetrics.With(s.metricsLabels).Set(ticker.Sell.Float64()) + spreadMetrics.With(s.metricsLabels).Set(currentSpread.Float64()) + return nil +} + func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval) { + metricsTicker := time.NewTicker(5 * time.Second) + defer metricsTicker.Stop() + ticker := time.NewTicker(interval.Duration()) defer ticker.Stop() @@ -150,6 +167,9 @@ func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval) case <-ctx.Done(): return + case <-metricsTicker.C: + s.updateMarketMetrics(ctx) + case <-ticker.C: s.placeLiquidityOrders(ctx) }