|
| 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 | +} |
0 commit comments