liqmaker: sum exposure in quote quantity (usd)

This commit is contained in:
c9s 2024-10-28 15:08:09 +08:00
parent 17d57502f1
commit 74cc36121b
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 52 additions and 3 deletions

View File

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

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
@ -81,6 +82,7 @@ type Strategy struct {
orderGenerator *LiquidityOrderGenerator orderGenerator *LiquidityOrderGenerator
logger log.FieldLogger logger log.FieldLogger
metricsLabels prometheus.Labels
} }
func (s *Strategy) Initialize() error { func (s *Strategy) Initialize() error {
@ -88,9 +90,19 @@ func (s *Strategy) Initialize() error {
s.Strategy = &common.Strategy{} s.Strategy = &common.Strategy{}
} }
s.logger = log.WithField("strategy", ID).WithFields(log.Fields{ s.logger = log.WithFields(log.Fields{
"symbol": s.Symbol, "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 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 var orderForms []types.SubmitOrder
if placeBid { if placeBid {
bidOrders := s.orderGenerator.Generate(types.SideTypeBuy, bidOrders := s.orderGenerator.Generate(types.SideTypeBuy,
@ -416,6 +430,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
s.NumOfLiquidityLayers, s.NumOfLiquidityLayers,
s.liquidityScale) s.liquidityScale)
bidExposureInUsd = sumOrderQuoteQuantity(bidOrders)
orderForms = append(orderForms, bidOrders...) orderForms = append(orderForms, bidOrders...)
} }
@ -428,6 +443,7 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
s.liquidityScale) s.liquidityScale)
askOrders = filterAskOrders(askOrders, baseBal.Available) askOrders = filterAskOrders(askOrders, baseBal.Available)
askExposureInUsd = sumOrderQuoteQuantity(askOrders)
orderForms = append(orderForms, askOrders...) orderForms = append(orderForms, askOrders...)
} }
@ -440,6 +456,9 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) {
s.liquidityOrderBook.Add(createdOrders...) 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)) s.logger.Infof("%d liq orders are placed successfully", len(orderForms))
for _, o := range createdOrders { for _, o := range createdOrders {
s.logger.Infof("liq order: %+v", o) s.logger.Infof("liq order: %+v", o)
@ -464,6 +483,14 @@ func profitProtectedPrice(
return price 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) { func filterAskOrders(askOrders []types.SubmitOrder, available fixedpoint.Value) (out []types.SubmitOrder) {
usedBase := fixedpoint.Zero usedBase := fixedpoint.Zero
for _, askOrder := range askOrders { for _, askOrder := range askOrders {