def format_amount(exchange, symbol, amount): precision = exchange.markets[symbol]["precision"]["amount"] amount = float( dtp.decimal_to_precision(amount, rounding_mode=dtp.TRUNCATE, precision=precision, counting_mode=exchange.precisionMode, padding_mode=dtp.NO_PADDING)) return ("{:,.%df}" % Utils.num_of_decimal_places( exchange, amount, precision)).format(amount)
def amount_to_precision(self, pair: str, amount: float) -> float: ''' Returns the amount to buy or sell to a precision the Exchange accepts Reimplementation of ccxt internal methods - ensuring we can test the result is correct based on our definitions. ''' if self.markets[pair]['precision']['amount']: amount = float(decimal_to_precision(amount, rounding_mode=TRUNCATE, precision=self.markets[pair]['precision']['amount'], counting_mode=self.precisionMode, )) return amount
def format_price(exchange, symbol, price): precision = 8 if ( exchange.markets[symbol]["precision"]["price"] is None if "price" in exchange.markets[symbol]["precision"] else True) else exchange.markets[symbol]["precision"]["price"] price = float( dtp.decimal_to_precision(price, rounding_mode=dtp.ROUND, precision=precision, counting_mode=exchange.precisionMode, padding_mode=dtp.PAD_WITH_ZERO)) return ("{:,.%df}" % Utils.num_of_decimal_places( exchange, price, precision)).format(price)
assert number_to_string(-7.0005e27) == '-7000500000000000000000000000' assert number_to_string(7.0005e27) == '7000500000000000000000000000' assert number_to_string(-7.9e27) == '-7900000000000000000000000000' assert number_to_string(7.9e27) == '7900000000000000000000000000' assert number_to_string(-12.345) == '-12.345' assert number_to_string(12.345) == '12.345' assert number_to_string(0) == '0' assert number_to_string(7.35946e21) == '7359460000000000000000' assert number_to_string(0.00000001) == '0.00000001' assert number_to_string(1e-7) == '0.0000001' assert number_to_string(-1e-7) == '-0.0000001' # ---------------------------------------------------------------------------- # testDecimalToPrecisionTruncationToNDigitsAfterDot assert decimal_to_precision('12.3456000', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456' assert decimal_to_precision('12.3456', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456' assert decimal_to_precision('12.3456', TRUNCATE, 4, DECIMAL_PLACES) == '12.3456' assert decimal_to_precision('12.3456', TRUNCATE, 3, DECIMAL_PLACES) == '12.345' assert decimal_to_precision('12.3456', TRUNCATE, 2, DECIMAL_PLACES) == '12.34' assert decimal_to_precision('12.3456', TRUNCATE, 1, DECIMAL_PLACES) == '12.3' assert decimal_to_precision('12.3456', TRUNCATE, 0, DECIMAL_PLACES) == '12' assert decimal_to_precision('0.0000001', TRUNCATE, 8, DECIMAL_PLACES) == '0.0000001' assert decimal_to_precision('0.00000001', TRUNCATE, 8, DECIMAL_PLACES) == '0.00000001' assert decimal_to_precision('0.000000000', TRUNCATE, 9, DECIMAL_PLACES, PAD_WITH_ZERO) == '0.000000000' assert decimal_to_precision('0.000000001', TRUNCATE, 9, DECIMAL_PLACES, PAD_WITH_ZERO) == '0.000000001' assert decimal_to_precision('12.3456', TRUNCATE, -1, DECIMAL_PLACES) == '10' assert decimal_to_precision('123.456', TRUNCATE, -1, DECIMAL_PLACES) == '120'
# ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- # number_to_string works, not supported in Python and PHP yet # assert(number_to_string(-7.9e-7) == '-0.0000007899999999999999') # assert(number_to_string( 7.9e-7) == '0.0000007899999999999999') # assert(number_to_string(-12.345) == '-12.345') # assert(number_to_string( 12.345) == '12.345') # assert(number_to_string(0) == '0') # ---------------------------------------------------------------------------- # testDecimalToPrecisionTruncationToNDigitsAfterDot assert (decimal_to_precision('12.3456000', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456') assert (decimal_to_precision('12.3456', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456') assert (decimal_to_precision('12.3456', TRUNCATE, 4, DECIMAL_PLACES) == '12.3456') assert (decimal_to_precision('12.3456', TRUNCATE, 3, DECIMAL_PLACES) == '12.345') assert (decimal_to_precision('12.3456', TRUNCATE, 2, DECIMAL_PLACES) == '12.34') assert (decimal_to_precision('12.3456', TRUNCATE, 1, DECIMAL_PLACES) == '12.3') assert (decimal_to_precision('12.3456', TRUNCATE, 0, DECIMAL_PLACES) == '12') assert (decimal_to_precision('0.0000001', TRUNCATE, 8, DECIMAL_PLACES) == '0.0000001') assert (decimal_to_precision('0.00000001', TRUNCATE, 8, DECIMAL_PLACES) == '0.00000001')
def format_amount(self, exchangeId, symbol, amount): exchange = self.exchanges[exchangeId].properties precision = exchange.markets.get(symbol, {}).get("precision", {}).get("amount", 8) return [dtp.decimal_to_precision(amount, rounding_mode=dtp.TRUNCATE, precision=precision, counting_mode=exchange.precisionMode, padding_mode=dtp.NO_PADDING).encode()]
def format_price(self, exchangeId, symbol, price): exchange = self.exchanges[exchangeId].properties precision = exchange.markets.get(symbol, {}).get("precision", {}).get("price", 8) return [dtp.decimal_to_precision(price, rounding_mode=dtp.ROUND, precision=precision, counting_mode=exchange.precisionMode, padding_mode=dtp.PAD_WITH_ZERO).encode()]
def round_down(self, number, precision): return decimal_to_precision(number_to_string(number), TRUNCATE, precision)
from ccxt.base.decimal_to_precision import decimal_to_precision # noqa F401 from ccxt.base.decimal_to_precision import TRUNCATE # noqa F401 from ccxt.base.decimal_to_precision import ROUND # noqa F401 from ccxt.base.decimal_to_precision import AFTER_DOT # noqa F401 from ccxt.base.decimal_to_precision import SIGNIFICANT_DIGITS # noqa F401 from ccxt.base.decimal_to_precision import PAD_WITH_ZERO # noqa F401 def equal(a, b): """I'm lazy""" if a != b: raise ValueError('{} does not equal {}'.format(a, b)) equal(decimal_to_precision('12.3456000', TRUNCATE, 20, AFTER_DOT), '12.3456') equal(decimal_to_precision('12.3456', TRUNCATE, 20, AFTER_DOT), '12.3456') equal(decimal_to_precision('12.3456', TRUNCATE, 4, AFTER_DOT), '12.3456') equal(decimal_to_precision('12.3456', TRUNCATE, 3, AFTER_DOT), '12.345') equal(decimal_to_precision('12.3456', TRUNCATE, 2, AFTER_DOT), '12.34') equal(decimal_to_precision('12.3456', TRUNCATE, 1, AFTER_DOT), '12.3') equal(decimal_to_precision('12.3456', TRUNCATE, 0, AFTER_DOT), '12') # equal(decimal_to_precision('12.3456', TRUNCATE, -1, AFTER_DOT), '10') # not yet supported # equal(decimal_to_precision('123.456', TRUNCATE, -2, AFTER_DOT), '120') # not yet supported # equal(decimal_to_precision('123.456', TRUNCATE, -3, AFTER_DOT), '100') # not yet supported # -------------------------------------------------------------------------------------------------------- equal(decimal_to_precision('0.000123456700', TRUNCATE, 20, SIGNIFICANT_DIGITS), '0.0001234567') equal(decimal_to_precision('0.0001234567', TRUNCATE, 20, SIGNIFICANT_DIGITS),
# ---------------------------------------------------------------------------- # ---------------------------------------------------------------------------- # number_to_string works, not supported in Python and PHP yet # assert(number_to_string(-7.9e-7) == '-0.0000007899999999999999') # assert(number_to_string( 7.9e-7) == '0.0000007899999999999999') # assert(number_to_string(-12.345) == '-12.345') # assert(number_to_string( 12.345) == '12.345') # assert(number_to_string(0) == '0') # ---------------------------------------------------------------------------- # testDecimalToPrecisionTruncationToNDigitsAfterDot assert(decimal_to_precision('12.3456000', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456') assert(decimal_to_precision('12.3456', TRUNCATE, 100, DECIMAL_PLACES) == '12.3456') assert(decimal_to_precision('12.3456', TRUNCATE, 4, DECIMAL_PLACES) == '12.3456') assert(decimal_to_precision('12.3456', TRUNCATE, 3, DECIMAL_PLACES) == '12.345') assert(decimal_to_precision('12.3456', TRUNCATE, 2, DECIMAL_PLACES) == '12.34') assert(decimal_to_precision('12.3456', TRUNCATE, 1, DECIMAL_PLACES) == '12.3') assert(decimal_to_precision('12.3456', TRUNCATE, 0, DECIMAL_PLACES) == '12') # assert(decimal_to_precision('12.3456', TRUNCATE, -1, DECIMAL_PLACES) == '10') # not yet supported # assert(decimal_to_precision('123.456', TRUNCATE, -2, DECIMAL_PLACES) == '120') # not yet supported # assert(decimal_to_precision('123.456', TRUNCATE, -3, DECIMAL_PLACES) == '100') # not yet supported assert(decimal_to_precision('0', TRUNCATE, 0, DECIMAL_PLACES) == '0') # ---------------------------------------------------------------------------- # testDecimalToPrecisionTruncationToNSignificantDigits