mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
198 lines
6.1 KiB
Go
198 lines
6.1 KiB
Go
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)
|
|
}
|
|
}
|
|
}
|