Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMPROVE: [metrics] make the metrics name consistent and update best ask/bid prices #1825

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 34 additions & 17 deletions pkg/metrics/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,74 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

var OpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
var makerOpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_bid_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var OpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
var makerOpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_ask_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var OpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec(
var makerOpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_bid_count",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var OpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec(
var makerOpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_open_order_ask_count",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var MakerBestBidPriceMetrics = prometheus.NewGaugeVec(
var makerBestBidPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_best_bid_price",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var MakerBestAskPriceMetrics = prometheus.NewGaugeVec(
var makerBestAskPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_maker_best_ask_price",
Help: "",
}, []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
askOrderCount := 0
bidExposureQuoteAmount := fixedpoint.Zero
askExposureQuoteAmount := fixedpoint.Zero

bestAskPrice := fixedpoint.Zero
bestBidPrice := fixedpoint.Zero

for _, submitOrder := range submitOrders {
quoteAmount := submitOrder.Quantity.Mul(submitOrder.Price)

switch submitOrder.Side {
case types.SideTypeSell:
askExposureQuoteAmount = askExposureQuoteAmount.Add(quoteAmount)
askOrderCount++
if bestAskPrice.IsZero() {
bestAskPrice = submitOrder.Price
} else {
bestAskPrice = fixedpoint.Min(bestAskPrice, submitOrder.Price)
}

case types.SideTypeBuy:
bidExposureQuoteAmount = bidExposureQuoteAmount.Add(quoteAmount)
bidOrderCount++

if bestBidPrice.IsZero() {
bestBidPrice = submitOrder.Price
} else {
bestBidPrice = fixedpoint.Max(bestBidPrice, submitOrder.Price)
}

}
}

Expand All @@ -70,19 +85,21 @@ func UpdateOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol strin
"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))
makerOpenOrderBidExposureInUsdMetrics.With(labels).Set(bidExposureQuoteAmount.Float64())
makerOpenOrderAskExposureInUsdMetrics.With(labels).Set(askExposureQuoteAmount.Float64())
makerOpenOrderBidOrderCountMetrics.With(labels).Set(float64(bidOrderCount))
makerOpenOrderAskOrderCountMetrics.With(labels).Set(float64(askOrderCount))
makerBestBidPriceMetrics.With(labels).Set(bestBidPrice.Float64())
makerBestAskPriceMetrics.With(labels).Set(bestAskPrice.Float64())
}

func init() {
prometheus.MustRegister(
OpenOrderAskExposureInUsdMetrics,
OpenOrderBidExposureInUsdMetrics,
MakerBestAskPriceMetrics,
MakerBestBidPriceMetrics,
OpenOrderAskOrderCountMetrics,
OpenOrderBidOrderCountMetrics,
makerOpenOrderAskExposureInUsdMetrics,
makerOpenOrderBidExposureInUsdMetrics,
makerBestAskPriceMetrics,
makerBestBidPriceMetrics,
makerOpenOrderAskOrderCountMetrics,
makerOpenOrderBidOrderCountMetrics,
)
}
2 changes: 1 addition & 1 deletion pkg/strategy/xdepthmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,7 +1194,7 @@ 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)
metrics.UpdateMakerOpenOrderMetrics(ID, s.InstanceID(), s.MakerExchange, s.Symbol, submitOrders)

_, err = s.MakerOrderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil {
Expand Down
Loading