Merge pull request #1168 from andycheng123/profit-report-parameter

FEATURE: Accumulated Profit report
This commit is contained in:
c9s 2023-06-15 18:14:44 +08:00 committed by GitHub
commit 8bd5fc246c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 213 additions and 148 deletions

197
pkg/report/profit_report.go Normal file
View File

@ -0,0 +1,197 @@
package report
import (
"fmt"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
// AccumulatedProfitReport For accumulated profit report output
type AccumulatedProfitReport struct {
// AccumulatedProfitMAWindow Accumulated profit SMA window, in number of trades
AccumulatedProfitMAWindow int `json:"accumulatedProfitMAWindow"`
// IntervalWindow interval window, in days
IntervalWindow int `json:"intervalWindow"`
// NumberOfInterval How many intervals to output to TSV
NumberOfInterval int `json:"NumberOfInterval"`
// TsvReportPath The path to output report to
TsvReportPath string `json:"tsvReportPath"`
// AccumulatedDailyProfitWindow The window to sum up the daily profit, in days
AccumulatedDailyProfitWindow int `json:"accumulatedDailyProfitWindow"`
Symbol string
// Accumulated profit
accumulatedProfit fixedpoint.Value
accumulatedProfitPerDay floats.Slice
previousAccumulatedProfit fixedpoint.Value
// Accumulated profit MA
accumulatedProfitMA *indicator.SMA
accumulatedProfitMAPerDay floats.Slice
// Daily profit
dailyProfit floats.Slice
// Accumulated fee
accumulatedFee fixedpoint.Value
accumulatedFeePerDay floats.Slice
// Win ratio
winRatioPerDay floats.Slice
// Profit factor
profitFactorPerDay floats.Slice
// Trade number
dailyTrades floats.Slice
accumulatedTrades int
previousAccumulatedTrades int
// Extra values
extraValues [][2]string
}
func (r *AccumulatedProfitReport) Initialize(Symbol string, session *bbgo.ExchangeSession, orderExecutor *bbgo.GeneralOrderExecutor, TradeStats *types.TradeStats) {
r.Symbol = Symbol
if r.AccumulatedProfitMAWindow <= 0 {
r.AccumulatedProfitMAWindow = 60
}
if r.IntervalWindow <= 0 {
r.IntervalWindow = 7
}
if r.AccumulatedDailyProfitWindow <= 0 {
r.AccumulatedDailyProfitWindow = 7
}
if r.NumberOfInterval <= 0 {
r.NumberOfInterval = 1
}
r.accumulatedProfitMA = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: types.Interval1d, Window: r.AccumulatedProfitMAWindow}}
session.Subscribe(types.KLineChannel, r.Symbol, types.SubscribeOptions{Interval: types.Interval1d})
// Record profit
orderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit == nil {
return
}
r.RecordProfit(profit.Profit)
})
// Record trade
orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
r.RecordTrade(trade.Fee)
})
// Record daily status
session.MarketDataStream.OnKLineClosed(types.KLineWith(r.Symbol, types.Interval1d, func(kline types.KLine) {
r.DailyUpdate(TradeStats)
}))
}
func (r *AccumulatedProfitReport) AddExtraValue(valueAndTitle [2]string) {
r.extraValues = append(r.extraValues, valueAndTitle)
}
func (r *AccumulatedProfitReport) RecordProfit(profit fixedpoint.Value) {
r.accumulatedProfit = r.accumulatedProfit.Add(profit)
}
func (r *AccumulatedProfitReport) RecordTrade(fee fixedpoint.Value) {
r.accumulatedFee = r.accumulatedFee.Add(fee)
r.accumulatedTrades += 1
}
func (r *AccumulatedProfitReport) DailyUpdate(tradeStats *types.TradeStats) {
// Daily profit
r.dailyProfit.Update(r.accumulatedProfit.Sub(r.previousAccumulatedProfit).Float64())
r.previousAccumulatedProfit = r.accumulatedProfit
// Accumulated profit
r.accumulatedProfitPerDay.Update(r.accumulatedProfit.Float64())
// Accumulated profit MA
r.accumulatedProfitMA.Update(r.accumulatedProfit.Float64())
r.accumulatedProfitMAPerDay.Update(r.accumulatedProfitMA.Last(0))
// Accumulated Fee
r.accumulatedFeePerDay.Update(r.accumulatedFee.Float64())
// Win ratio
r.winRatioPerDay.Update(tradeStats.WinningRatio.Float64())
// Profit factor
r.profitFactorPerDay.Update(tradeStats.ProfitFactor.Float64())
// Daily trades
r.dailyTrades.Update(float64(r.accumulatedTrades - r.previousAccumulatedTrades))
r.previousAccumulatedTrades = r.accumulatedTrades
}
// Output Accumulated profit report to a TSV file
func (r *AccumulatedProfitReport) Output(symbol string) {
if r.TsvReportPath != "" {
tsvwiter, err := tsv.AppendWriterFile(r.TsvReportPath)
if err != nil {
panic(err)
}
defer tsvwiter.Close()
// Output title row
titles := []string{
"#",
"Symbol",
"accumulatedProfit",
"accumulatedProfitMA",
fmt.Sprintf("%dd profit", r.AccumulatedDailyProfitWindow),
"accumulatedFee",
"accumulatedNetProfit",
"winRatio",
"profitFactor",
"60D trades",
}
for i := 0; i < len(r.extraValues); i++ {
titles = append(titles, r.extraValues[i][0])
}
_ = tsvwiter.Write(titles)
// Output data row
for i := 0; i <= r.NumberOfInterval-1; i++ {
accumulatedProfit := r.accumulatedProfitPerDay.Index(r.IntervalWindow * i)
accumulatedProfitStr := fmt.Sprintf("%f", accumulatedProfit)
accumulatedProfitMA := r.accumulatedProfitMAPerDay.Index(r.IntervalWindow * i)
accumulatedProfitMAStr := fmt.Sprintf("%f", accumulatedProfitMA)
intervalAccumulatedProfit := r.dailyProfit.Tail(r.AccumulatedDailyProfitWindow+r.IntervalWindow*i).Sum() - r.dailyProfit.Tail(r.IntervalWindow*i).Sum()
intervalAccumulatedProfitStr := fmt.Sprintf("%f", intervalAccumulatedProfit)
accumulatedFee := fmt.Sprintf("%f", r.accumulatedFeePerDay.Index(r.IntervalWindow*i))
accumulatedNetProfit := fmt.Sprintf("%f", accumulatedProfit-r.accumulatedFeePerDay.Index(r.IntervalWindow*i))
winRatio := fmt.Sprintf("%f", r.winRatioPerDay.Index(r.IntervalWindow*i))
profitFactor := fmt.Sprintf("%f", r.profitFactorPerDay.Index(r.IntervalWindow*i))
trades := r.dailyTrades.Tail(60+r.IntervalWindow*i).Sum() - r.dailyTrades.Tail(r.IntervalWindow*i).Sum()
tradesStr := fmt.Sprintf("%f", trades)
values := []string{
fmt.Sprintf("%d", i+1),
symbol, accumulatedProfitStr,
accumulatedProfitMAStr,
intervalAccumulatedProfitStr,
accumulatedFee,
accumulatedNetProfit,
winRatio, profitFactor,
tradesStr,
}
for j := 0; j < len(r.extraValues); j++ {
values = append(values, r.extraValues[j][1])
}
_ = tsvwiter.Write(values)
}
}
}

