fixedpoint: add counter func

This commit is contained in:
c9s 2022-09-07 14:05:44 +08:00
parent e28921879d
commit c62330b2c1
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 19 additions and 0 deletions

13
pkg/fixedpoint/count.go Normal file
View File

@ -0,0 +1,13 @@
package fixedpoint
type Counter func(a Value) bool
func Count(values []Value, counter Counter) int {
var c = 0
for _, value := range values {
if counter(value) {
c++
}
}
return c
}

View File

@ -291,6 +291,12 @@ func (s *TradeStats) update() {
netProfitsByOrder = append(netProfitsByOrder, sumNetProfit)
}
s.NumOfProfitTrade = fixedpoint.Count(profitsByOrder, func(a fixedpoint.Value) bool {
return a.Sign() > 0
})
s.NumOfLossTrade = fixedpoint.Count(profitsByOrder, func(a fixedpoint.Value) bool {
return a.Sign() < 0
})
s.TotalNetProfit = fixedpoint.Reduce(profitsByOrder, fixedpoint.SumReducer)
s.GrossProfit = fixedpoint.Reduce(profitsByOrder, grossProfitReducer)
s.GrossLoss = fixedpoint.Reduce(profitsByOrder, grossLossReducer)