bbgo_origin/pkg/report/profit_stats_tracker.go

97 lines
2.7 KiB
Go
Raw Normal View History

2023-06-16 06:56:22 +00:00
package report
import (
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
2023-06-16 06:56:22 +00:00
"github.com/c9s/bbgo/pkg/types"
)
type ProfitStatsTracker struct {
2023-06-16 06:56:22 +00:00
types.IntervalWindow
// Accumulated profit report
AccumulatedProfitReport *AccumulatedProfitReport `json:"accumulatedProfitReport"`
Market types.Market
ProfitStatsSlice []*types.ProfitStats
CurrentProfitStats **types.ProfitStats
tradeStats *types.TradeStats
2023-06-16 06:56:22 +00:00
}
func (p *ProfitStatsTracker) Subscribe(session *bbgo.ExchangeSession, symbol string) {
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: p.Interval})
}
// InitLegacy is for backward capability. ps is the ProfitStats of the strategy, Market is the strategy Market
func (p *ProfitStatsTracker) InitLegacy(market types.Market, ps **types.ProfitStats, ts *types.TradeStats) {
p.Market = market
2023-06-16 06:56:22 +00:00
if *ps == nil {
*ps = types.NewProfitStats(p.Market)
2023-06-16 06:56:22 +00:00
}
p.tradeStats = ts
p.CurrentProfitStats = ps
p.ProfitStatsSlice = append(p.ProfitStatsSlice, *ps)
2023-06-16 06:56:22 +00:00
if p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.Initialize(p.Market.Symbol, p.Interval, p.Window)
}
2023-06-16 06:56:22 +00:00
}
// Init initialize the tracker with the given Market
func (p *ProfitStatsTracker) Init(market types.Market, ts *types.TradeStats) {
ps := types.NewProfitStats(p.Market)
p.InitLegacy(market, &ps, ts)
2023-06-16 06:56:22 +00:00
}
func (p *ProfitStatsTracker) Bind(session *bbgo.ExchangeSession, tradeCollector *core.TradeCollector) {
tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit == nil {
return
}
p.AddProfit(*profit)
})
tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
p.AddTrade(trade)
})
// Rotate profitStats slice
session.MarketDataStream.OnKLineClosed(types.KLineWith(p.Market.Symbol, p.Interval, func(kline types.KLine) {
p.Rotate()
}))
}
2023-06-16 06:56:22 +00:00
// Rotate the tracker to make a new ProfitStats to record the profits
func (p *ProfitStatsTracker) Rotate() {
2023-06-16 10:32:44 +00:00
// 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)
2023-06-16 06:56:22 +00:00
// Truncate
if len(p.ProfitStatsSlice) > p.Window {
p.ProfitStatsSlice = p.ProfitStatsSlice[len(p.ProfitStatsSlice)-p.Window:]
2023-06-16 06:56:22 +00:00
}
}
func (p *ProfitStatsTracker) AddProfit(profit types.Profit) {
(*p.CurrentProfitStats).AddProfit(profit)
2023-06-16 06:56:22 +00:00
}
2023-06-16 07:06:58 +00:00
func (p *ProfitStatsTracker) AddTrade(trade types.Trade) {
(*p.CurrentProfitStats).AddTrade(trade)
if p.AccumulatedProfitReport != nil {
p.AccumulatedProfitReport.AddTrade(trade)
}
2023-06-16 07:06:58 +00:00
}