From 74cc36121b8a2ebab817f0dc2b646ac69920f348 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 28 Oct 2024 15:08:09 +0800 Subject: [PATCH] liqmaker: sum exposure in quote quantity (usd) --- pkg/strategy/liquiditymaker/metrics.go | 22 +++++++++++++++++ pkg/strategy/liquiditymaker/strategy.go | 33 ++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 pkg/strategy/liquiditymaker/metrics.go diff --git a/pkg/strategy/liquiditymaker/metrics.go b/pkg/strategy/liquiditymaker/metrics.go new file mode 100644 index 000000000..7dfccf3a2 --- /dev/null +++ b/pkg/strategy/liquiditymaker/metrics.go @@ -0,0 +1,22 @@ +package liquiditymaker + +import "github.com/prometheus/client_golang/prometheus" + +var openOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_open_order_bid_exposure_in_usd", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +var openOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "liqmaker_open_order_ask_exposure_in_usd", + Help: "", + }, []string{"strategy_type", "strategy_id", "exchange", "symbol"}) + +func init() { + prometheus.MustRegister( + openOrderBidExposureInUsdMetrics, + openOrderAskExposureInUsdMetrics, + ) +} diff --git a/pkg/strategy/liquiditymaker/strategy.go b/pkg/strategy/liquiditymaker/strategy.go index 817596409..2921e3dd2 100644 --- a/pkg/strategy/liquiditymaker/strategy.go +++ b/pkg/strategy/liquiditymaker/strategy.go @@ -5,6 +5,7 @@ import ( "fmt" "sync" + "github.com/prometheus/client_golang/prometheus" log "github.com/sirupsen/logrus" "github.com/c9s/bbgo/pkg/bbgo" @@ -80,7 +81,8 @@ type Strategy struct { orderGenerator *LiquidityOrderGenerator - logger log.FieldLogger + logger log.FieldLogger + metricsLabels prometheus.Labels } func (s *Strategy) Initialize() error { @@ -88,9 +90,19 @@ func (s *Strategy) Initialize() error { s.Strategy = &common.Strategy{} } - s.logger = log.WithField("strategy", ID).WithFields(log.Fields{ - "symbol": s.Symbol, + s.logger = log.WithFields(log.Fields{ + "symbol": s.Symbol, + "strategy": ID, + "strategy_id": s.InstanceID(), }) + + s.metricsLabels = prometheus.Labels{ + "strategy_type": ID, + "strategy_id": s.InstanceID(), + "exchange": string(s.Session.Exchange.Name()), + "symbol": s.Symbol, + } + return nil } @@ -407,6 +419,8 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { } } + var bidExposureInUsd = fixedpoint.Zero + var askExposureInUsd = fixedpoint.Zero var orderForms []types.SubmitOrder if placeBid { bidOrders := s.orderGenerator.Generate(types.SideTypeBuy, @@ -416,6 +430,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { s.NumOfLiquidityLayers, s.liquidityScale) + bidExposureInUsd = sumOrderQuoteQuantity(bidOrders) orderForms = append(orderForms, bidOrders...) } @@ -428,6 +443,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { s.liquidityScale) askOrders = filterAskOrders(askOrders, baseBal.Available) + askExposureInUsd = sumOrderQuoteQuantity(askOrders) orderForms = append(orderForms, askOrders...) } @@ -440,6 +456,9 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { s.liquidityOrderBook.Add(createdOrders...) + openOrderBidExposureInUsdMetrics.With(s.metricsLabels).Set(bidExposureInUsd.Float64()) + openOrderAskExposureInUsdMetrics.With(s.metricsLabels).Set(askExposureInUsd.Float64()) + s.logger.Infof("%d liq orders are placed successfully", len(orderForms)) for _, o := range createdOrders { s.logger.Infof("liq order: %+v", o) @@ -464,6 +483,14 @@ func profitProtectedPrice( return price } +func sumOrderQuoteQuantity(orders []types.SubmitOrder) fixedpoint.Value { + sum := fixedpoint.Zero + for _, order := range orders { + sum = sum.Add(order.Price.Mul(order.Quantity)) + } + return sum +} + func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value) (out []types.SubmitOrder) { usedBase := fixedpoint.Zero for _, askOrder := range askOrders {