Wrap Precise into FtPecise

This commit is contained in:
Matthias 2022-08-10 11:26:06 +00:00
parent adc8ee88e2
commit e7cb1b7375
4 changed files with 41 additions and 30 deletions

View File

@ -16,7 +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 ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE, Precise, decimal_to_precision from ccxt import ROUND_DOWN, ROUND_UP, TICK_SIZE, TRUNCATE, decimal_to_precision
from pandas import DataFrame from pandas import DataFrame
from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, BuySell, from freqtrade.constants import (DEFAULT_AMOUNT_RESERVE_PERCENT, NON_OPEN_EXCHANGE_STATES, BuySell,
@ -32,6 +32,7 @@ from freqtrade.exchange.common import (API_FETCH_ORDER_RETRY_COUNT, BAD_EXCHANGE
retrier_async) retrier_async)
from freqtrade.misc import chunks, deep_merge_dicts, safe_value_fallback2 from freqtrade.misc import chunks, deep_merge_dicts, safe_value_fallback2
from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist from freqtrade.plugins.pairlist.pairlist_helpers import expand_pairlist
from freqtrade.util import FtPrecise
CcxtModuleType = Any CcxtModuleType = Any
@ -708,10 +709,10 @@ class Exchange:
# counting_mode=self.precisionMode, # counting_mode=self.precisionMode,
# )) # ))
if self.precisionMode == TICK_SIZE: if self.precisionMode == TICK_SIZE:
precision = Precise(str(self.markets[pair]['precision']['price'])) precision = FtPrecise(str(self.markets[pair]['precision']['price']))
price_str = Precise(str(price)) price_str = FtPrecise(str(price))
missing = price_str % precision missing = price_str % precision
if not missing == Precise("0"): if not missing == FtPrecise("0"):
price = round(float(str(price_str - missing + precision)), 14) price = round(float(str(price_str - missing + precision)), 14)
else: else:
symbol_prec = self.markets[pair]['precision']['price'] symbol_prec = self.markets[pair]['precision']['price']

View File

@ -1,2 +1,3 @@
# flake8: noqa: F401 # flake8: noqa: F401
from freqtrade.util.ft_precise import FtPrecise
from freqtrade.util.periodic_cache import PeriodicCache from freqtrade.util.periodic_cache import PeriodicCache

View File

@ -0,0 +1,9 @@
"""
Slim wrapper around ccxt's Precise (string math)
To have imports from freqtrade
"""
from ccxt import Precise
class FtPrecise(Precise):
pass

View File

@ -1,14 +1,14 @@
from ccxt import Precise from freqtrade.util import FtPrecise
ws = Precise('-1.123e-6') ws = FtPrecise('-1.123e-6')
ws = Precise('-1.123e-6') ws = FtPrecise('-1.123e-6')
xs = Precise('0.00000002') xs = FtPrecise('0.00000002')
ys = Precise('69696900000') ys = FtPrecise('69696900000')
zs = Precise('0') zs = FtPrecise('0')
def test_precise(): def test_FtPrecise():
assert ys * xs == '1393.938' assert ys * xs == '1393.938'
assert xs * ys == '1393.938' assert xs * ys == '1393.938'
@ -45,31 +45,31 @@ def test_precise():
assert xs + zs == '0.00000002' assert xs + zs == '0.00000002'
assert ys + zs == '69696900000' assert ys + zs == '69696900000'
assert abs(Precise('-500.1')) == '500.1' assert abs(FtPrecise('-500.1')) == '500.1'
assert abs(Precise('213')) == '213' assert abs(FtPrecise('213')) == '213'
assert abs(Precise('-500.1')) == '500.1' assert abs(FtPrecise('-500.1')) == '500.1'
assert -Precise('213') == '-213' assert -FtPrecise('213') == '-213'
assert Precise('10.1') % Precise('0.5') == '0.1' assert FtPrecise('10.1') % FtPrecise('0.5') == '0.1'
assert Precise('5550') % Precise('120') == '30' assert FtPrecise('5550') % FtPrecise('120') == '30'
assert Precise('-0.0') == Precise('0') assert FtPrecise('-0.0') == FtPrecise('0')
assert Precise('5.534000') == Precise('5.5340') assert FtPrecise('5.534000') == FtPrecise('5.5340')
assert min(Precise('-3.1415'), Precise('-2')) == '-3.1415' assert min(FtPrecise('-3.1415'), FtPrecise('-2')) == '-3.1415'
assert max(Precise('3.1415'), Precise('-2')) == '3.1415' assert max(FtPrecise('3.1415'), FtPrecise('-2')) == '3.1415'
assert Precise('2') > Precise('1.2345') assert FtPrecise('2') > FtPrecise('1.2345')
assert not Precise('-3.1415') > Precise('-2') assert not FtPrecise('-3.1415') > FtPrecise('-2')
assert not Precise('3.1415') > Precise('3.1415') assert not FtPrecise('3.1415') > FtPrecise('3.1415')
assert Precise.string_gt('3.14150000000000000000001', '3.1415') assert FtPrecise.string_gt('3.14150000000000000000001', '3.1415')
assert Precise('3.1415') >= Precise('3.1415') assert FtPrecise('3.1415') >= FtPrecise('3.1415')
assert Precise('3.14150000000000000000001') >= Precise('3.1415') assert FtPrecise('3.14150000000000000000001') >= FtPrecise('3.1415')
assert not Precise('3.1415') < Precise('3.1415') assert not FtPrecise('3.1415') < FtPrecise('3.1415')
assert Precise('3.1415') <= Precise('3.1415') assert FtPrecise('3.1415') <= FtPrecise('3.1415')
assert Precise('3.1415') <= Precise('3.14150000000000000000001') assert FtPrecise('3.1415') <= FtPrecise('3.14150000000000000000001')