bbgo_origin/pkg/strategy/grid2/trade.go

46 lines
1.0 KiB
Go
Raw Normal View History

2022-12-25 17:04:17 +00:00
package grid2
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 {
2023-07-31 10:06:20 +00:00
if t.FeeDiscounted {
continue
}
2022-12-25 17:04:17 +00:00
if fee, ok := fees[t.FeeCurrency]; ok {
fees[t.FeeCurrency] = fee.Add(t.Fee)
} else {
fees[t.FeeCurrency] = t.Fee
}
}
return fees
}
func aggregateTradesQuantity(trades []types.Trade) fixedpoint.Value {
tq := fixedpoint.Zero
for _, t := range trades {
tq = tq.Add(t.Quantity)
}
return tq
}
// 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
}