-
Notifications
You must be signed in to change notification settings - Fork 0
/
sangyong_api_trade.py
453 lines (401 loc) · 22.1 KB
/
sangyong_api_trade.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding:utf-8 -*-
import urllib2
import json
from time import time, sleep
import hmac, hashlib
import threading
import ssl
global cpdax_url
global cpdax_api_prefix
def createTimeStamp(datestr, format="%Y-%m-%d %H:%M:%S"):
return time.mktime(time.strptime(datestr, format))
colByRow = 5
def makeAddr(f_in, f_out):
cnt = 1
nf = open(f_out, 'w')
with open(f_in) as obj:
for line in obj:
if len(line) < 5: continue
elif cnt > 1: nf.write(",")
nf.write("('" + line.strip() + "')")
if cnt > 0 and cnt % colByRow == 0: nf.write("\n")
cnt = cnt + 1
nf.write(";")
class cpdax:
def __init__(self, APIKey, Secret):
self.APIKey = APIKey
self.Secret = Secret
def post_process(self, before):
after = before
# Add timestamps if there isnt one but is a datetime
if ('return' in after):
if (isinstance(after['return'], list)):
for x in xrange(0, len(after['return'])):
if (isinstance(after['return'][x], dict)):
if ('datetime' in after['return'][x] and 'timestamp' not in after['return'][x]):
after['return'][x]['timestamp'] = float(createTimeStamp(after['return'][x]['datetime']))
return after
def api_query(self, command, req={}, param={}, method="GET"):
try:
if (command == "getCurrencies"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'currencies'))
return json.loads(ret.read())
elif (command == "getTicker"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'tickers'))
return json.loads(ret.read())
elif (command == "getTickerCurrency"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'tickers/' + str(req['currencyPair'])))
return json.loads(ret.read())
elif (command == "getTickerCurrencyDetailed"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'tickers/' + str(req['currencyPair']) + "/detailed"))
return json.loads(ret.read())
elif (command == "getTickersDetailed"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'tickers/detailed'))
return json.loads(ret.read())
elif (command == "getProducts"):
ret = urllib2.urlopen(urllib2.Request(cpdax_url + 'products'))
return json.loads(ret.read())
elif (command == "getOrderbook"):
ret = urllib2.urlopen(urllib2.Request(
cpdax_url + 'orderbook/' + str(req['currencyPair']) + "?limit=" + str(param['limit'])))
return json.loads(ret.read())
elif (command == "getTrades"):
parameter = ""
if param['start'] != "":
parameter += "?start=" + param['start']
if param['end'] != "":
if param['start'] != "":
parameter += "&end=" + param['end']
else:
parameter += "?end=" + param['end']
if param['limit'] != "":
if "?" in parameter:
parameter += "&limit=" + param['limit']
else:
parameter += "?limit=" + param['limit']
ret = urllib2.urlopen(
urllib2.Request(cpdax_url + 'trades/' + str(req['currencyPair']) + parameter))
return json.loads(ret.read())
else:
cp_access_timestamp = str(int(time()))
digest_string = self.APIKey + "" + str(
cp_access_timestamp) + "" + method + "" + cpdax_api_prefix + "" + command
if req != {}:
digest_string = digest_string + json.dumps(req).replace(" ", "")
cp_access_digest = hmac.new(self.Secret, digest_string, hashlib.sha256).hexdigest()
headers = {
'CP-ACCESS-KEY': self.APIKey,
'CP-ACCESS-TIMESTAMP': cp_access_timestamp,
'CP-ACCESS-DIGEST': cp_access_digest,
'Content-Type': 'application/json'
}
if "transactions" in command:
if param['start'] != "":
command += "?start=" + param['start']
if param['end'] != "":
if param['start'] != "":
command += "&end=" + param['end']
else:
command += "?end=" + param['end']
if param['page'] != "":
if "?" in command:
command += "&page=" + param['page']
else:
command += "?page=" + param['page']
if param['limit'] != "":
if "?" in command:
command += "&limit=" + param['limit']
else:
command += "?limit=" + param['limit']
if method == 'POST':
ret = urllib2.urlopen(urllib2.Request(cpdax_url + command, json.dumps(req), headers))
elif method == 'DELETE':
request = urllib2.Request(cpdax_url + command, None, headers)
request.get_method = lambda: 'DELETE'
ret = urllib2.urlopen(request)
else:
if ("orders" in command) and param != {}:
if param['side'] != "":
command += "?side=" + param['side']
if param['page'] != "":
if "?" in command:
command += "&page=" + param['page']
else:
command += "?page=" + param['page']
if param['limit'] != "":
if "?" in command:
command += "&limit=" + param['limit']
else:
command += "?limit=" + param['limit']
ret = urllib2.urlopen(urllib2.Request(cpdax_url + command, None, headers))
jsonRet = json.loads(ret.read())
return self.post_process(jsonRet)
except urllib2.HTTPError, e:
print("HTTP Error! code : " + str(e.code) + ", message : " + str(json.loads(e.read())['message']))
def getCurrencies(self):
return self.api_query("getCurrencies")
def getTicker(self):
return self.api_query("getTicker")
def getTickerCurrency(self, currency_pair):
return self.api_query("getTickerCurrency", {'currencyPair': currency_pair})
def getTickerCurrencyDetailed(self, currency_pair):
return self.api_query("getTickerCurrencyDetailed", {'currencyPair': currency_pair})
def getTickersDetailed(self):
return self.api_query("getTickersDetailed")
def getProducts(self):
return self.api_query("getProducts")
def getOrderbook(self, currency_pair, limit=15):
return self.api_query("getOrderbook", {'currencyPair': currency_pair}, {'limit': limit})
def getTrades(self, currency_pair, start, end, limit):
return self.api_query("getTrades", {'currencyPair': currency_pair},
{'start': start, 'end': end, 'limit': limit})
def getBalance(self):
return self.api_query("balance")
def getFeeRates(self):
return self.api_query("fee-rates")
def getTransactions(self, product_id, start=None, end=None, limit=None, page=None):
return self.api_query("transactions/" + product_id, {},
{'start': start, 'end': end, 'limit': limit, 'page': page})
def limitOrder(self, product_id, type, side, price, size):
return self.api_query("orders",
{"type": type, "side": side, "product_id": product_id, "size": size, "price": price}, {},
"POST")
def marketOrder(self, product_id, type, side, funds=None, size=None):
if funds == None:
return self.api_query("orders", {"type": type, "side": side, "product_id": product_id, "size": size}, {},
"POST")
else:
return self.api_query("orders", {"type": type, "side": side, "product_id": product_id, "funds": funds}, {},
"POST")
def getOrderList(self, product_id, side, limit, page):
return self.api_query("orders/" + product_id, {}, {'side': side, 'limit': limit, 'page': page})
def getOrderStatus(self, product_id, order_id):
return self.api_query("orders/" + product_id + "/" + order_id)
def cancelOrder(self, product_id, order_id):
return self.api_query("orders/" + product_id + "/" + order_id, {}, {}, "DELETE")
def cancelOrders(self, product_id):
return self.api_query("orders/" + product_id, {}, {}, "DELETE")
def threadTest(self):
balances = cpdax_obj.getBalance()
print(json.dumps(balances, indent=2))
dic = {}
if __name__ == "__main__":
cpdax_address = "https://www.cpdax.com" # Cheetah stage=> Changed
# cpdax_address = "https://a4b9d8f51de2.coingift.co.kr" # Cheetah stage
# cpdax_address = "https://sd5762e9d.cpdax.com" # stage
# cpdax_address = "https://api.cpdax.com" # prod
# cpdax_address = "https://www.coingift.co.kr" # coingift
# cpdax_address = "https://d2e0059e6.cpdax.com" # dev
cpdax_api_prefix = "/v1/"
cpdax_url = cpdax_address + "" + cpdax_api_prefix
# Product rsi@coinplug.com
api_key = "570d5365ed70d528ca2f4f85002f40fd679ae3355e95835c91b7f391076b9e3e" #rsi@coinplug.com
secret_key = "YTI4Zjg2ODMtZmNiOC00NTAwLWE1OGQtYzEwMWQ0NGY5ZmI2" #rsi@coinplug.com
cpdax_obj = cpdax(api_key, secret_key)
## order test user
# user 1 - level : ?, order ratio : ?
user1_api_key = ""
user1_secret_key = ""
user1_cpdax = cpdax(user1_api_key, user1_secret_key)
# user 2 - level : ?, order ratio : ?
user2_api_key = ""
user2_secret_key = ""
user2_cpdax = cpdax(user2_api_key, user2_secret_key)
while 1:
print "===== CPDAX Open API Test ====="
print "# Public API"
print "1. ticker"
print "2. ticker(Currency Pair)"
print "3. ticker(Currency Pair Detailed)"
print "4. Show Products"
print "5. Show Orderbook"
print "6. Show Recent Trade"
print "# Private API"
print "7. My balance"
print "8. My Info"
print "9. Limit Order"
print "10. Market Order"
print "11. My Order List"
print "12. Cancel Order"
print "13. Cancel Orders"
print "14. My Trade Transactions"
print "==============================="
print "15. RateLimit Test"
print "16. tickers Detailed"
print "17. order information"
print "a. Auto Trading from the files"
print "q. exit"
operation = raw_input("Choose operation number : ")
if operation == "1":
ticker = cpdax_obj.getTicker()
print(json.dumps(ticker, indent=2))
elif operation == "2":
product_id = raw_input("Input productId : ")
ticker_currency = cpdax_obj.getTickerCurrency(product_id)
print(json.dumps(ticker_currency, indent=2))
elif operation == "3":
product_id = raw_input("Input productId : ")
ticker_currency_detailed = cpdax_obj.getTickerCurrencyDetailed(product_id)
print(json.dumps(ticker_currency_detailed, indent=2))
elif operation == "4":
products = cpdax_obj.getProducts()
print(json.dumps(products, indent=2))
elif operation == "5":
product_id = raw_input("Input productId : ")
orderbook = cpdax_obj.getOrderbook(product_id, 15)
print(json.dumps(orderbook, indent=2))
elif operation == "6":
product_id = raw_input("Input productId : ")
start = raw_input("start time(optional) : ")
end = raw_input("end time(optional) : ")
limit = ""
if start == "" and end == "":
limit = raw_input("limit(optional) : ")
trades = cpdax_obj.getTrades(product_id, start, end, limit)
print(json.dumps(trades, indent=2))
elif operation == "7":
balances = cpdax_obj.getBalance()
print(json.dumps(balances, indent=2))
elif operation == "8":
fee_rates = cpdax_obj.getFeeRates()
print(json.dumps(fee_rates, indent=2))
elif operation == "9":
product_id = raw_input("Input productId : ")
side = raw_input("side(buy/sell) : ")
price = raw_input("price : ")
size = raw_input("size : ")
order = cpdax_obj.limitOrder(product_id, "limit", side, price, size)
print(json.dumps(order, indent=2))
elif operation == "10":
product_id = raw_input("Input productId : ")
side = raw_input("side(buy/sell) : ")
if side == "buy":
funds = raw_input("funds : ")
order = cpdax_obj.marketOrder(product_id, "market", side, funds, None)
else:
size = raw_input("size : ")
order = cpdax_obj.marketOrder(product_id, "market", side, None, size)
print(json.dumps(order, indent=2))
elif operation == "11":
product_id = raw_input("Input productId(BTC-KRW) : ")
side = raw_input("side(buy/sell)(optional) : ")
limit = raw_input("limit(optional) : ")
page = raw_input("page(optional) : ")
order_list = cpdax_obj.getOrderList(product_id, side, limit, page)
print(json.dumps(order_list, indent=2))
elif operation == "12":
product_id = raw_input("Input productId(BTC-KRW) : ")
order_id = raw_input("Input orderId : ")
canceled_order = cpdax_obj.cancelOrder(product_id, order_id)
print(json.dumps(canceled_order, indent=2))
elif operation == "13":
product_id = raw_input("Input productId(BTC-KRW) : ")
canceled_orders = cpdax_obj.cancelOrders(product_id)
print(json.dumps(canceled_orders, indent=2))
elif operation == "14":
product_id = raw_input("Input productId : ")
start = raw_input("start time(optional:utc) : ")
end = raw_input("end time(optional:utc) : ")
limit = raw_input("limit(optional) : ")
page = raw_input("page(optional) : ")
transactions = cpdax_obj.getTransactions(product_id, start, end, limit, page)
print(json.dumps(transactions, indent=2))
elif operation == "15":
for x in range(0, 20):
t1 = threading.Thread(target=cpdax_obj.threadTest)
t1.daemon = True
t1.start()
elif operation == "16":
tickers = cpdax_obj.getTickersDetailed()
print(json.dumps(tickers, indent=2))
elif operation == "17":
product_id = raw_input("Input productId(BTC-KRW) : ")
order_id = raw_input("Input orderId : ")
order = cpdax_obj.getOrderStatus(product_id, order_id)
print(json.dumps(order, indent=2))
elif operation == "a":
# ## KRW마켓에 각 Coin별 기준가격 설정
# TCAPI_BOX_KRW_01A = user4_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.8", "1020")
# TCAPI_BOX_KRW_01B = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.8", "1550")
# TCAPI_BOX_KRW_02A = user4_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.7", "3190")
# TCAPI_BOX_KRW_02B = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.8", "2660")
# TCAPI_BOX_KRW_02A = user3_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.6", "8400")
# TCAPI_BOX_KRW_02B = user2_cpdax.limitOrder("BOX-KRW", "limit", "buy", "22.7", "8400")
# TCAPI_BOX_KRW_03A = user2_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.2", "13400")
# TCAPI_BOX_KRW_03B = user3_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.2", "13400")
# TCAPI_BOX_KRW_04A = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "19.2", "46120")
# TCAPI_BOX_KRW_04B = user4_cpdax.limitOrder("BOX-KRW", "limit", "buy", "19.4", "46120")
# TCAPI_BOX_KRW_05C = user3_cpdax.limitOrder("BOX-KRW", "limit", "sell", "19.4", "34120")
# TCAPI_BOX_KRW_05D = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "19.8", "34120")
# TCAPI_BOX_KRW_06A = user4_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.0", "30840")
# TCAPI_BOX_KRW_06B = user2_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.4", "30840")
# TCAPI_BOX_KRW_07A = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.6", "40900")
# TCAPI_BOX_KRW_07B = user3_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.4", "40900")
# TCAPI_BOX_KRW_08A = user4_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.8", "42970")
# TCAPI_BOX_KRW_08B = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.0", "210")
# TCAPI_BOX_KRW_08C = user3_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.2", "230")
# TCAPI_BOX_KRW_08D = user2_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.2", "42530")
# TCAPI_BOX_KRW_09A = user4_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.0", "1260")
# TCAPI_BOX_KRW_09B = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.6", "3300")
# TCAPI_BOX_KRW_10A = user4_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.4", "5350")
# TCAPI_BOX_KRW_10B = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.4", "3310")
# TCAPI_BOX_KRW_11A = user3_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.4", "1380")
# TCAPI_BOX_KRW_11B = user2_cpdax.limitOrder("BOX-KRW", "limit", "sell", "19.2", "1380")
# TCAPI_BOX_KRW_12A = user2_cpdax.limitOrder("BOX-KRW", "limit", "buy", "19.6", "9240")
# TCAPI_BOX_KRW_12B = user3_cpdax.limitOrder("BOX-KRW", "limit", "sell", "18.8", "9240")
# TCAPI_BOX_KRW_13A = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "22.0", "41920")
# TCAPI_BOX_KRW_13B = user4_cpdax.limitOrder("BOX-KRW", "limit", "sell", "21.1", "41920")
# TCAPI_BOX_KRW_14A = user3_cpdax.limitOrder("BOX-KRW", "limit", "buy", "22.2", "32230")
# TCAPI_BOX_KRW_14B = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "22.2", "32230")
# TCAPI_BOX_KRW_15A = user4_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.8", "17590")
# TCAPI_BOX_KRW_15B = user2_cpdax.limitOrder("BOX-KRW", "limit", "sell", "21.1", "17590")
# TCAPI_BOX_KRW_16A = user1_cpdax.limitOrder("BOX-KRW", "limit", "buy", "20.5", "17590")
# TCAPI_BOX_KRW_16B = user3_cpdax.limitOrder("BOX-KRW", "limit", "sell", "19.9", "17590")
# TCAPI_BOX_KRW_17A = user4_cpdax.limitOrder("BOX-KRW", "limit", "buy", "21.5", "61090")
# TCAPI_BOX_KRW_17B = user1_cpdax.limitOrder("BOX-KRW", "limit", "sell", "21.3", "1120")
# TCAPI_BOX_KRW_17C = user3_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.4", "1160")
# TCAPI_BOX_KRW_17D = user2_cpdax.limitOrder("BOX-KRW", "limit", "sell", "20.0", "58810")
# sleep(2)
## 파일을 읽어서 실행하는 방법 (sleep() 추가와 파일이름을 변수로 읽어오도록 변경필요)
# def efile():
# f = open("Trade_EthKrw.txt", 'r')
# while True:
# line = f.readline()
# if not line: break
# print line
# _user,_productId,_type,_side,_price,_size = line.split(",")
# if _user == '1':
# user1_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '2':
# user2_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '3':
# user3_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '4':
# user4_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '5':
# user5_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '6':
# user6_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '7':
# user7_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# elif _user == '8':
# user8_cpdax.limitOrder (str(_productId),str(_type),str(_side),str(_price),str(_size)[:1])
# f.close()
# sleep(1)
# efile()
#실행결과를 파일로 저장
import datetime
now = datetime.datetime.now()
nowTuple = now.timetuple()
filenm = str(nowTuple.tm_year) + str(nowTuple.tm_mon) + str(nowTuple.tm_mday) + "-" + str(nowTuple.tm_hour) + str(nowTuple.tm_min)
f = open('/Users/rsi/test/api/rslt/' + filenm + '_result.txt', 'w')
f.write("== user1 balance ==" + "\n" + json.dumps(user1_cpdax.getBalance(), indent=2))
f.write("== user2 balance ==" + "\n" + json.dumps(user2_cpdax.getBalance(), indent=2))
f.write("== user3 balance ==" + "\n" + json.dumps(user3_cpdax.getBalance(), indent=2))
f.write("== user4 balance ==" + "\n" + json.dumps(user4_cpdax.getBalance(), indent=2))
f.close()
elif operation == "q":
exit()
else:
print "Wrong choose!!"
print "----- 프로그램 종료 -----"