Skip to content

Commit 73d07cd

Browse files
committed
all: implement stats tracker
- rename Add() to AddProfit() or AddTrade() so that we can apply interface here
1 parent 31b6786 commit 73d07cd

File tree

10 files changed

+103
-10
lines changed

10 files changed

+103
-10
lines changed

pkg/bbgo/order_executor_general.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (e *GeneralOrderExecutor) BindTradeStats(tradeStats *types.TradeStats) {
143143
return
144144
}
145145

146-
tradeStats.Add(profit)
146+
tradeStats.AddProfit(profit)
147147
})
148148
}
149149

@@ -154,7 +154,7 @@ func (e *GeneralOrderExecutor) BindProfitStats(profitStats *types.ProfitStats) {
154154
return
155155
}
156156

157-
profitStats.AddProfit(*profit)
157+
profitStats.AddProfit(profit)
158158

159159
if !e.disableNotify {
160160
Notify(profit)

pkg/cmd/backtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ var BacktestCmd = &cobra.Command{
344344
if profit == nil {
345345
return
346346
}
347-
tradeStats.Add(profit)
347+
tradeStats.AddProfit(profit)
348348
})
349349
tradeStatsMap[usedSymbol] = tradeStats
350350

pkg/report/profit_stats_tracker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (p *ProfitStatsTracker) Rotate() {
8484
}
8585

8686
func (p *ProfitStatsTracker) AddProfit(profit types.Profit) {
87-
(*p.CurrentProfitStats).AddProfit(profit)
87+
(*p.CurrentProfitStats).AddProfit(&profit)
8888
}
8989

9090
func (p *ProfitStatsTracker) AddTrade(trade types.Trade) {

pkg/report/stats_collector.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package report
2+
3+
import (
4+
"github.com/c9s/bbgo/pkg/bbgo"
5+
"github.com/c9s/bbgo/pkg/core"
6+
"github.com/c9s/bbgo/pkg/types"
7+
)
8+
9+
type TradeAdder interface {
10+
AddTrade(trade *types.Trade)
11+
}
12+
13+
type ProfitAdder interface {
14+
AddProfit(trade *types.Profit)
15+
}
16+
17+
// StatsCollector is the v2 profit stats tracker
18+
type StatsCollector struct {
19+
Market types.Market `json:"market"`
20+
Interval types.Interval `json:"interval"`
21+
Window int `json:"window"`
22+
23+
CurrentProfitStats *types.ProfitStats `json:"profitStats"`
24+
AccumulatedProfitStats *types.ProfitStats `json:"accumulatedProfitStats"`
25+
HistoryProfitStats []types.ProfitStats `json:"historyProfitStats"`
26+
27+
CurrentTradeStats *types.TradeStats `json:"tradeStats"`
28+
AccumulatedTradeStats *types.TradeStats `json:"accumulatedTradeStats"`
29+
HistoryTradeStats []types.TradeStats `json:"historyTradeStats"`
30+
31+
tradeCollector *core.TradeCollector
32+
}
33+
34+
func NewStatsCollector(market types.Market, interval types.Interval, window int, tradeCollector *core.TradeCollector) *StatsCollector {
35+
return &StatsCollector{
36+
Market: market,
37+
Interval: interval,
38+
Window: window,
39+
CurrentProfitStats: types.NewProfitStats(market),
40+
CurrentTradeStats: types.NewTradeStats(market.Symbol),
41+
AccumulatedProfitStats: types.NewProfitStats(market),
42+
AccumulatedTradeStats: types.NewTradeStats(market.Symbol),
43+
tradeCollector: tradeCollector,
44+
}
45+
}
46+
47+
func (c *StatsCollector) Subscribe(session *bbgo.ExchangeSession) {
48+
session.Subscribe(types.KLineChannel, c.Market.Symbol, types.SubscribeOptions{Interval: c.Interval})
49+
}
50+
51+
func (c *StatsCollector) Bind(session *bbgo.ExchangeSession) {
52+
c.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
53+
if profit != nil {
54+
c.CurrentProfitStats.AddProfit(profit)
55+
c.AccumulatedProfitStats.AddProfit(profit)
56+
}
57+
58+
c.CurrentProfitStats.AddTrade(trade)
59+
c.AccumulatedProfitStats.AddTrade(trade)
60+
61+
c.CurrentTradeStats.AddProfit(profit)
62+
c.AccumulatedTradeStats.AddProfit(profit)
63+
})
64+
65+
// Rotate profitStats slice
66+
session.MarketDataStream.OnKLineClosed(types.KLineWith(c.Market.Symbol, c.Interval, func(k types.KLine) {
67+
// p.Rotate()
68+
}))
69+
}
70+
71+
// Rotate the tracker to make a new ProfitStats to record the profits
72+
func (c *StatsCollector) Rotate() {
73+
c.HistoryProfitStats = append(c.HistoryProfitStats, *c.CurrentProfitStats)
74+
c.HistoryTradeStats = append(c.HistoryTradeStats, *c.CurrentTradeStats)
75+
/*
76+
*p.CurrentProfitStats = types.NewProfitStats(p.Market)
77+
p.ProfitStatsSlice = append(p.ProfitStatsSlice, *p.CurrentProfitStats)
78+
// Truncate
79+
if len(p.ProfitStatsSlice) > p.Window {
80+
p.ProfitStatsSlice = p.ProfitStatsSlice[len(p.ProfitStatsSlice)-p.Window:]
81+
}
82+
*/
83+
}

