Python Bitmex trading Bot -. Connection error while making get request to api/v1/instrument/active and api/v1/user/margin

557 views Asked by At

I am learning how to build a Python Trading bot, I am in my first steps in programming and I need some help. I came across this error while trying to use an API connector to get info from the Base URL and Websocket from bitmex.com.

This is the code to connect to the host:

class BitmexClient:

def __init__(self, public_key: str, secret_key: str, testnet: bool):
    if testnet:
        self._base_url = "https://testnet.bitmex.com"
        self._wss_url = "wss://testnet.bitmex.com/realtime"
    else:
        self._base_url = "https://www.bitmex.com"
        self._wss_url = "wss://www.bitmex.com/realtime"

    self._public_key = public_key
    self._secret_key = secret_key

    self._ws = None

    self.contracts = self.get_contracts()
    self.balances = self.get_balances()

    self.prices = dict()

    t = threading.Thread(target=self._start_ws)
    t.start()

    logger.info("Bitmex Client successfully initialized")

The piece of code related to the error:

def get_contracts(self) -> typing.Dict[str, Contract]:

    instruments = self._make_request("GET", "api/v1/instrument/active", dict())

    contracts = dict()

    if instruments is not None:
        for s in instruments:
            contracts[s['symbol']] = Contract(s, "bitmex")

    return contracts

def get_balances(self) -> typing.Dict[str, Balance]:

    data = dict()
    data['currency'] = "all"

    margin_data = self._make_request("GET", "api/v1/user/margin", data)

    balances = dict()

    if margin_data is not None:
        for a in margin_data:
            balances[a['currency']] = Balance(a, "bitmex")

    return balances

This is the error:

2021-04-30 22:38:45,969 ERROR :: Connection error while making GET request to api/v1/instrument/active: HTTPSConnectionPool(host='testnet.bitmex.comapi', port=443): Max retries exceeded with url: /v1/instrument/active (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002065A061520>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
2021-04-30 22:38:45,972 ERROR :: Connection error while making GET request to api/v1/user/margin: HTTPSConnectionPool(host='testnet.bitmex.comapi', port=443): Max retries exceeded with url: /v1/user/margin?currency=all (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002065A061760>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
1

There are 1 answers

1
Sean Thorburn On

It looks like the problem is that you are trying to connect to a host of "testnet.bitmex.comapi".

The host should be "testnet.bitmex.com".