From 13a8597d59518a9d756baae2d1bb67851b6fc0fb Mon Sep 17 00:00:00 2001 From: c9s Date: Sun, 4 Apr 2021 11:14:09 +0800 Subject: [PATCH] add MaxExposurePosition settings --- pkg/strategy/xmaker/strategy.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 2d944a52c..727555ff3 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -50,12 +50,13 @@ type Strategy struct { UpdateInterval types.Duration `json:"updateInterval"` HedgeInterval types.Duration `json:"hedgeInterval"` - Margin fixedpoint.Value `json:"margin"` - BidMargin fixedpoint.Value `json:"bidMargin"` - AskMargin fixedpoint.Value `json:"askMargin"` - Quantity fixedpoint.Value `json:"quantity"` - QuantityMultiplier fixedpoint.Value `json:"quantityMultiplier"` - DisableHedge bool `json:"disableHedge"` + Margin fixedpoint.Value `json:"margin"` + BidMargin fixedpoint.Value `json:"bidMargin"` + AskMargin fixedpoint.Value `json:"askMargin"` + Quantity fixedpoint.Value `json:"quantity"` + QuantityMultiplier fixedpoint.Value `json:"quantityMultiplier"` + MaxExposurePosition fixedpoint.Value `json:"maxExposurePosition"` + DisableHedge bool `json:"disableHedge"` NumLayers int `json:"numLayers"` Pips int `json:"pips"` @@ -150,6 +151,18 @@ func (s *Strategy) updateQuote(ctx context.Context) { } } + // if max exposure position is configured, we should not: + // 1. place bid orders when we already bought too much + // 2. place ask orders when we already sold too much + if s.MaxExposurePosition > 0 { + pos := s.state.HedgePosition.AtomicLoad() + if pos < -s.MaxExposurePosition { + disableMakerAsk = true + } else if pos > s.MaxExposurePosition { + disableMakerBid = true + } + } + hedgeBalances := s.sourceSession.Account.Balances() hedgeQuota := &bbgo.QuotaTransaction{} if b, ok := hedgeBalances[s.sourceMarket.BaseCurrency]; ok { @@ -481,4 +494,3 @@ func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, se return nil } -