"""# Automatic Crypto Seller. This is a wrapper about [`fin_depo`] for automatically individual financial assets a one way, by using repeated trades over a period. The primary motivation is for trading low-volume crypto assets slowly without affecting the price too much. Supported sites: - [KuCoin](https://www.kucoin.com/): Online crypto-currency exchange. ## Installation Install dependencies: ```shell pip install -r requirements.txt ``` ## Usage Configure the sell rate: TODO Run using from the top directory: ```shell python -m crypto_seller ``` The script will now automatically sell assets over time. The interval between sell-offs are randomized to avoid front-running, and the amounts are also randomized to avoid too consistent behaviour. The log will both be displayed in the shell, and be placed in a log file `output/log.txt`. Every sell-off will be logged to the `output/trades.csv` file, with as much information as possible. Keep both of these for tax purposes, if relevant. ## Auditing information The most relevant libraries for auditing are: - [`fin_depo`](https://gitfub.space/Jmaa/fin-depo): Library for programmatic fetching of depository assets and in some cases allows for order placement. This is the library that reads balances and places market orders. - [`fin_defs`](https://gitfub.space/Jmaa/fin-defs): Definitions of financial assets and instruments. Used by `fin_depo`. - [`python-kucoin`](https://python-kucoin.readthedocs.io/en/latest/) is used by `fin_depo` for providing KuCoin support. Todo ## Taxation Some parts of the world suffers from weird and draconian taxation rules on cryptocurrencies, so it helps to keep track of the relevant trading information. As mentioned above, keep (and backup) both the log file (`output/log.txt`) and the trades file (`output/trades.csv`). To help with tax reporting, it might be useful to sign up to tax oriented websites. For example, [CryptoSkat](https://cryptoskat.dk/) seems to be the most mature on the danish market, and does support KuCoin. """ __all__ = ['__version__', 'run_auto_sell'] import dataclasses import datetime import logging import random import time from decimal import Decimal from pathlib import Path from typing import Any import fin_defs import fin_depo from ._version import __version__ logger = logging.getLogger(__name__) ################################################################################ # Constants # PATH_OUTPUT = Path('./output').absolute() PATH_LOG_FILE = PATH_OUTPUT / 'log.txt' PATH_TRADES_FILE = PATH_OUTPUT / 'trades.csv' ################################################################################ # Main code # @dataclasses.dataclass class AutoSellConfig: interval_range: tuple[datetime.timedelta, datetime.timedelta] input_asset: fin_defs.Asset input_amount_range: tuple[Decimal, Decimal] output_asset: fin_defs.Asset exit_when_empty: bool = True def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: multiplier = rng.random() if isinstance(range[0], Decimal): multiplier = Decimal(multiplier) return range[0] + (range[1] - range[0]) * multiplier def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSellConfig): rng = random.SystemRandom() while True: # Check that account has tokens. input_amount_available = seller.get_depo().get_amount_of_asset(config.input_asset) logger.info('Currently own %s %s', input_amount_available, config.input_asset) if input_amount_available > 0: amount_to_sell = sample_from_range(rng, config.input_amount_range) amount_to_sell = min(input_amount_available, amount_to_sell) logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset) order_details = seller.place_market_order( config.input_asset, amount_to_sell, config.output_asset) print(order_details) # TODO: Write order details to file. del amount_to_sell elif config.exit_when_empty: break # Time out time_to_sleep = sample_from_range(rng, config.interval_range) time_to_sleep_secs = time_to_sleep.total_seconds() logger.info('Sleeping %s (%d seconds)', time_to_sleep, time_to_sleep_secs) time.sleep(time_to_sleep_secs) del input_amount_available