forked from BitMEX/api-connectors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmexClient.py
More file actions
70 lines (54 loc) · 2.09 KB
/
bitmexClient.py
File metadata and controls
70 lines (54 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
from bravado.client import SwaggerClient
from bravado.requests_client import RequestsClient
from BitMEXAPIKeyAuthenticator import APIKeyAuthenticator
import pprint
HOST = "https://testnet.bitmex.com"
SPEC_URI = HOST + "/api/explorer/swagger.json"
# 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,
# 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,
}
bitMEX = SwaggerClient.from_url(
SPEC_URI,
config=config)
pp = pprint.PrettyPrinter(indent=2)
# You can get a feel for what is available by printing these objects.
# See also https://testnet.bitmex.com/api/explorer
print('---The BitMEX Object:---')
print(dir(bitMEX))
print('\n---The BitMEX.Trade Object:---')
print(dir(bitMEX.Trade))
# Basic unauthenticated call
res, http_response = bitMEX.Trade.Trade_get(symbol='XBTUSD', count=1).result()
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_KEY_HERE>'
API_SECRET = '<API_SECRET_HERE>'
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.Position))
# Basic authenticated call
print('\n---A basic Position GET:---')
res, http_response = bitMEXAuthenticated.Position.Position_get(filter=json.dumps({'symbol': 'XBTUSD'})).result()
pp.pprint(res)
# Basic order placement
# print(dir(bitMEXAuthenticated.Order))
# res, http_response = bitMEXAuthenticated.Order.Order_new(symbol='XBTUSD', orderQty=3, price=1000).result()
# print(res)