okxv5 1.8.11__py3-none-any.whl → 1.8.13__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
okxv5/exceptions.py CHANGED
@@ -2,76 +2,43 @@
2
2
 
3
3
 
4
4
  class OkxAPIException(Exception):
5
- """
6
- OKX API异常类,用于处理从OKX API返回的错误响应。
7
- """
5
+
8
6
  def __init__(self, response):
9
- """
10
- 构造函数,初始化API异常。
11
- :param response: HTTP响应对象,通常包含错误信息。
12
- """
13
- # 打印响应文本和状态码
14
7
  print(response.text + ', ' + str(response.status_code))
15
- self.code = 0 # 错误码初始化
8
+ self.code = 0
16
9
  try:
17
- json_res = response.json() # 尝试将响应解析为JSON
10
+ json_res = response.json()
18
11
  except ValueError:
19
- # 如果解析失败,则消息为无效JSON格式
20
12
  self.message = 'Invalid JSON error message from Okx: {}'.format(response.text)
21
13
  else:
22
- # 如果JSON解析成功,检查是否包含'code'和'msg'字段
23
14
  if "code" in json_res.keys() and "msg" in json_res.keys():
24
- self.code = json_res['code'] # 提取错误码
25
- self.message = json_res['msg'] # 提取错误消息
15
+ self.code = json_res['code']
16
+ self.message = json_res['msg']
26
17
  else:
27
- self.code = 'None' # 如果没有'code'和'msg',则为系统错误
18
+ self.code = 'None'
28
19
  self.message = 'System error'
29
20
 
30
- self.status_code = response.status_code # HTTP状态码
31
- self.response = response # 原始HTTP响应
32
- self.request = getattr(response, 'request', None) # 原始HTTP请求
21
+ self.status_code = response.status_code
22
+ self.response = response
23
+ self.request = getattr(response, 'request', None)
33
24
 
34
25
  def __str__(self): # pragma: no cover
35
- """
36
- 返回此异常的字符串表示。
37
- :return: 包含错误码和消息的字符串。
38
- """
39
26
  return 'API Request Error(code=%s): %s' % (self.code, self.message)
40
27
 
41
28
 
42
29
  class OkxRequestException(Exception):
43
- """
44
- OKX 请求异常类,用于处理在发送请求时发生的错误(例如网络问题)。
45
- """
30
+
46
31
  def __init__(self, message):
47
- """
48
- 构造函数,初始化请求异常。
49
- :param message: 异常消息。
50
- """
51
32
  self.message = message
52
33
 
53
34
  def __str__(self):
54
- """
55
- 返回此异常的字符串表示。
56
- :return: 包含请求异常消息的字符串。
57
- """
58
35
  return 'OkxRequestException: %s' % self.message
59
36
 
60
37
 
61
38
  class OkxParamsException(Exception):
62
- """
63
- OKX 参数异常类,用于处理由于参数错误导致的问题。
64
- """
39
+
65
40
  def __init__(self, message):
66
- """
67
- 构造函数,初始化参数异常。
68
- :param message: 异常消息。
69
- """
70
41
  self.message = message
71
42
 
72
43
  def __str__(self):
73
- """
74
- 返回此异常的字符串表示。
75
- :return: 包含参数异常消息的字符串。
76
- """
77
44
  return 'OkxParamsException: %s' % self.message
okxv5/status_api.py CHANGED
@@ -1,35 +1,21 @@
1
- # 从当前包导入Client类
2
1
  from .client import Client
3
- # 从当前包导入所有常量
4
2
  from .consts import *
5
3
 
6
4
 
7
- # StatusAPI类继承自Client类
8
5
  class StatusAPI(Client):
9
- # 构造函数
10
6
  def __init__(self, api_key, api_secret_key, passphrase, use_server_time=False, flag='1'):
11
- # 调用父类Client的构造函数
12
7
  Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag)
13
8
 
14
- # 获取系统状态
15
9
  def status(self, state=''):
16
- # 构建请求参数字典,'state' 为状态参数
17
10
  params = {'state': state}
18
- # 调用父类的_request_with_params方法发送GET请求,获取状态信息
19
11
  return self._request_with_params(GET, STATUS, params)
