From 97369e0b742d0d16714e94adc5df3c413b770bb1 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 2 Sep 2024 20:26:17 +0200 Subject: [PATCH] Quantize correctly --- crypto_seller/__init__.py | 7 +++++-- test/test_auto_sell.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index d3dd1ee..2622a38 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -119,7 +119,7 @@ import datetime import logging import random from collections.abc import Callable -from decimal import Decimal +from decimal import Decimal, ROUND_DOWN from typing import Any import fin_defs @@ -192,6 +192,9 @@ def sample_from_range(rng: random.Random, rang: tuple[Any, Any]) -> Any: return rang[0] + (rang[1] - rang[0]) * multiplier +ROUND_TO_WHOLE = Decimal('1') + + def run_auto_sell(config: AutoSellConfig) -> AutoSellRunResults: """Executes the sell-off. @@ -214,7 +217,7 @@ def run_auto_sell(config: AutoSellConfig) -> AutoSellRunResults: if input_amount_available > 0: amount_to_sell = sample_from_range(rng, config.input_amount_range) amount_to_sell = min(input_amount_available, amount_to_sell) - amount_to_sell = int(amount_to_sell + Decimal('0.5')) + amount_to_sell = amount_to_sell.quantize(ROUND_TO_WHOLE, rounding=ROUND_DOWN) logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset) order_details = config.seller.place_market_order( diff --git a/test/test_auto_sell.py b/test/test_auto_sell.py index 64c6075..e930211 100644 --- a/test/test_auto_sell.py +++ b/test/test_auto_sell.py @@ -26,7 +26,7 @@ class SellerMock(fin_depo.data.DepoFetcher): input_asset: fin_defs.Asset, input_amount: Decimal, output_asset: fin_defs.Asset, - ) -> fin_depo.data.Depo: + ) -> fin_depo.data.TradeOrderDetails: assert input_amount <= self.amount_left, 'Attempt to sell too much' self.amount_left -= input_amount @@ -40,7 +40,7 @@ class SellerMock(fin_depo.data.DepoFetcher): output_amount=input_amount, fee_asset=input_asset, fee_amount=Decimal(0), - order_id=10000000 - self.amount_left, + order_id=Decimal(10000000 - self.amount_left), raw_order_details={'TEST': 1, 'DATA': 2}, )