pkg/strategy/drift/strategy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
831831
if madeProfit {
832832
p := s.Position.NewProfit(trade, profit, netProfit)
833833
s.Environment.RecordPosition(s.Position, trade, &p)
834-
s.TradeStats.Add(&p)
834+
s.TradeStats.AddProfit(&p)
835835
s.ProfitStats.AddTrade(trade)
836-
s.ProfitStats.AddProfit(p)
836+
s.ProfitStats.AddProfit(&p)
837837
bbgo.Notify(&p)
838838
bbgo.Notify(s.ProfitStats)
839839
}

pkg/strategy/fmaker/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
193193
p.StrategyInstanceID = instanceID
194194
bbgo.Notify(&p)
195195

196-
s.ProfitStats.AddProfit(p)
196+
s.ProfitStats.AddProfit(&p)
197197
bbgo.Notify(&s.ProfitStats)
198198

199199
s.Environment.RecordPosition(s.Position, trade, &p)

pkg/strategy/xfunding/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ func (s *Strategy) allocateOrderExecutor(
11531153

11541154
if profit, netProfit, madeProfit := s.NeutralPosition.AddTrade(trade); madeProfit {
11551155
p := s.NeutralPosition.NewProfit(trade, profit, netProfit)
1156-
s.ProfitStats.AddProfit(p)
1156+
s.ProfitStats.AddProfit(&p)
11571157
}
11581158
})
11591159
return orderExecutor

pkg/strategy/xmaker/strategy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2442,6 +2442,16 @@ func (s *Strategy) CrossRun(
24422442
s.tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
24432443
if profit.Compare(fixedpoint.Zero) == 0 {
24442444
s.Environment.RecordPosition(s.Position, trade, nil)
2445+
} else {
2446+
log.Infof("%s generated profit: %v", s.Symbol, profit)
2447+
2448+
p := s.Position.NewProfit(trade, profit, netProfit)
2449+
p.Strategy = ID
2450+
p.StrategyInstanceID = instanceID
2451+
bbgo.Notify(&p)
2452+
s.ProfitStats.AddProfit(&p)
2453+
2454+
s.Environment.RecordPosition(s.Position, trade, &p)
24452455
}
24462456
})
24472457
s.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {

pkg/types/profit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func (s *ProfitStats) Init(market Market) {
215215
}
216216
}
217217

218-
func (s *ProfitStats) AddProfit(profit Profit) {
218+
func (s *ProfitStats) AddProfit(profit *Profit) {
219219
if s.IsOver24Hours() {
220220
s.ResetToday(profit.TradedAt)
221221
}

pkg/types/trade_stats.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func (s *TradeStats) CsvRecords() [][]string {
240240
}
241241
}
242242

243-
func (s *TradeStats) Add(profit *Profit) {
243+
func (s *TradeStats) AddProfit(profit *Profit) {
244244
if s.Symbol != "" && profit.Symbol != s.Symbol {
245245
return
246246
}

0 commit comments

Comments
 (0)