Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR: move trading related utility functions to the tradingutil package #1547

Merged
merged 2 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pkg/strategy/grid2/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type Grid struct {

type Pin fixedpoint.Value

// filterPrice filters price with the given precision
func filterPrice(p fixedpoint.Value, prec int) fixedpoint.Value {
// roundAndTruncatePrice rounds the given price at prec-1 and then truncate the price at prec
func roundAndTruncatePrice(p fixedpoint.Value, prec int) fixedpoint.Value {
var pow10 = math.Pow10(prec)
pp := math.Round(p.Float64()*pow10*10.0) / 10.0
pp = math.Trunc(pp) / pow10
Expand Down Expand Up @@ -71,12 +71,12 @@ func calculateArithmeticPins(lower, upper, spread, tickSize fixedpoint.Value) []
var ts = tickSize.Float64()
var prec = int(math.Round(math.Log10(ts) * -1.0))
for p := lower; p.Compare(upper.Sub(spread)) <= 0; p = p.Add(spread) {
price := filterPrice(p, prec)
price := roundAndTruncatePrice(p, prec)
pins = append(pins, Pin(price))
}

// this makes sure there is no error at the upper price
upperPrice := filterPrice(upper, prec)
upperPrice := roundAndTruncatePrice(upper, prec)
pins = append(pins, Pin(upperPrice))

return pins
Expand Down
4 changes: 2 additions & 2 deletions pkg/strategy/grid2/grid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ func Test_filterPrice1(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rst := filterPrice(tt.args.p, tt.args.prec)
assert.Equalf(t, tt.want, rst.String(), "filterPrice(%v, %v)", tt.args.p, tt.args.prec)
rst := roundAndTruncatePrice(tt.args.p, tt.args.prec)
assert.Equalf(t, tt.want, rst.String(), "roundAndTruncatePrice(%v, %v)", tt.args.p, tt.args.prec)
})
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/strategy/grid2/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
45 changes: 0 additions & 45 deletions pkg/strategy/grid2/trade.go

This file was deleted.

21 changes: 20 additions & 1 deletion pkg/util/tradingutil/trades.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,39 @@ 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 {
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
}
Loading