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
# higher highs in long position and lower lows in short position
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.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit == nil {
return
}
profitTracker.AddProfit(*profit)
})

View File

@ -12,7 +12,7 @@ import (
// AccumulatedProfitReport For accumulated profit report output
type AccumulatedProfitReport struct {
// ProfitMAWindow Accumulated profit SMA window
ProfitMAWindow int `json:"ProfitMAWindow"`
ProfitMAWindow int `json:"profitMAWindow"`
// ShortTermProfitWindow The window to sum up the short-term profit
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) {
// Accumulated profit
r.accumulatedProfit.Add(ps.AccumulatedNetProfit)
r.accumulatedProfit = r.accumulatedProfit.Add(ps.AccumulatedNetProfit)
r.accumulatedProfitPerInterval.Update(r.accumulatedProfit.Float64())
// Profit of each interval
@ -120,12 +120,12 @@ func (r *AccumulatedProfitReport) Output() {
"#",
"Symbol",
"Total Net Profit",
fmt.Sprintf("Total Net Profit %sMA%d", r.Interval, r.Window),
fmt.Sprintf("%s %d Net Profit", r.Interval, r.ShortTermProfitWindow),
fmt.Sprintf("Total Net Profit %sMA%d", r.Interval, r.ProfitMAWindow),
fmt.Sprintf("%s%d Net Profit", r.Interval, r.ShortTermProfitWindow),
"accumulatedFee",
"winRatio",
"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++ {
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
func (p *ProfitTracker) Rotate() {
// Update report
if p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.Rotate(*p.CurrentProfitStats, p.tradeStats)
}
*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 p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.Rotate(*p.CurrentProfitStats, p.tradeStats)
}
}
func (p *ProfitTracker) AddProfit(profit types.Profit) {

View File

@ -39,6 +39,8 @@ type Strategy struct {
ProfitStats *types.ProfitStats `persistence:"profit_stats"`
TradeStats *types.TradeStats `persistence:"trade_stats"`
ProfitTracker *report.ProfitTracker `json:"profitTracker"`
// Symbol is the market symbol you want to trade
Symbol string `json:"symbol"`
@ -101,8 +103,6 @@ type Strategy struct {
// StrategyController
bbgo.StrategyController
ProfitTracker *report.ProfitTracker `json:"profitTracker" persistence:"profit_tracker"`
}
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.BindProfitStats(s.ProfitStats)
s.orderExecutor.BindTradeStats(s.TradeStats)
s.orderExecutor.BindProfitTracker(s.ProfitTracker)
if s.ProfitTracker != nil {
s.orderExecutor.BindProfitTracker(s.ProfitTracker)
}
s.orderExecutor.Bind()
// AccountValueCalculator