From 5a8a2e8631bd4145591b636ae902b3787ae073f6 Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Mon, 10 Jul 2023 15:37:19 +0800 Subject: [PATCH] improve/profitStatsTracker: rename ProfitTracker to ProfitStatsTracker --- config/supertrend.yaml | 2 +- pkg/report/profit_tracker.go | 16 ++++++------ pkg/strategy/supertrend/strategy.go | 40 ++++++++++++++--------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/config/supertrend.yaml b/config/supertrend.yaml index 673d3f47e..99815245b 100644 --- a/config/supertrend.yaml +++ b/config/supertrend.yaml @@ -113,7 +113,7 @@ exchangeStrategies: # higher highs in long position and lower lows in short position oppositeDirectionAsPosition: false - profitTracker: + profitStatsTracker: interval: 1d window: 30 accumulatedProfitReport: diff --git a/pkg/report/profit_tracker.go b/pkg/report/profit_tracker.go index 95e83341f..9a0171010 100644 --- a/pkg/report/profit_tracker.go +++ b/pkg/report/profit_tracker.go @@ -6,7 +6,7 @@ import ( "github.com/c9s/bbgo/pkg/types" ) -type ProfitTracker struct { +type ProfitStatsTracker struct { types.IntervalWindow // Accumulated profit report @@ -20,12 +20,12 @@ type ProfitTracker struct { tradeStats *types.TradeStats } -func (p *ProfitTracker) Subscribe(session *bbgo.ExchangeSession) { +func (p *ProfitStatsTracker) Subscribe(session *bbgo.ExchangeSession) { session.Subscribe(types.KLineChannel, p.Market.Symbol, types.SubscribeOptions{Interval: p.Interval}) } // InitOld is for backward capability. ps is the ProfitStats of the strategy, Market is the strategy Market -func (p *ProfitTracker) InitOld(market types.Market, ps **types.ProfitStats, ts *types.TradeStats) { +func (p *ProfitStatsTracker) InitOld(market types.Market, ps **types.ProfitStats, ts *types.TradeStats) { p.Market = market if *ps == nil { @@ -43,12 +43,12 @@ func (p *ProfitTracker) InitOld(market types.Market, ps **types.ProfitStats, ts } // Init initialize the tracker with the given Market -func (p *ProfitTracker) Init(market types.Market, ts *types.TradeStats) { +func (p *ProfitStatsTracker) Init(market types.Market, ts *types.TradeStats) { ps := types.NewProfitStats(p.Market) p.InitOld(market, &ps, ts) } -func (p *ProfitTracker) Bind(session *bbgo.ExchangeSession, tradeCollector *bbgo.TradeCollector) { +func (p *ProfitStatsTracker) Bind(session *bbgo.ExchangeSession, tradeCollector *bbgo.TradeCollector) { tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) { if profit == nil { return @@ -68,7 +68,7 @@ func (p *ProfitTracker) Bind(session *bbgo.ExchangeSession, tradeCollector *bbgo } // Rotate the tracker to make a new ProfitStats to record the profits -func (p *ProfitTracker) Rotate() { +func (p *ProfitStatsTracker) Rotate() { // Update report if p.AccumulatedProfitReport != nil { p.AccumulatedProfitReport.Rotate(*p.CurrentProfitStats, p.tradeStats) @@ -82,11 +82,11 @@ func (p *ProfitTracker) Rotate() { } } -func (p *ProfitTracker) AddProfit(profit types.Profit) { +func (p *ProfitStatsTracker) AddProfit(profit types.Profit) { (*p.CurrentProfitStats).AddProfit(profit) } -func (p *ProfitTracker) AddTrade(trade types.Trade) { +func (p *ProfitStatsTracker) AddTrade(trade types.Trade) { (*p.CurrentProfitStats).AddTrade(trade) if p.AccumulatedProfitReport != nil { diff --git a/pkg/strategy/supertrend/strategy.go b/pkg/strategy/supertrend/strategy.go index 40eead9b0..da904a064 100644 --- a/pkg/strategy/supertrend/strategy.go +++ b/pkg/strategy/supertrend/strategy.go @@ -39,7 +39,7 @@ type Strategy struct { ProfitStats *types.ProfitStats `persistence:"profit_stats"` TradeStats *types.TradeStats `persistence:"trade_stats"` - ProfitTracker *report.ProfitTracker `json:"profitTracker"` + ProfitStatsTracker *report.ProfitStatsTracker `json:"profitStatsTracker"` // Symbol is the market symbol you want to trade Symbol string `json:"symbol"` @@ -132,8 +132,8 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { s.ExitMethods.SetAndSubscribe(session, s) // Profit tracker - if s.ProfitTracker != nil { - s.ProfitTracker.Subscribe(session) + if s.ProfitStatsTracker != nil { + s.ProfitStatsTracker.Subscribe(session) } } @@ -356,25 +356,25 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.orderExecutor.Bind() // Setup profit tracker - if s.ProfitTracker != nil { - if s.ProfitTracker.CurrentProfitStats == nil { - s.ProfitTracker.InitOld(s.Market, &s.ProfitStats, s.TradeStats) + if s.ProfitStatsTracker != nil { + if s.ProfitStatsTracker.CurrentProfitStats == nil { + s.ProfitStatsTracker.InitOld(s.Market, &s.ProfitStats, s.TradeStats) } // Add strategy parameters to report - if s.ProfitTracker.AccumulatedProfitReport != nil { - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("window", fmt.Sprintf("%d", s.Window)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("multiplier", fmt.Sprintf("%f", s.SupertrendMultiplier)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("fastDEMA", fmt.Sprintf("%d", s.FastDEMAWindow)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("slowDEMA", fmt.Sprintf("%d", s.SlowDEMAWindow)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("takeProfitAtrMultiplier", fmt.Sprintf("%f", s.TakeProfitAtrMultiplier)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopLossByTriggeringK", fmt.Sprintf("%t", s.StopLossByTriggeringK)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedSupertrend", fmt.Sprintf("%t", s.StopByReversedSupertrend)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedDema", fmt.Sprintf("%t", s.StopByReversedDema)) - s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedLinGre", fmt.Sprintf("%t", s.StopByReversedLinGre)) + if s.ProfitStatsTracker.AccumulatedProfitReport != nil { + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("window", fmt.Sprintf("%d", s.Window)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("multiplier", fmt.Sprintf("%f", s.SupertrendMultiplier)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("fastDEMA", fmt.Sprintf("%d", s.FastDEMAWindow)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("slowDEMA", fmt.Sprintf("%d", s.SlowDEMAWindow)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("takeProfitAtrMultiplier", fmt.Sprintf("%f", s.TakeProfitAtrMultiplier)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("stopLossByTriggeringK", fmt.Sprintf("%t", s.StopLossByTriggeringK)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedSupertrend", fmt.Sprintf("%t", s.StopByReversedSupertrend)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedDema", fmt.Sprintf("%t", s.StopByReversedDema)) + s.ProfitStatsTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedLinGre", fmt.Sprintf("%t", s.StopByReversedLinGre)) } - s.ProfitTracker.Bind(s.session, s.orderExecutor.TradeCollector()) + s.ProfitStatsTracker.Bind(s.session, s.orderExecutor.TradeCollector()) } // AccountValueCalculator @@ -528,9 +528,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se defer wg.Done() // Output profit report - if s.ProfitTracker != nil { - if s.ProfitTracker.AccumulatedProfitReport != nil { - s.ProfitTracker.AccumulatedProfitReport.Output() + if s.ProfitStatsTracker != nil { + if s.ProfitStatsTracker.AccumulatedProfitReport != nil { + s.ProfitStatsTracker.AccumulatedProfitReport.Output() } }