1
0
crypto-seller/crypto_seller/__main__.py

91 lines
2.3 KiB
Python

import argparse
import datetime
import logging
import logging.handlers
import time
from decimal import Decimal
from pathlib import Path
import fin_defs
import fin_depo
from . import (
AutoSellConfig,
config,
log_results,
order_csv,
run_auto_sell,
)
from . import logger as module_logger
logger = logging.getLogger(__name__)
################################################################################
# Constants #
PATH_OUTPUT = Path('./output').absolute()
PATH_LOG_FILE = PATH_OUTPUT / 'log.txt'
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)
file_handler = logging.handlers.WatchedFileHandler(filename=PATH_LOG_FILE)
file_handler.setFormatter(
logging.Formatter(
'%(levelname)s:%(asctime)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
),
)
logging.basicConfig(handlers=[logging.StreamHandler(), file_handler])
logger.setLevel('INFO')
module_logger.setLevel('INFO')
CLI_DESCRIPTION = """
Sells financial assets from an online account.
""".strip()
def parse_args():
parser = argparse.ArgumentParser('crypto_seller', description=CLI_DESCRIPTION)
return parser.parse_args()
def main():
"""Initializes the program."""
setup_logging()
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),
)
results = run_auto_sell(auto_sell_config)
logging.info('Sell-offs complete')
log_results(results)
if __name__ == '__main__':
main()