Skip to content

Commit b72a176

Browse files
authored
Merge pull request #1547 from c9s/refactor/tradingutil
REFACTOR: move trading related utility functions to the tradingutil package
2 parents 24013a8 + 36e90cf commit b72a176

File tree

5 files changed

+32
-57
lines changed

5 files changed

+32
-57
lines changed

pkg/strategy/grid2/grid.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ type Grid struct {
3535

3636
type Pin fixedpoint.Value
3737

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

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

8282
return pins

pkg/strategy/grid2/grid_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ func Test_filterPrice1(t *testing.T) {
243243
}
244244
for _, tt := range tests {
245245
t.Run(tt.name, func(t *testing.T) {
246-
rst := filterPrice(tt.args.p, tt.args.prec)
247-
assert.Equalf(t, tt.want, rst.String(), "filterPrice(%v, %v)", tt.args.p, tt.args.prec)
246+
rst := roundAndTruncatePrice(tt.args.p, tt.args.prec)
247+
assert.Equalf(t, tt.want, rst.String(), "roundAndTruncatePrice(%v, %v)", tt.args.p, tt.args.prec)
248248
})
249249
}
250250
}

pkg/strategy/grid2/strategy.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/c9s/bbgo/pkg/fixedpoint"
2323
"github.com/c9s/bbgo/pkg/types"
2424
"github.com/c9s/bbgo/pkg/util"
25+
"github.com/c9s/bbgo/pkg/util/tradingutil"
2526
)
2627

2728
const ID = "grid2"
@@ -351,7 +352,7 @@ func (s *Strategy) calculateProfit(o types.Order, buyPrice, buyQuantity fixedpoi
351352
}
352353

353354
func (s *Strategy) verifyOrderTrades(o types.Order, trades []types.Trade) bool {
354-
tq := aggregateTradesQuantity(trades)
355+
tq := tradingutil.AggregateTradesQuantity(trades)
355356

356357
// on MAX: if order.status == filled, it does not mean order.executedQuantity == order.quantity
357358
// order.executedQuantity can be less than order.quantity
@@ -400,8 +401,8 @@ func (s *Strategy) aggregateOrderQuoteAmountAndFee(o types.Order) (fixedpoint.Va
400401
// if one of the trades is missing, we need to query the trades from the RESTful API
401402
if s.verifyOrderTrades(o, orderTrades) {
402403
// if trades are verified
403-
quoteAmount := aggregateTradesQuoteQuantity(orderTrades)
404-
fees := collectTradeFee(orderTrades)
404+
quoteAmount := tradingutil.AggregateTradesQuoteQuantity(orderTrades)
405+
fees := tradingutil.CollectTradeFee(orderTrades)
405406
if fee, ok := fees[feeCurrency]; ok {
406407
return quoteAmount, fee, feeCurrency
407408
}
@@ -428,9 +429,9 @@ func (s *Strategy) aggregateOrderQuoteAmountAndFee(o types.Order) (fixedpoint.Va
428429
}
429430
}
430431

431-
quoteAmount := aggregateTradesQuoteQuantity(orderTrades)
432+
quoteAmount := tradingutil.AggregateTradesQuoteQuantity(orderTrades)
432433
// still try to aggregate the trades quantity if we can:
433-
fees := collectTradeFee(orderTrades)
434+
fees := tradingutil.CollectTradeFee(orderTrades)
434435
if fee, ok := fees[feeCurrency]; ok {
435436
return quoteAmount, fee, feeCurrency
436437
}

pkg/strategy/grid2/trade.go

-45
This file was deleted.

pkg/util/tradingutil/trades.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,39 @@ import (
99
func CollectTradeFee(trades []types.Trade) map[string]fixedpoint.Value {
1010
fees := make(map[string]fixedpoint.Value)
1111
for _, t := range trades {
12+
if t.FeeDiscounted {
13+
continue
14+
}
15+
1216
if fee, ok := fees[t.FeeCurrency]; ok {
1317
fees[t.FeeCurrency] = fee.Add(t.Fee)
1418
} else {
1519
fees[t.FeeCurrency] = t.Fee
1620
}
1721
}
18-
1922
return fees
2023
}
2124

25+
// AggregateTradesQuantity sums up the quantity from the given trades
26+
// totalQuantity = SUM(trade1.Quantity, trade2.Quantity, ...)
2227
func AggregateTradesQuantity(trades []types.Trade) fixedpoint.Value {
2328
tq := fixedpoint.Zero
2429
for _, t := range trades {
2530
tq = tq.Add(t.Quantity)
2631
}
2732
return tq
2833
}
34+
35+
// AggregateTradesQuoteQuantity aggregates the quote quantity from the given trade slice
36+
func AggregateTradesQuoteQuantity(trades []types.Trade) fixedpoint.Value {
37+
quoteQuantity := fixedpoint.Zero
38+
for _, t := range trades {
39+
if t.QuoteQuantity.IsZero() {
40+
quoteQuantity = quoteQuantity.Add(t.Price.Mul(t.Quantity))
41+
} else {
42+
quoteQuantity = quoteQuantity.Add(t.QuoteQuantity)
43+
}
44+
}
45+
46+
return quoteQuantity
47+
}

0 commit comments

Comments
 (0)