20
12
 
21
13
  # GET /api/v5/support/announcements
22
- # 获取公告列表
23
14
  def get_announcements(self, annType = '', page = ''):
24
- # 构建请求参数字典,'annType' 为公告类型,'page' 为页码
25
15
  params = {'annType': annType, 'page': page}
26
- # 调用父类的_request_with_params方法发送GET请求,获取公告信息
27
16
  return self._request_with_params(GET, GET_ANNOUNCEMENTS, params)
28
17
 
29
18
  # GET /api/v5/support/announcement-types
30
- # 获取公告类型
31
19
  def get_announcements_types(self):
32
- # 构建空的请求参数字典
33
20
  params = {}
34
- # 调用父类的_request_with_params方法发送GET请求,获取公告类型信息
35
21
  return self._request_with_params(GET, GET_ANNOUNCEMENTS_TYPES, params)
okxv5/subAccount_api.py CHANGED
@@ -3,232 +3,61 @@ from .consts import *
3
3
 
4
4
 
5
5
  class SubAccountAPI(Client):
6
- """
7
- 子账户API类,继承自Client,用于与子账户相关的操作。
8
- """
9
6
  def __init__(self, api_key, api_secret_key, passphrase, use_server_time=False, flag='1'):
10
- """
11
- 初始化SubAccountAPI实例。
12
-
13
- 参数:
14
- api_key (str): API密钥。
15
- api_secret_key (str): API私钥。
16
- passphrase (str): 密码。
17
- use_server_time (bool, optional): 是否使用服务器时间,默认为False。
18
- flag (str, optional): 区域标识,'1'为真实环境,'0'为模拟盘。
19
- """
20
7
  Client.__init__(self, api_key, api_secret_key, passphrase, use_server_time, flag)
21
8
 
22
9
  def balances(self, subAcct):
23
- """
24
- 获取子账户资产余额。
25
-
26
- 参数:
27
- subAcct (str): 子账户名称。
28
-
29
- 返回:
30
- dict: 包含子账户余额信息的响应。
31
- """
32
10
  params = {"subAcct": subAcct}
33
11
  return self._request_with_params(GET, BALANCE, params)
34
12
 
35
13
  def bills(self, ccy='', type='', subAcct='', after='', before='', limit=''):
36
- """
37
- 获取子账户账单记录。
38
-
39
- 参数:
40
- ccy (str, optional): 币种,如“BTC”,默认为空。
41
- type (str, optional): 账单类型,默认为空。
42
- subAcct (str, optional): 子账户名称,默认为空。
43
- after (str, optional): 查询起始ID,默认为空。
44
- before (str, optional): 查询结束ID,默认为空。
45
- limit (str, optional): 返回结果的数量,默认为空。
46
-
47
- 返回:
48
- dict: 包含子账户账单记录的响应。
49
- """
50
14
  params = {"ccy": ccy, 'type': type, 'subAcct': subAcct, 'after': after, 'before': before, 'limit': limit}
51
15
  return self._request_with_params(GET, BILLs, params)
52
16
  # 移除此接口
53
17
  def delete(self, pwd, subAcct, apiKey):
54
- """
55
- 删除子账户(此接口已移除)。
56
-
57
- 参数:
58
- pwd (str): 密码。
59
- subAcct (str): 子账户名称。
60
- apiKey (str): API密钥。
61
-
62
- 返回:
63
- dict: 删除子账户的响应。
64
- """
65
18
  params = {'pwd': pwd, 'subAcct': subAcct, 'apiKey': apiKey}
66
19
  return self._request_with_params(POST, DELETE, params)
67
20
  # 移除此接口
68
21
  def reset(self, pwd, subAcct, label, apiKey, perm, ip=''):
69
- """
70
- 重置子账户API密钥(此接口已移除)。
71
-
72
- 参数:
73
- pwd (str): 密码。
74
- subAcct (str): 子账户名称。
75
- label (str): API密钥标签。
76
- apiKey (str): API密钥。
77
- perm (str): 权限。
78
- ip (str, optional): IP地址,默认为空。
79
-
80
- 返回:
81
- dict: 重置API密钥的响应。
82
- """
83
22
  params = {'pwd': pwd, 'subAcct': subAcct, 'label': label, 'apiKey': apiKey, 'perm': perm, 'ip': ip}
