Skip to content

Commit 046ed88

Browse files
committed
all: implement stats tracker
- rename Add() to AddProfit() or AddTrade() so that we can apply interface here
1 parent 2c4b6e8 commit 046ed88

File tree

12 files changed

+101
-17
lines changed

12 files changed

+101
-17
lines changed

pkg/bbgo/order_executor_general.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ func (e *GeneralOrderExecutor) BindTradeStats(tradeStats *types.TradeStats) {
160160
return
161161
}
162162

163-
tradeStats.Add(profit)
163+
tradeStats.AddProfit(profit)
164164
})
165165
}
166166

@@ -171,7 +171,7 @@ func (e *GeneralOrderExecutor) BindProfitStats(profitStats *types.ProfitStats) {
171171
return
172172
}
173173

174-
profitStats.AddProfit(*profit)
174+
profitStats.AddProfit(profit)
175175

176176
if !e.disableNotify {
177177
Notify(profit)

pkg/cmd/backtest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ var BacktestCmd = &cobra.Command{
325325
if profit == nil {
326326
return
327327
}
328-
tradeStats.Add(profit)
328+
tradeStats.AddProfit(profit)
329329
})
330330
tradeStatsMap[usedSymbol] = tradeStats
331331

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/risk/riskcontrol/circuit_break.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ func (c *CircuitBreakRiskControl) IsHalted() bool {
3737
log.Infof("[CircuitBreakRiskControl] realized PnL = %f, unrealized PnL = %f\n",
3838
c.profitStats.TodayPnL.Float64(),
3939
unrealized.Float64())
40+
4041
return unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0
4142
}

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/wall/strategy.go

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

295-
s.ProfitStats.AddProfit(p)
295+
s.ProfitStats.AddProfit(&p)
296296
bbgo.Notify(&s.ProfitStats)
297297

298298
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
@@ -1097,7 +1097,7 @@ func (s *Strategy) allocateOrderExecutor(ctx context.Context, session *bbgo.Exch
10971097

10981098
if profit, netProfit, madeProfit := s.NeutralPosition.AddTrade(trade); madeProfit {
10991099
p := s.NeutralPosition.NewProfit(trade, profit, netProfit)
1100-
s.ProfitStats.AddProfit(p)
1100+
s.ProfitStats.AddProfit(&p)
11011101
}
11021102
})
11031103
return orderExecutor

pkg/strategy/xmaker/strategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
801801
p.Strategy = ID
802802
p.StrategyInstanceID = instanceID
803803
bbgo.Notify(&p)
804-
s.ProfitStats.AddProfit(p)
804+
s.ProfitStats.AddProfit(&p)
805805

806806
s.Environment.RecordPosition(s.Position, trade, &p)
807807
}

0 commit comments

Comments
 (0)