1
0

Main loop implemented

This commit is contained in:
Jon Michael Aanes 2024-07-17 22:21:58 +02:00
parent 4938160f7c
commit ea21556e01
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
2 changed files with 88 additions and 1 deletions

View File

@ -3,6 +3,68 @@
Description TODO.
"""
__all__ = ['__version__']
__all__ = ['__version__', 'run_auto_sell']
import datetime
import dataclasses
import random
from typing import Any
import logging
from decimal import Decimal
import time
import fin_defs
from ._version import __version__
logger = logging.getLogger(__name__)
logger.setLevel('INFO')
@dataclasses.dataclass
class AutoSellConfig:
sell_amount_range: tuple[Decimal, Decimal]
sell_interval_range: tuple[datetime.timedelta, datetime.timedelta]
token_input: fin_defs.Asset
token_output: 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 determine_available_tokens(token: fin_defs.Asset) -> Decimal:
# TODO
return Decimal(10000)
def sell_tokens(token_input: fin_defs.Asset, tokens_to_sell: Decimal, token_output: fin_defs.Asset):
pass
def run_auto_sell(config: AutoSellConfig):
rng = random.SystemRandom()
while True:
# Check that account has tokens.
num_tokens_available = determine_available_tokens(config.token_input)
logger.info('Currently own %s %s', num_tokens_available, config.token_input)
if num_tokens_available > 0:
tokens_to_sell = sample_from_range(rng, config.sell_amount_range)
tokens_to_sell = min(num_tokens_available, tokens_to_sell)
logger.info('Attempting to sell %s %s', tokens_to_sell, config.token_input)
sell_tokens(config.token_input, tokens_to_sell, config.token_output)
del tokens_to_sell
elif config.exit_when_empty:
break
# Time out
time_to_sleep = sample_from_range(rng, config.sell_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 num_tokens_available

25
crypto_seller/__main__.py Normal file
View File

@ -0,0 +1,25 @@
import datetime
import logging
from decimal import Decimal
import fin_defs
from . import run_auto_sell, AutoSellConfig
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.setLevel('INFO')
def main():
logger.info('Initializing crypto_seller')
config = AutoSellConfig(
sell_amount_range= (Decimal(100), Decimal(1000)),
sell_interval_range= (datetime.timedelta(minutes=20), datetime.timedelta(minutes=60)),
token_input= fin_defs.BTC,
token_output= fin_defs.USDT,
exit_when_empty = True,
)
run_auto_sell(config)
if __name__ == '__main__':
main()