84
23
  return self._request_with_params(POST, RESET, params)
85
24
  # 移除此接口
86
25
  def create(self, pwd, subAcct, label, Passphrase, perm='', ip=''):
87
- """
88
- 创建子账户(此接口已移除)。
89
-
90
- 参数:
91
- pwd (str): 密码。
92
- subAcct (str): 子账户名称。
93
- label (str): API密钥标签。
94
- Passphrase (str): 密码。
95
- perm (str, optional): 权限,默认为空。
96
- ip (str, optional): IP地址,默认为空。
97
-
98
- 返回:
99
- dict: 创建子账户的响应。
100
- """
101
26
  params = {'pwd': pwd, 'subAcct': subAcct, 'label': label, 'Passphrase': Passphrase, 'perm': perm, 'ip': ip}
102
27
  return self._request_with_params(POST, CREATE, params)
103
28
  # 移除此接口
104
29
  def watch(self, subAcct,apiKey=''):
105
- """
106
- 监控子账户(此接口已移除)。
107
-
108
- 参数:
109
- subAcct (str): 子账户名称。
110
- apiKey (str, optional): API密钥,默认为空。
111
-
112
- 返回:
113
- dict: 监控子账户的响应。
114
- """
115
30
  params = {'subAcct': subAcct,'apiKey':apiKey}
116
31
  return self._request_with_params(GET, WATCH, params)
117
32
 
118
33
  def view_list(self, enable='', subAcct='', after='', before='', limit='',uid=''):
119
- """
120
- 查看子账户列表。
121
-
122
- 参数:
123
- enable (str, optional): 是否启用,默认为空。
124
- subAcct (str, optional): 子账户名称,默认为空。
125
- after (str, optional): 查询起始ID,默认为空。
126
- before (str, optional): 查询结束ID,默认为空。
127
- limit (str, optional): 返回结果的数量,默认为空。
128
- uid (str, optional): 用户ID,默认为空。
129
-
130
- 返回:
131
- dict: 包含子账户列表的响应。
132
- """
133
34
  params = {'enable': enable, 'subAcct': subAcct, 'after': after,
134
35
  'before': before, 'limit': limit,'uid':uid}
135
36
  return self._request_with_params(GET, VIEW_LIST, params)
136
37
 
137
38
  def subAccount_transfer(self, ccy, amt, froms, to, fromSubAccount,toSubAccount,loanTrans='',omitPosRisk=''):
138
- """
139
- 子账户资金划转。
140
-
141
- 参数:
142
- ccy (str): 币种。
143
- amt (str): 划转数量。
144
- froms (str): 转出方。
145
- to (str): 转入方。
146
- fromSubAccount (str): 转出子账户名称。
147
- toSubAccount (str): 转入子账户名称。
148
- loanTrans (str, optional): 借贷划转类型,默认为空。
149
- omitPosRisk (str, optional): 是否忽略持仓风险,默认为空。
150
-
151
- 返回:
152
- dict: 子账户资金划转的响应。
153
- """
154
39
  params = {'ccy': ccy, 'amt': amt, 'from': froms, 'to': to, 'fromSubAccount': fromSubAccount, 'toSubAccount': toSubAccount,'loanTrans':loanTrans,'omitPosRisk':omitPosRisk}
155
40
  return self._request_with_params(POST, SUBACCOUNT_TRANSFER, params)
156
41
 
157
42
  def entrust_subaccount_list(self, subAcct):
158
- """
159
- 查询托管子账户列表。
160
-
161
- 参数:
162
- subAcct (str): 子账户名称。
163
-
164
- 返回:
165
- dict: 包含托管子账户列表的响应。
166
- """
167
43
  params = {'subAcct': subAcct}
168
44
  return self._request_with_params(GET, ENTRUST_SUBACCOUNT_LIST, params)
169
45
 
170
46
  def modify_apikey(self, subAcct, apiKey, label, perm, ip):
