Test sleep
This commit is contained in:
parent
3fb2d001ad
commit
5c6c0d5df1
|
@ -76,7 +76,7 @@ import random
|
||||||
import time
|
import time
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any,Callable
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
import fin_depo
|
import fin_depo
|
||||||
|
@ -96,27 +96,28 @@ PATH_TRADES_FILE = PATH_OUTPUT / 'trades.csv'
|
||||||
# Main code #
|
# Main code #
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass(frozen = True)
|
||||||
class AutoSellConfig:
|
class AutoSellConfig:
|
||||||
interval_range: tuple[datetime.timedelta, datetime.timedelta]
|
interval_range: tuple[datetime.timedelta, datetime.timedelta]
|
||||||
input_asset: fin_defs.Asset
|
input_asset: fin_defs.Asset
|
||||||
input_amount_range: tuple[Decimal, Decimal]
|
input_amount_range: tuple[Decimal, Decimal]
|
||||||
output_asset: fin_defs.Asset
|
output_asset: fin_defs.Asset
|
||||||
|
seller: fin_depo.defi_kucoin.KucoinDepoFetcher
|
||||||
|
sleep: Callable[[float],None]
|
||||||
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:
|
||||||
multiplier = rng.random()
|
multiplier = rng.random()
|
||||||
if isinstance(range[0], Decimal):
|
if isinstance(range[0], Decimal):
|
||||||
multiplier = Decimal(multiplier)
|
multiplier = Decimal(multiplier)
|
||||||
return range[0] + (range[1] - range[0]) * multiplier
|
return range[0] + (range[1] - range[0]) * multiplier
|
||||||
|
|
||||||
def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, 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.
|
||||||
input_amount_available = seller.get_depo().get_amount_of_asset(config.input_asset)
|
input_amount_available = config.seller.get_depo().get_amount_of_asset(config.input_asset)
|
||||||
logger.info('Currently own %s %s', input_amount_available, config.input_asset)
|
logger.info('Currently own %s %s', input_amount_available, config.input_asset)
|
||||||
|
|
||||||
if input_amount_available > 0:
|
if input_amount_available > 0:
|
||||||
|
@ -124,7 +125,7 @@ def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSe
|
||||||
amount_to_sell = min(input_amount_available, amount_to_sell)
|
amount_to_sell = min(input_amount_available, amount_to_sell)
|
||||||
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 = seller.place_market_order(
|
order_details = config.seller.place_market_order(
|
||||||
config.input_asset, amount_to_sell, config.output_asset)
|
config.input_asset, amount_to_sell, config.output_asset)
|
||||||
|
|
||||||
print(order_details)
|
print(order_details)
|
||||||
|
@ -138,6 +139,6 @@ def run_auto_sell(seller: fin_depo.defi_kucoin.KucoinDepoFetcher, config: AutoSe
|
||||||
time_to_sleep = sample_from_range(rng, config.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)
|
config.sleep(time_to_sleep_secs)
|
||||||
|
|
||||||
del input_amount_available
|
del input_amount_available
|
||||||
|
|
|
@ -2,6 +2,7 @@ import datetime
|
||||||
import logging
|
import logging
|
||||||
import logging.handlers
|
import logging.handlers
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
import time
|
||||||
|
|
||||||
import fin_defs
|
import fin_defs
|
||||||
|
|
||||||
|
@ -41,8 +42,10 @@ def main():
|
||||||
input_asset=fin_defs.MPC,
|
input_asset=fin_defs.MPC,
|
||||||
output_asset=fin_defs.USDT,
|
output_asset=fin_defs.USDT,
|
||||||
exit_when_empty=True,
|
exit_when_empty=True,
|
||||||
|
seller = seller_backend,
|
||||||
|
sleep = time.sleep
|
||||||
)
|
)
|
||||||
run_auto_sell(seller_backend, auto_sell_config)
|
run_auto_sell(auto_sell_config)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
@ -32,17 +32,31 @@ class SellerMock(fin_depo.data.DepoFetcher):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SleepMock:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.time_slept = 0.0
|
||||||
|
|
||||||
|
def __call__(self, amount: float):
|
||||||
|
self.time_slept = self.time_slept + amount
|
||||||
|
|
||||||
def test_auto_run():
|
def test_auto_run():
|
||||||
|
|
||||||
|
sleep_mock = SleepMock()
|
||||||
|
seller_mock = SellerMock(fin_defs.USDT, Decimal('1000'))
|
||||||
|
|
||||||
config = crypto_seller.AutoSellConfig(
|
config = crypto_seller.AutoSellConfig(
|
||||||
input_amount_range=(Decimal('1'), Decimal('1')),
|
input_amount_range=(Decimal('1'), Decimal('1')),
|
||||||
interval_range=(datetime.timedelta(seconds=0), datetime.timedelta(seconds=0)),
|
interval_range=(datetime.timedelta(seconds=1), datetime.timedelta(seconds=1)),
|
||||||
input_asset=fin_defs.USDT,
|
input_asset=fin_defs.USDT,
|
||||||
output_asset=fin_defs.USD,
|
output_asset=fin_defs.USD,
|
||||||
exit_when_empty=True,
|
exit_when_empty=True,
|
||||||
|
seller = seller_mock,
|
||||||
|
sleep = sleep_mock ,
|
||||||
)
|
)
|
||||||
seller = SellerMock(fin_defs.USDT, Decimal('1000'))
|
|
||||||
|
|
||||||
crypto_seller.run_auto_sell(seller, config)
|
crypto_seller.run_auto_sell(config)
|
||||||
|
|
||||||
assert seller.amount_left == 0
|
assert seller_mock.amount_left == 0
|
||||||
|
assert sleep_mock.time_slept == 1000
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user