Source code for mlthon.basics.tick_format

import decimal
import math
from decimal import Decimal
from enum import Enum

from mlthon.basics.price import Price
from mlthon.basics.qty import Qty


[docs]class TickUnit(Enum): # All int values are expressed in number of satoshis Satoshi = 1 # 1 satoshi is 1 Satoshi Cent = 1000000 # 1 cent is 1000000 Satoshis Lot = 100000000 # 1 full lot is 100000000 Satoshis
[docs]class TickFormat: @staticmethod def __float_to_ticked_decimal(float_val: float, rounding_func, tick_size: int, tick_unit: TickUnit) -> Decimal: satoshi_factor = 1000000000 raw_val = int(float_val * satoshi_factor) raw_tick = tick_unit.value * tick_size * 10 val_in_num_ticks = int(rounding_func(raw_val / float(raw_tick))) rounded_raw_val = val_in_num_ticks * raw_tick test = Decimal(rounded_raw_val) / Decimal(satoshi_factor) return test @staticmethod def __decimal_to_ticked_decimal(decimal_val: Decimal, rounding_mode, tick_size: int, tick_unit: TickUnit) -> Decimal: satoshi_factor = Decimal(1000000000) raw_val = decimal_val * satoshi_factor raw_tick = tick_unit.value * tick_size * 10 val_in_num_ticks = (raw_val / raw_tick).quantize(Decimal('1'), rounding=rounding_mode) rounded_raw_val = val_in_num_ticks * raw_tick test = Decimal(rounded_raw_val) / Decimal(satoshi_factor) return test
[docs] @staticmethod def format_price_up(price: Price, tick_size: int = 50, tick_unit: TickUnit = TickUnit.Cent) -> Price: return Price(TickFormat.__decimal_to_ticked_decimal(price.raw_value_, decimal.ROUND_UP, tick_size, tick_unit))
[docs] @staticmethod def format_price_down(price: Price, tick_size: int = 50, tick_unit: TickUnit = TickUnit.Cent) -> Price: return Price(TickFormat.__decimal_to_ticked_decimal(price.raw_value_, decimal.ROUND_DOWN, tick_size, tick_unit))
[docs] @staticmethod def format_qty_up(qty: Qty, tick_size: int = 1, tick_unit: TickUnit = TickUnit.Satoshi) -> Qty: return Qty(TickFormat.__decimal_to_ticked_decimal(qty.raw_value_, decimal.ROUND_UP, tick_size, tick_unit))
[docs] @staticmethod def format_qty_down(qty: Qty, tick_size: int = 1, tick_unit: TickUnit = TickUnit.Satoshi) -> Qty: return Qty(TickFormat.__decimal_to_ticked_decimal(qty.raw_value_, decimal.ROUND_DOWN, tick_size, tick_unit))
[docs] @staticmethod def price_up_from_float(float_price: float, tick_size: int = 50, tick_unit: TickUnit = TickUnit.Cent) -> Price: return Price(TickFormat.__float_to_ticked_decimal(float_price, math.ceil, tick_size, tick_unit))
[docs] @staticmethod def price_down_from_float(float_price: float, tick_size: int = 50, tick_unit: TickUnit = TickUnit.Cent) -> Price: return Price(TickFormat.__float_to_ticked_decimal(float_price, math.floor, tick_size, tick_unit))
[docs] @staticmethod def qty_up_from_float(float_qty: float, tick_size: int = 1, tick_unit: TickUnit = TickUnit.Satoshi) -> Qty: return Qty(TickFormat.__float_to_ticked_decimal(float_qty, math.ceil, tick_size, tick_unit))
[docs] @staticmethod def qty_down_from_float(float_qty: float, tick_size: int = 1, tick_unit: TickUnit = TickUnit.Satoshi) -> Qty: return Qty(TickFormat.__float_to_ticked_decimal(float_qty, math.floor, tick_size, tick_unit))