diff --git a/config/README.md b/config/README.md new file mode 100644 index 0000000..c432e62 --- /dev/null +++ b/config/README.md @@ -0,0 +1,3 @@ +# Config Examples + +This directory contains configuration examples. diff --git a/config/example_btc_monthly_savings.json b/config/example_btc_monthly_savings.json new file mode 100644 index 0000000..64104d6 --- /dev/null +++ b/config/example_btc_monthly_savings.json @@ -0,0 +1,9 @@ +{ + "__comment": "This configuration buys 100 USD worth Bitcoin ever 30 days.", + "input_asset": "USDT", + "output_asset": "BTC", + "input_amount_low": 100, + "input_amount_high": 100, + "interval_minutes_low": 43200, + "interval_minutes_high": 43200 +} diff --git a/config/example_mpc_for_usdt_mini.json b/config/example_mpc_for_usdt_mini.json new file mode 100644 index 0000000..7e6a3aa --- /dev/null +++ b/config/example_mpc_for_usdt_mini.json @@ -0,0 +1,10 @@ +{ + "__comment1": "This configuration sells a small amount of MPC tokens", + "__comment2": "for USDT every minute. Mainly for testing", + "input_asset": "MPC", + "output_asset": "USDT", + "input_amount_low": 0.5, + "input_amount_high": 1.5, + "interval_minutes_low": 1, + "interval_minutes_high": 2 +} diff --git a/crypto_seller/__init__.py b/crypto_seller/__init__.py index 2772ced..7fec5a0 100644 --- a/crypto_seller/__init__.py +++ b/crypto_seller/__init__.py @@ -65,13 +65,13 @@ most mature on the danish market, and does support KuCoin. ## TODO -- [ ] Parse configuration from json. - [ ] Ensure that a failure during selling results in a safe winding down of the system. * Catch runtime errors when selling * Show errors to log. * Stop loop and exit with results, and error indicator. - [ ] Document configuration - [ ] Document code auditing +- [X] Parse configuration from json. - [X] Ensure sell time is included in order details - [X] Log all trades to CSV file. - [X] Collect information during the run and output after run diff --git a/crypto_seller/__main__.py b/crypto_seller/__main__.py index ec7dddd..43df3fa 100644 --- a/crypto_seller/__main__.py +++ b/crypto_seller/__main__.py @@ -1,6 +1,7 @@ import argparse import datetime import logging +import json import logging.handlers import time from decimal import Decimal @@ -18,12 +19,6 @@ from . import ( ) from . import logger as module_logger -try: - import logging_color - logging_color.monkey_patch() -except ImportError: - pass - logger = logging.getLogger(__name__) ################################################################################ @@ -47,7 +42,14 @@ def setup_logging(): ), ) - logging.basicConfig(handlers=[logging.StreamHandler(), file_handler]) + stream_handler = logging.StreamHandler() + try: + import logging_color + stream_handler = logging_color.ColorStreamHandler() + except ImportError: + pass + + logging.basicConfig(handlers=[stream_handler, file_handler]) logger.setLevel('INFO') module_logger.setLevel('INFO') @@ -58,8 +60,36 @@ Sells financial assets from an online account. """.strip() +def load_config(config_path: Path) -> AutoSellConfig: + logger.info('Loading configuration') + + seller_backend = fin_depo.defi_kucoin.KucoinDepoFetcher( + kucoin_secret=config.KUCOIN_SECRET, + kucoin_key=config.KUCOIN_KEY, + kucoin_pass=config.KUCOIN_PASS, + 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_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, + seller=seller_backend, + sleep=time.sleep, + log_order_to_csv=order_csv.CsvFileLogger(PATH_TRADES_FILE), + ) + + def parse_args(): parser = argparse.ArgumentParser('crypto_seller', description=CLI_DESCRIPTION) + parser.add_argument('--config', type=Path, dest="config_file", required=True) return parser.parse_args() @@ -67,26 +97,10 @@ def main(): """Initializes the program.""" setup_logging() + logger.info('Initializing crypto_seller') args = parse_args() - logger.info('Initializing crypto_seller') - - seller_backend = fin_depo.defi_kucoin.KucoinDepoFetcher( - kucoin_secret=config.KUCOIN_SECRET, - kucoin_key=config.KUCOIN_KEY, - kucoin_pass=config.KUCOIN_PASS, - ) - - auto_sell_config = AutoSellConfig( - input_amount_range=(Decimal('0.5'), Decimal('1')), - interval_range=(datetime.timedelta(seconds=2), datetime.timedelta(seconds=6)), - input_asset=fin_defs.MPC, - output_asset=fin_defs.USDT, - exit_when_empty=True, - seller=seller_backend, - sleep=time.sleep, - log_order_to_csv=order_csv.CsvFileLogger(PATH_TRADES_FILE), - ) + auto_sell_config = load_config(args.config_file) results = run_auto_sell(auto_sell_config) logging.info('Sell-offs complete') log_results(results)