1
0
crypto-seller/crypto_seller/__main__.py

97 lines
2.4 KiB
Python
Raw Normal View History

2024-07-22 13:48:55 +00:00
import argparse
2024-07-17 20:21:58 +00:00
import datetime
import logging
import logging.handlers
2024-07-22 11:31:53 +00:00
import time
2024-07-22 11:36:52 +00:00
from decimal import Decimal
2024-07-22 13:48:55 +00:00
from pathlib import Path
2024-07-17 20:21:58 +00:00
import fin_defs
2024-07-22 12:53:48 +00:00
import fin_depo
2024-07-17 20:21:58 +00:00
2024-07-22 13:48:55 +00:00
from . import (
AutoSellConfig,
config,
log_results,
order_csv,
run_auto_sell,
)
from . import logger as module_logger
2024-07-17 20:21:58 +00:00
2024-07-22 19:48:25 +00:00
try:
import logging_color
logging_color.monkey_patch()
except ImportError:
pass
2024-07-17 20:21:58 +00:00
logger = logging.getLogger(__name__)
2024-07-22 13:48:55 +00:00
################################################################################
# Constants #
PATH_OUTPUT = Path('./output').absolute()
PATH_LOG_FILE = PATH_OUTPUT / 'log.txt'
PATH_TRADES_FILE = PATH_OUTPUT / 'trades.csv'
################################################################################
# Application Setup #
2024-07-22 11:36:52 +00:00
2024-07-22 11:21:41 +00:00
def setup_logging():
2024-07-22 12:53:48 +00:00
"""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(
2024-07-22 11:36:52 +00:00
'%(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')
2024-07-22 13:48:55 +00:00
2024-07-22 12:53:48 +00:00
CLI_DESCRIPTION = """
Sells financial assets from an online account.
""".strip()
2024-07-22 13:48:55 +00:00
2024-07-22 12:53:48 +00:00
def parse_args():
2024-07-22 13:48:55 +00:00
parser = argparse.ArgumentParser('crypto_seller', description=CLI_DESCRIPTION)
2024-07-22 12:53:48 +00:00
return parser.parse_args()
2024-07-22 11:36:52 +00:00
2024-07-22 13:48:55 +00:00
2024-07-22 11:21:41 +00:00
def main():
2024-07-22 12:53:48 +00:00
"""Initializes the program."""
2024-07-22 11:21:41 +00:00
setup_logging()
2024-07-22 12:53:48 +00:00
args = parse_args()
2024-07-17 20:21:58 +00:00
logger.info('Initializing crypto_seller')
2024-07-22 11:21:41 +00:00
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,
2024-07-22 11:36:52 +00:00
seller=seller_backend,
sleep=time.sleep,
2024-07-22 13:48:55 +00:00
log_order_to_csv=order_csv.CsvFileLogger(PATH_TRADES_FILE),
2024-07-17 20:21:58 +00:00
)
2024-07-22 12:12:05 +00:00
results = run_auto_sell(auto_sell_config)
logging.info('Sell-offs complete')
log_results(results)
2024-07-17 20:21:58 +00:00
2024-07-17 20:21:58 +00:00
if __name__ == '__main__':
main()