1
0

Load configuration from json

This commit is contained in:
Jon Michael Aanes 2024-07-22 23:05:30 +02:00
parent 5e6b88b78a
commit f236586183
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 21 additions and 15 deletions

View File

@ -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:

View File

@ -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,6 +46,7 @@ def setup_logging():
stream_handler = logging.StreamHandler()
try:
import logging_color
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()

View File

@ -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,