2024-11-15 15:55:38 +00:00
|
|
|
package metrics
|
|
|
|
|
2024-11-15 16:05:26 +00:00
|
|
|
import (
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
2024-11-15 15:55:38 +00:00
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerOpenOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
|
2024-11-15 15:55:38 +00:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "bbgo_maker_open_order_bid_exposure_in_usd",
|
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerOpenOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
|
2024-11-15 15:55:38 +00:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "bbgo_maker_open_order_ask_exposure_in_usd",
|
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerOpenOrderBidOrderCountMetrics = prometheus.NewGaugeVec(
|
2024-11-15 16:05:26 +00:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "bbgo_maker_open_order_bid_count",
|
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerOpenOrderAskOrderCountMetrics = prometheus.NewGaugeVec(
|
2024-11-15 16:05:26 +00:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "bbgo_maker_open_order_ask_count",
|
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerBestBidPriceMetrics = prometheus.NewGaugeVec(
|
2024-11-15 15:55:38 +00:00
|
|
|
prometheus.GaugeOpts{
|
2024-11-15 16:05:26 +00:00
|
|
|
Name: "bbgo_maker_best_bid_price",
|
2024-11-15 15:55:38 +00:00
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
var makerBestAskPriceMetrics = prometheus.NewGaugeVec(
|
2024-11-15 15:55:38 +00:00
|
|
|
prometheus.GaugeOpts{
|
2024-11-15 16:05:26 +00:00
|
|
|
Name: "bbgo_maker_best_ask_price",
|
2024-11-15 15:55:38 +00:00
|
|
|
Help: "",
|
|
|
|
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
func UpdateMakerOpenOrderMetrics(strategyType, strategyId, exchangeName, symbol string, submitOrders []types.SubmitOrder) {
|
2024-11-15 16:05:26 +00:00
|
|
|
bidOrderCount := 0
|
|
|
|
askOrderCount := 0
|
|
|
|
bidExposureQuoteAmount := fixedpoint.Zero
|
|
|
|
askExposureQuoteAmount := fixedpoint.Zero
|
2024-11-15 16:17:06 +00:00
|
|
|
|
|
|
|
bestAskPrice := fixedpoint.Zero
|
|
|
|
bestBidPrice := fixedpoint.Zero
|
|
|
|
|
2024-11-15 16:05:26 +00:00
|
|
|
for _, submitOrder := range submitOrders {
|
|
|
|
quoteAmount := submitOrder.Quantity.Mul(submitOrder.Price)
|
|
|
|
|
|
|
|
switch submitOrder.Side {
|
|
|
|
case types.SideTypeSell:
|
|
|
|
askExposureQuoteAmount = askExposureQuoteAmount.Add(quoteAmount)
|
|
|
|
askOrderCount++
|
2024-11-15 16:17:06 +00:00
|
|
|
if bestAskPrice.IsZero() {
|
|
|
|
bestAskPrice = submitOrder.Price
|
|
|
|
} else {
|
|
|
|
bestAskPrice = fixedpoint.Min(bestAskPrice, submitOrder.Price)
|
|
|
|
}
|
2024-11-15 16:05:26 +00:00
|
|
|
|
|
|
|
case types.SideTypeBuy:
|
|
|
|
bidExposureQuoteAmount = bidExposureQuoteAmount.Add(quoteAmount)
|
|
|
|
bidOrderCount++
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
if bestBidPrice.IsZero() {
|
|
|
|
bestBidPrice = submitOrder.Price
|
|
|
|
} else {
|
|
|
|
bestBidPrice = fixedpoint.Max(bestBidPrice, submitOrder.Price)
|
|
|
|
}
|
|
|
|
|
2024-11-15 16:05:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := prometheus.Labels{
|
|
|
|
"strategy_type": strategyType,
|
|
|
|
"strategy_id": strategyId,
|
|
|
|
"exchange": exchangeName,
|
|
|
|
"symbol": symbol,
|
|
|
|
}
|
|
|
|
|
2024-11-15 16:17:06 +00:00
|
|
|
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())
|
2024-11-15 16:05:26 +00:00
|
|
|
}
|
|
|
|
|
2024-11-15 15:55:38 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(
|
2024-11-15 16:17:06 +00:00
|
|
|
makerOpenOrderAskExposureInUsdMetrics,
|
|
|
|
makerOpenOrderBidExposureInUsdMetrics,
|
|
|
|
makerBestAskPriceMetrics,
|
|
|
|
makerBestBidPriceMetrics,
|
|
|
|
makerOpenOrderAskOrderCountMetrics,
|
|
|
|
makerOpenOrderBidOrderCountMetrics,
|
2024-11-15 15:55:38 +00:00
|
|
|
)
|
|
|
|
}
|