Skip to content

Commit

Permalink
liqmaker: add more metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Oct 30, 2024
1 parent c3fec1c commit 14f8062
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 33 deletions.
68 changes: 68 additions & 0 deletions pkg/strategy/liquiditymaker/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,77 @@ var openOrderAskExposureInUsdMetrics = prometheus.NewGaugeVec(
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

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

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

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

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

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

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

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

var orderPlacementStatusMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "liqmaker_order_placement_status",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol", "side"})

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

func init() {
prometheus.MustRegister(
openOrderBidExposureInUsdMetrics,
openOrderAskExposureInUsdMetrics,

midPriceMetrics,

askLiquidityAmountMetrics,
bidLiquidityAmountMetrics,
liquidityPriceRangeMetrics,

askLiquidityPriceHighMetrics,
askLiquidityPriceLowMetrics,

bidLiquidityPriceHighMetrics,
bidLiquidityPriceLowMetrics,

orderPlacementStatusMetrics,
)
}
113 changes: 80 additions & 33 deletions pkg/strategy/liquiditymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,6 @@ type Strategy struct {
metricsLabels prometheus.Labels
}

func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}

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": "", // FIXME
"symbol": s.Symbol,
}

return nil
}

func (s *Strategy) ID() string {
return ID
}
Expand Down Expand Up @@ -140,6 +119,27 @@ func (s *Strategy) Defaults() error {
return nil
}

func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}

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": "", // FIXME
"symbol": s.Symbol,
}

return nil
}

func (s *Strategy) liquidityWorker(ctx context.Context, interval types.Interval) {
ticker := time.NewTicker(interval.Duration())
defer ticker.Stop()
Expand Down Expand Up @@ -176,6 +176,8 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
s.stopEMA = session.Indicators(s.Symbol).EMA(s.StopEMA.IntervalWindow)
}

s.metricsLabels["exchange"] = session.ExchangeName.String()

s.orderGenerator = &LiquidityOrderGenerator{
Symbol: s.Symbol,
Market: s.Market,
Expand Down Expand Up @@ -359,12 +361,12 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {

s.logger.Infof("ticker: %+v", ticker)

lastTradedPrice := ticker.Last
lastTradedPrice := ticker.GetValidPrice()
midPrice := ticker.Sell.Add(ticker.Buy).Div(fixedpoint.Two)
currentSpread := ticker.Sell.Sub(ticker.Buy)
sideSpread := s.Spread.Div(fixedpoint.Two)

if s.UseLastTradePrice && !lastTradedPrice.IsZero() {
if !lastTradedPrice.IsZero() && (s.UseLastTradePrice || midPrice.IsZero()) {
midPrice = lastTradedPrice
}

Expand All @@ -380,6 +382,8 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
ask1Price.Float64(), askLastPrice.Float64(),
bid1Price.Float64(), bidLastPrice.Float64())

midPriceMetrics.With(s.metricsLabels).Set(midPrice.Float64())

placeBid := true
placeAsk := true

Expand Down Expand Up @@ -456,17 +460,14 @@ 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,
fixedpoint.Min(s.BidLiquidityAmount, quoteBal.Available),
bid1Price,
bidLastPrice,
s.NumOfLiquidityLayers,
s.liquidityScale)

bidExposureInUsd = sumOrderQuoteQuantity(bidOrders)
orderForms = append(orderForms, bidOrders...)
}
orderPlacementStatusMetrics.With(extendLabels(s.metricsLabels, prometheus.Labels{
"side": "bid",
})).Set(bool2float(placeBid))

orderPlacementStatusMetrics.With(extendLabels(s.metricsLabels, prometheus.Labels{
"side": "ask",
})).Set(bool2float(placeAsk))

if placeAsk {
askOrders := s.orderGenerator.Generate(types.SideTypeSell,
Expand All @@ -477,10 +478,38 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
s.liquidityScale)

askOrders = filterAskOrders(askOrders, baseBal.Available)

if len(askOrders) > 0 {
askLiquidityPriceLowMetrics.With(s.metricsLabels).Set(askOrders[0].Price.Float64())
askLiquidityPriceHighMetrics.With(s.metricsLabels).Set(askOrders[len(askOrders)-1].Price.Float64())
}

askExposureInUsd = sumOrderQuoteQuantity(askOrders)
orderForms = append(orderForms, askOrders...)
}

bidLiquidityAmountMetrics.With(s.metricsLabels).Set(s.BidLiquidityAmount.Float64())
askLiquidityAmountMetrics.With(s.metricsLabels).Set(s.AskLiquidityAmount.Float64())
liquidityPriceRangeMetrics.With(s.metricsLabels).Set(s.LiquidityPriceRange.Float64())

if placeBid {
bidOrders := s.orderGenerator.Generate(types.SideTypeBuy,
fixedpoint.Min(s.BidLiquidityAmount, quoteBal.Available),
bid1Price,
bidLastPrice,
s.NumOfLiquidityLayers,
s.liquidityScale)

bidExposureInUsd = sumOrderQuoteQuantity(bidOrders)

if len(bidOrders) > 0 {
bidLiquidityPriceHighMetrics.With(s.metricsLabels).Set(bidOrders[0].Price.Float64())
bidLiquidityPriceLowMetrics.With(s.metricsLabels).Set(bidOrders[len(bidOrders)-1].Price.Float64())
}

orderForms = append(orderForms, bidOrders...)
}

dbg.DebugSubmitOrders(s.logger, orderForms)

createdOrders, err := s.OrderExecutor.SubmitOrders(ctx, orderForms...)
Expand Down Expand Up @@ -538,3 +567,21 @@ func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value)

return out
}

func extendLabels(a, o prometheus.Labels) prometheus.Labels {
for k, v := range a {
if _, exists := o[k]; !exists {
o[k] = v
}
}

return o
}

func bool2float(b bool) float64 {
if b {
return 1.0
} else {
return -1.0
}
}

0 comments on commit 14f8062

Please sign in to comment.