Use ccxt's "precise" to do precise math

This commit is contained in:
Matthias 2022-03-30 07:10:46 +02:00
parent 528509f809
commit a793cf8f05
2 changed files with 7 additions and 4 deletions

View File

@ -16,6 +16,7 @@ import arrow
import ccxt import ccxt
import ccxt.async_support as ccxt_async import ccxt.async_support as ccxt_async
from cachetools import TTLCache from cachetools import TTLCache
from ccxt import Precise
from ccxt.base.decimal_to_precision import (ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE, from ccxt.base.decimal_to_precision import (ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE,
decimal_to_precision) decimal_to_precision)
from pandas import DataFrame from pandas import DataFrame
@ -704,10 +705,11 @@ class Exchange:
# counting_mode=self.precisionMode, # counting_mode=self.precisionMode,
# )) # ))
if self.precisionMode == TICK_SIZE: if self.precisionMode == TICK_SIZE:
precision = self.markets[pair]['precision']['price'] precision = Precise(str(self.markets[pair]['precision']['price']))
missing = price % precision price_str = Precise(str(price))
if missing != 0: missing = price_str.mod(precision)
price = round(price - missing + precision, 10) if not missing.equals(Precise("0")):
price = round(float(str(price_str.sub(missing).add(precision))), 14)
else: else:
symbol_prec = self.markets[pair]['precision']['price'] symbol_prec = self.markets[pair]['precision']['price']
big_price = price * pow(10, symbol_prec) big_price = price * pow(10, symbol_prec)

View File

@ -305,6 +305,7 @@ def test_amount_to_precision(
(234.53, 4, 0.5, 235.0), (234.53, 4, 0.5, 235.0),
(0.891534, 4, 0.0001, 0.8916), (0.891534, 4, 0.0001, 0.8916),
(64968.89, 4, 0.01, 64968.89), (64968.89, 4, 0.01, 64968.89),
(0.000000003483, 4, 1e-12, 0.000000003483),
]) ])
def test_price_to_precision(default_conf, mocker, price, precision_mode, precision, expected): def test_price_to_precision(default_conf, mocker, price, precision_mode, precision, expected):