1
0
crypto-seller/test/test_auto_sell.py

86 lines
2.5 KiB
Python
Raw Normal View History

2024-07-22 11:36:52 +00:00
import datetime
from decimal import Decimal
2024-07-22 13:48:55 +00:00
from pathlib import Path
2024-07-22 11:21:41 +00:00
import fin_defs
import fin_depo
import crypto_seller
2024-07-22 13:48:55 +00:00
import crypto_seller.order_csv
2024-07-22 11:21:41 +00:00
2024-07-22 11:36:52 +00:00
class SellerMock(fin_depo.data.DepoFetcher):
2024-07-22 11:21:41 +00:00
def __init__(self, asset: fin_defs.Asset, initial_amount: Decimal):
self.asset = asset
self.amount_left = initial_amount
def get_depo(self) -> fin_depo.data.Depo:
2024-07-22 11:36:52 +00:00
return fin_depo.data.DepoSingle(
'TestDepo',
datetime.datetime.now(tz=datetime.UTC),
{self.asset: self.amount_left},
)
2024-07-22 11:21:41 +00:00
2024-07-22 11:36:52 +00:00
def place_market_order(
self,
input_asset: fin_defs.Asset,
input_amount: Decimal,
output_asset: fin_defs.Asset,
2024-09-02 18:26:17 +00:00
) -> fin_depo.data.TradeOrderDetails:
2024-07-22 11:21:41 +00:00
assert input_amount <= self.amount_left, 'Attempt to sell too much'
self.amount_left -= input_amount
2024-07-22 14:05:42 +00:00
executed_time = datetime.datetime.now(tz=datetime.UTC)
2024-07-22 11:21:41 +00:00
return fin_depo.data.TradeOrderDetails(
2024-07-22 21:05:30 +00:00
executed_time=executed_time,
2024-07-22 11:21:41 +00:00
input_asset=input_asset,
input_amount=input_amount,
output_asset=output_asset,
2024-07-22 12:12:05 +00:00
output_amount=input_amount,
2024-07-22 11:21:41 +00:00
fee_asset=input_asset,
fee_amount=Decimal(0),
2024-09-02 18:26:17 +00:00
order_id=Decimal(10000000 - self.amount_left),
2024-07-22 11:21:41 +00:00
raw_order_details={'TEST': 1, 'DATA': 2},
)
2024-07-22 11:31:53 +00:00
class SleepMock:
def __init__(self):
self.time_slept = 0.0
def __call__(self, amount: float):
self.time_slept = self.time_slept + amount
2024-07-22 11:36:52 +00:00
def test_auto_run():
2024-07-22 11:31:53 +00:00
sleep_mock = SleepMock()
seller_mock = SellerMock(fin_defs.USDT, Decimal('1000'))
2024-07-22 11:21:41 +00:00
config = crypto_seller.AutoSellConfig(
input_amount_range=(Decimal('1'), Decimal('1')),
2024-07-22 11:31:53 +00:00
interval_range=(datetime.timedelta(seconds=1), datetime.timedelta(seconds=1)),
2024-07-22 11:21:41 +00:00
input_asset=fin_defs.USDT,
output_asset=fin_defs.USD,
exit_when_empty=True,
2024-07-22 11:36:52 +00:00
seller=seller_mock,
2024-07-22 13:48:55 +00:00
log_order_to_csv=crypto_seller.order_csv.CsvFileLogger(
Path('output/test-trades.csv'),
),
2024-07-22 11:36:52 +00:00
sleep=sleep_mock,
2024-07-22 11:21:41 +00:00
)
crypto_seller.log_estimates(config)
2024-07-22 12:12:05 +00:00
results = crypto_seller.run_auto_sell(config)
2024-07-22 11:21:41 +00:00
2024-07-22 12:12:05 +00:00
# Check results
assert len(results.order_details) == 1000
assert results.total_sleep_duration == datetime.timedelta(seconds=1000)
assert results.total_input_amount() == 1000
assert results.total_output_amount() == 1000
# Check mocks agree
2024-07-22 11:31:53 +00:00
assert seller_mock.amount_left == 0
assert sleep_mock.time_slept == 1000 + 23