Skip to content

Commit

Permalink
Merge pull request #1566 from c9s/c9s/xdepthmaker-pnl-fixer
Browse files Browse the repository at this point in the history
FIX: [xdepthmaker] fix crash
  • Loading branch information
c9s committed Mar 6, 2024
2 parents de6bdf6 + b6ddb49 commit 9a76d2e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 7 additions & 2 deletions pkg/strategy/xdepthmaker/profitfixer.go
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
24 changes: 19 additions & 5 deletions pkg/strategy/xdepthmaker/strategy.go
Expand Up @@ -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,
Expand Down

0 comments on commit 9a76d2e

Please sign in to comment.