"""See `KucoinDepoFetcher` for documentation.""" import datetime import logging from decimal import Decimal import fin_defs import kucoin.client from .data import DepoFetcher, DepoGroup, DepoSingle, TradeOrderDetails logger = logging.getLogger(__name__) class KucoinDepoFetcher(DepoFetcher): """`Depo` fetcher for [Kucoin](https://www.kucoin.com), the online crypto currency exchange. Requirements for use: - Account on [Kucoin](https://www.kucoin.com). - Have performed Know Your Customer (KYC) for your account. - Created API key from [settings menu](https://www.kucoin.com/account/api). API key must have the **General Permission**. Unless you are using the `place_market_order` functionality, the API key **should not have any additional permissions**. Employ principle of least priviledge. - Install [`python-kucoin`](https://python-kucoin.readthedocs.io/en/latest/) library. Depository structure: An upper level depo split for each of the sub-accounts (Funding, Trading, Margin, Futures...) """ def __init__( self, kucoin_key: str, kucoin_secret: str, kucoin_pass: str, allow_trades: bool = False, ): self.assert_param('kucoin_key', str, kucoin_key) self.assert_param('kucoin_secret', str, kucoin_secret) self.assert_param('kucoin_pass', str, kucoin_pass) self.allow_trades = allow_trades self.kucoin_client = kucoin.client.Client( kucoin_key, kucoin_secret, kucoin_pass, ) def get_depo(self) -> DepoGroup: # We would ideally get timestamp from request, # but this is fine for now. now = datetime.datetime.now(tz=datetime.UTC) # Assets are spread across account types, but we would like them # clustered into different depos. assets_by_account_type: dict[str, dict[fin_defs.Asset, Decimal]] = {} for account_data in self.kucoin_client.get_accounts(): asset = fin_defs.WELL_KNOWN_SYMBOLS[account_data['currency']] balance = Decimal(account_data['balance']) assets_for_account_type = assets_by_account_type.setdefault( account_data['type'], {}, ) assets_for_account_type[asset] = ( assets_for_account_type.get(asset, Decimal(0)) + balance ) del account_data, asset, balance, assets_for_account_type return DepoGroup( 'Kucoin', now, [ DepoSingle('Kucoin ' + account_type, now, assets) for account_type, assets in assets_by_account_type.items() ], ) def place_market_order( self, input_asset: fin_defs.Asset, input_amount: Decimal, output_asset: fin_defs.Asset, ) -> TradeOrderDetails: """Executes a market order through the Kucoin market. Will automatically determine the market based on the input and output assets. Requirements: - Fetcher must have been created with `allow_trades=True`. - API key used with fetcher must have **Spot Trading** permissions. - Assets must be on trading account. Assets on funding accounts or other accounts cannot be used. Note: - A fee will be paid to Kucoin, with the rate determined by your WIP level and the asset being traded. - The full `input_amount` may not be used. Inspect the resulting `TradeOrderDetails` to see how much of the `input_amount` have been used. References: - POST Market Order: - GET Market Order by id: """ # Check requirements if not self.allow_trades: msg = 'KucoinDepoFetcher.allow_trades is not enabled: Cannot make trades' raise PermissionError(msg) assert fin_defs.USDT in [input_asset, output_asset], 'USDT markets only for now' # Convert arguments to kucoin client arguments if input_asset == fin_defs.USDT: symbol: str = f'{output_asset}-{input_asset}' side: str = 'buy' size = None funds = str(input_amount) else: symbol = f'{input_asset}-{output_asset}' side = 'sell' size = str(input_amount) funds = None # Place order logger.info('Placing order: %s', str([symbol, side, size, funds])) response = self.kucoin_client.create_market_order( symbol=symbol, side=side, size=size, funds=funds, ) del symbol, side, size, funds, input_amount # Determine order details order_id = response['orderId'] order_details = self.kucoin_client.get_order(order_id) # Convert from kucoin results if input_asset == fin_defs.USDT: input_amount_final = Decimal(order_details['dealFunds']) output_amount_final = Decimal(order_details['dealSize']) else: output_amount_final = Decimal(order_details['dealFunds']) input_amount_final = Decimal(order_details['dealSize']) return TradeOrderDetails( input_asset=input_asset, input_amount=input_amount_final, output_asset=output_asset, output_amount=output_amount_final, fee_asset=fin_defs.WELL_KNOWN_SYMBOLS[order_details['feeCurrency']], fee_amount=Decimal(order_details['fee']), executed_time=datetime.datetime.fromtimestamp( order_details['createdAt'] / 1000, tz=datetime.UTC ), order_id=order_id, raw_order_details=order_details, )