2023-08-04 17:59:36 +00:00
|
|
|
package tradingutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/fixedpoint"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CollectTradeFee collects the fee from the given trade slice
|
|
|
|
func CollectTradeFee(trades []types.Trade) map[string]fixedpoint.Value {
|
|
|
|
fees := make(map[string]fixedpoint.Value)
|
|
|
|
for _, t := range trades {
|
2024-02-23 10:45:58 +00:00
|
|
|
if t.FeeDiscounted {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:59:36 +00:00
|
|
|
if fee, ok := fees[t.FeeCurrency]; ok {
|
|
|
|
fees[t.FeeCurrency] = fee.Add(t.Fee)
|
|
|
|
} else {
|
|
|
|
fees[t.FeeCurrency] = t.Fee
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fees
|
|
|
|
}
|
2023-08-04 18:37:53 +00:00
|
|
|
|
2024-02-23 10:45:58 +00:00
|
|
|
// AggregateTradesQuantity sums up the quantity from the given trades
|
|
|
|
// totalQuantity = SUM(trade1.Quantity, trade2.Quantity, ...)
|
2023-08-04 18:37:53 +00:00
|
|
|
func AggregateTradesQuantity(trades []types.Trade) fixedpoint.Value {
|
|
|
|
tq := fixedpoint.Zero
|
|
|
|
for _, t := range trades {
|
|
|
|
tq = tq.Add(t.Quantity)
|
|
|
|
}
|
|
|
|
return tq
|
|
|
|
}
|
2024-02-23 10:45:58 +00:00
|
|
|
|
|
|
|
// AggregateTradesQuoteQuantity aggregates the quote quantity from the given trade slice
|
|
|
|
func AggregateTradesQuoteQuantity(trades []types.Trade) fixedpoint.Value {
|
|
|
|
quoteQuantity := fixedpoint.Zero
|
|
|
|
for _, t := range trades {
|
|
|
|
if t.QuoteQuantity.IsZero() {
|
|
|
|
quoteQuantity = quoteQuantity.Add(t.Price.Mul(t.Quantity))
|
|
|
|
} else {
|
|
|
|
quoteQuantity = quoteQuantity.Add(t.QuoteQuantity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return quoteQuantity
|
|
|
|
}
|