From be89292cbb2ad6617129c46afd1ed04756d5faa6 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 6 Mar 2024 17:19:50 +0800 Subject: [PATCH 1/4] xdepthmaker: another fix --- pkg/strategy/xdepthmaker/strategy.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index 53bc918cf2..d87d2bd843 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -324,14 +324,14 @@ func (s *Strategy) CrossRun( return errors.New("tradesSince time can not be zero") } - fixer := NewProfitFixer(s.makerMarket) - fixer.AddExchange(makerSession.Name, makerSession.Exchange.(types.ExchangeTradeHistoryService)) - fixer.AddExchange(hedgeSession.Name, hedgeSession.Exchange.(types.ExchangeTradeHistoryService)) - - makerMarket, _ := s.makerSession.Market(s.Symbol) + makerMarket, _ := makerSession.Market(s.Symbol) s.CrossExchangeMarketMakingStrategy.Position = types.NewPositionFromMarket(makerMarket) s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket) + fixer := NewProfitFixer(makerMarket) + fixer.AddExchange(makerSession.Name, makerSession.Exchange.(types.ExchangeTradeHistoryService)) + fixer.AddExchange(hedgeSession.Name, hedgeSession.Exchange.(types.ExchangeTradeHistoryService)) + if err2 := fixer.Fix(ctx, s.ProfitFixerConfig.TradesSince.Time(), time.Now(), s.CrossExchangeMarketMakingStrategy.ProfitStats, s.CrossExchangeMarketMakingStrategy.Position); err2 != nil { return err2 } From 188231e2fbe1c462c60930e924f2957d50e02ebc Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 6 Mar 2024 17:47:18 +0800 Subject: [PATCH 2/4] add more logs to profitFixer --- pkg/strategy/xdepthmaker/profitfixer.go | 2 ++ pkg/strategy/xdepthmaker/strategy.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/xdepthmaker/profitfixer.go b/pkg/strategy/xdepthmaker/profitfixer.go index 66905580eb..129792b15a 100644 --- a/pkg/strategy/xdepthmaker/profitfixer.go +++ b/pkg/strategy/xdepthmaker/profitfixer.go @@ -47,6 +47,7 @@ func (f *ProfitFixer) batchQueryTrades( } func (f *ProfitFixer) Fix(ctx context.Context, since, until time.Time, stats *types.ProfitStats, position *types.Position) error { + log.Infof("starting profitFixer with time range %s <=> %s", since, until) var mu sync.Mutex var allTrades = make([]types.Trade, 0, 1000) @@ -80,5 +81,6 @@ func (f *ProfitFixer) Fix(ctx context.Context, since, until time.Time, stats *ty position.AddTrade(trade) } + log.Infof("profitFixer done: profitStats and position are updated from %d trades", len(allTrades)) return nil } diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index d87d2bd843..e875014eaf 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -320,6 +320,8 @@ func (s *Strategy) CrossRun( log.Infof("makerSession: %s hedgeSession: %s", makerSession.Name, hedgeSession.Name) if s.ProfitFixerConfig != nil { + log.Infof("profitFixer is enabled, checking checkpoint: %+v", s.ProfitFixerConfig.TradesSince) + if s.ProfitFixerConfig.TradesSince.Time().IsZero() { return errors.New("tradesSince time can not be zero") } @@ -329,8 +331,15 @@ func (s *Strategy) CrossRun( s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket) fixer := NewProfitFixer(makerMarket) - fixer.AddExchange(makerSession.Name, makerSession.Exchange.(types.ExchangeTradeHistoryService)) - fixer.AddExchange(hedgeSession.Name, hedgeSession.Exchange.(types.ExchangeTradeHistoryService)) + if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok { + log.Infof("adding makerSession %s to profitFixer", makerSession.Name) + fixer.AddExchange(makerSession.Name, ss) + } + + if ss, ok := hedgeSession.Exchange.(types.ExchangeTradeHistoryService); ok { + log.Infof("adding hedgeSession %s to profitFixer", hedgeSession.Name) + fixer.AddExchange(hedgeSession.Name, ss) + } if err2 := fixer.Fix(ctx, s.ProfitFixerConfig.TradesSince.Time(), time.Now(), s.CrossExchangeMarketMakingStrategy.ProfitStats, s.CrossExchangeMarketMakingStrategy.Position); err2 != nil { return err2 From 441ebbdbe522604dd6ac08342ceeb378eaefb787 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 6 Mar 2024 17:48:53 +0800 Subject: [PATCH 3/4] xdepthmaker: add notification --- pkg/strategy/xdepthmaker/strategy.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index e875014eaf..e051f64655 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -320,6 +320,8 @@ func (s *Strategy) CrossRun( log.Infof("makerSession: %s hedgeSession: %s", makerSession.Name, hedgeSession.Name) if s.ProfitFixerConfig != nil { + bbgo.Notify("Fixing %s profitStats and position...", s.Symbol) + log.Infof("profitFixer is enabled, checking checkpoint: %+v", s.ProfitFixerConfig.TradesSince) if s.ProfitFixerConfig.TradesSince.Time().IsZero() { @@ -344,6 +346,9 @@ func (s *Strategy) CrossRun( if err2 := fixer.Fix(ctx, s.ProfitFixerConfig.TradesSince.Time(), time.Now(), s.CrossExchangeMarketMakingStrategy.ProfitStats, s.CrossExchangeMarketMakingStrategy.Position); err2 != nil { return err2 } + + bbgo.Notify("Fixed %s position", s.Symbol, s.CrossExchangeMarketMakingStrategy.Position) + bbgo.Notify("Fixed %s profitStats", s.Symbol, s.CrossExchangeMarketMakingStrategy.ProfitStats) } if err := s.CrossExchangeMarketMakingStrategy.Initialize(ctx, From b6ddb49d0aed39a3f798533b0f395e6d7e44689d Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 6 Mar 2024 18:12:24 +0800 Subject: [PATCH 4/4] xdepthmaker: fix stats fixer --- pkg/strategy/xdepthmaker/profitfixer.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/strategy/xdepthmaker/profitfixer.go b/pkg/strategy/xdepthmaker/profitfixer.go index 129792b15a..cc27c69bed 100644 --- a/pkg/strategy/xdepthmaker/profitfixer.go +++ b/pkg/strategy/xdepthmaker/profitfixer.go @@ -77,8 +77,11 @@ func (f *ProfitFixer) Fix(ctx context.Context, since, until time.Time, stats *ty allTrades = types.SortTradesAscending(allTrades) for _, trade := range allTrades { - stats.AddTrade(trade) - position.AddTrade(trade) + profit, netProfit, madeProfit := position.AddTrade(trade) + if madeProfit { + p := position.NewProfit(trade, profit, netProfit) + stats.AddProfit(p) + } } log.Infof("profitFixer done: profitStats and position are updated from %d trades", len(allTrades))