diff --git a/pkg/report/profit_tracker.go b/pkg/report/profit_tracker.go index 85b7972d7..86885034e 100644 --- a/pkg/report/profit_tracker.go +++ b/pkg/report/profit_tracker.go @@ -9,9 +9,10 @@ import ( type ProfitTracker struct { types.IntervalWindow - market types.Market - profitStatsSlice []*types.ProfitStats - currentProfitStats **types.ProfitStats + ProfitStatsSlice []*types.ProfitStats + CurrentProfitStats **types.ProfitStats + + market types.Market } // InitOld is for backward capability. ps is the ProfitStats of the strategy, market is the strategy market @@ -22,18 +23,20 @@ func (p *ProfitTracker) InitOld(ps **types.ProfitStats, market types.Market) { *ps = types.NewProfitStats(p.market) } - p.currentProfitStats = ps - p.profitStatsSlice = append(p.profitStatsSlice, *ps) + p.CurrentProfitStats = ps + p.ProfitStatsSlice = append(p.ProfitStatsSlice, *ps) } // Init initialize the tracker with the given market func (p *ProfitTracker) Init(market types.Market) { p.market = market - *p.currentProfitStats = types.NewProfitStats(p.market) - p.profitStatsSlice = append(p.profitStatsSlice, *p.currentProfitStats) + *p.CurrentProfitStats = types.NewProfitStats(p.market) + p.ProfitStatsSlice = append(p.ProfitStatsSlice, *p.CurrentProfitStats) } func (p *ProfitTracker) Bind(tradeCollector *bbgo.TradeCollector, session *bbgo.ExchangeSession) { + session.Subscribe(types.KLineChannel, p.market.Symbol, types.SubscribeOptions{Interval: p.Interval}) + tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) { p.AddProfit(*profit) }) @@ -50,18 +53,18 @@ func (p *ProfitTracker) Bind(tradeCollector *bbgo.TradeCollector, session *bbgo. // Rotate the tracker to make a new ProfitStats to record the profits func (p *ProfitTracker) Rotate() { - *p.currentProfitStats = types.NewProfitStats(p.market) - p.profitStatsSlice = append(p.profitStatsSlice, *p.currentProfitStats) + *p.CurrentProfitStats = types.NewProfitStats(p.market) + p.ProfitStatsSlice = append(p.ProfitStatsSlice, *p.CurrentProfitStats) // Truncate - if len(p.profitStatsSlice) > p.Window { - p.profitStatsSlice = p.profitStatsSlice[len(p.profitStatsSlice)-p.Window:] + if len(p.ProfitStatsSlice) > p.Window { + p.ProfitStatsSlice = p.ProfitStatsSlice[len(p.ProfitStatsSlice)-p.Window:] } } func (p *ProfitTracker) AddProfit(profit types.Profit) { - (*p.currentProfitStats).AddProfit(profit) + (*p.CurrentProfitStats).AddProfit(profit) } func (p *ProfitTracker) AddTrade(trade types.Trade) { - (*p.currentProfitStats).AddTrade(trade) + (*p.CurrentProfitStats).AddTrade(trade) } diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 3bf569d32..57891b7a2 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -39,6 +39,8 @@ type Strategy struct { ProfitStats *types.ProfitStats `persistence:"profit_stats"` TradeStats *types.TradeStats `persistence:"trade_stats"` + ProfitTracker *report.ProfitTracker `json:"profitTracker" persistence:"profit_tracker"` + // Symbol is the market symbol you want to trade Symbol string `json:"symbol"` @@ -328,6 +330,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.TradeStats = types.NewTradeStats(s.Symbol) } + if s.ProfitTracker.CurrentProfitStats == nil { + s.ProfitTracker.InitOld(&s.ProfitStats, s.Market) + } + // Interval profit report if bbgo.IsBackTesting { startTime := s.Environment.StartTime() @@ -349,6 +355,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindTradeStats(s.TradeStats) + s.orderExecutor.BindProfitTracker(s.ProfitTracker) s.orderExecutor.Bind() // AccountValueCalculator