From 5c6a00cc87ada5a4aa6772db27847d8dd3826e8c Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 16 Nov 2024 00:05:26 +0800 Subject: [PATCH] metrics: add maker metrics for xdepthmaker --- pkg/metrics/maker.go | 58 ++++++++++++++++++++++++++-- pkg/strategy/xdepthmaker/strategy.go | 3 ++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/pkg/metrics/maker.go b/pkg/metrics/maker.go index ace54ebba..380ac21b0 100644 --- a/pkg/metrics/maker.go +++ b/pkg/metrics/maker.go @@ -1,6 +1,11 @@ package metrics -import "github.com/prometheus/client_golang/prometheus" +import ( + "github.com/prometheus/client_golang/prometheus" + + "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/types" +) var OpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ @@ -14,23 +19,70 @@ var OpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec( Help: "", }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) +var OpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "bbgo_maker_open_order_bid_count", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var OpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "bbgo_maker_open_order_ask_count", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + var MakerBestBidPriceMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: "bbgo_maker_maker_best_bid_price", + Name: "bbgo_maker_best_bid_price", Help: "", }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) var MakerBestAskPriceMetrics = prometheus.NewGaugeVec( prometheus.GaugeOpts{ - Name: "bbgo_maker_maker_best_ask_price", + Name: "bbgo_maker_best_ask_price", Help: "", }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) +func UpdateOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol string, submitOrders []types.SubmitOrder) { + bidOrderCount := 0 + askOrderCount := 0 + bidExposureQuoteAmount := fixedpoint.Zero + askExposureQuoteAmount := fixedpoint.Zero + for _, submitOrder := range submitOrders { + quoteAmount := submitOrder.Quantity.Mul(submitOrder.Price) + + switch submitOrder.Side { + case types.SideTypeSell: + askExposureQuoteAmount = askExposureQuoteAmount.Add(quoteAmount) + askOrderCount++ + + case types.SideTypeBuy: + bidExposureQuoteAmount = bidExposureQuoteAmount.Add(quoteAmount) + bidOrderCount++ + + } + } + + labels := prometheus.Labels{ + "strategy_type": strategyType, + "strategy_id": strategyId, + "exchange": exchangeName, + "symbol": symbol, + } + + OpenOrderBidExposureInUsdMetrics.With(labels).Set(bidExposureQuoteAmount.Float64()) + OpenOrderAskExposureInUsdMetrics.With(labels).Set(askExposureQuoteAmount.Float64()) + OpenOrderBidOrderCountMetrics.With(labels).Set(float64(bidOrderCount)) + OpenOrderAskOrderCountMetrics.With(labels).Set(float64(askOrderCount)) +} + func init() { prometheus.MustRegister( OpenOrderAskExposureInUsdMetrics, OpenOrderBidExposureInUsdMetrics, MakerBestAskPriceMetrics, MakerBestBidPriceMetrics, + OpenOrderAskOrderCountMetrics, + OpenOrderBidOrderCountMetrics, ) } diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index ce36e4afa..b91ac9055 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -16,6 +16,7 @@ import ( "github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/exchange/retry" "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/metrics" "github.com/c9s/bbgo/pkg/pricesolver" "github.com/c9s/bbgo/pkg/sigchan" "github.com/c9s/bbgo/pkg/strategy/common" @@ -1193,6 +1194,8 @@ func (s *Strategy) updateQuote(ctx context.Context, maxLayer int) { s.logger.Infof("%d orders are generated, placing...", len(submitOrders)) + metrics.UpdateOpenOrderMetrics(ID, s.InstanceID(), s.MakerExchange, s.Symbol, submitOrders) + _, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...) if err != nil { s.logger.WithError(err).Errorf("submit order error: %s", err.Error())