Skip to content

Commit

Permalink
Merge pull request #1824 from c9s/c9s/xdepthmaker-maker-metrics
Browse files Browse the repository at this point in the history
FEATURE: [xdepthmaker] add maker metrics
  • Loading branch information
c9s authored Nov 15, 2024
2 parents 2f0932c + 5c6a00c commit b8f8e09
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
88 changes: 88 additions & 0 deletions pkg/metrics/maker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package metrics

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{
Name: "bbgo_maker_open_order_bid_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

var OpenOrderAskExposureInUsdMetrics = 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(
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_best_bid_price",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

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) {
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,
)
}
3 changes: 3 additions & 0 deletions pkg/strategy/xdepthmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit b8f8e09

Please sign in to comment.