fixedpoint: positive tester and negative tester

This commit is contained in:
c9s 2022-09-07 15:11:07 +08:00
parent c62330b2c1
commit 56c53958cd
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 22 additions and 0 deletions

20
pkg/fixedpoint/filter.go Normal file
View File

@ -0,0 +1,20 @@
package fixedpoint
type Tester func(value Value) bool
func PositiveTester(value Value) bool {
return value.Sign() > 0
}
func NegativeTester(value Value) bool {
return value.Sign() < 0
}
func Filter(values []Value, f Tester) (slice []Value) {
for _, v := range values {
if f(v) {
slice = append(slice, v)
}
}
return slice
}

View File

@ -304,6 +304,8 @@ func (s *TradeStats) update() {
sort.Sort(fixedpoint.Descending(profitsByOrder))
sort.Sort(fixedpoint.Descending(netProfitsByOrder))
s.Losses = fixedpoint.Filter(profitsByOrder, fixedpoint.NegativeTester)
s.Profits = fixedpoint.Filter(profitsByOrder, fixedpoint.PositiveTester)
s.LargestProfitTrade = profitsByOrder[0]
s.LargestLossTrade = profitsByOrder[len(profitsByOrder)-1]
if s.LargestLossTrade.Sign() > 0 {