From 6d75a5401163ba7505ca213afa5e86ff151b8164 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 29 Nov 2024 17:20:38 +0800 Subject: [PATCH] xmaker: use quote, base as much as possible --- pkg/strategy/xmaker/strategy.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index c1f41db9bb..13e402190f 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1022,7 +1022,16 @@ func (s *Strategy) updateQuote(ctx context.Context) error { makerBestBidPriceMetrics.With(s.metricsLabels).Set(bidPrice.Float64()) } - if makerQuota.QuoteAsset.Lock(bidQuantity.Mul(bidPrice)) && hedgeQuota.BaseAsset.Lock(bidQuantity) { + requiredQuote := fixedpoint.Min( + bidQuantity.Mul(bidPrice), + makerQuota.QuoteAsset.Available, + ) + + // if we bought, then we need to sell the base from the hedge session + // if the hedge session is a margin session, we don't need to lock the base asset + if makerQuota.QuoteAsset.Lock(requiredQuote) && + (s.sourceSession.Margin || hedgeQuota.BaseAsset.Lock(bidQuantity)) { + // if we bought, then we need to sell the base from the hedge session submitOrders = append(submitOrders, types.SubmitOrder{ Symbol: s.Symbol, @@ -1036,7 +1045,7 @@ func (s *Strategy) updateQuote(ctx context.Context) error { makerQuota.Commit() hedgeQuota.Commit() - bidExposureInUsd = bidExposureInUsd.Add(bidQuantity.Mul(bidPrice)) + bidExposureInUsd = bidExposureInUsd.Add(requiredQuote) } else { makerQuota.Rollback() hedgeQuota.Rollback() @@ -1071,7 +1080,9 @@ func (s *Strategy) updateQuote(ctx context.Context) error { makerBestAskPriceMetrics.With(s.metricsLabels).Set(askPrice.Float64()) } - if makerQuota.BaseAsset.Lock(askQuantity) && hedgeQuota.QuoteAsset.Lock(askQuantity.Mul(askPrice)) { + requiredBase := fixedpoint.Min(askQuantity, makerQuota.BaseAsset.Available) + if makerQuota.BaseAsset.Lock(requiredBase) && + (s.sourceSession.Margin || 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{