From 969e813c7f6f2f9a82f94a727749d7ef35f1f9e1 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 7 Oct 2024 17:03:45 +0800 Subject: [PATCH 1/3] xmaker: fix profit fixer fee settings --- pkg/strategy/xmaker/strategy.go | 34 ++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index aefd86d10..a1a24da1f 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1453,6 +1453,20 @@ func (s *Strategy) CrossRun( s.Position.StrategyInstanceID = instanceID } + if s.makerSession.MakerFeeRate.Sign() > 0 || s.makerSession.TakerFeeRate.Sign() > 0 { + s.Position.SetExchangeFeeRate(types.ExchangeName(s.MakerExchange), types.ExchangeFee{ + MakerFeeRate: s.makerSession.MakerFeeRate, + TakerFeeRate: s.makerSession.TakerFeeRate, + }) + } + + if s.sourceSession.MakerFeeRate.Sign() > 0 || s.sourceSession.TakerFeeRate.Sign() > 0 { + s.Position.SetExchangeFeeRate(types.ExchangeName(s.SourceExchange), types.ExchangeFee{ + MakerFeeRate: s.sourceSession.MakerFeeRate, + TakerFeeRate: s.sourceSession.TakerFeeRate, + }) + } + s.Position.UpdateMetrics() bbgo.Notify("xmaker: %s position is restored", s.Symbol, s.Position) @@ -1469,20 +1483,6 @@ func (s *Strategy) CrossRun( } } - if s.makerSession.MakerFeeRate.Sign() > 0 || s.makerSession.TakerFeeRate.Sign() > 0 { - s.Position.SetExchangeFeeRate(types.ExchangeName(s.MakerExchange), types.ExchangeFee{ - MakerFeeRate: s.makerSession.MakerFeeRate, - TakerFeeRate: s.makerSession.TakerFeeRate, - }) - } - - if s.sourceSession.MakerFeeRate.Sign() > 0 || s.sourceSession.TakerFeeRate.Sign() > 0 { - s.Position.SetExchangeFeeRate(types.ExchangeName(s.SourceExchange), types.ExchangeFee{ - MakerFeeRate: s.sourceSession.MakerFeeRate, - TakerFeeRate: s.sourceSession.TakerFeeRate, - }) - } - s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets) s.priceSolver.BindStream(s.sourceSession.MarketDataStream) @@ -1492,7 +1492,6 @@ func (s *Strategy) CrossRun( } s.sourceSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(k types.KLine) { - s.priceSolver.Update(k.Symbol, k.Close) feeToken := s.sourceSession.Exchange.PlatformFeeCurrency() if feePrice, ok := s.priceSolver.ResolvePrice(feeToken, "USDT"); ok { s.Position.SetFeeAverageCost(feeToken, feePrice) @@ -1509,6 +1508,11 @@ func (s *Strategy) CrossRun( } position := types.NewPositionFromMarket(s.makerMarket) + position.ExchangeFeeRates = s.Position.ExchangeFeeRates + position.FeeRate = s.Position.FeeRate + position.StrategyInstanceID = s.Position.StrategyInstanceID + position.Strategy = s.Position.Strategy + profitStats := types.NewProfitStats(s.makerMarket) fixer := common.NewProfitFixer() From 2599a4bcd3ea1927e5c9877ee9fd6182591f9f47 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 7 Oct 2024 17:07:09 +0800 Subject: [PATCH 2/3] xmaker: add SubscribeFeeTokenMarkets option --- pkg/strategy/xmaker/strategy.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index a1a24da1f..c8ad15187 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -88,6 +88,8 @@ type Strategy struct { HedgeInterval types.Duration `json:"hedgeInterval"` OrderCancelWaitTime types.Duration `json:"orderCancelWaitTime"` + SubscribeFeeTokenMarkets bool `json:"subscribeFeeTokenMarkets"` + EnableSignalMargin bool `json:"enableSignalMargin"` SignalConfigList []SignalConfig `json:"signals"` SignalMarginScale *bbgo.SlideRule `json:"signalMarginScale,omitempty"` @@ -232,6 +234,13 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) { sourceSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: sig.BollingerBandTrendSignal.Interval}) } } + + if s.SubscribeFeeTokenMarkets { + feeTokenQuote := "USDT" + subscribeOpts := types.SubscribeOptions{Interval: "1m"} + sourceSession.Subscribe(types.KLineChannel, sourceSession.Exchange.PlatformFeeCurrency()+feeTokenQuote, subscribeOpts) + makerSession.Subscribe(types.KLineChannel, makerSession.Exchange.PlatformFeeCurrency()+feeTokenQuote, subscribeOpts) + } } func aggregatePrice(pvs types.PriceVolumeSlice, requiredQuantity fixedpoint.Value) (price fixedpoint.Value) { From 544c172a9c096591c742f380afe6ee3e24de7a59 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 7 Oct 2024 17:12:49 +0800 Subject: [PATCH 3/3] xmaker: improve fee price updating --- pkg/strategy/xmaker/strategy.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index c8ad15187..3036d760c 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -26,6 +26,8 @@ import ( var defaultMargin = fixedpoint.NewFromFloat(0.003) var two = fixedpoint.NewFromInt(2) +const feeTokenQuote = "USDT" + const priceUpdateTimeout = 30 * time.Second const ID = "xmaker" @@ -236,7 +238,6 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) { } if s.SubscribeFeeTokenMarkets { - feeTokenQuote := "USDT" subscribeOpts := types.SubscribeOptions{Interval: "1m"} sourceSession.Subscribe(types.KLineChannel, sourceSession.Exchange.PlatformFeeCurrency()+feeTokenQuote, subscribeOpts) makerSession.Subscribe(types.KLineChannel, makerSession.Exchange.PlatformFeeCurrency()+feeTokenQuote, subscribeOpts) @@ -1494,6 +1495,7 @@ func (s *Strategy) CrossRun( s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets) s.priceSolver.BindStream(s.sourceSession.MarketDataStream) + s.sourceSession.UserDataStream.OnTradeUpdate(s.priceSolver.UpdateFromTrade) s.accountValueCalculator = bbgo.NewAccountValueCalculator(s.sourceSession, s.priceSolver, s.sourceMarket.QuoteCurrency) if err := s.accountValueCalculator.UpdatePrices(ctx); err != nil { @@ -1502,7 +1504,14 @@ func (s *Strategy) CrossRun( s.sourceSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(k types.KLine) { feeToken := s.sourceSession.Exchange.PlatformFeeCurrency() - if feePrice, ok := s.priceSolver.ResolvePrice(feeToken, "USDT"); ok { + if feePrice, ok := s.priceSolver.ResolvePrice(feeToken, feeTokenQuote); ok { + s.Position.SetFeeAverageCost(feeToken, feePrice) + } + })) + + s.makerSession.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1m, func(k types.KLine) { + feeToken := s.makerSession.Exchange.PlatformFeeCurrency() + if feePrice, ok := s.priceSolver.ResolvePrice(feeToken, feeTokenQuote); ok { s.Position.SetFeeAverageCost(feeToken, feePrice) } }))