171
- """
172
- 修改子账户API密钥。
173
-
174
- 参数:
175
- subAcct (str): 子账户名称。
176
- apiKey (str): API密钥。
177
- label (str): API密钥标签。
178
- perm (str): 权限。
179
- ip (str): IP地址。
180
-
181
- 返回:
182
- dict: 修改API密钥的响应。
183
- """
184
47
  params = {'subAcct': subAcct, 'apiKey': apiKey, 'label': label, 'perm': perm, 'ip': ip}
185
48
  return self._request_with_params(POST, MODIFY_APIKEY, params)
186
49
 
187
50
  def partner_if_rebate(self, apiKey = ''):
188
- """
189
- 查询合伙人返佣信息。
190
-
191
- 参数:
192
- apiKey (str, optional): API密钥,默认为空。
193
-
194
- 返回:
195
- dict: 包含合伙人返佣信息的响应。
196
- """
197
51
  params = {'apiKey': apiKey}
198
52
  return self._request_with_params(GET, PARTNER_IF_REBATE, params)
199
53
 
200
54
  # 获取子账户最大可转余额 max-withdrawal
201
55
  def max_withdrawal(self, subAcct, ccy = ''):
202
- """
203
- 获取子账户最大可划转余额。
204
-
205
- 参数:
206
- subAcct (str): 子账户名称。
207
- ccy (str, optional): 币种,默认为空。
208
-
209
- 返回:
210
- dict: 包含最大可划转余额的响应。
211
- """
212
56
  params = {'subAcct': subAcct,'ccy': ccy,}
213
57
  return self._request_with_params(GET, MAX_WITHDRAW, params)
214
58
 
215
59
  # 查询托管子账户转账记录 managed-subaccount-bills
216
60
  def managed_subaccount_bills(self,ccy='',type='',subAcct='',subUid='',after='',before='',limit=''):
217
- """
218
- 查询托管子账户转账记录。
219
-
220
- 参数:
221
- ccy (str, optional): 币种,默认为空。
222
- type (str, optional): 账单类型,默认为空。
223
- subAcct (str, optional): 子账户名称,默认为空。
224
- subUid (str, optional): 子账户UID,默认为空。
225
- after (str, optional): 查询起始ID,默认为空。
226
- before (str, optional): 查询结束ID,默认为空。
227
- limit (str, optional): 返回结果的数量,默认为空。
228
-
229
- 返回:
230
- dict: 包含托管子账户转账记录的响应。
231
- """
232
61
  params = {'ccy': ccy,'type': type,'subAcct': subAcct,'subUid': subUid,'after': after,'before': before,
233
62
  'limit': limit,}
234
- return self._request_with_params(GET,SUB_BILLS,params)
63
+ return self._request_with_params(GET,SUB_BILLS,params)
okxv5/utils.py CHANGED
@@ -6,98 +6,45 @@ from . import consts as c
6
6
 
7
7
 
8
8
  def sign(message, secretKey):
