1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
9709c9b68a
Ruff
All checks were successful
Test Python / Test (push) Successful in 28s
2024-09-04 17:55:02 +02:00
af07c75820
Improved CLI 2024-09-04 17:53:06 +02:00
3 changed files with 77 additions and 24 deletions

View File

@ -95,14 +95,20 @@ most mature on the danish market, and does support KuCoin.
## TODO ## TODO
- [ ] Present an overview of what the script will be doing - [ ] Allow multiple secrets configs
- [ ] Allow multiple output directories
- [ ] Fix `TimeoutError` issue occuring from slow Kucoin endpoint. Might
require implementing own kucoin backend in `fin_depo`.
- [ ] Ensure that a failure during selling results in a safe winding down of the system.
* Catch runtime errors when selling
* Show status
* Show errors to log.
* Stop loop and exit with results, and error indicator.
- [X] Present an overview of what the script will be doing
* Sell what for what? How much, how often? * Sell what for what? How much, how often?
* Give an estimate of how long it will take. * Give an estimate of how long it will take.
* Wait 20 seconds before starting, to allow the user to review. * Wait 20 seconds before starting, to allow the user to review.
- [ ] Ensure that a failure during selling results in a safe winding down of the system. - [X] Document command-line arguments
* Catch runtime errors when selling
* Show errors to log.
* Stop loop and exit with results, and error indicator.
- [X] Document configuration - [X] Document configuration
- [X] Document code auditing - [X] Document code auditing
- [X] Parse configuration from json. - [X] Parse configuration from json.
@ -119,7 +125,7 @@ import datetime
import logging import logging
import random import random
from collections.abc import Callable from collections.abc import Callable
from decimal import Decimal, ROUND_DOWN from decimal import ROUND_DOWN, Decimal
from typing import Any from typing import Any
import fin_defs import fin_defs
@ -195,7 +201,10 @@ def sample_from_range(rng: random.Random, rang: tuple[Any, Any]) -> Any:
ROUND_TO_WHOLE = Decimal('1') ROUND_TO_WHOLE = Decimal('1')
def run_auto_sell(config: AutoSellConfig, skip_first:int=0) -> AutoSellRunResults: def run_auto_sell(
config: AutoSellConfig,
initial_rounds_to_skip: int = 0,
) -> AutoSellRunResults:
"""Executes the sell-off. """Executes the sell-off.
Sell-offs are performed in rounds of sizes and with intervals randomly Sell-offs are performed in rounds of sizes and with intervals randomly
@ -212,15 +221,22 @@ def run_auto_sell(config: AutoSellConfig, skip_first:int=0) -> AutoSellRunResult
input_amount_available = config.seller.get_depo().get_amount_of_asset( input_amount_available = config.seller.get_depo().get_amount_of_asset(
config.input_asset, config.input_asset,
) )
logger.info('Currently own %s %s', input_amount_available, config.input_asset.raw_short_name()) logger.info(
'Currently own %s %s',
input_amount_available,
config.input_asset.raw_short_name(),
)
if skip_first > 0: if initial_rounds_to_skip > 0:
skip_first -= 1 initial_rounds_to_skip -= 1
logger.info('Skipping this round') logger.info('skipping this round')
elif input_amount_available > 0: elif input_amount_available > 0:
amount_to_sell = sample_from_range(rng, config.input_amount_range) amount_to_sell = sample_from_range(rng, config.input_amount_range)
amount_to_sell = min(input_amount_available, amount_to_sell) amount_to_sell = min(input_amount_available, amount_to_sell)
amount_to_sell = amount_to_sell.quantize(ROUND_TO_WHOLE, rounding=ROUND_DOWN) amount_to_sell = amount_to_sell.quantize(
ROUND_TO_WHOLE,
rounding=ROUND_DOWN,
)
logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset) logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset)
order_details = config.seller.place_market_order( order_details = config.seller.place_market_order(
@ -265,8 +281,12 @@ def log_estimates(config: AutoSellConfig):
expected_num_sell_offs = current_balance / average_amount expected_num_sell_offs = current_balance / average_amount
expected_duration = average_sleep * float(expected_num_sell_offs) expected_duration = average_sleep * float(expected_num_sell_offs)
fastest_duration = config.interval_range[0] * float(current_balance / config.input_amount_range[1]) fastest_duration = config.interval_range[0] * float(
slowest_duration = config.interval_range[1] * float(current_balance / config.input_amount_range[0]) current_balance / config.input_amount_range[1],
)
slowest_duration = config.interval_range[1] * float(
current_balance / config.input_amount_range[0],
)
logger.info('Welcome to crypto seller!') logger.info('Welcome to crypto seller!')
config.sleep(1) config.sleep(1)
@ -277,11 +297,19 @@ def log_estimates(config: AutoSellConfig):
config.sleep(1) config.sleep(1)
logger.info('') logger.info('')
config.sleep(1) config.sleep(1)
logger.info('- Current balance: %s %s', current_balance, config.input_asset.raw_short_name()) logger.info(
'- Current balance: %s %s',
current_balance,
config.input_asset.raw_short_name(),
)
config.sleep(1) config.sleep(1)
logger.info('- Average sleep: %s seconds', average_sleep) logger.info('- Average sleep: %s seconds', average_sleep)
config.sleep(1) config.sleep(1)
logger.info('- Average amount: %s %s', average_amount, config.input_asset.raw_short_name()) logger.info(
'- Average amount: %s %s',
average_amount,
config.input_asset.raw_short_name(),
)
config.sleep(1) config.sleep(1)
logger.info('- Expected counts: %s', expected_num_sell_offs) logger.info('- Expected counts: %s', expected_num_sell_offs)
config.sleep(1) config.sleep(1)

View File

@ -58,7 +58,14 @@ def setup_logging():
CLI_DESCRIPTION = """ CLI_DESCRIPTION = """
Sells financial assets from an online account. Script to automatically and with little effort sell financial assets from
online accounts.
""".strip()
CLI_EPILOG = """
Author : Jon Michael Aanes (jonjmaa@gmail.com)
Website : https://gitfub.space/Jmaa/crypto-seller
License : MIT License (see website for full text)
""".strip() """.strip()
@ -96,9 +103,25 @@ def load_config(config_path: Path) -> AutoSellConfig:
def parse_args(): def parse_args():
parser = argparse.ArgumentParser('crypto_seller', description=CLI_DESCRIPTION) parser = argparse.ArgumentParser(
parser.add_argument('--config', type=Path, dest='config_file', required=True) prog='crypto_seller',
parser.add_argument('--skip-first', action='store_true', dest='skip_first') description=CLI_DESCRIPTION,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=CLI_EPILOG,
)
parser.add_argument(
'--config',
type=Path,
dest='config_file',
required=True,
help='Trading configuration file',
)
parser.add_argument(
'--wait-before-first',
action='store_true',
dest='wait_before_first',
help='Skip the first sell-off round, and wait for the next.',
)
return parser.parse_args() return parser.parse_args()
@ -116,7 +139,10 @@ def main():
log_estimates(auto_sell_config) log_estimates(auto_sell_config)
# Run auto sell # Run auto sell
results = run_auto_sell(auto_sell_config, skip_first=args.skip_first and 1 or 0) results = run_auto_sell(
auto_sell_config,
initial_rounds_to_skip=args.wait_before_first and 1 or 0,
)
# Display results # Display results
logging.info('Sell-offs complete') logging.info('Sell-offs complete')

View File

@ -8,7 +8,6 @@ import fin_depo
import crypto_seller import crypto_seller
import crypto_seller.order_csv import crypto_seller.order_csv
import pytest
class SellerMock(fin_depo.data.DepoFetcher): class SellerMock(fin_depo.data.DepoFetcher):
def __init__(self, asset: fin_defs.Asset, initial_amount: Decimal): def __init__(self, asset: fin_defs.Asset, initial_amount: Decimal):