mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
improve/profitTracker: do not bind in order executor
This commit is contained in:
parent
54c5c81266
commit
e589a95c97
|
@ -3,7 +3,6 @@ package bbgo
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/c9s/bbgo/pkg/report"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -163,27 +162,6 @@ func (e *GeneralOrderExecutor) BindProfitStats(profitStats *types.ProfitStats) {
|
|||
})
|
||||
}
|
||||
|
||||
func (e *GeneralOrderExecutor) BindProfitTracker(profitTracker *report.ProfitTracker) {
|
||||
e.session.Subscribe(types.KLineChannel, profitTracker.Market.Symbol, types.SubscribeOptions{Interval: profitTracker.Interval})
|
||||
|
||||
e.tradeCollector.OnProfit(func(trade types.Trade, profit *types.Profit) {
|
||||
if profit == nil {
|
||||
return
|
||||
}
|
||||
|
||||
profitTracker.AddProfit(*profit)
|
||||
})
|
||||
|
||||
e.tradeCollector.OnTrade(func(trade types.Trade, profit fixedpoint.Value, netProfit fixedpoint.Value) {
|
||||
profitTracker.AddTrade(trade)
|
||||
})
|
||||
|
||||
// Rotate profitStats slice
|
||||
e.session.MarketDataStream.OnKLineClosed(types.KLineWith(profitTracker.Market.Symbol, profitTracker.Interval, func(kline types.KLine) {
|
||||
profitTracker.Rotate()
|
||||
}))
|
||||
}
|
||||
|
||||
func (e *GeneralOrderExecutor) Bind() {
|
||||
e.activeMakerOrders.BindStream(e.session.UserDataStream)
|
||||
e.orderStore.BindStream(e.session.UserDataStream)
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package report
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
|
@ -42,6 +44,27 @@ func (p *ProfitTracker) Init(market types.Market, ts *types.TradeStats) {
|
|||
p.InitOld(market, &ps, ts)
|
||||
}
|
||||
|
||||
func (p *ProfitTracker) Bind(session *bbgo.ExchangeSession, tradeCollector *bbgo.TradeCollector) {
|
||||
session.Subscribe(types.KLineChannel, p.Market.Symbol, types.SubscribeOptions{Interval: p.Interval})
|
||||
|
||||
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()
|
||||
}))
|
||||
}
|
||||
|
||||
// Rotate the tracker to make a new ProfitStats to record the profits
|
||||
func (p *ProfitTracker) Rotate() {
|
||||
// Update report
|
||||
|
|
|
@ -327,25 +327,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
s.TradeStats = types.NewTradeStats(s.Symbol)
|
||||
}
|
||||
|
||||
if s.ProfitTracker != nil {
|
||||
if s.ProfitTracker.CurrentProfitStats == nil {
|
||||
s.ProfitTracker.InitOld(s.Market, &s.ProfitStats, s.TradeStats)
|
||||
}
|
||||
|
||||
// Add strategy parameters to report
|
||||
if s.ProfitTracker.AccumulatedProfitReport != nil {
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("window", fmt.Sprintf("%d", s.Window))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("multiplier", fmt.Sprintf("%f", s.SupertrendMultiplier))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("fastDEMA", fmt.Sprintf("%d", s.FastDEMAWindow))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("slowDEMA", fmt.Sprintf("%d", s.SlowDEMAWindow))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("takeProfitAtrMultiplier", fmt.Sprintf("%f", s.TakeProfitAtrMultiplier))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopLossByTriggeringK", fmt.Sprintf("%t", s.StopLossByTriggeringK))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedSupertrend", fmt.Sprintf("%t", s.StopByReversedSupertrend))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedDema", fmt.Sprintf("%t", s.StopByReversedDema))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedLinGre", fmt.Sprintf("%t", s.StopByReversedLinGre))
|
||||
}
|
||||
}
|
||||
|
||||
// Interval profit report
|
||||
if bbgo.IsBackTesting {
|
||||
startTime := s.Environment.StartTime()
|
||||
|
@ -367,11 +348,30 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
|||
s.orderExecutor.BindEnvironment(s.Environment)
|
||||
s.orderExecutor.BindProfitStats(s.ProfitStats)
|
||||
s.orderExecutor.BindTradeStats(s.TradeStats)
|
||||
if s.ProfitTracker != nil {
|
||||
s.orderExecutor.BindProfitTracker(s.ProfitTracker)
|
||||
}
|
||||
s.orderExecutor.Bind()
|
||||
|
||||
// Setup profit tracker
|
||||
if s.ProfitTracker != nil {
|
||||
if s.ProfitTracker.CurrentProfitStats == nil {
|
||||
s.ProfitTracker.InitOld(s.Market, &s.ProfitStats, s.TradeStats)
|
||||
}
|
||||
|
||||
// Add strategy parameters to report
|
||||
if s.ProfitTracker.AccumulatedProfitReport != nil {
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("window", fmt.Sprintf("%d", s.Window))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("multiplier", fmt.Sprintf("%f", s.SupertrendMultiplier))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("fastDEMA", fmt.Sprintf("%d", s.FastDEMAWindow))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("slowDEMA", fmt.Sprintf("%d", s.SlowDEMAWindow))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("takeProfitAtrMultiplier", fmt.Sprintf("%f", s.TakeProfitAtrMultiplier))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopLossByTriggeringK", fmt.Sprintf("%t", s.StopLossByTriggeringK))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedSupertrend", fmt.Sprintf("%t", s.StopByReversedSupertrend))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedDema", fmt.Sprintf("%t", s.StopByReversedDema))
|
||||
s.ProfitTracker.AccumulatedProfitReport.AddStrategyParameter("stopByReversedLinGre", fmt.Sprintf("%t", s.StopByReversedLinGre))
|
||||
}
|
||||
|
||||
s.ProfitTracker.Bind(s.session, s.orderExecutor.TradeCollector())
|
||||
}
|
||||
|
||||
// AccountValueCalculator
|
||||
s.AccountValueCalculator = bbgo.NewAccountValueCalculator(s.session, s.Market.QuoteCurrency)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user