From 2448fa6f8373f02c46fc36d1f5269edc17c01f82 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 19 Jun 2023 13:46:45 +0800 Subject: [PATCH] scmaker: add MaxExposure option --- pkg/strategy/scmaker/strategy.go | 24 ++++++++++++++++++------ pkg/types/trade_stats_test.go | 13 +++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/pkg/strategy/scmaker/strategy.go b/pkg/strategy/scmaker/strategy.go index 553608e8e..83a7bc6e6 100644 --- a/pkg/strategy/scmaker/strategy.go +++ b/pkg/strategy/scmaker/strategy.go @@ -46,6 +46,8 @@ type Strategy struct { LiquiditySlideRule *bbgo.SlideRule `json:"liquidityScale"` LiquidityLayerTickSize fixedpoint.Value `json:"liquidityLayerTickSize"` + MaxExposure fixedpoint.Value `json:"maxExposure"` + MinProfit fixedpoint.Value `json:"minProfit"` Position *types.Position `json:"position,omitempty" persistence:"position"` @@ -264,10 +266,6 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { midPriceEMA := s.ewma.Last(0) midPrice := fixedpoint.NewFromFloat(midPriceEMA) - makerQuota := &bbgo.QuotaTransaction{} - makerQuota.QuoteAsset.Add(quoteBal.Available) - makerQuota.BaseAsset.Add(baseBal.Available) - bandWidth := s.boll.Last(0) log.Infof("spread: %f mid price ema: %f boll band width: %f", spread.Float64(), midPriceEMA, bandWidth) @@ -294,11 +292,11 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { askPrice = midPrice.Add(sp) } - if i > 0 && bidPrice.Compare(ticker.Buy) < 0 { + if i > 0 && bidPrice.Compare(ticker.Buy) > 0 { bidPrice = ticker.Buy.Sub(sp) } - if i > 0 && askPrice.Compare(ticker.Sell) > 0 { + if i > 0 && askPrice.Compare(ticker.Sell) < 0 { askPrice = ticker.Sell.Add(sp) } @@ -312,6 +310,20 @@ func (s *Strategy) placeLiquidityOrders(ctx context.Context) { availableBase := baseBal.Available availableQuote := quoteBal.Available + // check max exposure + if s.MaxExposure.Sign() > 0 { + availableQuote = fixedpoint.Min(availableQuote, s.MaxExposure) + + baseQuoteValue := availableBase.Mul(ticker.Sell) + if baseQuoteValue.Compare(s.MaxExposure) > 0 { + availableBase = s.MaxExposure.Div(ticker.Sell) + } + } + + makerQuota := &bbgo.QuotaTransaction{} + makerQuota.QuoteAsset.Add(availableQuote) + makerQuota.BaseAsset.Add(availableBase) + log.Infof("balances before liq orders: %s, %s", baseBal.String(), quoteBal.String()) diff --git a/pkg/types/trade_stats_test.go b/pkg/types/trade_stats_test.go index a543476d4..24de27cf2 100644 --- a/pkg/types/trade_stats_test.go +++ b/pkg/types/trade_stats_test.go @@ -8,8 +8,17 @@ import ( "github.com/c9s/bbgo/pkg/fixedpoint" ) -func number(v float64) fixedpoint.Value { - return fixedpoint.NewFromFloat(v) +func number(v interface{}) fixedpoint.Value { + switch tv := v.(type) { + case float64: + return fixedpoint.NewFromFloat(tv) + + case string: + return fixedpoint.MustNewFromString(tv) + + default: + panic("invalid number input") + } } func TestTradeStats_consecutiveCounterAndAmount(t *testing.T) {