1
0

Base functionality mostly implemented. Just need logging and output files.

This commit is contained in:
Jon Michael Aanes 2024-07-21 12:00:55 +02:00
parent 14fec39541
commit 113f1ca158
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 51 additions and 23 deletions

View File

@ -15,7 +15,9 @@ import time
import fin_defs import fin_defs
import fin_depo
from . import config
from ._version import __version__ from ._version import __version__
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -23,10 +25,10 @@ logger.setLevel('INFO')
@dataclasses.dataclass @dataclasses.dataclass
class AutoSellConfig: class AutoSellConfig:
sell_amount_range: tuple[Decimal, Decimal] interval_range: tuple[datetime.timedelta, datetime.timedelta]
sell_interval_range: tuple[datetime.timedelta, datetime.timedelta] input_asset: fin_defs.Asset
token_input: fin_defs.Asset input_amount_range: tuple[Decimal, Decimal]
token_output: fin_defs.Asset output_asset: fin_defs.Asset
exit_when_empty: bool = True exit_when_empty: bool = True
def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any: def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any:
@ -35,36 +37,54 @@ def sample_from_range(rng: random.Random, range: tuple[Any, Any]) -> Any:
multiplier = Decimal(multiplier) multiplier = Decimal(multiplier)
return range[0] + (range[1] - range[0]) * multiplier return range[0] + (range[1] - range[0]) * multiplier
def determine_available_tokens(token: fin_defs.Asset) -> Decimal: SELLER = fin_depo.defi_kucoin.KucoinDepoFetcher(
# TODO kucoin_secret = config.KUCOIN_SECRET,
return Decimal(10000) kucoin_key = config.KUCOIN_KEY,
kucoin_pass = config.KUCOIN_PASS,
)
def sell_tokens(token_input: fin_defs.Asset, tokens_to_sell: Decimal, token_output: fin_defs.Asset): def determine_available_tokens(token: fin_defs.Asset) -> Decimal:
pass 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): def run_auto_sell(config: AutoSellConfig):
rng = random.SystemRandom() rng = random.SystemRandom()
while True: while True:
# Check that account has tokens. # Check that account has tokens.
num_tokens_available = determine_available_tokens(config.token_input) input_amount_available = determine_available_tokens(config.input_asset)
logger.info('Currently own %s %s', num_tokens_available, config.token_input) logger.info('Currently own %s %s', input_amount_available, config.input_asset)
if num_tokens_available > 0: if input_amount_available > 0:
tokens_to_sell = sample_from_range(rng, config.sell_amount_range) amount_to_sell = sample_from_range(rng, config.input_amount_range)
tokens_to_sell = min(num_tokens_available, tokens_to_sell) amount_to_sell = min(input_amount_available, amount_to_sell)
logger.info('Attempting to sell %s %s', tokens_to_sell, config.token_input) logger.info('Attempting to sell %s %s', amount_to_sell, config.input_asset)
sell_tokens(config.token_input, tokens_to_sell, config.token_output) order_details = sell_tokens(config.input_asset, amount_to_sell, config.output_asset)
print(order_details)
# TODO: Write order details to file.
del tokens_to_sell del amount_to_sell
elif config.exit_when_empty: elif config.exit_when_empty:
break break
# Time out # Time out
time_to_sleep = sample_from_range(rng, config.sell_interval_range) time_to_sleep = sample_from_range(rng, config.interval_range)
time_to_sleep_secs = time_to_sleep.total_seconds() time_to_sleep_secs = time_to_sleep.total_seconds()
logger.info('Sleeping %s (%d seconds)', time_to_sleep, time_to_sleep_secs) logger.info('Sleeping %s (%d seconds)', time_to_sleep, time_to_sleep_secs)
time.sleep(time_to_sleep_secs) time.sleep(time_to_sleep_secs)
del num_tokens_available del input_amount_available

View File

@ -13,10 +13,11 @@ logger.setLevel('INFO')
def main(): def main():
logger.info('Initializing crypto_seller') logger.info('Initializing crypto_seller')
config = AutoSellConfig( config = AutoSellConfig(
sell_amount_range= (Decimal(100), Decimal(1000)), input_amount_range= (Decimal('0.5'), Decimal('1')),
sell_interval_range= (datetime.timedelta(minutes=20), datetime.timedelta(minutes=60)), interval_range= (datetime.timedelta(seconds=2),
token_input= fin_defs.BTC, datetime.timedelta(seconds=6)),
token_output= fin_defs.USDT, input_asset = fin_defs.MPC,
output_asset = fin_defs.USDT,
exit_when_empty = True, exit_when_empty = True,
) )
run_auto_sell(config) run_auto_sell(config)

7
crypto_seller/config.py Normal file
View File

@ -0,0 +1,7 @@
import secret_loader
load_secret = secret_loader.SecretLoader().load
KUCOIN_KEY = load_secret('KUCOIN_KEY')
KUCOIN_SECRET = load_secret('KUCOIN_SECRET')
KUCOIN_PASS = load_secret('KUCOIN_PASS')