metrics: make the metrics name consistent and update best ask/bid prices

This commit is contained in:
c9s 2024-11-16 00:17:06 +08:00
parent 5c6a00cc87
commit e8944082a7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 35 additions and 18 deletions

View File

@ -7,47 +7,51 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
var OpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec( var makerOpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_bid_exposure_in_usd", Name: "bbgo_maker_open_order_bid_exposure_in_usd",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
var OpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec( var makerOpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_ask_exposure_in_usd", Name: "bbgo_maker_open_order_ask_exposure_in_usd",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
var OpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec( var makerOpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_bid_count", Name: "bbgo_maker_open_order_bid_count",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
var OpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec( var makerOpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_ask_count", Name: "bbgo_maker_open_order_ask_count",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
var MakerBestBidPriceMetrics = prometheus.NewGaugeVec( var makerBestBidPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_best_bid_price", Name: "bbgo_maker_best_bid_price",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
var MakerBestAskPriceMetrics = prometheus.NewGaugeVec( var makerBestAskPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "bbgo_maker_best_ask_price", Name: "bbgo_maker_best_ask_price",
Help: "", Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) }, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
func UpdateOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol string, submitOrders []types.SubmitOrder) { func UpdateMakerOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol string, submitOrders []types.SubmitOrder) {
bidOrderCount := 0 bidOrderCount := 0
askOrderCount := 0 askOrderCount := 0
bidExposureQuoteAmount := fixedpoint.Zero bidExposureQuoteAmount := fixedpoint.Zero
askExposureQuoteAmount := fixedpoint.Zero askExposureQuoteAmount := fixedpoint.Zero
bestAskPrice := fixedpoint.Zero
bestBidPrice := fixedpoint.Zero
for _, submitOrder := range submitOrders { for _, submitOrder := range submitOrders {
quoteAmount := submitOrder.Quantity.Mul(submitOrder.Price) quoteAmount := submitOrder.Quantity.Mul(submitOrder.Price)
@ -55,11 +59,22 @@ func UpdateOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol strin
case types.SideTypeSell: case types.SideTypeSell:
askExposureQuoteAmount = askExposureQuoteAmount.Add(quoteAmount) askExposureQuoteAmount = askExposureQuoteAmount.Add(quoteAmount)
askOrderCount++ askOrderCount++
if bestAskPrice.IsZero() {
bestAskPrice = submitOrder.Price
} else {
bestAskPrice = fixedpoint.Min(bestAskPrice, submitOrder.Price)
}
case types.SideTypeBuy: case types.SideTypeBuy:
bidExposureQuoteAmount = bidExposureQuoteAmount.Add(quoteAmount) bidExposureQuoteAmount = bidExposureQuoteAmount.Add(quoteAmount)
bidOrderCount++ bidOrderCount++
if bestBidPrice.IsZero() {
bestBidPrice = submitOrder.Price
} else {
bestBidPrice = fixedpoint.Max(bestBidPrice, submitOrder.Price)
}
} }
} }
@ -70,19 +85,21 @@ func UpdateOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol strin
"symbol": symbol, "symbol": symbol,
} }
OpenOrderBidExposureInUsdMetrics.With(labels).Set(bidExposureQuoteAmount.Float64()) makerOpenOrderBidExposureInUsdMetrics.With(labels).Set(bidExposureQuoteAmount.Float64())
OpenOrderAskExposureInUsdMetrics.With(labels).Set(askExposureQuoteAmount.Float64()) makerOpenOrderAskExposureInUsdMetrics.With(labels).Set(askExposureQuoteAmount.Float64())
OpenOrderBidOrderCountMetrics.With(labels).Set(float64(bidOrderCount)) makerOpenOrderBidOrderCountMetrics.With(labels).Set(float64(bidOrderCount))
OpenOrderAskOrderCountMetrics.With(labels).Set(float64(askOrderCount)) makerOpenOrderAskOrderCountMetrics.With(labels).Set(float64(askOrderCount))
makerBestBidPriceMetrics.With(labels).Set(bestBidPrice.Float64())
makerBestAskPriceMetrics.With(labels).Set(bestAskPrice.Float64())
} }
func init() { func init() {
prometheus.MustRegister( prometheus.MustRegister(
OpenOrderAskExposureInUsdMetrics, makerOpenOrderAskExposureInUsdMetrics,
OpenOrderBidExposureInUsdMetrics, makerOpenOrderBidExposureInUsdMetrics,
MakerBestAskPriceMetrics, makerBestAskPriceMetrics,
MakerBestBidPriceMetrics, makerBestBidPriceMetrics,
OpenOrderAskOrderCountMetrics, makerOpenOrderAskOrderCountMetrics,
OpenOrderBidOrderCountMetrics, makerOpenOrderBidOrderCountMetrics,
) )
} }

View File

@ -1194,7 +1194,7 @@ func (s *Strategy) updateQuote(ctx context.Context, maxLayer int) {
s.logger.Infof("%d orders are generated, placing...", len(submitOrders)) s.logger.Infof("%d orders are generated, placing...", len(submitOrders))
metrics.UpdateOpenOrderMetrics(ID, s.InstanceID(), s.MakerExchange, s.Symbol, submitOrders) metrics.UpdateMakerOpenOrderMetrics(ID, s.InstanceID(), s.MakerExchange, s.Symbol, submitOrders)
_, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...) _, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil { if err != nil {