From 9709c9b68aff72a11df0b90aa660c4e40bc47465 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Wed, 4 Sep 2024 17:54:22 +0200 Subject: [PATCH] Ruff --- crypto_seller/__init__.py | 38 ++++++++++++++++++++++++++++++-------- crypto_seller/__main__.py | 33 ++++++++++++++++++++++++--------- test/test_auto_sell.py | 1 - 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index 9861ed0..cbbd0af 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -125,7 +125,7 @@ import datetime import logging import random from collections.abc import Callable -from decimal import Decimal, ROUND_DOWN +from decimal import ROUND_DOWN, Decimal from typing import Any import fin_defs @@ -201,7 +201,10 @@ def sample_from_range(rng: random.Random, rang: tuple[Any, Any]) -> Any: ROUND_TO_WHOLE = Decimal('1') -def run_auto_sell(config: AutoSellConfig, initial_rounds_to_skip:int=0) -> AutoSellRunResults: +def run_auto_sell( + config: AutoSellConfig, + initial_rounds_to_skip: int = 0, +) -> AutoSellRunResults: """Executes the sell-off. Sell-offs are performed in rounds of sizes and with intervals randomly @@ -218,7 +221,11 @@ def run_auto_sell(config: AutoSellConfig, initial_rounds_to_skip:int=0) -> AutoS input_amount_available = config.seller.get_depo().get_amount_of_asset( config.input_asset, ) - logger.info('Currently own %s %s', input_amount_available, config.input_asset.raw_short_name()) + logger.info( + 'Currently own %s %s', + input_amount_available, + config.input_asset.raw_short_name(), + ) if initial_rounds_to_skip > 0: initial_rounds_to_skip -= 1 @@ -226,7 +233,10 @@ def run_auto_sell(config: AutoSellConfig, initial_rounds_to_skip:int=0) -> AutoS elif 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 = amount_to_sell.quantize(ROUND_TO_WHOLE, rounding=ROUND_DOWN) + 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( @@ -271,8 +281,12 @@ def log_estimates(config: AutoSellConfig): expected_num_sell_offs = current_balance / average_amount expected_duration = average_sleep * float(expected_num_sell_offs) - fastest_duration = config.interval_range[0] * float(current_balance / config.input_amount_range[1]) - slowest_duration = config.interval_range[1] * float(current_balance / config.input_amount_range[0]) + fastest_duration = config.interval_range[0] * float( + current_balance / config.input_amount_range[1], + ) + slowest_duration = config.interval_range[1] * float( + current_balance / config.input_amount_range[0], + ) logger.info('Welcome to crypto seller!') config.sleep(1) @@ -283,11 +297,19 @@ def log_estimates(config: AutoSellConfig): config.sleep(1) logger.info('') config.sleep(1) - logger.info('- Current balance: %s %s', current_balance, config.input_asset.raw_short_name()) + logger.info( + '- Current balance: %s %s', + current_balance, + config.input_asset.raw_short_name(), + ) config.sleep(1) logger.info('- Average sleep: %s seconds', average_sleep) config.sleep(1) - logger.info('- Average amount: %s %s', average_amount, config.input_asset.raw_short_name()) + logger.info( + '- Average amount: %s %s', + average_amount, + config.input_asset.raw_short_name(), + ) config.sleep(1) logger.info('- Expected counts: %s', expected_num_sell_offs) config.sleep(1) diff --git a/crypto_seller/__main__.py b/crypto_seller/__main__.py index 2f7956c..64ef928 100644 --- a/crypto_seller/__main__.py +++ b/crypto_seller/__main__.py @@ -68,6 +68,7 @@ Website : https://gitfub.space/Jmaa/crypto-seller License : MIT License (see website for full text) """.strip() + def load_config(config_path: Path) -> AutoSellConfig: logger.info('Loading configuration') @@ -102,14 +103,25 @@ def load_config(config_path: Path) -> AutoSellConfig: def parse_args(): - parser = argparse.ArgumentParser(prog='crypto_seller', - description=CLI_DESCRIPTION, - formatter_class=argparse.RawDescriptionHelpFormatter, - epilog=CLI_EPILOG) - parser.add_argument('--config', type=Path, dest='config_file', - required=True, help='Trading configuration file') - parser.add_argument('--wait-before-first', action='store_true', dest='wait_before_first', - help='Skip the first sell-off round, and wait for the next.') + parser = argparse.ArgumentParser( + prog='crypto_seller', + description=CLI_DESCRIPTION, + formatter_class=argparse.RawDescriptionHelpFormatter, + epilog=CLI_EPILOG, + ) + parser.add_argument( + '--config', + type=Path, + dest='config_file', + required=True, + help='Trading configuration file', + ) + parser.add_argument( + '--wait-before-first', + action='store_true', + dest='wait_before_first', + help='Skip the first sell-off round, and wait for the next.', + ) return parser.parse_args() @@ -127,7 +139,10 @@ def main(): log_estimates(auto_sell_config) # Run auto sell - results = run_auto_sell(auto_sell_config, initial_rounds_to_skip=args.wait_before_first and 1 or 0) + results = run_auto_sell( + auto_sell_config, + initial_rounds_to_skip=args.wait_before_first and 1 or 0, + ) # Display results logging.info('Sell-offs complete') diff --git a/test/test_auto_sell.py b/test/test_auto_sell.py index 5d4ea02..afc8a92 100644 --- a/test/test_auto_sell.py +++ b/test/test_auto_sell.py @@ -8,7 +8,6 @@ import fin_depo import crypto_seller import crypto_seller.order_csv -import pytest class SellerMock(fin_depo.data.DepoFetcher): def __init__(self, asset: fin_defs.Asset, initial_amount: Decimal):