metrics: add maker metrics for xdepthmaker

This commit is contained in:
c9s 2024-11-16 00:05:26 +08:00
parent 60c7dfea29
commit 5c6a00cc87
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 58 additions and 3 deletions

View File

@ -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,
)
}

View File

@ -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())