-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpdax.py
279 lines (222 loc) · 9.63 KB
/
cpdax.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
"""
@author: Songyang Guo
@contact: [email protected]
@file: Cpdax.py
@time: 08/27/2018
@document file: https://apidocs-eng.cpdax.com/reference
"""
import requests
import hmac
import hashlib
import base64
import time
import json
class Cpdax:
domain_url = r"https://api.cpdax.com"
api_url = r"https://api.cpdax.com/v1/"
order_url = r"https://api.cpdax.com/v1/orders/"
version = "v1"
def __init__(self, api_key=None, api_secret=None):
self.api_key = "" if api_key is None else api_key
self.api_secret = "" if api_secret is None else api_secret
self.private_method = True if (self.api_key and self.api_secret) else False
def __sign(self, url_postfix, time_stamp, http_method="GET", params=None):
url = "/" + self.version + "/" + url_postfix
request_body = ""
if params and http_method == "POST":
request_body = self.json(params, {'convertArraysToObjects': True})
data = self.api_key + str(time_stamp) + http_method + url + request_body
return {"cmd": params, 'data': data, "method": http_method, "time": time_stamp, "url_postfix": url_postfix}
def __get_header(self, signed_information):
return {
"CP-ACCESS-KEY": self.api_key,
"CP-ACCESS-TIMESTAMP": signed_information["time"],
"CP-ACCESS-DIGEST": self.hmac(self.encode(signed_information["data"]), self.encode(self.api_secret))
}
def __private_request(self, signed_information):
url = self.api_url + signed_information["url_postfix"]
header = self.__get_header(signed_information)
if signed_information["method"] == "POST":
response = requests.post(url, json=signed_information["cmd"], headers=header)
else:
if signed_information["cmd"]:
response = requests.request(signed_information["method"], url, headers=header,
params=signed_information["cmd"])
else:
response = requests.request(signed_information["method"], url, headers=header)
return response
def get_currencies(self):
url = self.api_url + "currencies"
response = requests.request("GET", url)
return response.json()
def get_products(self):
url = self.api_url + "products"
response = requests.request("GET", url)
return response.json()
def get_all_tickers(self):
url = self.api_url + "tickers"
response = requests.request("GET", url)
return response.json()
def get_all_tickers_detail(self):
url = self.api_url + "tickers/detailed"
response = requests.request("GET", url)
return response.json()
def get_tickers(self, ticker):
url = self.api_url + "tickers/" + ticker
response = requests.request("GET", url)
return response.json()
def get_tickers_detail(self, ticker):
url = self.api_url + "tickers/" + ticker + "/detailed"
response = requests.request("GET", url)
return response.json()
def get_recent_trading_list(self, ticker, start=None, end=None, limit=None):
url = self.api_url + "trades/" + ticker
querystring = {}
if start:
querystring["start"] = str(start)
if end:
querystring["end"] = str(end)
if limit:
querystring["limit"] = str(limit)
response = requests.request("GET", url, params=querystring)
return response.text
def get_orderbook(self, ticker, limit=50):
url = self.api_url + "orderbook/" + ticker
querystring = {}
if limit:
querystring = {"limit": str(limit)}
response = requests.request("GET", url, params=querystring)
return response.json()
def create_order(self, ticker, order_type, side, size=None, price=None, params={}):
order_cmd = {
"type": order_type,
"side": side,
"product_id": ticker,
}
if order_type == "limit":
if price and size:
order_cmd["price"] = price
order_cmd["size"] = size
else:
raise ValueError("Price or Size parameter is missing for limit order")
elif order_type == "market":
if side == "buy":
if params["funds"]:
order_cmd["fund"] = params["funds"]
else:
raise ValueError("Funds parameter is missing for market buy order")
elif side == "sell":
if size:
order_cmd["size"] = size
else:
raise ValueError("Size parameter is missing for market sell order")
else:
raise ValueError("side should be [buy|sell]")
else:
raise ValueError("order_type should be [limit|market]")
utc_timestamp = str(int(time.time()))
url_postfix = "orders"
signature = self.__sign(url_postfix, utc_timestamp, http_method="POST", params=order_cmd)
response = self.__private_request(signature)
return response.json()
def create_limit_order(self, symbol, *args):
return self.create_order(symbol, 'limit', *args)
def create_limit_buy_order(self, symbol, *args):
return self.create_order(symbol, 'limit', 'buy', *args)
def create_limit_sell_order(self, symbol, *args):
return self.create_order(symbol, 'limit', 'sell', *args)
def create_market_buy_order(self, symbol, size, funds):
return self.create_order(symbol, 'market', 'buy', size, None, {"funds": funds})
def create_market_sell_order(self, symbol, size):
return self.create_order(symbol, 'market', 'sell', size, None)
def fetch_all_orders(self, ticker, side=None, page=None, limit=None):
utc_timestamp = str(int(time.time()))
url_suffix = "orders/" + ticker
querystring = {}
if side:
querystring["side"] = side
if page:
querystring["page"] = page
if limit:
querystring["limit"] = limit
signature = self.__sign(url_suffix, utc_timestamp, http_method="GET", params=querystring)
response = self.__private_request(signature)
return response.text
def fetch_order(self, ticker, order_id):
utc_timestamp = str(int(time.time()))
url_suffix = "orders/" + ticker + "/" + order_id
signature = self.__sign(url_suffix, utc_timestamp, http_method="GET", params=None)
response = self.__private_request(signature)
return response.text
def cancel_order(self, ticker, order_id):
utc_timestamp = str(int(time.time()))
url_suffix = "orders/" + ticker + "/" + order_id
signature = self.__sign(url_suffix, utc_timestamp, http_method="DELETE", params=None)
response = self.__private_request(signature)
return response.text
def cancel_all_orders(self, ticker, side=None):
utc_timestamp = str(int(time.time()))
url_suffix = "orders/" + ticker
querystring = {}
if side:
querystring["side"] = side
signature = self.__sign(url_suffix, utc_timestamp, http_method="DELETE", params=querystring)
response = self.__private_request(signature)
return response.text
def fetch_fee_rates(self):
# todo: still not functional
utc_timestamp = str(int(time.time()))
url_suffix = "fee-rates"
signature = self.__sign(url_suffix, utc_timestamp, http_method="GET")
response = self.__private_request(signature)
return response.text
def fetch_balance(self):
utc_timestamp = str(int(time.time()))
url_suffix = "balance"
signature = self.__sign(url_suffix, utc_timestamp, http_method="GET")
response = self.__private_request(signature)
return response.json()
@staticmethod
def hmac(request, secret, algorithm=hashlib.sha256, digest="hex"):
h = hmac.new(secret, request, algorithm)
if digest == 'hex':
return h.hexdigest()
elif digest == 'base64':
return base64.b64encode(h.digest())
return h.digest()
@staticmethod
def json(data, params=None):
return json.dumps(data, separators=(",", ":"))
@staticmethod
def encode(string):
return string.encode()
def main():
api_key = ""
api_secret = ""
test_conn = Cpdax(api_key, api_secret)
# Public API function
# print(test_conn.get_currencies())
# print(test_conn.get_products())
# print(test_conn.get_tickers("ETH-BTC"))
# print(test_conn.get_tickers_detail("ETH-BTC"))
# print(test_conn.get_all_tickers())
# print(test_conn.get_all_tickers_detail())
# print(test_conn.get_recent_trading_list("ETH-BTC"))
# print(test_conn.get_orderbook("ETH-BTC", limit=50))
# Create order
# print(test_conn.create_order("ETH-BTC", "limit", "buy", 1000, 0.05094476))
# print(test_conn.create_limit_order("ETH-BTC", "buy", 0.00261507, 0.0372132))
# print(test_conn.create_limit_buy_order("ETH-BTC", 0.00261507, 0.0372132))
# print(test_conn.create_limit_sell_order("ETH-BTC", 0.11914155, 0.04544666))
# Fetch orders
# print(test_conn.fetch_all_orders("ETH-BTC", limit= 10, side="sell"))
# print(test_conn.fetch_order("ETH-BTC", "755c1721-c032-4315-ac44-02d032458414"))
# Cancel orders
# print(test_conn.cancel_order("ETH-BTC", "755c1721-c032-4315-ac44-02d032458414"))
# print(test_conn.cancel_all_orders("ETH-BTC", side='sell'))
# Fetch Balance and fee rates
# print(test_conn.fetch_balance())
# print(test_conn.fetch_fee_rates())
if __name__ == "__main__":
main()