liqmaker: add market metrics updater

This commit is contained in:
c9s 2024-10-30 14:45:50 +08:00
parent 92a934b6f2
commit 31aea5753e
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 56 additions and 11 deletions

View File

@ -2,71 +2,91 @@ package liquiditymaker
import "github.com/prometheus/client_golang/prometheus"
var generalLabels = []string{"strategy_type", "strategy_id", "exchange", "symbol"}
var spreadMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_spread",
Help: "",
}, generalLabels)
var tickerBidMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_ticker_bid",
Help: "",
}, generalLabels)
var tickerAskMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_ticker_ask",
Help: "",
}, generalLabels)
var openOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_open_order_bid_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var openOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_open_order_ask_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var askLiquidityAmountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_ask_liquidity_amount",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var bidLiquidityAmountMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_bid_liquidity_amount",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var askLiquidityPriceHighMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_ask_liquidity_price_high",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var askLiquidityPriceLowMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_ask_liquidity_price_low",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var bidLiquidityPriceHighMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_bid_liquidity_price_high",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var bidLiquidityPriceLowMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_bid_liquidity_price_low",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var midPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_mid_price",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
var orderPlacementStatusMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_order_placement_status",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol", "side"})
}, generalLabels)
var liquidityPriceRangeMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_liquidity_price_range",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})
}, generalLabels)
func init() {
prometheus.MustRegister(
@ -86,5 +106,10 @@ func init() {
bidLiquidityPriceLowMetrics,
orderPlacementStatusMetrics,
tickerBidMetrics,
tickerAskMetrics,
spreadMetrics,
)
}

View File

@ -140,7 +140,24 @@ func (s *Strategy) Initialize() error {
return nil
}
func (s *Strategy) updateMarketMetrics(ctx context.Context) error {
ticker, err := s.Session.Exchange.QueryTicker(ctx, s.Symbol)
if err != nil {
return err
}
currentSpread := ticker.Sell.Sub(ticker.Buy)
tickerBidMetrics.With(s.metricsLabels).Set(ticker.Buy.Float64())
tickerAskMetrics.With(s.metricsLabels).Set(ticker.Sell.Float64())
spreadMetrics.With(s.metricsLabels).Set(currentSpread.Float64())
return nil
}
func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval) {
metricsTicker := time.NewTicker(5 * time.Second)
defer metricsTicker.Stop()
ticker := time.NewTicker(interval.Duration())
defer ticker.Stop()
@ -150,6 +167,9 @@ func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval)
case <-ctx.Done():
return
case <-metricsTicker.C:
s.updateMarketMetrics(ctx)
case <-ticker.C:
s.placeLiquidityOrders(ctx)
}