1
0

Retry get order because we might too fast to ask for it after placing order
All checks were successful
Test Python / Test (push) Successful in 26s

This commit is contained in:
Jon Michael Aanes 2024-07-22 22:18:17 +02:00
parent 2738738fda
commit cc50cba210
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA
3 changed files with 34 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import datetime
import logging
from decimal import Decimal
import time
import fin_defs
import kucoin.client
@ -135,8 +136,17 @@ class KucoinDepoFetcher(DepoFetcher):
del symbol, side, size, funds, input_amount
# Determine order details
order_id = response['orderId']
order_details = self.kucoin_client.get_order(order_id)
return self._get_order_details(response['orderId'], input_asset, output_asset)
def _get_order_details(self, order_id: str,
input_asset: fin_defs.Asset,
output_asset: fin_defs.Asset) -> TradeOrderDetails:
"""Determine the order details for the order with the given id.
Retries the order a few times, as KuCoin might not have propagated the
order through their systems.
"""
order_details = self._get_order_with_retries(order_id, num_retries=10)
# Convert from kucoin results
if input_asset == fin_defs.USDT:
@ -159,3 +169,16 @@ class KucoinDepoFetcher(DepoFetcher):
order_id=order_id,
raw_order_details=order_details,
)
def _get_order_with_retries(self, order_id: str, *, num_retries: int, sleep_between_tries:float = 1.0) -> dict:
"""Get the order details from KuCoin backend.
Retries the order a few times, as KuCoin might not have propagated the
order through their systems since it was sent.
"""
for _ in range(num_retries):
try:
return self.kucoin_client.get_order(order_id)
except kucoin.exceptions.KucoinAPIException as e: # noqa
time.sleep(sleep_between_tries)
return self.kucoin_client.get_order(order_id)

View File

@ -15,6 +15,6 @@ NORDNET_PASSWORD = load_secret('NORDNET_PASSWORD')
KRAKEN_KEY = load_secret('KRAKEN_KEY')
KRAKEN_SECRET = load_secret('KRAKEN_SECRET')
KUCOIN_KEY = load_secret('KUCOIN_KEY')
KUCOIN_SECRET = load_secret('KUCOIN_SECRET')
KUCOIN_PASS = load_secret('KUCOIN_PASS')
KUCOIN_KEY = load_secret('KUCOIN_KEY_TRADING')
KUCOIN_SECRET = load_secret('KUCOIN_SECRET_TRADING')
KUCOIN_PASS = load_secret('KUCOIN_PASS_TRADING')

View File

@ -2,12 +2,13 @@ from decimal import Decimal
import fin_defs
import pytest
import datetime
import fin_depo
from . import secrets
TEST_MARKET_ORDERS = False
TEST_MARKET_ORDERS = True
needs_secrets = pytest.mark.skipif(
not secrets.KUCOIN_KEY,
@ -16,6 +17,7 @@ needs_secrets = pytest.mark.skipif(
fin_depo.defi_kucoin.logger.setLevel('INFO')
NOW = datetime.datetime.now(tz=datetime.UTC)
@needs_secrets
def test_get_depo():
@ -82,6 +84,7 @@ def test_place_buy_side_order():
assert order_details.fee_asset == fin_defs.USDT
assert order_details.fee_amount <= Decimal('0.0002')
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)
@needs_secrets
def test_place_sell_side_order():
@ -115,3 +118,5 @@ def test_place_sell_side_order():
assert order_details.fee_asset == fin_defs.USDT
assert order_details.fee_amount is not None
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)