move trading related utility functions to the tradingutil package

This commit is contained in:
c9s 2024-02-23 18:45:58 +08:00
parent 945c442b92
commit a298950be8
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 26 additions and 51 deletions

View File

@ -22,6 +22,7 @@ import (
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
"github.com/c9s/bbgo/pkg/util/tradingutil"
)
const ID = "grid2"
@ -351,7 +352,7 @@ func (s *Strategy) calculateProfit(o types.Order, buyPrice, buyQuantity fixedpoi
}
func (s *Strategy) verifyOrderTrades(o types.Order, trades []types.Trade) bool {
tq := aggregateTradesQuantity(trades)
tq := tradingutil.AggregateTradesQuantity(trades)
// on MAX: if order.status == filled, it does not mean order.executedQuantity == order.quantity
// order.executedQuantity can be less than order.quantity
@ -400,8 +401,8 @@ func (s *Strategy) aggregateOrderQuoteAmountAndFee(o types.Order) (fixedpoint.Va
// if one of the trades is missing, we need to query the trades from the RESTful API
if s.verifyOrderTrades(o, orderTrades) {
// if trades are verified
quoteAmount := aggregateTradesQuoteQuantity(orderTrades)
fees := collectTradeFee(orderTrades)
quoteAmount := tradingutil.AggregateTradesQuoteQuantity(orderTrades)
fees := tradingutil.CollectTradeFee(orderTrades)
if fee, ok := fees[feeCurrency]; ok {
return quoteAmount, fee, feeCurrency
}
@ -428,9 +429,9 @@ func (s *Strategy) aggregateOrderQuoteAmountAndFee(o types.Order) (fixedpoint.Va
}
}
quoteAmount := aggregateTradesQuoteQuantity(orderTrades)
quoteAmount := tradingutil.AggregateTradesQuoteQuantity(orderTrades)
// still try to aggregate the trades quantity if we can:
fees := collectTradeFee(orderTrades)
fees := tradingutil.CollectTradeFee(orderTrades)
if fee, ok := fees[feeCurrency]; ok {
return quoteAmount, fee, feeCurrency
}

View File

@ -1,45 +0,0 @@
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 {
if t.FeeDiscounted {
continue
}
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
}

View File

@ -9,16 +9,21 @@ import (
func CollectTradeFee(trades []types.Trade) map[string]fixedpoint.Value {
fees := make(map[string]fixedpoint.Value)
for _, t := range trades {
if t.FeeDiscounted {
continue
}
if fee, ok := fees[t.FeeCurrency]; ok {
fees[t.FeeCurrency] = fee.Add(t.Fee)
} else {
fees[t.FeeCurrency] = t.Fee
}
}
return fees
}
// AggregateTradesQuantity sums up the quantity from the given trades
// totalQuantity = SUM(trade1.Quantity, trade2.Quantity, ...)
func AggregateTradesQuantity(trades []types.Trade) fixedpoint.Value {
tq := fixedpoint.Zero
for _, t := range trades {
@ -26,3 +31,17 @@ func AggregateTradesQuantity(trades []types.Trade) fixedpoint.Value {
}
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
}