Fix and improve Tick size ROUND_DOWN logic

This commit is contained in:
Matthias
2023-08-30 08:21:02 +00:00
parent 3a6c00dbaf
commit bfc2c70b44
2 changed files with 8 additions and 5 deletions

View File

@@ -279,15 +279,15 @@ def price_to_precision(
))
if precisionMode == TICK_SIZE:
if rounding_mode == ROUND:
ticks = price / price_precision
rounded_ticks = round(ticks)
return rounded_ticks * price_precision
precision = FtPrecise(price_precision)
price_str = FtPrecise(price)
missing = price_str % precision
if not missing == FtPrecise("0"):
return round(float(str(price_str - missing + precision)), 14)
if rounding_mode == ROUND_UP:
res = price_str - missing + precision
elif rounding_mode == ROUND_DOWN:
res = price_str - missing
return round(float(str(res)), 14)
return price
elif precisionMode in (SIGNIFICANT_DIGITS, DECIMAL_PLACES):

View File

@@ -247,11 +247,14 @@ def test_amount_to_precision(amount, precision_mode, precision, expected,):
(2.34559, TICK_SIZE, 0.001, 2.346, ROUND_UP),
(2.9999, TICK_SIZE, 0.001, 3.000, ROUND_UP),
(2.9909, TICK_SIZE, 0.001, 2.991, ROUND_UP),
(2.9909, TICK_SIZE, 0.001, 2.990, ROUND_DOWN),
(2.9909, TICK_SIZE, 0.005, 2.995, ROUND_UP),
(2.9973, TICK_SIZE, 0.005, 3.0, ROUND_UP),
(2.9977, TICK_SIZE, 0.005, 3.0, ROUND_UP),
(234.43, TICK_SIZE, 0.5, 234.5, ROUND_UP),
(234.43, TICK_SIZE, 0.5, 234.0, ROUND_DOWN),
(234.53, TICK_SIZE, 0.5, 235.0, ROUND_UP),
(234.53, TICK_SIZE, 0.5, 234.5, ROUND_DOWN),
(0.891534, TICK_SIZE, 0.0001, 0.8916, ROUND_UP),
(64968.89, TICK_SIZE, 0.01, 64968.89, ROUND_UP),
(0.000000003483, TICK_SIZE, 1e-12, 0.000000003483, ROUND_UP),