From ed004236ce8310a722b5737c5647ee4232a45068 Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 10 Aug 2022 11:29:04 +0000 Subject: [PATCH] Add float initializer to FtPrecise --- freqtrade/exchange/exchange.py | 4 ++-- freqtrade/util/ft_precise.py | 7 +++++-- tests/exchange/test_ccxt_precise.py | 5 +++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/freqtrade/exchange/exchange.py b/freqtrade/exchange/exchange.py index 2806a4f42..18598e92d 100644 --- a/freqtrade/exchange/exchange.py +++ b/freqtrade/exchange/exchange.py @@ -709,8 +709,8 @@ class Exchange: # counting_mode=self.precisionMode, # )) if self.precisionMode == TICK_SIZE: - precision = FtPrecise(str(self.markets[pair]['precision']['price'])) - price_str = FtPrecise(str(price)) + precision = FtPrecise(self.markets[pair]['precision']['price']) + price_str = FtPrecise(price) missing = price_str % precision if not missing == FtPrecise("0"): price = round(float(str(price_str - missing + precision)), 14) diff --git a/freqtrade/util/ft_precise.py b/freqtrade/util/ft_precise.py index 9c79552c2..aba0517a9 100644 --- a/freqtrade/util/ft_precise.py +++ b/freqtrade/util/ft_precise.py @@ -1,9 +1,12 @@ """ Slim wrapper around ccxt's Precise (string math) -To have imports from freqtrade +To have imports from freqtrade - and support float initializers """ from ccxt import Precise class FtPrecise(Precise): - pass + def __init__(self, number, decimals=None): + if not isinstance(number, str): + number = str(number) + super().__init__(number, decimals) diff --git a/tests/exchange/test_ccxt_precise.py b/tests/exchange/test_ccxt_precise.py index 770dde13c..8b599093f 100644 --- a/tests/exchange/test_ccxt_precise.py +++ b/tests/exchange/test_ccxt_precise.py @@ -73,3 +73,8 @@ def test_FtPrecise(): assert FtPrecise('3.1415') <= FtPrecise('3.1415') assert FtPrecise('3.1415') <= FtPrecise('3.14150000000000000000001') + + assert FtPrecise(213) == '213' + assert FtPrecise(-213) == '-213' + assert str(FtPrecise(-213)) == '-213' + assert FtPrecise(213.2) == '213.2'