From e1c2ed40ff4ff6adc5907381a35242b5bfc83407 Mon Sep 17 00:00:00 2001 From: zenix Date: Thu, 18 Aug 2022 11:54:06 +0900 Subject: [PATCH] fix: truncate price in backtest, don't truncate amount, add TruncatePrice function --- pkg/backtest/matching.go | 6 ++++-- pkg/types/market.go | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 3bec810c3..92b9dafc7 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -142,9 +142,11 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty case types.OrderTypeStopMarket: // the actual price might be different. + o.StopPrice = m.Market.TruncatePrice(o.StopPrice) price = o.StopPrice case types.OrderTypeLimit, types.OrderTypeStopLimit, types.OrderTypeLimitMaker: + o.Price = m.Market.TruncatePrice(o.Price) price = o.Price } @@ -154,7 +156,7 @@ func (m *SimplePriceMatching) PlaceOrder(o types.SubmitOrder) (*types.Order, *ty return nil, nil, fmt.Errorf("order quantity %s is less than minQuantity %s, order: %+v", o.Quantity.String(), m.Market.MinQuantity.String(), o) } - quoteQuantity := m.Market.TruncateQuantity(o.Quantity.Mul(price)) + quoteQuantity := o.Quantity.Mul(price) if quoteQuantity.Compare(m.Market.MinNotional) < 0 { return nil, nil, fmt.Errorf("order amount %s is less than minNotional %s, order: %+v", quoteQuantity.String(), m.Market.MinNotional.String(), o) } @@ -299,7 +301,7 @@ func (m *SimplePriceMatching) newTradeFromOrder(order *types.Order, isMaker bool // BINANCE uses 0.1% for both maker and taker // MAX uses 0.050% for maker and 0.15% for taker var feeRate = m.getFeeRate(isMaker) - var quoteQuantity = m.Market.TruncateQuantity(order.Quantity.Mul(price)) + var quoteQuantity = order.Quantity.Mul(price) var fee fixedpoint.Value var feeCurrency string diff --git a/pkg/types/market.go b/pkg/types/market.go index 665c286b0..1092b441e 100644 --- a/pkg/types/market.go +++ b/pkg/types/market.go @@ -100,8 +100,11 @@ func (m Market) IsDustQuantity(quantity, price fixedpoint.Value) bool { // TruncateQuantity uses the step size to truncate floating number, in order to avoid the rounding issue func (m Market) TruncateQuantity(quantity fixedpoint.Value) fixedpoint.Value { - stepRound := math.Pow10(-int(math.Log10(m.StepSize.Float64()))) - return fixedpoint.NewFromFloat(math.Trunc(quantity.Float64()*stepRound) / stepRound) + return fixedpoint.MustNewFromString(m.FormatQuantity(quantity)) +} + +func (m Market) TruncatePrice(price fixedpoint.Value) fixedpoint.Value { + return fixedpoint.MustNewFromString(m.FormatPrice(price)) } func (m Market) BaseCurrencyFormatter() *accounting.Accounting {