View File

@ -9,12 +9,12 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/data/tsv"
"github.com/c9s/bbgo/pkg/datatype/floats"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/report"
"github.com/c9s/bbgo/pkg/types"
)
@ -30,130 +30,6 @@ func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
// AccumulatedProfitReport For accumulated profit report output
type AccumulatedProfitReport struct {
// AccumulatedProfitMAWindow Accumulated profit SMA window, in number of trades
AccumulatedProfitMAWindow int `json:"accumulatedProfitMAWindow"`
// IntervalWindow interval window, in days
IntervalWindow int `json:"intervalWindow"`
// NumberOfInterval How many intervals to output to TSV
NumberOfInterval int `json:"NumberOfInterval"`
// TsvReportPath The path to output report to
TsvReportPath string `json:"tsvReportPath"`
// AccumulatedDailyProfitWindow The window to sum up the daily profit, in days
AccumulatedDailyProfitWindow int `json:"accumulatedDailyProfitWindow"`
// Accumulated profit
accumulatedProfit fixedpoint.Value
accumulatedProfitPerDay floats.Slice
previousAccumulatedProfit fixedpoint.Value
// Accumulated profit MA
accumulatedProfitMA *indicator.SMA
accumulatedProfitMAPerDay floats.Slice
// Daily profit
dailyProfit floats.Slice
// Accumulated fee
accumulatedFee fixedpoint.Value
accumulatedFeePerDay floats.Slice
// Win ratio
winRatioPerDay floats.Slice
// Profit factor
profitFactorPerDay floats.Slice
// Trade number
dailyTrades floats.Slice
accumulatedTrades int
previousAccumulatedTrades int
}
func (r *AccumulatedProfitReport) Initialize() {
if r.AccumulatedProfitMAWindow <= 0 {
r.AccumulatedProfitMAWindow = 60
}
if r.IntervalWindow <= 0 {
r.IntervalWindow = 7
}
if r.AccumulatedDailyProfitWindow <= 0 {
r.AccumulatedDailyProfitWindow = 7
}
if r.NumberOfInterval <= 0 {
r.NumberOfInterval = 1
}
r.accumulatedProfitMA = &indicator.SMA{IntervalWindow: types.IntervalWindow{Interval: types.Interval1d, Window: r.AccumulatedProfitMAWindow}}
}
func (r *AccumulatedProfitReport) RecordProfit(profit fixedpoint.Value) {
r.accumulatedProfit = r.accumulatedProfit.Add(profit)
}
func (r *AccumulatedProfitReport) RecordTrade(fee fixedpoint.Value) {
r.accumulatedFee = r.accumulatedFee.Add(fee)
r.accumulatedTrades += 1
}
func (r *AccumulatedProfitReport) DailyUpdate(tradeStats *types.TradeStats) {
// Daily profit
r.dailyProfit.Update(r.accumulatedProfit.Sub(r.previousAccumulatedProfit).Float64())
r.previousAccumulatedProfit = r.accumulatedProfit
// Accumulated profit
r.accumulatedProfitPerDay.Update(r.accumulatedProfit.Float64())
// Accumulated profit MA
r.accumulatedProfitMA.Update(r.accumulatedProfit.Float64())
r.accumulatedProfitMAPerDay.Update(r.accumulatedProfitMA.Last(0))
// Accumulated Fee
r.accumulatedFeePerDay.Update(r.accumulatedFee.Float64())
// Win ratio
r.winRatioPerDay.Update(tradeStats.WinningRatio.Float64())
// Profit factor
r.profitFactorPerDay.Update(tradeStats.ProfitFactor.Float64())
// Daily trades
r.dailyTrades.Update(float64(r.accumulatedTrades - r.previousAccumulatedTrades))
r.previousAccumulatedTrades = r.accumulatedTrades
}
// Output Accumulated profit report to a TSV file
func (r *AccumulatedProfitReport) Output(symbol string) {
if r.TsvReportPath != "" {
tsvwiter, err := tsv.AppendWriterFile(r.TsvReportPath)
if err != nil {
panic(err)
}
defer tsvwiter.Close()
// Output symbol, total acc. profit, acc. profit 60MA, interval acc. profit, fee, win rate, profit factor
_ = tsvwiter.Write([]string{"#", "Symbol", "accumulatedProfit", "accumulatedProfitMA", fmt.Sprintf("%dd profit", r.AccumulatedDailyProfitWindow), "accumulatedFee", "winRatio", "profitFactor", "60D trades"})
for i := 0; i <= r.NumberOfInterval-1; i++ {
accumulatedProfit := r.accumulatedProfitPerDay.Index(r.IntervalWindow * i)
accumulatedProfitStr := fmt.Sprintf("%f", accumulatedProfit)
accumulatedProfitMA := r.accumulatedProfitMAPerDay.Index(r.IntervalWindow * i)
accumulatedProfitMAStr := fmt.Sprintf("%f", accumulatedProfitMA)
intervalAccumulatedProfit := r.dailyProfit.Tail(r.AccumulatedDailyProfitWindow+r.IntervalWindow*i).Sum() - r.dailyProfit.Tail(r.IntervalWindow*i).Sum()
intervalAccumulatedProfitStr := fmt.Sprintf("%f", intervalAccumulatedProfit)
accumulatedFee := fmt.Sprintf("%f", r.accumulatedFeePerDay.Index(r.IntervalWindow*i))
winRatio := fmt.Sprintf("%f", r.winRatioPerDay.Index(r.IntervalWindow*i))
profitFactor := fmt.Sprintf("%f", r.profitFactorPerDay.Index(r.IntervalWindow*i))
trades := r.dailyTrades.Tail(60+r.IntervalWindow*i).Sum() - r.dailyTrades.Tail(r.IntervalWindow*i).Sum()
tradesStr := fmt.Sprintf("%f", trades)
_ = tsvwiter.Write([]string{fmt.Sprintf("%d", i+1), symbol, accumulatedProfitStr, accumulatedProfitMAStr, intervalAccumulatedProfitStr, accumulatedFee, winRatio, profitFactor, tradesStr})
}
}
}
type Strategy struct {
Environment *bbgo.Environment
Market types.Market
@ -227,7 +103,7 @@ type Strategy struct {
bbgo.StrategyController
// Accumulated profit report
AccumulatedProfitReport *AccumulatedProfitReport `json:"accumulatedProfitReport"`
AccumulatedProfitReport *report.AccumulatedProfitReport `json:"accumulatedProfitReport"`
}
func (s *Strategy) ID() string {
@ -255,9 +131,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.LinearRegression.Interval})
s.ExitMethods.SetAndSubscribe(session, s)
// Accumulated profit report
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1d})
}
// Position control
@ -484,22 +357,20 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Accumulated profit report
if bbgo.IsBackTesting {
if s.AccumulatedProfitReport == nil {
s.AccumulatedProfitReport = &AccumulatedProfitReport{}
s.AccumulatedProfitReport = &report.AccumulatedProfitReport{}
}
s.AccumulatedProfitReport.Initialize()
s.orderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit == nil {
return
}
s.AccumulatedProfitReport.Initialize(s.Symbol, session, s.orderExecutor, s.TradeStats)
s.AccumulatedProfitReport.RecordProfit(profit.Profit)
})
// s.orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
// s.AccumulatedProfitReport.RecordTrade(trade.Fee)
// })
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, types.Interval1d, func(kline types.KLine) {
s.AccumulatedProfitReport.DailyUpdate(s.TradeStats)
}))
// Add strategy parameters to report
s.AccumulatedProfitReport.AddExtraValue([2]string{"window", fmt.Sprintf("%d", s.Window)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"multiplier", fmt.Sprintf("%f", s.SupertrendMultiplier)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"fastDEMA", fmt.Sprintf("%d", s.FastDEMAWindow)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"slowDEMA", fmt.Sprintf("%d", s.SlowDEMAWindow)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"takeProfitAtrMultiplier", fmt.Sprintf("%f", s.TakeProfitAtrMultiplier)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"stopLossByTriggeringK", fmt.Sprintf("%t", s.StopLossByTriggeringK)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"stopByReversedSupertrend", fmt.Sprintf("%t", s.StopByReversedSupertrend)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"stopByReversedDema", fmt.Sprintf("%t", s.StopByReversedDema)})
s.AccumulatedProfitReport.AddExtraValue([2]string{"stopByReversedLinGre", fmt.Sprintf("%t", s.StopByReversedLinGre)})
}
// For drawing
@ -509,10 +380,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
cumProfitSlice := floats.Slice{initAsset, initAsset}
s.orderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
if bbgo.IsBackTesting {
s.AccumulatedProfitReport.RecordTrade(trade.Fee)
}
// For drawing/charting
price := trade.Price.Float64()
if s.buyPrice > 0 {
@ -653,10 +520,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
// Output accumulated profit report
if bbgo.IsBackTesting {
// Output accumulated profit report
defer s.AccumulatedProfitReport.Output(s.Symbol)
// Draw graph
if s.DrawGraph {
if err := s.Draw(&profitSlice, &cumProfitSlice); err != nil {
log.WithError(err).Errorf("cannot draw graph")