From 92a934b6f2c7f0f589b7d82ee8b5b20da4676c93 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 30 Oct 2024 14:29:37 +0800 Subject: [PATCH] liqmaker: add more metrics --- pkg/strategy/liquiditymaker/metrics.go | 68 ++++++++++++++ pkg/strategy/liquiditymaker/strategy.go | 113 +++++++++++++++++------- 2 files changed, 148 insertions(+), 33 deletions(-) diff --git a/pkg/strategy/liquiditymaker/metrics.go b/pkg/strategy/liquiditymaker/metrics.go index 7dfccf3a2..afb9bf93b 100644 --- a/pkg/strategy/liquiditymaker/metrics.go +++ b/pkg/strategy/liquiditymaker/metrics.go @@ -14,9 +14,77 @@ var openOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec( Help: "", }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) +var askLiquidityAmountMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_ask_liquidity_amount", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var bidLiquidityAmountMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_bid_liquidity_amount", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var askLiquidityPriceHighMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_ask_liquidity_price_high", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var askLiquidityPriceLowMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_ask_liquidity_price_low", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var bidLiquidityPriceHighMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_bid_liquidity_price_high", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var bidLiquidityPriceLowMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_bid_liquidity_price_low", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var midPriceMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_mid_price", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var orderPlacementStatusMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_order_placement_status", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol", "side"}) + +var liquidityPriceRangeMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_liquidity_price_range", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + func init() { prometheus.MustRegister( openOrderBidExposureInUsdMetrics, openOrderAskExposureInUsdMetrics, + + midPriceMetrics, + + askLiquidityAmountMetrics, + bidLiquidityAmountMetrics, + liquidityPriceRangeMetrics, + + askLiquidityPriceHighMetrics, + askLiquidityPriceLowMetrics, + + bidLiquidityPriceHighMetrics, + bidLiquidityPriceLowMetrics, + + orderPlacementStatusMetrics, ) } diff --git a/pkg/strategy/liquiditymaker/strategy.go b/pkg/strategy/liquiditymaker/strategy.go index a5101e1ca..25ae7f9b0 100644 --- a/pkg/strategy/liquiditymaker/strategy.go +++ b/pkg/strategy/liquiditymaker/strategy.go @@ -86,27 +86,6 @@ type Strategy struct { metricsLabels prometheus.Labels } -func (s *Strategy) Initialize() error { - if s.Strategy == nil { - s.Strategy = &common.Strategy{} - } - - s.logger = log.WithFields(log.Fields{ - "symbol": s.Symbol, - "strategy": ID, - "strategy_id": s.InstanceID(), - }) - - s.metricsLabels = prometheus.Labels{ - "strategy_type": ID, - "strategy_id": s.InstanceID(), - "exchange": "", // FIXME - "symbol": s.Symbol, - } - - return nil -} - func (s *Strategy) ID() string { return ID } @@ -140,6 +119,27 @@ func (s *Strategy) Defaults() error { return nil } +func (s *Strategy) Initialize() error { + if s.Strategy == nil { + s.Strategy = &common.Strategy{} + } + + s.logger = log.WithFields(log.Fields{ + "symbol": s.Symbol, + "strategy": ID, + "strategy_id": s.InstanceID(), + }) + + s.metricsLabels = prometheus.Labels{ + "strategy_type": ID, + "strategy_id": s.InstanceID(), + "exchange": "", // FIXME + "symbol": s.Symbol, + } + + return nil +} + func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval) { ticker := time.NewTicker(interval.Duration()) defer ticker.Stop() @@ -176,6 +176,8 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.stopEMA = session.Indicators(s.Symbol).EMA(s.StopEMA.IntervalWindow) } + s.metricsLabels["exchange"] = session.ExchangeName.String() + s.orderGenerator = &LiquidityOrderGenerator{ Symbol: s.Symbol, Market: s.Market, @@ -359,12 +361,12 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { s.logger.Infof("ticker: %+v", ticker) - lastTradedPrice := ticker.Last + lastTradedPrice := ticker.GetValidPrice() midPrice := ticker.Sell.Add(ticker.Buy).Div(fixedpoint.Two) currentSpread := ticker.Sell.Sub(ticker.Buy) sideSpread := s.Spread.Div(fixedpoint.Two) - if s.UseLastTradePrice && !lastTradedPrice.IsZero() { + if !lastTradedPrice.IsZero() && (s.UseLastTradePrice || midPrice.IsZero()) { midPrice = lastTradedPrice } @@ -380,6 +382,8 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { ask1Price.Float64(), askLastPrice.Float64(), bid1Price.Float64(), bidLastPrice.Float64()) + midPriceMetrics.With(s.metricsLabels).Set(midPrice.Float64()) + placeBid := true placeAsk := true @@ -456,17 +460,14 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { var bidExposureInUsd = fixedpoint.Zero var askExposureInUsd = fixedpoint.Zero var orderForms []types.SubmitOrder - if placeBid { - bidOrders := s.orderGenerator.Generate(types.SideTypeBuy, - fixedpoint.Min(s.BidLiquidityAmount, quoteBal.Available), - bid1Price, - bidLastPrice, - s.NumOfLiquidityLayers, - s.liquidityScale) - bidExposureInUsd = sumOrderQuoteQuantity(bidOrders) - orderForms = append(orderForms, bidOrders...) - } + orderPlacementStatusMetrics.With(extendLabels(s.metricsLabels, prometheus.Labels{ + "side": "bid", + })).Set(bool2float(placeBid)) + + orderPlacementStatusMetrics.With(extendLabels(s.metricsLabels, prometheus.Labels{ + "side": "ask", + })).Set(bool2float(placeAsk)) if placeAsk { askOrders := s.orderGenerator.Generate(types.SideTypeSell, @@ -477,10 +478,38 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { s.liquidityScale) askOrders = filterAskOrders(askOrders, baseBal.Available) + + if len(askOrders) > 0 { + askLiquidityPriceLowMetrics.With(s.metricsLabels).Set(askOrders[0].Price.Float64()) + askLiquidityPriceHighMetrics.With(s.metricsLabels).Set(askOrders[len(askOrders)-1].Price.Float64()) + } + askExposureInUsd = sumOrderQuoteQuantity(askOrders) orderForms = append(orderForms, askOrders...) } + bidLiquidityAmountMetrics.With(s.metricsLabels).Set(s.BidLiquidityAmount.Float64()) + askLiquidityAmountMetrics.With(s.metricsLabels).Set(s.AskLiquidityAmount.Float64()) + liquidityPriceRangeMetrics.With(s.metricsLabels).Set(s.LiquidityPriceRange.Float64()) + + if placeBid { + bidOrders := s.orderGenerator.Generate(types.SideTypeBuy, + fixedpoint.Min(s.BidLiquidityAmount, quoteBal.Available), + bid1Price, + bidLastPrice, + s.NumOfLiquidityLayers, + s.liquidityScale) + + bidExposureInUsd = sumOrderQuoteQuantity(bidOrders) + + if len(bidOrders) > 0 { + bidLiquidityPriceHighMetrics.With(s.metricsLabels).Set(bidOrders[0].Price.Float64()) + bidLiquidityPriceLowMetrics.With(s.metricsLabels).Set(bidOrders[len(bidOrders)-1].Price.Float64()) + } + + orderForms = append(orderForms, bidOrders...) + } + dbg.DebugSubmitOrders(s.logger, orderForms) createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...) @@ -538,3 +567,21 @@ func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value) return out } + +func extendLabels(a, o prometheus.Labels) prometheus.Labels { + for k, v := range a { + if _, exists := o[k]; !exists { + o[k] = v + } + } + + return o +} + +func bool2float(b bool) float64 { + if b { + return 1.0 + } else { + return -1.0 + } +}