9
- """
10
- 使用 HMAC-SHA256 算法对消息进行签名。
11
- :param message: 要签名的消息。
12
- :param secretKey: 密钥。
13
- :return: 经过 base64 编码的签名。
14
- """
15
- # 使用 secretKey 作为密钥,对 message 进行 HMAC-SHA256 加密
16
9
  mac = hmac.new(bytes(secretKey, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
17
- d = mac.digest() # 获取摘要
18
- return base64.b64encode(d) # 对摘要进行 base64 编码
10
+ d = mac.digest()
11
+ return base64.b64encode(d)
19
12
 
20
13
 
21
14
  def pre_hash(timestamp, method, request_path, body):
22
- """
23
- 生成预哈希字符串,用于构建签名消息。
24
- :param timestamp: 时间戳。
25
- :param method: HTTP 请求方法 (GET, POST 等)。
26
- :param request_path: 请求路径。
27
- :param body: 请求体。
28
- :return: 拼接后的字符串。
29
- """
30
- # 拼接时间戳、大写的请求方法、请求路径和请求体
31
15
  return str(timestamp) + str.upper(method) + request_path + body
32
16
 
33
17
 
34
18
  def get_header(api_key, sign, timestamp, passphrase, flag):
35
- """
36
- 生成 HTTP 请求头。
37
- :param api_key: API 密钥。
38
- :param sign: 签名。
39
- :param timestamp: 时间戳。
40
- :param passphrase: 密码。
41
- :param flag: 模拟交易标志。
42
- :return: 包含所有必要信息的字典形式的请求头。
43
- """
44
19
  header = dict()
45
- header[c.CONTENT_TYPE] = c.APPLICATION_JSON # 设置内容类型为 JSON
46
- header[c.OK_ACCESS_KEY] = api_key # 设置 API 密钥
47
- header[c.OK_ACCESS_SIGN] = sign # 设置签名
48
- header[c.OK_ACCESS_TIMESTAMP] = str(timestamp) # 设置时间戳
49
- header[c.OK_ACCESS_PASSPHRASE] = passphrase # 设置密码
50
-
51
- # OKX API: 'x-simulated-trading': '1' for simulated, '0' for real
52
- # Our 'flag' parameter: '1' for real, '0' for simulated
53
- # 所以,如果我们的 flag 是 '0' (模拟盘),x-simulated-trading 设置为 '1'。
54
- # 如果我们的 flag 是 '1' (实盘),x-simulated-trading 设置为 '0'。
55
- header['x-simulated-trading'] = '1' if flag == '0' else '0'
56
-
20
+ header[c.CONTENT_TYPE] = c.APPLICATION_JSON
21
+ header[c.OK_ACCESS_KEY] = api_key
22
+ header[c.OK_ACCESS_SIGN] = sign
23
+ header[c.OK_ACCESS_TIMESTAMP] = str(timestamp)
24
+ header[c.OK_ACCESS_PASSPHRASE] = passphrase
25
+ header['x-simulated-trading'] = flag
57
26
  return header
58
27
 
59
28
 
60
29
  def parse_params_to_str(params):
61
- """
62
- 将字典形式的参数解析为 URL 查询字符串。
63
- :param params: 字典形式的参数。
64
- :return: URL 查询字符串。
65
- """
66
30
  url = '?'
67
- # 遍历参数字典,将键值对拼接成 URL 查询字符串
68
31
  for key, value in params.items():
69
32
  url = url + str(key) + '=' + str(value) + '&'
70
- return url[0:-1] # 去掉最后一个 '&'
33
+ return url[0:-1]
71
34
 
72
35
 
73
36
  def get_timestamp():
74
- """
75
- 获取 UTC 时间戳,格式为 ISO 8601,带毫秒和 'Z'。
76
- :return: UTC 时间戳字符串。
77
- """
78
- now = datetime.datetime.utcnow() # 获取当前 UTC 时间
79
- t = now.isoformat("T", "milliseconds") # 格式化为 ISO 8601 字符串,包含毫秒
80
- return t + "Z" # 添加 'Z' 表示 UTC
37
+ now = datetime.datetime.utcnow()
38
+ t = now.isoformat("T", "milliseconds")
39
+ return t + "Z"
81
40
 
82
41
 
83
42
  def signature(timestamp, method, request_path, body, secret_key):
84
- """
85
- 生成完整的签名。
86
- :param timestamp: 时间戳。
87
- :param method: HTTP 请求方法 (GET, POST 等)。
88
- :param request_path: 请求路径。
89
- :param body: 请求体。
90
- :param secret_key: 密钥。
91
- :return: 经过 base64 编码的签名。
92
- """
93
- # 如果请求体为空字典或 None,则将其设置为空字符串
94
43
  if str(body) == '{}' or str(body) == 'None':
95
44
  body = ''
96
- # 拼接时间戳、大写的请求方法、请求路径和请求体,形成待签名的消息
97
45
  message = str(timestamp) + str.upper(method) + request_path + str(body)
98
46
 
99
- # 使用 secret_key 作为密钥,对 message 进行 HMAC-SHA256 加密
100
47
  mac = hmac.new(bytes(secret_key, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
101
- d = mac.digest() # 获取摘要
48
+ d = mac.digest()
102
49
 
103
- return base64.b64encode(d) # 对摘要进行 base64 编码
50
+ return base64.b64encode(d)