forked from fruitlollipop/OKEXAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OKCoinAPI.py
352 lines (310 loc) · 13.6 KB
/
OKCoinAPI.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# To access the REST API of OKCOIN
from HttpsMD5Util import HttpsRequest
class OKCoinBase(object):
RESOURCES_URL = {
'ticker': '/api/v1/{}ticker.do',
'depth': '/api/v1/{}depth.do',
'trades': '/api/v1/{}trades.do',
'index': '/api/v1/future_index.do',
'exchange_rate': '/api/v1/exchange_rate.do',
'estimated_price': '/api/v1/future_estimated_price.do',
'kline': '/api/v1/future_kline.do',
'hold_amount': '/api/v1/future_hold_amount.do',
'price_limit': '/api/v1/future_price_limit.do',
'user_info': '/api/v1/future_userinfo.do',
'position': '/api/v1/future_position.do',
'trades_history': '/api/v1/future_trades_history.do',
'batch_trade': '/api/v1/future_batch_trade.do',
'cancel': '/api/v1/future_cancel.do',
'order_info': '/api/v1/future_order_info.do',
'orders_info': '/api/v1/future_orders_info.do',
'user_info_4fix': '/api/v1/future_userinfo_4fix.do',
'position_4fix': '/api/v1/future_position_4fix.do',
'explosive': '/api/v1/future_explosive.do',
'withdraw': '/api/v1/withdraw.do',
'cancel_withdraw': '/api/v1/cancel_withdraw.do',
'withdraw_info': '/api/v1/withdraw_info.do'
}
Symbols = ('btc_usd', 'ltc_usd')
ContractType = ('this_week', 'next_week', 'quarter')
Types = ('1min',
'3min',
'5min',
'15min',
'30min',
'1day',
'3day',
'1week',
'1hour',
'2hour',
'4hour',
'6hour',
'12hour'
)
def __init__(self, url, api_key, secret_key):
"""
Constructor for class of OKCoinBase.
:param url: Base URL for REST API of Future
:param api_key: String of API KEY
:param secret_key: String of SECRET KEY
:return: None
"""
self.__url = url
self.__api_key = api_key
self.__secret_key = secret_key
self.__request = HttpsRequest(self.__url)
class OKCoinMarketAPI(OKCoinBase):
def __init__(self, url, api_key, secret_key):
"""
Constructor for class of OKCoinMarketAPI.
:param url: Base URL for REST API of Future
:param api_key: String of API KEY
:param secret_key: String of SECRET KEY
:return: None
"""
super(OKCoinMarketAPI, self).__init__(url, api_key, secret_key)
@classmethod
def build_request_string(cls, name, value, params='', choice=()):
if value:
if value in choice:
return params + '&' + name + '=' + str(value) if params else name + '=' + str(value)
else:
raise ValueError('{0} should be in {1}'.format(value), choice)
else:
return params
# OKCOIN行情信息
def ticker(self, symbol, contract_type, future_or_spot=True):
"""
:param symbol:
:param contract_type:
:param future_or_spot:
:return:
"""
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
return self.__request.get(OKCoinBase.RESOURCES_URL['ticker'].format('future_' if future_or_spot else ''),
params)
# OKCoin期货市场深度信息
def depth(self, symbol, contract_type, size=0, merge=0, future_or_spot=True):
"""
:param symbol:
:param contract_type:
:param size:
:param merge:
:param future_or_spot:
:return:
"""
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
params = OKCoinMarketAPI.build_request_string('size', size, params, range(1, 201))
params = OKCoinMarketAPI.build_request_string('merge', merge, params, (0, 1))
return self.__request.get(OKCoinBase.RESOURCES_URL['depth'].format('future_' if future_or_spot else ''),
params)
# OKCoin期货交易记录信息
def trades(self, symbol, contract_type, future_or_spot=True):
"""
:param symbol:
:param contract_type:
:param future_or_spot:
:return:
"""
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
return self.__request.get(OKCoinBase.RESOURCES_URL['trades'].format('future_' if future_or_spot else ''),
params)
# OKCoin期货指数
def future_index(self, symbol):
"""
:param symbol:
:return:
"""
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
return self.__request.get(OKCoinBase.RESOURCES_URL['index'], params)
# 获取美元人民币汇率
def exchange_rate(self):
"""
:return:
"""
return self.__request.get(OKCoinBase.RESOURCES_URL['exchange_rate'], '')
# 获取预估交割价
def future_estimated_price(self, symbol):
"""
:param symbol:
:return:
"""
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
return self.__request.get(OKCoinBase.RESOURCES_URL['estimated_price'], params)
# 获取虚拟合约的K线数据
def future_future_kline(self, symbol, type_, contract_type, size=0, since=0):
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('type', type_, params, OKCoinBase.Types)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
if size:
params += '&size=' + str(size) if params else 'size=' + str(size)
if since:
params += '&since=' + str(since) if params else 'since=' + str(since)
return self.__request.get(OKCoinBase.RESOURCES_URL['kline'], params)
# 获取当前可用合约总持仓量
def future_hold_amount(self, symbol, contract_type):
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
return self.__request.get(OKCoinBase.RESOURCES_URL['hold_amount'], params)
# 获取合约最高买价和最低卖价
def future_price_limit(self, symbol, contract_type):
params = OKCoinMarketAPI.build_request_string('symbol', symbol, '', OKCoinBase.Symbols)
params = OKCoinMarketAPI.build_request_string('contract_type', contract_type, params, OKCoinBase.ContractType)
return self.__request.get(OKCoinBase.RESOURCES_URL['price_limit'], params)
class OKCoinDealsAPI(OKCoinBase):
def __init__(self, url, api_key, secret_key):
"""
Constructor for class of OKCoinFuture.
:param url: Base URL for REST API of Future
:param api_key: String of API KEY
:param secret_key: String of SECRET KEY
:return: None
"""
super(OKCoinDealsAPI, self).__init__(url, api_key, secret_key)
# 期货全仓账户信息
def future_user_info(self):
params = {'api_key': self.__api_key}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['user_info'], params)
# 期货全仓持仓信息
def future_position(self, symbol, contract_type):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['position'], params)
# 期货下单
def future_trade(self, symbol, contract_type, price='', amount='', trade_type='', match_price='', lever_rate=''):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'amount': amount,
'type': trade_type,
'match_price': match_price,
'lever_rate': lever_rate
}
if price:
params['price'] = price
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['trades'], params)
# 获取OKEX合约交易历史(非个人)
def future_trade_history(self, symbol, date, since):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'date': date,
'since': since
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['trades_history'], params)
# 期货批量下单
def future_batch_trade(self, symbol, contract_type, orders_data, lever_rate):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'orders_data': orders_data,
'lever_rate': lever_rate
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['batch_trade'], params)
# 期货取消订单
def future_cancel(self, symbol, contract_type, order_id):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'order_id': order_id
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['cancel'], params)
# 期货获取订单信息
def future_order_info(self, symbol, contract_type, order_id, status, current_page, page_length):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'order_id': order_id,
'status': status,
'current_page': current_page,
'page_length': page_length
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['order_info'], params)
# 期货获取订单信息
def future_orders_info(self, symbol, contract_type, order_id):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'order_id': order_id
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['orders_info'], params)
# 期货逐仓账户信息
def future_user_info_4fix(self):
params = {'api_key': self.__api_key}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['user_info_4fix'], params)
# 期货逐仓持仓信息
def future_position_4fix(self, symbol, contract_type, trade_type):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'type': trade_type
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['position_4fix'], params)
# 获取合约爆仓单
def future_explosive(self, symbol, contract_type, status, current_page, page_number, page_length):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'contract_type': contract_type,
'status': status,
'current_page': current_page,
'page_number': page_number,
'page_length': page_length
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['explosive'], params)
# 提币BTC/LTC
def future_withdraw(self, symbol, charge_fee, trade_pwd, withdraw_address, withdraw_amount, target):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'charge_fee': charge_fee,
'trade_pwd': trade_pwd,
'withdraw_address': withdraw_address,
'withdraw_amount': withdraw_amount,
'target': target
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['withdraw'], params)
# 取消提币BTC/LTC
def future_cancel_withdraw(self, symbol, withdraw_id):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'withdraw_id': withdraw_id
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['cancel_withdraw'], params)
# 查询提币BTC/LTC信息
def future_withdraw_info(self, symbol, withdraw_id):
params = {
'api_key': self.__api_key,
'symbol': symbol,
'withdraw_id': withdraw_id
}
params['sign'] = HttpsRequest.build_sign(params, self.__secret_key)
return HttpsRequest.post(OKCoinBase.RESOURCES_URL['withdraw_info'], params)