feature/profitTracker: fix bugs

This commit is contained in:
Andy Cheng 2023-06-16 18:32:44 +08:00
parent 027acfe3b5
commit 5513330816
No known key found for this signature in database
GPG Key ID: 936427CF651A9D28
5 changed files with 27 additions and 12 deletions

View File

@ -112,3 +112,11 @@ exchangeStrategies:
# If true, looking for lower lows in long position and higher highs in short position. If false, looking for # If true, looking for lower lows in long position and higher highs in short position. If false, looking for
# higher highs in long position and lower lows in short position # higher highs in long position and lower lows in short position
oppositeDirectionAsPosition: false oppositeDirectionAsPosition: false
profitTracker:
Interval: 1d
Window: 30
accumulatedProfitReport:
profitMAWindow: 60
shortTermProfitWindow: 14
tsvReportPath: res.tsv

View File

@ -167,6 +167,10 @@ func (e *GeneralOrderExecutor) BindProfitTracker(profitTracker *report.ProfitTra
e.session.Subscribe(types.KLineChannel, profitTracker.Market.Symbol, types.SubscribeOptions{Interval: profitTracker.Interval}) e.session.Subscribe(types.KLineChannel, profitTracker.Market.Symbol, types.SubscribeOptions{Interval: profitTracker.Interval})
e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) { e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit == nil {
return
}
profitTracker.AddProfit(*profit) profitTracker.AddProfit(*profit)
}) })

View File

@ -12,7 +12,7 @@ import (
// AccumulatedProfitReport For accumulated profit report output // AccumulatedProfitReport For accumulated profit report output
type AccumulatedProfitReport struct { type AccumulatedProfitReport struct {
// ProfitMAWindow Accumulated profit SMA window // ProfitMAWindow Accumulated profit SMA window
ProfitMAWindow int `json:"ProfitMAWindow"` ProfitMAWindow int `json:"profitMAWindow"`
// ShortTermProfitWindow The window to sum up the short-term profit // ShortTermProfitWindow The window to sum up the short-term profit
ShortTermProfitWindow int `json:"shortTermProfitWindow"` ShortTermProfitWindow int `json:"shortTermProfitWindow"`
@ -84,7 +84,7 @@ func (r *AccumulatedProfitReport) AddTrade(trade types.Trade) {
func (r *AccumulatedProfitReport) Rotate(ps *types.ProfitStats, ts *types.TradeStats) { func (r *AccumulatedProfitReport) Rotate(ps *types.ProfitStats, ts *types.TradeStats) {
// Accumulated profit // Accumulated profit
r.accumulatedProfit.Add(ps.AccumulatedNetProfit) r.accumulatedProfit = r.accumulatedProfit.Add(ps.AccumulatedNetProfit)
r.accumulatedProfitPerInterval.Update(r.accumulatedProfit.Float64()) r.accumulatedProfitPerInterval.Update(r.accumulatedProfit.Float64())
// Profit of each interval // Profit of each interval
@ -120,12 +120,12 @@ func (r *AccumulatedProfitReport) Output() {
"#", "#",
"Symbol", "Symbol",
"Total Net Profit", "Total Net Profit",
fmt.Sprintf("Total Net Profit %sMA%d", r.Interval, r.Window), fmt.Sprintf("Total Net Profit %sMA%d", r.Interval, r.ProfitMAWindow),
fmt.Sprintf("%s %d Net Profit", r.Interval, r.ShortTermProfitWindow), fmt.Sprintf("%s%d Net Profit", r.Interval, r.ShortTermProfitWindow),
"accumulatedFee", "accumulatedFee",
"winRatio", "winRatio",
"profitFactor", "profitFactor",
fmt.Sprintf("%s %d Trades", r.Interval, r.Window), fmt.Sprintf("%s%d Trades", r.Interval, r.Window),
} }
for i := 0; i < len(r.strategyParameters); i++ { for i := 0; i < len(r.strategyParameters); i++ {
titles = append(titles, r.strategyParameters[i][0]) titles = append(titles, r.strategyParameters[i][0])

View File

@ -44,16 +44,17 @@ func (p *ProfitTracker) Init(market types.Market, ts *types.TradeStats) {
// Rotate the tracker to make a new ProfitStats to record the profits // Rotate the tracker to make a new ProfitStats to record the profits
func (p *ProfitTracker) Rotate() { func (p *ProfitTracker) Rotate() {
// Update report
if p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.Rotate(*p.CurrentProfitStats, p.tradeStats)
}
*p.CurrentProfitStats = types.NewProfitStats(p.Market) *p.CurrentProfitStats = types.NewProfitStats(p.Market)
p.ProfitStatsSlice = append(p.ProfitStatsSlice, *p.CurrentProfitStats) p.ProfitStatsSlice = append(p.ProfitStatsSlice, *p.CurrentProfitStats)
// Truncate // Truncate
if len(p.ProfitStatsSlice) > p.Window { if len(p.ProfitStatsSlice) > p.Window {
p.ProfitStatsSlice = p.ProfitStatsSlice[len(p.ProfitStatsSlice)-p.Window:] p.ProfitStatsSlice = p.ProfitStatsSlice[len(p.ProfitStatsSlice)-p.Window:]
} }
if p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.Rotate(*p.CurrentProfitStats, p.tradeStats)
}
} }
func (p *ProfitTracker) AddProfit(profit types.Profit) { func (p *ProfitTracker) AddProfit(profit types.Profit) {

View File

@ -39,6 +39,8 @@ type Strategy struct {
ProfitStats *types.ProfitStats `persistence:"profit_stats"` ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"` TradeStats *types.TradeStats `persistence:"trade_stats"`
ProfitTracker *report.ProfitTracker `json:"profitTracker"`
// Symbol is the market symbol you want to trade // Symbol is the market symbol you want to trade
Symbol string `json:"symbol"` Symbol string `json:"symbol"`
@ -101,8 +103,6 @@ type Strategy struct {
// StrategyController // StrategyController
bbgo.StrategyController bbgo.StrategyController
ProfitTracker *report.ProfitTracker `json:"profitTracker" persistence:"profit_tracker"`
} }
func (s *Strategy) ID() string { func (s *Strategy) ID() string {
@ -367,7 +367,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.orderExecutor.BindEnvironment(s.Environment) s.orderExecutor.BindEnvironment(s.Environment)
s.orderExecutor.BindProfitStats(s.ProfitStats) s.orderExecutor.BindProfitStats(s.ProfitStats)
s.orderExecutor.BindTradeStats(s.TradeStats) s.orderExecutor.BindTradeStats(s.TradeStats)
if s.ProfitTracker != nil {
s.orderExecutor.BindProfitTracker(s.ProfitTracker) s.orderExecutor.BindProfitTracker(s.ProfitTracker)
}
s.orderExecutor.Bind() s.orderExecutor.Bind()
// AccountValueCalculator // AccountValueCalculator