From 1b0d4599e2e130fd454685d1189fb68a8c694320 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 12 Aug 2024 15:02:02 +0800 Subject: [PATCH] all: add trade converter to trade pnl fixer --- pkg/core/tradecollector.go | 2 +- pkg/strategy/common/profit_fixer.go | 5 +++++ pkg/strategy/xdepthmaker/strategy.go | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/core/tradecollector.go b/pkg/core/tradecollector.go index d961890af..7dda8b686 100644 --- a/pkg/core/tradecollector.go +++ b/pkg/core/tradecollector.go @@ -134,7 +134,7 @@ func (c *TradeCollector) BindStreamForBackground(stream types.Stream) { func (c *TradeCollector) BindStream(stream types.Stream) { stream.OnTradeUpdate(func(trade types.Trade) { - c.processTrade(trade) + c.ProcessTrade(trade) }) } diff --git a/pkg/strategy/common/profit_fixer.go b/pkg/strategy/common/profit_fixer.go index 44817acdf..9aebcf617 100644 --- a/pkg/strategy/common/profit_fixer.go +++ b/pkg/strategy/common/profit_fixer.go @@ -10,6 +10,7 @@ import ( "golang.org/x/sync/errgroup" "github.com/c9s/bbgo/pkg/bbgo" + "github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/exchange/batch" "github.com/c9s/bbgo/pkg/types" ) @@ -22,6 +23,8 @@ type ProfitFixerConfig struct { // ProfitFixer implements a trade-history-based profit fixer type ProfitFixer struct { sessions map[string]types.ExchangeTradeHistoryService + + core.ConverterManager } func NewProfitFixer() *ProfitFixer { @@ -106,6 +109,8 @@ func (f *ProfitFixer) Fix( func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.ProfitStats, position *types.Position) error { for _, trade := range allTrades { + trade = f.ConverterManager.ConvertTrade(trade) + profit, netProfit, madeProfit := position.AddTrade(trade) if madeProfit { p := position.NewProfit(trade, profit, netProfit) diff --git a/pkg/strategy/xdepthmaker/strategy.go b/pkg/strategy/xdepthmaker/strategy.go index 7a8d80c4c..9e35997aa 100644 --- a/pkg/strategy/xdepthmaker/strategy.go +++ b/pkg/strategy/xdepthmaker/strategy.go @@ -160,6 +160,7 @@ type Strategy struct { Environment *bbgo.Environment + // Symbol is the maker exchange symbol Symbol string `json:"symbol"` // HedgeSymbol is the symbol for the hedge exchange @@ -262,6 +263,7 @@ func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) { }) hedgeSession.Subscribe(types.KLineChannel, s.HedgeSymbol, types.SubscribeOptions{Interval: "1m"}) + makerSession.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: "1m"}) } @@ -355,6 +357,8 @@ func (s *Strategy) CrossRun( s.CrossExchangeMarketMakingStrategy.ProfitStats = types.NewProfitStats(makerMarket) fixer := common.NewProfitFixer() + fixer.ConverterManager = s.ConverterManager + if ss, ok := makerSession.Exchange.(types.ExchangeTradeHistoryService); ok { log.Infof("adding makerSession %s to profitFixer", makerSession.Name) fixer.AddExchange(makerSession.Name, ss)