From f236586183dc642d9c69fc0a6efb935d09a0535c Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Mon, 22 Jul 2024 23:05:30 +0200 Subject: [PATCH] Load configuration from json --- crypto_seller/__init__.py | 12 ++++++------ crypto_seller/__main__.py | 22 ++++++++++++++-------- test/test_auto_sell.py | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index 7fec5a0..e35dc77 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -86,7 +86,6 @@ import logging import random from collections.abc import Callable from decimal import Decimal -from pathlib import Path from typing import Any import fin_defs @@ -99,6 +98,7 @@ logger = logging.getLogger(__name__) ################################################################################ # Main code # + @dataclasses.dataclass(frozen=True) class AutoSellConfig: """Configuration for `run_auto_sell`. @@ -143,19 +143,19 @@ class AutoSellRunResults: def total_input_amount(self) -> Decimal: """Total amount of assets sold.""" - return sum(o.input_amount for o in self.order_details) + return sum((o.input_amount for o in self.order_details), start=Decimal(0)) def total_output_amount(self) -> Decimal: """Total amount of assets "bought".""" - return sum(o.output_amount for o in self.order_details) + return sum((o.output_amount for o in self.order_details), start=Decimal(0)) -def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: +def sample_from_range(rng: random.Random, rang: tuple[Any, Any]) -> Any: """Samples uniformly from the given range.""" multiplier: float | Decimal = rng.random() - if isinstance(range[0], Decimal): + if isinstance(rang[0], Decimal): multiplier = Decimal(multiplier) - return range[0] + (range[1] - range[0]) * multiplier + return rang[0] + (rang[1] - rang[0]) * multiplier def run_auto_sell(config: AutoSellConfig) -> AutoSellRunResults: diff --git a/crypto_seller/__main__.py b/crypto_seller/__main__.py index 43df3fa..4c5c32f 100644 --- a/crypto_seller/__main__.py +++ b/crypto_seller/__main__.py @@ -1,7 +1,7 @@ import argparse import datetime -import logging import json +import logging import logging.handlers import time from decimal import Decimal @@ -31,6 +31,7 @@ PATH_TRADES_FILE = PATH_OUTPUT / 'trades.csv' ################################################################################ # Application Setup # + def setup_logging(): """Enables logging for the terminal and to a log file.""" PATH_LOG_FILE.parent.mkdir(parents=True, exist_ok=True) @@ -45,7 +46,8 @@ def setup_logging(): stream_handler = logging.StreamHandler() try: import logging_color - stream_handler = logging_color.ColorStreamHandler() + + stream_handler = logging_color.ColorStreamHandler() except ImportError: pass @@ -67,17 +69,21 @@ def load_config(config_path: Path) -> AutoSellConfig: kucoin_secret=config.KUCOIN_SECRET, kucoin_key=config.KUCOIN_KEY, kucoin_pass=config.KUCOIN_PASS, - allow_trades = True, + allow_trades=True, ) with open(config_path) as f: json_config = json.load(f) return AutoSellConfig( - input_amount_range=(Decimal(json_config['input_amount_low']), - Decimal(json_config['input_amount_high'])), - interval_range=(datetime.timedelta(minutes=json_config['interval_minutes_low']), - datetime.timedelta(minutes=json_config['interval_minutes_high'])), + input_amount_range=( + Decimal(json_config['input_amount_low']), + Decimal(json_config['input_amount_high']), + ), + interval_range=( + datetime.timedelta(minutes=json_config['interval_minutes_low']), + datetime.timedelta(minutes=json_config['interval_minutes_high']), + ), input_asset=fin_defs.WELL_KNOWN_SYMBOLS[json_config['input_asset']], output_asset=fin_defs.WELL_KNOWN_SYMBOLS[json_config['output_asset']], exit_when_empty=True, @@ -89,7 +95,7 @@ def load_config(config_path: Path) -> AutoSellConfig: def parse_args(): parser = argparse.ArgumentParser('crypto_seller', description=CLI_DESCRIPTION) - parser.add_argument('--config', type=Path, dest="config_file", required=True) + parser.add_argument('--config', type=Path, dest='config_file', required=True) return parser.parse_args() diff --git a/test/test_auto_sell.py b/test/test_auto_sell.py index 7b8ecb1..64c6075 100644 --- a/test/test_auto_sell.py +++ b/test/test_auto_sell.py @@ -33,7 +33,7 @@ class SellerMock(fin_depo.data.DepoFetcher): executed_time = datetime.datetime.now(tz=datetime.UTC) return fin_depo.data.TradeOrderDetails( - executed_time = executed_time, + executed_time=executed_time, input_asset=input_asset, input_amount=input_amount, output_asset=output_asset,