From c62330b2c11a3e4f537f90b739fca00970c44700 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 7 Sep 2022 14:05:44 +0800 Subject: [PATCH] fixedpoint: add counter func --- pkg/fixedpoint/count.go | 13 +++++++++++++ pkg/types/trade_stats.go | 6 ++++++ 2 files changed, 19 insertions(+) create mode 100644 pkg/fixedpoint/count.go diff --git a/pkg/fixedpoint/count.go b/pkg/fixedpoint/count.go new file mode 100644 index 000000000..84a3d1c8c --- /dev/null +++ b/pkg/fixedpoint/count.go @@ -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 +} diff --git a/pkg/types/trade_stats.go b/pkg/types/trade_stats.go index 6e923c85b..7be5d0390 100644 --- a/pkg/types/trade_stats.go +++ b/pkg/types/trade_stats.go @@ -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)