all: use tradeStats constructor

This commit is contained in:
c9s 2022-07-05 11:14:50 +08:00
parent bfd64813f8
commit 193703a9a0
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
5 changed files with 19 additions and 5 deletions

View File

@ -53,7 +53,8 @@ func (e *GeneralOrderExecutor) BindTradeStats(tradeStats *types.TradeStats) {
if profit == nil {
return
}
tradeStats.Add(profit.Profit)
tradeStats.Add(profit)
})
}

View File

@ -235,7 +235,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}
if s.TradeStats == nil {
s.TradeStats = &types.TradeStats{}
s.TradeStats = types.NewTradeStats(s.Symbol)
}
// StrategyController

View File

@ -413,7 +413,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
}
if s.TradeStats == nil {
s.TradeStats = &types.TradeStats{}
s.TradeStats = types.NewTradeStats(s.Symbol)
}
// initial required information

View File

@ -334,7 +334,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// trade stats
if s.TradeStats == nil {
s.TradeStats = &types.TradeStats{}
s.TradeStats = types.NewTradeStats(s.Symbol)
}
s.orderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)

View File

@ -9,6 +9,7 @@ import (
// TODO: Add more stats from the reference:
// See https://www.metatrader5.com/en/terminal/help/algotrading/testing_report
type TradeStats struct {
Symbol string `json:"symbol"`
WinningRatio fixedpoint.Value `json:"winningRatio" yaml:"winningRatio"`
NumOfLossTrade int `json:"numOfLossTrade" yaml:"numOfLossTrade"`
NumOfProfitTrade int `json:"numOfProfitTrade" yaml:"numOfProfitTrade"`
@ -22,7 +23,19 @@ type TradeStats struct {
TotalNetProfit fixedpoint.Value `json:"totalNetProfit" yaml:"totalNetProfit"`
}
func (s *TradeStats) Add(pnl fixedpoint.Value) {
func NewTradeStats(symbol string) *TradeStats {
return &TradeStats{Symbol: symbol}
}
func (s *TradeStats) Add(profit *Profit) {
if profit.Symbol != s.Symbol {
return
}
s.add(profit.Profit)
}
func (s *TradeStats) add(pnl fixedpoint.Value) {
if pnl.Sign() > 0 {
s.NumOfProfitTrade++
s.Profits = append(s.Profits, pnl)