175 lines
5.1 KiB
Python
175 lines
5.1 KiB
Python
"""# Automatic Crypto Seller.
|
|
|
|
This is a system for automatically trading individual financial assets a single
|
|
way, by using repeated trades over a period.
|
|
|
|
The primary motivation is for trading low-volume crypto assets 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.
|
|
|
|
Todo:
|
|
----
|
|
## 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 . import config
|
|
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
|
|
|
|
|
|
SELLER = fin_depo.defi_kucoin.KucoinDepoFetcher(
|
|
kucoin_secret=config.KUCOIN_SECRET,
|
|
kucoin_key=config.KUCOIN_KEY,
|
|
kucoin_pass=config.KUCOIN_PASS,
|
|
)
|
|
|
|
|
|
def determine_available_tokens(token: fin_defs.Asset) -> Decimal:
|
|
return SELLER.get_depo().get_amount_of_asset(token)
|
|
|
|
|
|
def sell_tokens(
|
|
input_asset: fin_defs.Asset, input_amount: Decimal, output_asset: fin_defs.Asset,
|
|
) -> fin_depo.data.TradeOrderDetails:
|
|
# return SELLER.place_market_order(input_asset, input_amount, output_asset)
|
|
return fin_depo.data.TradeOrderDetails(
|
|
input_asset=input_asset,
|
|
input_amount=input_amount,
|
|
output_asset=output_asset,
|
|
output_amount=input_amount,
|
|
fee_asset=input_asset,
|
|
fee_amount=input_amount,
|
|
order_id=10001,
|
|
raw_order_details={'TEST': 1, 'DATA': 2},
|
|
)
|
|
|
|
|
|
def run_auto_sell(config: AutoSellConfig):
|
|
rng = random.SystemRandom()
|
|
|
|
while True:
|
|
# Check that account has tokens.
|
|
input_amount_available = determine_available_tokens(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 = sell_tokens(
|
|
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
|