fix: truncate price in backtest, don't truncate amount, add TruncatePrice function

This commit is contained in:
zenix 2022-08-18 11:54:06 +09:00
parent 66a2f55f9a
commit e1c2ed40ff
2 changed files with 9 additions and 4 deletions

View File

@ -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

View File

@ -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 {