msg['From'] = MY_ADDRESS
msg['To'] = MY_ADDRESS
msg['Subject'] = "KuCoin Trading Bot"
#exception module
from kucoin.exceptions import KucoinAPIException

#KuCoin API constants
buy_order = client.SIDE_BUY
sell_order = client.SIDE_SELL
stop_loss = client.STOP_LOSS

#retrieves account information and buying power in trading account
accounts = client.get_accounts()
account = pd.DataFrame(accounts)
account = account[(account.type == 'trade')]
trade_hist = client.get_trade_histories('MTV-USDT')
trade_hist = pd.DataFrame(trade_hist)
start_balance = 200.00

#Trade USDT account
buypower = account.loc[account['currency'] == 'USDT', 'available'].values[0]
print(account)
#converts buying power to float and divides by 2 to represent buy size
buypow = float(buypower)
buypowT = buypow / 1.01
buypowT = "{:.2f}".format(buypowT)
#converts buy size back to str for create_market_order
buysize = str(buypowT)
print(buysize)

#checks for starting balance
Beispiel #2
0
class KucoinApi(PyexAPI):
    """kucoin API interface.
    """

    logger = logging.getLogger()

    def __init__(self,
                 api_server: str,
                 api_key: str,
                 secret_key: str,
                 api_passphrase: str,
                 timeout: float,
                 requests_params=None):
        assert (isinstance(api_server, str))
        assert (isinstance(api_key, str))
        assert (isinstance(secret_key, str))
        assert (isinstance(api_passphrase, str))
        assert (isinstance(timeout, float))

        self.api_server = api_server
        self.api_key = api_key
        self.secret_key = secret_key
        self.timeout = timeout
        self.client = Client(api_key, secret_key, api_passphrase)

    def get_symbols(self):
        return self.client.get_symbols()

    def ticker(self, pair: str):
        assert (isinstance(pair, str))
        return self.client.get_ticker(pair)

    def get_balances(self):
        return self.client.get_accounts()

    def get_coin_info(self, coin: str):
        assert (isinstance(coin, str))
        return self.client.get_currency(coin)

    def order_book(self, pair: str, limit=None):
        assert (isinstance(pair, str))
        return self.client.get_order_book(pair)

    def get_orders(self, pair: str) -> List[Order]:
        assert (isinstance(pair, str))

        orders = self.client.get_orders(pair, 'active')

        return list(
            map(lambda item: Order.to_order(item, pair), orders['items']))

    def place_order(self, pair: str, is_sell: bool, price: Wad,
                    amount: Wad) -> str:
        assert (isinstance(pair, str))
        assert (isinstance(is_sell, bool))
        assert (isinstance(price, Wad))
        assert (isinstance(amount, Wad))

        side = self.client.SIDE_SELL if is_sell else self.client.SIDE_BUY

        self.logger.info(f"Placing order ({side}, amount {amount} of {pair},"
                         f" price {price})...")

        result = self.client.create_limit_order(pair, side, str(price),
                                                str(amount))
        order_id = result['orderId']

        self.logger.info(f"Placed order as #{order_id}")
        return order_id

    def cancel_order(self, order_id: str, is_sell: bool, pair: str):
        assert (isinstance(order_id, str))
        assert (isinstance(is_sell, bool))
        assert (isinstance(pair, str))

        side = self.client.SIDE_SELL if is_sell else self.client.SIDE_BUY

        self.logger.info(f"Cancelling order #{order_id} of type {side}...")

        try:
            self.client.cancel_order(order_id)
            self.logger.info(f"Canceled order #{order_id}...")
            return True
        except Exception as e:
            self.logger.error(f"Failed to cancel order #{order_id}... {e}")
            return False

    def get_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (isinstance(page_number, int))

        page_number = page_number - 1
        limit = 100

        result = self.client.get_fills(symbol=pair,
                                       page=page_number,
                                       page_size=limit)

        return list(
            map(lambda item: Trade.from_dict(pair, item), result['items']))

    def get_all_trades(self, pair: str, page_number: int = 1) -> List[Trade]:
        assert (isinstance(pair, str))
        assert (page_number == 1)

        result = self.client.get_trade_histories(pair)

        return list(map(lambda item: Trade.from_list(pair, item), result))