Skip to content

Commit

Permalink
xmaker: rewrite maker order submission logics and integrate metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Aug 24, 2024
1 parent c76a80d commit 9a1d9ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
15 changes: 11 additions & 4 deletions pkg/strategy/xmaker/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ package xmaker

import "github.com/prometheus/client_golang/prometheus"

var openOrderExposureInUsdMetrics = prometheus.NewGaugeVec(
var openOrderBidExposureInUsdMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "xmaker_open_order_exposure_in_usd",
Name: "xmaker_open_order_bid_exposure_in_usd",
Help: "",
}, []string{"strategy_type", "strategy_id", "exchange", "symbol", "side"})
}, []string{"strategy_type", "strategy_id", "exchange", "symbol"})

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

var makerBestBidPriceMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand All @@ -28,7 +34,8 @@ var numOfLayersMetrics = prometheus.NewGaugeVec(

func init() {
prometheus.MustRegister(
openOrderExposureInUsdMetrics,
openOrderBidExposureInUsdMetrics,
openOrderAskExposureInUsdMetrics,
makerBestBidPriceMetrics,
makerBestAskPriceMetrics,
numOfLayersMetrics,
Expand Down
41 changes: 37 additions & 4 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"

Expand Down Expand Up @@ -384,6 +385,16 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
}
}

labels := prometheus.Labels{
"strategy_type": ID,
"strategy_id": s.InstanceID(),
"exchange": s.MakerExchange,
"symbol": s.Symbol,
}

bidExposureInUsd := fixedpoint.Zero
askExposureInUsd := fixedpoint.Zero

bidPrice := bestBidPrice
askPrice := bestAskPrice
for i := 0; i < s.NumLayers; i++ {
Expand Down Expand Up @@ -417,6 +428,8 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
Mul(s.makerMarket.TickSize)))
}

makerBestBidPriceMetrics.With(labels).Set(bidPrice.Float64())

if makerQuota.QuoteAsset.Lock(bidQuantity.Mul(bidPrice)) && hedgeQuota.BaseAsset.Lock(bidQuantity) {
// if we bought, then we need to sell the base from the hedge session
submitOrders = append(submitOrders, types.SubmitOrder{
Expand All @@ -431,6 +444,7 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or

makerQuota.Commit()
hedgeQuota.Commit()
bidExposureInUsd = bidExposureInUsd.Add(bidQuantity.Mul(bidPrice))
} else {
makerQuota.Rollback()
hedgeQuota.Rollback()
Expand Down Expand Up @@ -470,7 +484,10 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
askPrice = askPrice.Add(pips.Mul(fixedpoint.NewFromInt(int64(i)).Mul(s.makerMarket.TickSize)))
}

makerBestAskPriceMetrics.With(labels).Set(askPrice.Float64())

if makerQuota.BaseAsset.Lock(askQuantity) && hedgeQuota.QuoteAsset.Lock(askQuantity.Mul(askPrice)) {

// if we bought, then we need to sell the base from the hedge session
submitOrders = append(submitOrders, types.SubmitOrder{
Symbol: s.Symbol,
Expand All @@ -484,6 +501,8 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
})
makerQuota.Commit()
hedgeQuota.Commit()

askExposureInUsd = askExposureInUsd.Add(askQuantity.Mul(askPrice))
} else {
makerQuota.Rollback()
hedgeQuota.Rollback()
Expand All @@ -500,14 +519,28 @@ func (s *Strategy) updateQuote(ctx context.Context, orderExecutionRouter bbgo.Or
return
}

makerOrders, err := orderExecutionRouter.SubmitOrdersTo(ctx, s.MakerExchange, submitOrders...)
formattedOrders, err := s.makerSession.FormatOrders(submitOrders)
if err != nil {
log.WithError(err).Errorf("order error: %s", err.Error())
return
}

s.activeMakerOrders.Add(makerOrders...)
s.orderStore.Add(makerOrders...)
orderCreateCallback := func(createdOrder types.Order) {
s.orderStore.Add(createdOrder)
s.activeMakerOrders.Add(createdOrder)
}

defer s.tradeCollector.Process()

createdOrders, errIdx, err := bbgo.BatchPlaceOrder(ctx, s.makerSession.Exchange, orderCreateCallback, formattedOrders...)
if err != nil {
log.WithError(err).Errorf("unable to place maker orders: %+v", formattedOrders)
}

openOrderBidExposureInUsdMetrics.With(labels).Set(bidExposureInUsd.Float64())
openOrderAskExposureInUsdMetrics.With(labels).Set(askExposureInUsd.Float64())

_ = errIdx
_ = createdOrders
}

var lastPriceModifier = fixedpoint.NewFromFloat(1.001)
Expand Down

0 comments on commit 9a1d9ae

Please sign in to comment.