def bitmex(test=True, config=None, api_key=None, api_secret=None): if config is None: # See full config options at http://bravado.readthedocs.io/en/latest/configuration.html config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # bravado has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, } if test: host = 'https://testnet.bitmex.com' else: host = 'https://www.bitmex.com' spec_uri = host + '/api/explorer/swagger.json' api_key = api_key api_secret = api_secret if api_key and api_secret: request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(host, api_key, api_secret) return SwaggerClient.from_url(spec_uri, config=config, http_client=request_client) else: return SwaggerClient.from_url(spec_uri, config=config)
def get_clients(): HOST = "https://www.bitmex.com" SPEC_URI = HOST + "/api/explorer/swagger.json" bitMEX = SwaggerClient.from_url(SPEC_URI, config=config) API_KEY = 'emjo2LdiVdhwmTqMESEcO9ut' API_SECRET = 'DspbFr4sWjnxUPY4L5yDh13b0MZ1oDGs4kr94EcdJcSH2QkR' request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(HOST, API_KEY, API_SECRET) bitMEXAuthenticated = SwaggerClient.from_url(SPEC_URI, config=config, http_client=request_client) return bitMEX, bitMEXAuthenticated
def bitmex(config): swagger_config = { 'use_models': False, 'validate_responses': False, 'also_return_response': True, } spec_url = config.HOST + config.SWAGGER_PATH client = ProxyClient(config.PROXIES) if config.api_key and config.api_secret: client.authenticator = APIKeyAuthenticator(config.HOST, config.API_KEY, config.API_SECRET) return SwaggerClient.from_url(spec_url, config=swagger_config, http_client=client)
def initialise_API(self, HOST, API_KEY, API_SECRET): SPEC_URI = HOST + "/api/explorer/swagger.json" config = { # Don't use models (Python classes) instead of dicts for #/definitions/{models} 'use_models': False, # This library has some issues with nullable fields 'validate_responses': False, # Returns response in 2-tuple of (body, response); if False, will only return body 'also_return_response': True, } # See full config options at http://bravado.readthedocs.io/en/latest/configuration.html request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator( HOST, API_KEY, API_SECRET) self.bitMEXAuth = SwaggerClient.from_url(SPEC_URI, config=config, http_client=request_client) time.sleep(2)
print('\n---A basic Trade GET:---') pp.pprint(res) print('\n---Response details:---') print("Status Code: %d, headers: %s" % (http_response.status_code, http_response.headers)) # # Authenticated calls # # To do authentication, you must generate an API key. # Do so at https://testnet.bitmex.com/app/apiKeys API_KEY = api_keys.bmx_api_key API_SECRET = api_keys.bmx_api_secret request_client = RequestsClient() request_client.authenticator = APIKeyAuthenticator(HOST, API_KEY, API_SECRET) bitMEXAuthenticated = SwaggerClient.from_url( SPEC_URI, config=config, http_client=request_client) print(dir(bitMEXAuthenticated)) print(dir(bitMEXAuthenticated.Position)) # Basic authenticated call print('\n---A basic Position then funding then user deposit addr GET:---') res, http_response = bitMEXAuthenticated.Position.Position_get().result() pp.pprint(res) pp.pprint(http_response.status_code) res, http_response = bitMEXAuthenticated.Funding.Funding_get(symbol='XBTUSD', reverse=True, count=1).result() pp.pprint(res)