liqmaker: add more metrics

This commit is contained in:
c9s 2024-10-30 14:29:37 +08:00
parent a9eb903f01
commit 92a934b6f2
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 148 additions and 33 deletions

View File

@ -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,
)
}

View File

@ -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
}
@ -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()
@ -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,
@ -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
}
@ -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
@ -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,
@ -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...)
@ -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
}
}