diff --git a/pkg/strategy/xdepthmaker/profitfixer.go b/pkg/strategy/xdepthmaker/profitfixer.go index 66905580eb..cc27c69bed 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) @@ -76,9 +77,13 @@ 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)) return nil } diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index 53bc918cf2..e051f64655 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -320,21 +320,35 @@ 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() { 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) + 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 } + + 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,