1
0

Parse configuration from json

This commit is contained in:
Jon Michael Aanes 2024-07-22 23:02:25 +02:00
parent 6092550153
commit 5e6b88b78a
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
5 changed files with 62 additions and 26 deletions

3
config/README.md Normal file
View File

@ -0,0 +1,3 @@
# Config Examples
This directory contains configuration examples.

View File

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

View File

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

View File

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

View File

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