PageRenderTime 4226ms CodeModel.GetById 6ms RepoModel.GetById 2ms app.codeStats 1ms

/python/ccxt/okex.py

https://github.com/kroitor/ccxt
Python | 3180 lines | 2264 code | 38 blank | 878 comment | 158 complexity | 5580fc887fc6fa5c3b3cbe63ce95a293 MD5 | raw file
Possible License(s): MIT
  1. # -*- coding: utf-8 -*-
  2. # PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
  3. # https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
  4. from ccxt.base.exchange import Exchange
  5. # -----------------------------------------------------------------------------
  6. try:
  7. basestring # Python 3
  8. except NameError:
  9. basestring = str # Python 2
  10. import hashlib
  11. from ccxt.base.errors import ExchangeError
  12. from ccxt.base.errors import AuthenticationError
  13. from ccxt.base.errors import PermissionDenied
  14. from ccxt.base.errors import AccountSuspended
  15. from ccxt.base.errors import ArgumentsRequired
  16. from ccxt.base.errors import BadRequest
  17. from ccxt.base.errors import BadSymbol
  18. from ccxt.base.errors import InsufficientFunds
  19. from ccxt.base.errors import InvalidAddress
  20. from ccxt.base.errors import InvalidOrder
  21. from ccxt.base.errors import OrderNotFound
  22. from ccxt.base.errors import CancelPending
  23. from ccxt.base.errors import NotSupported
  24. from ccxt.base.errors import DDoSProtection
  25. from ccxt.base.errors import ExchangeNotAvailable
  26. from ccxt.base.errors import OnMaintenance
  27. from ccxt.base.errors import InvalidNonce
  28. from ccxt.base.errors import RequestTimeout
  29. from ccxt.base.decimal_to_precision import TRUNCATE
  30. from ccxt.base.decimal_to_precision import TICK_SIZE
  31. class okex(Exchange):
  32. def describe(self):
  33. return self.deep_extend(super(okex, self).describe(), {
  34. 'id': 'okex',
  35. 'name': 'OKEX',
  36. 'countries': ['CN', 'US'],
  37. 'version': 'v3',
  38. 'rateLimit': 1000, # up to 3000 requests per 5 minutes 600 requests per minute 10 requests per second 100 ms
  39. 'pro': True,
  40. 'has': {
  41. 'cancelOrder': True,
  42. 'CORS': False,
  43. 'createOrder': True,
  44. 'fetchBalance': True,
  45. 'fetchClosedOrders': True,
  46. 'fetchCurrencies': False, # see below
  47. 'fetchDepositAddress': True,
  48. 'fetchDeposits': True,
  49. 'fetchLedger': True,
  50. 'fetchMarkets': True,
  51. 'fetchMyTrades': True,
  52. 'fetchOHLCV': True,
  53. 'fetchOpenOrders': True,
  54. 'fetchOrder': True,
  55. 'fetchOrderBook': True,
  56. 'fetchOrders': False,
  57. 'fetchOrderTrades': True,
  58. 'fetchTime': True,
  59. 'fetchTicker': True,
  60. 'fetchTickers': True,
  61. 'fetchTrades': True,
  62. 'fetchTransactions': False,
  63. 'fetchWithdrawals': True,
  64. 'futures': True,
  65. 'withdraw': True,
  66. },
  67. 'timeframes': {
  68. '1m': '60',
  69. '3m': '180',
  70. '5m': '300',
  71. '15m': '900',
  72. '30m': '1800',
  73. '1h': '3600',
  74. '2h': '7200',
  75. '4h': '14400',
  76. '6h': '21600',
  77. '12h': '43200',
  78. '1d': '86400',
  79. '1w': '604800',
  80. '1M': '2678400',
  81. '3M': '8035200',
  82. '6M': '16070400',
  83. '1y': '31536000',
  84. },
  85. 'hostname': 'okex.com',
  86. 'urls': {
  87. 'logo': 'https://user-images.githubusercontent.com/1294454/32552768-0d6dd3c6-c4a6-11e7-90f8-c043b64756a7.jpg',
  88. 'api': {
  89. 'rest': 'https://www.{hostname}',
  90. },
  91. 'www': 'https://www.okex.com',
  92. 'doc': 'https://www.okex.com/docs/en/',
  93. 'fees': 'https://www.okex.com/pages/products/fees.html',
  94. 'referral': 'https://www.okex.com/join/1888677',
  95. 'test': {
  96. 'rest': 'https://testnet.okex.com',
  97. },
  98. },
  99. 'api': {
  100. 'general': {
  101. 'get': [
  102. 'time',
  103. ],
  104. },
  105. 'account': {
  106. 'get': [
  107. 'wallet',
  108. 'sub-account',
  109. 'asset-valuation',
  110. 'wallet/{currency}',
  111. 'withdrawal/history',
  112. 'withdrawal/history/{currency}',
  113. 'ledger',
  114. 'deposit/address',
  115. 'deposit/history',
  116. 'deposit/history/{currency}',
  117. 'currencies',
  118. 'withdrawal/fee',
  119. ],
  120. 'post': [
  121. 'transfer',
  122. 'withdrawal',
  123. ],
  124. },
  125. 'spot': {
  126. 'get': [
  127. 'accounts',
  128. 'accounts/{currency}',
  129. 'accounts/{currency}/ledger',
  130. 'orders',
  131. 'orders_pending',
  132. 'orders/{order_id}',
  133. 'orders/{client_oid}',
  134. 'trade_fee',
  135. 'fills',
  136. 'algo',
  137. # public
  138. 'instruments',
  139. 'instruments/{instrument_id}/book',
  140. 'instruments/ticker',
  141. 'instruments/{instrument_id}/ticker',
  142. 'instruments/{instrument_id}/trades',
  143. 'instruments/{instrument_id}/candles',
  144. 'instruments/{instrument_id}/history/candles',
  145. ],
  146. 'post': [
  147. 'order_algo',
  148. 'orders',
  149. 'batch_orders',
  150. 'cancel_orders/{order_id}',
  151. 'cancel_orders/{client_oid}',
  152. 'cancel_batch_algos',
  153. 'cancel_batch_orders',
  154. ],
  155. },
  156. 'margin': {
  157. 'get': [
  158. 'accounts',
  159. 'accounts/{instrument_id}',
  160. 'accounts/{instrument_id}/ledger',
  161. 'accounts/availability',
  162. 'accounts/{instrument_id}/availability',
  163. 'accounts/borrowed',
  164. 'accounts/{instrument_id}/borrowed',
  165. 'orders',
  166. 'accounts/{instrument_id}/leverage',
  167. 'orders/{order_id}',
  168. 'orders/{client_oid}',
  169. 'orders_pending',
  170. 'fills',
  171. # public
  172. 'instruments/{instrument_id}/mark_price',
  173. ],
  174. 'post': [
  175. 'accounts/borrow',
  176. 'accounts/repayment',
  177. 'orders',
  178. 'batch_orders',
  179. 'cancel_orders',
  180. 'cancel_orders/{order_id}',
  181. 'cancel_orders/{client_oid}',
  182. 'cancel_batch_orders',
  183. 'accounts/{instrument_id}/leverage',
  184. ],
  185. },
  186. 'futures': {
  187. 'get': [
  188. 'position',
  189. '{instrument_id}/position',
  190. 'accounts',
  191. 'accounts/{underlying}',
  192. 'accounts/{underlying}/leverage',
  193. 'accounts/{underlying}/ledger',
  194. 'order_algo/{instrument_id}',
  195. 'orders/{instrument_id}',
  196. 'orders/{instrument_id}/{order_id}',
  197. 'orders/{instrument_id}/{client_oid}',
  198. 'fills',
  199. 'trade_fee',
  200. 'accounts/{instrument_id}/holds',
  201. 'order_algo/{instrument_id}',
  202. # public
  203. 'instruments',
  204. 'instruments/{instrument_id}/book',
  205. 'instruments/ticker',
  206. 'instruments/{instrument_id}/ticker',
  207. 'instruments/{instrument_id}/trades',
  208. 'instruments/{instrument_id}/candles',
  209. 'instruments/{instrument_id}/history/candles',
  210. 'instruments/{instrument_id}/index',
  211. 'rate',
  212. 'instruments/{instrument_id}/estimated_price',
  213. 'instruments/{instrument_id}/open_interest',
  214. 'instruments/{instrument_id}/price_limit',
  215. 'instruments/{instrument_id}/mark_price',
  216. 'instruments/{instrument_id}/liquidation',
  217. ],
  218. 'post': [
  219. 'accounts/{underlying}/leverage',
  220. 'order',
  221. 'orders',
  222. 'cancel_order/{instrument_id}/{order_id}',
  223. 'cancel_order/{instrument_id}/{client_oid}',
  224. 'cancel_batch_orders/{instrument_id}',
  225. 'accounts/margin_mode',
  226. 'close_position',
  227. 'cancel_all',
  228. 'order_algo',
  229. 'cancel_algos',
  230. ],
  231. },
  232. 'swap': {
  233. 'get': [
  234. 'position',
  235. '{instrument_id}/position',
  236. 'accounts',
  237. '{instrument_id}/accounts',
  238. 'accounts/{instrument_id}/settings',
  239. 'accounts/{instrument_id}/ledger',
  240. 'orders/{instrument_id}',
  241. 'orders/{instrument_id}/{order_id}',
  242. 'orders/{instrument_id}/{client_oid}',
  243. 'fills',
  244. 'accounts/{instrument_id}/holds',
  245. 'trade_fee',
  246. 'order_algo/{instrument_id}',
  247. # public
  248. 'instruments',
  249. 'instruments/{instrument_id}/depth',
  250. 'instruments/ticker',
  251. 'instruments/{instrument_id}/ticker',
  252. 'instruments/{instrument_id}/trades',
  253. 'instruments/{instrument_id}/candles',
  254. 'instruments/{instrument_id}/history/candles',
  255. 'instruments/{instrument_id}/index',
  256. 'rate',
  257. 'instruments/{instrument_id}/open_interest',
  258. 'instruments/{instrument_id}/price_limit',
  259. 'instruments/{instrument_id}/liquidation',
  260. 'instruments/{instrument_id}/funding_time',
  261. 'instruments/{instrument_id}/mark_price',
  262. 'instruments/{instrument_id}/historical_funding_rate',
  263. ],
  264. 'post': [
  265. 'accounts/{instrument_id}/leverage',
  266. 'order',
  267. 'orders',
  268. 'cancel_order/{instrument_id}/{order_id}',
  269. 'cancel_order/{instrument_id}/{client_oid}',
  270. 'cancel_batch_orders/{instrument_id}',
  271. 'order_algo',
  272. 'cancel_algos',
  273. ],
  274. },
  275. 'option': {
  276. 'get': [
  277. 'accounts',
  278. '{underlying}/position',
  279. 'accounts/{underlying}',
  280. 'orders/{underlying}',
  281. 'fills/{underlying}',
  282. 'accounts/{underlying}/ledger',
  283. 'trade_fee',
  284. 'orders/{underlying}/{order_id}',
  285. 'orders/{underlying}/{client_oid}',
  286. # public
  287. 'underlying',
  288. 'instruments/{underlying}',
  289. 'instruments/{underlying}/summary',
  290. 'instruments/{underlying}/summary/{instrument_id}',
  291. 'instruments/{instrument_id}/book',
  292. 'instruments/{instrument_id}/trades',
  293. 'instruments/{instrument_id}/ticker',
  294. 'instruments/{instrument_id}/candles',
  295. ],
  296. 'post': [
  297. 'order',
  298. 'orders',
  299. 'cancel_order/{underlying}/{order_id}',
  300. 'cancel_order/{underlying}/{client_oid}',
  301. 'cancel_batch_orders/{underlying}',
  302. 'amend_order/{underlying}',
  303. 'amend_batch_orders/{underlying}',
  304. ],
  305. },
  306. 'index': {
  307. 'get': [
  308. '{instrument_id}/constituents',
  309. ],
  310. },
  311. },
  312. 'fees': {
  313. 'trading': {
  314. 'taker': 0.0015,
  315. 'maker': 0.0010,
  316. },
  317. 'spot': {
  318. 'taker': 0.0015,
  319. 'maker': 0.0010,
  320. },
  321. 'futures': {
  322. 'taker': 0.0005,
  323. 'maker': 0.0002,
  324. },
  325. 'swap': {
  326. 'taker': 0.00075,
  327. 'maker': 0.00020,
  328. },
  329. },
  330. 'requiredCredentials': {
  331. 'apiKey': True,
  332. 'secret': True,
  333. 'password': True,
  334. },
  335. 'exceptions': {
  336. # http error codes
  337. # 400 Bad Request Invalid request format
  338. # 401 Unauthorized Invalid API Key
  339. # 403 Forbidden You do not have access to the requested resource
  340. # 404 Not Found
  341. # 429 Client Error: Too Many Requests for url
  342. # 500 Internal Server Error We had a problem with our server
  343. 'exact': {
  344. '1': ExchangeError, # {"code": 1, "message": "System error"}
  345. # undocumented
  346. 'failure to get a peer from the ring-balancer': ExchangeNotAvailable, # {"message": "failure to get a peer from the ring-balancer"}
  347. 'Server is busy, please try again': ExchangeNotAvailable, # {"message": "Server is busy, please try again."}
  348. 'An unexpected error occurred': ExchangeError, # {"message": "An unexpected error occurred"}
  349. 'System error': ExchangeError, # {"error_message":"System error","message":"System error"}
  350. '4010': PermissionDenied, # {"code": 4010, "message": "For the security of your funds, withdrawals are not permitted within 24 hours after changing fund password / mobile number / Google Authenticator settings "}
  351. # common
  352. # '0': ExchangeError, # 200 successful,when the order placement / cancellation / operation is successful
  353. '4001': ExchangeError, # no data received in 30s
  354. '4002': ExchangeError, # Buffer full. cannot write data
  355. # --------------------------------------------------------
  356. '30001': AuthenticationError, # {"code": 30001, "message": 'request header "OK_ACCESS_KEY" cannot be blank'}
  357. '30002': AuthenticationError, # {"code": 30002, "message": 'request header "OK_ACCESS_SIGN" cannot be blank'}
  358. '30003': AuthenticationError, # {"code": 30003, "message": 'request header "OK_ACCESS_TIMESTAMP" cannot be blank'}
  359. '30004': AuthenticationError, # {"code": 30004, "message": 'request header "OK_ACCESS_PASSPHRASE" cannot be blank'}
  360. '30005': InvalidNonce, # {"code": 30005, "message": "invalid OK_ACCESS_TIMESTAMP"}
  361. '30006': AuthenticationError, # {"code": 30006, "message": "invalid OK_ACCESS_KEY"}
  362. '30007': BadRequest, # {"code": 30007, "message": 'invalid Content_Type, please use "application/json" format'}
  363. '30008': RequestTimeout, # {"code": 30008, "message": "timestamp request expired"}
  364. '30009': ExchangeError, # {"code": 30009, "message": "system error"}
  365. '30010': AuthenticationError, # {"code": 30010, "message": "API validation failed"}
  366. '30011': PermissionDenied, # {"code": 30011, "message": "invalid IP"}
  367. '30012': AuthenticationError, # {"code": 30012, "message": "invalid authorization"}
  368. '30013': AuthenticationError, # {"code": 30013, "message": "invalid sign"}
  369. '30014': DDoSProtection, # {"code": 30014, "message": "request too frequent"}
  370. '30015': AuthenticationError, # {"code": 30015, "message": 'request header "OK_ACCESS_PASSPHRASE" incorrect'}
  371. '30016': ExchangeError, # {"code": 30015, "message": "you are using v1 apiKey, please use v1 endpoint. If you would like to use v3 endpoint, please subscribe to v3 apiKey"}
  372. '30017': ExchangeError, # {"code": 30017, "message": "apikey's broker id does not match"}
  373. '30018': ExchangeError, # {"code": 30018, "message": "apikey's domain does not match"}
  374. '30019': ExchangeNotAvailable, # {"code": 30019, "message": "Api is offline or unavailable"}
  375. '30020': BadRequest, # {"code": 30020, "message": "body cannot be blank"}
  376. '30021': BadRequest, # {"code": 30021, "message": "Json data format error"}, {"code": 30021, "message": "json data format error"}
  377. '30022': PermissionDenied, # {"code": 30022, "message": "Api has been frozen"}
  378. '30023': BadRequest, # {"code": 30023, "message": "{0} parameter cannot be blank"}
  379. '30024': BadSymbol, # {"code":30024,"message":"\"instrument_id\" is an invalid parameter"}
  380. '30025': BadRequest, # {"code": 30025, "message": "{0} parameter category error"}
  381. '30026': DDoSProtection, # {"code": 30026, "message": "requested too frequent"}
  382. '30027': AuthenticationError, # {"code": 30027, "message": "login failure"}
  383. '30028': PermissionDenied, # {"code": 30028, "message": "unauthorized execution"}
  384. '30029': AccountSuspended, # {"code": 30029, "message": "account suspended"}
  385. '30030': ExchangeNotAvailable, # {"code": 30030, "message": "endpoint request failed. Please try again"}
  386. '30031': BadRequest, # {"code": 30031, "message": "token does not exist"}
  387. '30032': BadSymbol, # {"code": 30032, "message": "pair does not exist"}
  388. '30033': BadRequest, # {"code": 30033, "message": "exchange domain does not exist"}
  389. '30034': ExchangeError, # {"code": 30034, "message": "exchange ID does not exist"}
  390. '30035': ExchangeError, # {"code": 30035, "message": "trading is not supported in self website"}
  391. '30036': ExchangeError, # {"code": 30036, "message": "no relevant data"}
  392. '30037': ExchangeNotAvailable, # {"code": 30037, "message": "endpoint is offline or unavailable"}
  393. # '30038': AuthenticationError, # {"code": 30038, "message": "user does not exist"}
  394. '30038': OnMaintenance, # {"client_oid":"","code":"30038","error_code":"30038","error_message":"Matching engine is being upgraded. Please try in about 1 minute.","message":"Matching engine is being upgraded. Please try in about 1 minute.","order_id":"-1","result":false}
  395. '30044': RequestTimeout, # {"code":30044, "message":"Endpoint request timeout"}
  396. # futures
  397. '32001': AccountSuspended, # {"code": 32001, "message": "futures account suspended"}
  398. '32002': PermissionDenied, # {"code": 32002, "message": "futures account does not exist"}
  399. '32003': CancelPending, # {"code": 32003, "message": "canceling, please wait"}
  400. '32004': ExchangeError, # {"code": 32004, "message": "you have no unfilled orders"}
  401. '32005': InvalidOrder, # {"code": 32005, "message": "max order quantity"}
  402. '32006': InvalidOrder, # {"code": 32006, "message": "the order price or trigger price exceeds USD 1 million"}
  403. '32007': InvalidOrder, # {"code": 32007, "message": "leverage level must be the same for orders on the same side of the contract"}
  404. '32008': InvalidOrder, # {"code": 32008, "message": "Max. positions to open(cross margin)"}
  405. '32009': InvalidOrder, # {"code": 32009, "message": "Max. positions to open(fixed margin)"}
  406. '32010': ExchangeError, # {"code": 32010, "message": "leverage cannot be changed with open positions"}
  407. '32011': ExchangeError, # {"code": 32011, "message": "futures status error"}
  408. '32012': ExchangeError, # {"code": 32012, "message": "futures order update error"}
  409. '32013': ExchangeError, # {"code": 32013, "message": "token type is blank"}
  410. '32014': ExchangeError, # {"code": 32014, "message": "your number of contracts closing is larger than the number of contracts available"}
  411. '32015': ExchangeError, # {"code": 32015, "message": "margin ratio is lower than 100% before opening positions"}
  412. '32016': ExchangeError, # {"code": 32016, "message": "margin ratio is lower than 100% after opening position"}
  413. '32017': ExchangeError, # {"code": 32017, "message": "no BBO"}
  414. '32018': ExchangeError, # {"code": 32018, "message": "the order quantity is less than 1, please try again"}
  415. '32019': ExchangeError, # {"code": 32019, "message": "the order price deviates from the price of the previous minute by more than 3%"}
  416. '32020': ExchangeError, # {"code": 32020, "message": "the price is not in the range of the price limit"}
  417. '32021': ExchangeError, # {"code": 32021, "message": "leverage error"}
  418. '32022': ExchangeError, # {"code": 32022, "message": "self function is not supported in your country or region according to the regulations"}
  419. '32023': ExchangeError, # {"code": 32023, "message": "self account has outstanding loan"}
  420. '32024': ExchangeError, # {"code": 32024, "message": "order cannot be placed during delivery"}
  421. '32025': ExchangeError, # {"code": 32025, "message": "order cannot be placed during settlement"}
  422. '32026': ExchangeError, # {"code": 32026, "message": "your account is restricted from opening positions"}
  423. '32027': ExchangeError, # {"code": 32027, "message": "cancelled over 20 orders"}
  424. '32028': ExchangeError, # {"code": 32028, "message": "account is suspended and liquidated"}
  425. '32029': ExchangeError, # {"code": 32029, "message": "order info does not exist"}
  426. '32030': InvalidOrder, # The order cannot be cancelled
  427. '32031': ArgumentsRequired, # client_oid or order_id is required.
  428. '32038': AuthenticationError, # User does not exist
  429. '32040': ExchangeError, # User have open contract orders or position
  430. '32044': ExchangeError, # {"code": 32044, "message": "The margin ratio after submitting self order is lower than the minimum requirement({0}) for your tier."}
  431. '32045': ExchangeError, # String of commission over 1 million
  432. '32046': ExchangeError, # Each user can hold up to 10 trade plans at the same time
  433. '32047': ExchangeError, # system error
  434. '32048': InvalidOrder, # Order strategy track range error
  435. '32049': ExchangeError, # Each user can hold up to 10 track plans at the same time
  436. '32050': InvalidOrder, # Order strategy rang error
  437. '32051': InvalidOrder, # Order strategy ice depth error
  438. '32052': ExchangeError, # String of commission over 100 thousand
  439. '32053': ExchangeError, # Each user can hold up to 6 ice plans at the same time
  440. '32057': ExchangeError, # The order price is zero. Market-close-all function cannot be executed
  441. '32054': ExchangeError, # Trade not allow
  442. '32055': InvalidOrder, # cancel order error
  443. '32056': ExchangeError, # iceberg per order average should between {0}-{1} contracts
  444. '32058': ExchangeError, # Each user can hold up to 6 initiative plans at the same time
  445. '32059': InvalidOrder, # Total amount should exceed per order amount
  446. '32060': InvalidOrder, # Order strategy type error
  447. '32061': InvalidOrder, # Order strategy initiative limit error
  448. '32062': InvalidOrder, # Order strategy initiative range error
  449. '32063': InvalidOrder, # Order strategy initiative rate error
  450. '32064': ExchangeError, # Time Stringerval of orders should set between 5-120s
  451. '32065': ExchangeError, # Close amount exceeds the limit of Market-close-all(999 for BTC, and 9999 for the rest tokens)
  452. '32066': ExchangeError, # You have open orders. Please cancel all open orders before changing your leverage level.
  453. '32067': ExchangeError, # Account equity < required margin in self setting. Please adjust your leverage level again.
  454. '32068': ExchangeError, # The margin for self position will fall short of the required margin in self setting. Please adjust your leverage level or increase your margin to proceed.
  455. '32069': ExchangeError, # Target leverage level too low. Your account balance is insufficient to cover the margin required. Please adjust the leverage level again.
  456. '32070': ExchangeError, # Please check open position or unfilled order
  457. '32071': ExchangeError, # Your current liquidation mode does not support self action.
  458. '32072': ExchangeError, # The highest available margin for your orders tier is {0}. Please edit your margin and place a new order.
  459. '32073': ExchangeError, # The action does not apply to the token
  460. '32074': ExchangeError, # The number of contracts of your position, open orders, and the current order has exceeded the maximum order limit of self asset.
  461. '32075': ExchangeError, # Account risk rate breach
  462. '32076': ExchangeError, # Liquidation of the holding position(s) at market price will require cancellation of all pending close orders of the contracts.
  463. '32077': ExchangeError, # Your margin for self asset in futures account is insufficient and the position has been taken over for liquidation.(You will not be able to place orders, close positions, transfer funds, or add margin during self period of time. Your account will be restored after the liquidation is complete.)
  464. '32078': ExchangeError, # Please cancel all open orders before switching the liquidation mode(Please cancel all open orders before switching the liquidation mode)
  465. '32079': ExchangeError, # Your open positions are at high risk.(Please add margin or reduce positions before switching the mode)
  466. '32080': ExchangeError, # Funds cannot be transferred out within 30 minutes after futures settlement
  467. '32083': ExchangeError, # The number of contracts should be a positive multiple of %%. Please place your order again
  468. # token and margin trading
  469. '33001': PermissionDenied, # {"code": 33001, "message": "margin account for self pair is not enabled yet"}
  470. '33002': AccountSuspended, # {"code": 33002, "message": "margin account for self pair is suspended"}
  471. '33003': InsufficientFunds, # {"code": 33003, "message": "no loan balance"}
  472. '33004': ExchangeError, # {"code": 33004, "message": "loan amount cannot be smaller than the minimum limit"}
  473. '33005': ExchangeError, # {"code": 33005, "message": "repayment amount must exceed 0"}
  474. '33006': ExchangeError, # {"code": 33006, "message": "loan order not found"}
  475. '33007': ExchangeError, # {"code": 33007, "message": "status not found"}
  476. '33008': InsufficientFunds, # {"code": 33008, "message": "loan amount cannot exceed the maximum limit"}
  477. '33009': ExchangeError, # {"code": 33009, "message": "user ID is blank"}
  478. '33010': ExchangeError, # {"code": 33010, "message": "you cannot cancel an order during session 2 of call auction"}
  479. '33011': ExchangeError, # {"code": 33011, "message": "no new market data"}
  480. '33012': ExchangeError, # {"code": 33012, "message": "order cancellation failed"}
  481. '33013': InvalidOrder, # {"code": 33013, "message": "order placement failed"}
  482. '33014': OrderNotFound, # {"code": 33014, "message": "order does not exist"}
  483. '33015': InvalidOrder, # {"code": 33015, "message": "exceeded maximum limit"}
  484. '33016': ExchangeError, # {"code": 33016, "message": "margin trading is not open for self token"}
  485. '33017': InsufficientFunds, # {"code": 33017, "message": "insufficient balance"}
  486. '33018': ExchangeError, # {"code": 33018, "message": "self parameter must be smaller than 1"}
  487. '33020': ExchangeError, # {"code": 33020, "message": "request not supported"}
  488. '33021': BadRequest, # {"code": 33021, "message": "token and the pair do not match"}
  489. '33022': InvalidOrder, # {"code": 33022, "message": "pair and the order do not match"}
  490. '33023': ExchangeError, # {"code": 33023, "message": "you can only place market orders during call auction"}
  491. '33024': InvalidOrder, # {"code": 33024, "message": "trading amount too small"}
  492. '33025': InvalidOrder, # {"code": 33025, "message": "base token amount is blank"}
  493. '33026': ExchangeError, # {"code": 33026, "message": "transaction completed"}
  494. '33027': InvalidOrder, # {"code": 33027, "message": "cancelled order or order cancelling"}
  495. '33028': InvalidOrder, # {"code": 33028, "message": "the decimal places of the trading price exceeded the limit"}
  496. '33029': InvalidOrder, # {"code": 33029, "message": "the decimal places of the trading size exceeded the limit"}
  497. '33034': ExchangeError, # {"code": 33034, "message": "You can only place limit order after Call Auction has started"}
  498. '33035': ExchangeError, # This type of order cannot be canceled(This type of order cannot be canceled)
  499. '33036': ExchangeError, # Exceeding the limit of entrust order
  500. '33037': ExchangeError, # The buy order price should be lower than 130% of the trigger price
  501. '33038': ExchangeError, # The sell order price should be higher than 70% of the trigger price
  502. '33039': ExchangeError, # The limit of callback rate is 0 < x <= 5%
  503. '33040': ExchangeError, # The trigger price of a buy order should be lower than the latest transaction price
  504. '33041': ExchangeError, # The trigger price of a sell order should be higher than the latest transaction price
  505. '33042': ExchangeError, # The limit of price variance is 0 < x <= 1%
  506. '33043': ExchangeError, # The total amount must be larger than 0
  507. '33044': ExchangeError, # The average amount should be 1/1000 * total amount <= x <= total amount
  508. '33045': ExchangeError, # The price should not be 0, including trigger price, order price, and price limit
  509. '33046': ExchangeError, # Price variance should be 0 < x <= 1%
  510. '33047': ExchangeError, # Sweep ratio should be 0 < x <= 100%
  511. '33048': ExchangeError, # Per order limit: Total amount/1000 < x <= Total amount
  512. '33049': ExchangeError, # Total amount should be X > 0
  513. '33050': ExchangeError, # Time interval should be 5 <= x <= 120s
  514. '33051': ExchangeError, # cancel order number not higher limit: plan and track entrust no more than 10, ice and time entrust no more than 6
  515. '33059': BadRequest, # {"code": 33059, "message": "client_oid or order_id is required"}
  516. '33060': BadRequest, # {"code": 33060, "message": "Only fill in either parameter client_oid or order_id"}
  517. '33061': ExchangeError, # Value of a single market price order cannot exceed 100,000 USD
  518. '33062': ExchangeError, # The leverage ratio is too high. The borrowed position has exceeded the maximum position of self leverage ratio. Please readjust the leverage ratio
  519. '33063': ExchangeError, # Leverage multiple is too low, there is insufficient margin in the account, please readjust the leverage ratio
  520. '33064': ExchangeError, # The setting of the leverage ratio cannot be less than 2, please readjust the leverage ratio
  521. '33065': ExchangeError, # Leverage ratio exceeds maximum leverage ratio, please readjust leverage ratio
  522. '33085': InvalidOrder, # The value of the position and buying order has reached the position limit, and no further buying is allowed.
  523. # account
  524. '21009': ExchangeError, # Funds cannot be transferred out within 30 minutes after swap settlement(Funds cannot be transferred out within 30 minutes after swap settlement)
  525. '34001': PermissionDenied, # {"code": 34001, "message": "withdrawal suspended"}
  526. '34002': InvalidAddress, # {"code": 34002, "message": "please add a withdrawal address"}
  527. '34003': ExchangeError, # {"code": 34003, "message": "sorry, self token cannot be withdrawn to xx at the moment"}
  528. '34004': ExchangeError, # {"code": 34004, "message": "withdrawal fee is smaller than minimum limit"}
  529. '34005': ExchangeError, # {"code": 34005, "message": "withdrawal fee exceeds the maximum limit"}
  530. '34006': ExchangeError, # {"code": 34006, "message": "withdrawal amount is lower than the minimum limit"}
  531. '34007': ExchangeError, # {"code": 34007, "message": "withdrawal amount exceeds the maximum limit"}
  532. '34008': InsufficientFunds, # {"code": 34008, "message": "insufficient balance"}
  533. '34009': ExchangeError, # {"code": 34009, "message": "your withdrawal amount exceeds the daily limit"}
  534. '34010': ExchangeError, # {"code": 34010, "message": "transfer amount must be larger than 0"}
  535. '34011': ExchangeError, # {"code": 34011, "message": "conditions not met"}
  536. '34012': ExchangeError, # {"code": 34012, "message": "the minimum withdrawal amount for NEO is 1, and the amount must be an integer"}
  537. '34013': ExchangeError, # {"code": 34013, "message": "please transfer"}
  538. '34014': ExchangeError, # {"code": 34014, "message": "transfer limited"}
  539. '34015': ExchangeError, # {"code": 34015, "message": "subaccount does not exist"}
  540. '34016': PermissionDenied, # {"code": 34016, "message": "transfer suspended"}
  541. '34017': AccountSuspended, # {"code": 34017, "message": "account suspended"}
  542. '34018': AuthenticationError, # {"code": 34018, "message": "incorrect trades password"}
  543. '34019': PermissionDenied, # {"code": 34019, "message": "please bind your email before withdrawal"}
  544. '34020': PermissionDenied, # {"code": 34020, "message": "please bind your funds password before withdrawal"}
  545. '34021': InvalidAddress, # {"code": 34021, "message": "Not verified address"}
  546. '34022': ExchangeError, # {"code": 34022, "message": "Withdrawals are not available for sub accounts"}
  547. '34023': PermissionDenied, # {"code": 34023, "message": "Please enable futures trading before transferring your funds"}
  548. '34026': ExchangeError, # transfer too frequently(transfer too frequently)
  549. '34036': ExchangeError, # Parameter is incorrect, please refer to API documentation
  550. '34037': ExchangeError, # Get the sub-account balance interface, account type is not supported
  551. '34038': ExchangeError, # Since your C2C transaction is unusual, you are restricted from fund transfer. Please contact our customer support to cancel the restriction
  552. '34039': ExchangeError, # You are now restricted from transferring out your funds due to abnormal trades on C2C Market. Please transfer your fund on our website or app instead to verify your identity
  553. # swap
  554. '35001': ExchangeError, # {"code": 35001, "message": "Contract does not exist"}
  555. '35002': ExchangeError, # {"code": 35002, "message": "Contract settling"}
  556. '35003': ExchangeError, # {"code": 35003, "message": "Contract paused"}
  557. '35004': ExchangeError, # {"code": 35004, "message": "Contract pending settlement"}
  558. '35005': AuthenticationError, # {"code": 35005, "message": "User does not exist"}
  559. '35008': InvalidOrder, # {"code": 35008, "message": "Risk ratio too high"}
  560. '35010': InvalidOrder, # {"code": 35010, "message": "Position closing too large"}
  561. '35012': InvalidOrder, # {"code": 35012, "message": "Incorrect order size"}
  562. '35014': InvalidOrder, # {"code": 35014, "message": "Order price is not within limit"}
  563. '35015': InvalidOrder, # {"code": 35015, "message": "Invalid leverage level"}
  564. '35017': ExchangeError, # {"code": 35017, "message": "Open orders exist"}
  565. '35019': InvalidOrder, # {"code": 35019, "message": "Order size too large"}
  566. '35020': InvalidOrder, # {"code": 35020, "message": "Order price too high"}
  567. '35021': InvalidOrder, # {"code": 35021, "message": "Order size exceeded current tier limit"}
  568. '35022': BadRequest, # {"code": 35022, "message": "Contract status error"}
  569. '35024': BadRequest, # {"code": 35024, "message": "Contract not initialized"}
  570. '35025': InsufficientFunds, # {"code": 35025, "message": "No account balance"}
  571. '35026': BadRequest, # {"code": 35026, "message": "Contract settings not initialized"}
  572. '35029': OrderNotFound, # {"code": 35029, "message": "Order does not exist"}
  573. '35030': InvalidOrder, # {"code": 35030, "message": "Order size too large"}
  574. '35031': InvalidOrder, # {"code": 35031, "message": "Cancel order size too large"}
  575. '35032': ExchangeError, # {"code": 35032, "message": "Invalid user status"}
  576. '35037': ExchangeError, # No last traded price in cache
  577. '35039': ExchangeError, # {"code": 35039, "message": "Open order quantity exceeds limit"}
  578. '35040': InvalidOrder, # {"error_message":"Invalid order type","result":"true","error_code":"35040","order_id":"-1"}
  579. '35044': ExchangeError, # {"code": 35044, "message": "Invalid order status"}
  580. '35046': InsufficientFunds, # {"code": 35046, "message": "Negative account balance"}
  581. '35047': InsufficientFunds, # {"code": 35047, "message": "Insufficient account balance"}
  582. '35048': ExchangeError, # {"code": 35048, "message": "User contract is frozen and liquidating"}
  583. '35049': InvalidOrder, # {"code": 35049, "message": "Invalid order type"}
  584. '35050': InvalidOrder, # {"code": 35050, "message": "Position settings are blank"}
  585. '35052': InsufficientFunds, # {"code": 35052, "message": "Insufficient cross margin"}
  586. '35053': ExchangeError, # {"code": 35053, "message": "Account risk too high"}
  587. '35055': InsufficientFunds, # {"code": 35055, "message": "Insufficient account balance"}
  588. '35057': ExchangeError, # {"code": 35057, "message": "No last traded price"}
  589. '35058': ExchangeError, # {"code": 35058, "message": "No limit"}
  590. '35059': BadRequest, # {"code": 35059, "message": "client_oid or order_id is required"}
  591. '35060': BadRequest, # {"code": 35060, "message": "Only fill in either parameter client_oid or order_id"}
  592. '35061': BadRequest, # {"code": 35061, "message": "Invalid instrument_id"}
  593. '35062': InvalidOrder, # {"code": 35062, "message": "Invalid match_price"}
  594. '35063': InvalidOrder, # {"code": 35063, "message": "Invalid order_size"}
  595. '35064': InvalidOrder, # {"code": 35064, "message": "Invalid client_oid"}
  596. '35066': InvalidOrder, # Order interval error
  597. '35067': InvalidOrder, # Time-weighted order ratio error
  598. '35068': InvalidOrder, # Time-weighted order range error
  599. '35069': InvalidOrder, # Time-weighted single transaction limit error
  600. '35070': InvalidOrder, # Algo order type error
  601. '35071': InvalidOrder, # Order total must be larger than single order limit
  602. '35072': InvalidOrder, # Maximum 6 unfulfilled time-weighted orders can be held at the same time
  603. '35073': InvalidOrder, # Order price is 0. Market-close-all not available
  604. '35074': InvalidOrder, # Iceberg order single transaction average error
  605. '35075': InvalidOrder, # Failed to cancel order
  606. '35076': InvalidOrder, # LTC 20x leverage. Not allowed to open position
  607. '35077': InvalidOrder, # Maximum 6 unfulfilled iceberg orders can be held at the same time
  608. '35078': InvalidOrder, # Order amount exceeded 100,000
  609. '35079': InvalidOrder, # Iceberg order price variance error
  610. '35080': InvalidOrder, # Callback rate error
  611. '35081': InvalidOrder, # Maximum 10 unfulfilled trail orders can be held at the same time
  612. '35082': InvalidOrder, # Trail order callback rate error
  613. '35083': InvalidOrder, # Each user can only hold a maximum of 10 unfulfilled stop-limit orders at the same time
  614. '35084': InvalidOrder, # Order amount exceeded 1 million
  615. '35085': InvalidOrder, # Order amount is not in the correct range
  616. '35086': InvalidOrder, # Price exceeds 100 thousand
  617. '35087': InvalidOrder, # Price exceeds 100 thousand
  618. '35088': InvalidOrder, # Average amount error
  619. '35089': InvalidOrder, # Price exceeds 100 thousand
  620. '35090': ExchangeError, # No stop-limit orders available for cancelation
  621. '35091': ExchangeError, # No trail orders available for cancellation
  622. '35092': ExchangeError, # No iceberg orders available for cancellation
  623. '35093': ExchangeError, # No trail orders available for cancellation
  624. '35094': ExchangeError, # Stop-limit order last traded price error
  625. '35095': BadRequest, # Instrument_id error
  626. '35096': ExchangeError, # Algo order status error
  627. '35097': ExchangeError, # Order status and order ID cannot exist at the same time
  628. '35098': ExchangeError, # An order status or order ID must exist
  629. '35099': ExchangeError, # Algo order ID error
  630. # option
  631. '36001': BadRequest, # Invalid underlying index.
  632. '36002': BadRequest, # Instrument does not exist.
  633. '36005': ExchangeError, # Instrument status is invalid.
  634. '36101': AuthenticationError, # Account does not exist.
  635. '36102': PermissionDenied, # Account status is invalid.
  636. '36103': PermissionDenied, # Account is suspended due to ongoing liquidation.
  637. '36104': PermissionDenied, # Account is not enabled for options trading.
  638. '36105': PermissionDenied, # Please enable the account for option contract.
  639. '36106': PermissionDenied, # Funds cannot be transferred in or out, as account is suspended.
  640. '36107': PermissionDenied, # Funds cannot be transferred out within 30 minutes after option exercising or settlement.
  641. '36108': InsufficientFunds, # Funds cannot be transferred in or out, as equity of the account is less than zero.
  642. '36109': PermissionDenied, # Funds cannot be transferred in or out during option exercising or settlement.
  643. '36201': PermissionDenied, # New order function is blocked.
  644. '36202': PermissionDenied, # Account does not have permission to short option.
  645. '36203': InvalidOrder, # Invalid format for client_oid.
  646. '36204': ExchangeError, # Invalid format for request_id.
  647. '36205': BadRequest, # Instrument id does not match underlying index.
  648. '36206': BadRequest, # Order_id and client_oid can not be used at the same time.
  649. '36207': InvalidOrder, # Either order price or fartouch price must be present.
  650. '36208': InvalidOrder, # Either order price or size must be present.
  651. '36209': InvalidOrder, # Either order_id or client_oid must be present.
  652. '36210': InvalidOrder, # Either order_ids or client_oids must be present.
  653. '36211': InvalidOrder, # Exceeding max batch size for order submission.
  654. '36212': InvalidOrder, # Exceeding max batch size for oder cancellation.
  655. '36213': InvalidOrder, # Exceeding max batch size for order amendment.
  656. '36214': ExchangeError, # Instrument does not have valid bid/ask quote.
  657. '36216': OrderNotFound, # Order does not exist.
  658. '36217': InvalidOrder, # Order submission failed.
  659. '36218': InvalidOrder, # Order cancellation failed.
  660. '36219': InvalidOrder, # Order amendment failed.
  661. '36220': InvalidOrder, # Order is pending cancel.
  662. '36221': InvalidOrder, # Order qty is not valid multiple of lot size.
  663. '36222': InvalidOrder, # Order price is breaching highest buy limit.
  664. '36223': InvalidOrder, # Order price is breaching lowest sell limit.
  665. '36224': InvalidOrder, # Exceeding max order size.
  666. '36225': InvalidOrder, # Exceeding max open order count for instrument.
  667. '36226': InvalidOrder, # Exceeding max open order count for underlying.
  668. '36227': InvalidOrder, # Exceeding max open size across all orders for underlying
  669. '36228': InvalidOrder, # Exceeding max available qty for instrument.
  670. '36229': InvalidOrder, # Exceeding max available qty for underlying.
  671. '36230': InvalidOrder, # Exceeding max position limit for underlying.
  672. },
  673. 'broad': {
  674. },
  675. },
  676. 'precisionMode': TICK_SIZE,
  677. 'options': {
  678. 'fetchOHLCV': {
  679. 'type': 'Candles', # Candles or HistoryCandles
  680. },
  681. 'createMarketBuyOrderRequiresPrice': True,
  682. 'fetchMarkets': ['spot', 'futures', 'swap', 'option'],
  683. 'defaultType': 'spot', # 'account', 'spot', 'margin', 'futures', 'swap', 'option'
  684. 'auth': {
  685. 'time': 'public',
  686. 'currencies': 'private',
  687. 'instruments': 'public',
  688. 'rate': 'public',
  689. '{instrument_id}/constituents': 'public',
  690. },
  691. },
  692. 'commonCurrencies': {
  693. # OKEX refers to ERC20 version of Aeternity(AEToken)
  694. 'AE': 'AET', # https://github.com/ccxt/ccxt/issues/4981
  695. 'HOT': 'Hydro Protocol',
  696. 'HSR': 'HC',
  697. 'MAG': 'Maggie',
  698. 'YOYO': 'YOYOW',
  699. 'WIN': 'WinToken', # https://github.com/ccxt/ccxt/issues/5701
  700. },
  701. })
  702. def fetch_time(self, params={}):
  703. response = self.generalGetTime(params)
  704. #
  705. # {
  706. # "iso": "2015-01-07T23:47:25.201Z",
  707. # "epoch": 1420674445.201
  708. # }
  709. #
  710. return self.parse8601(self.safe_string(response, 'iso'))
  711. def fetch_markets(self, params={}):
  712. types = self.safe_value(self.options, 'fetchMarkets')
  713. result = []
  714. for i in range(0, len(types)):
  715. markets = self.fetch_markets_by_type(types[i], params)
  716. result = self.array_concat(result, markets)
  717. return result
  718. def parse_markets(self, markets):
  719. result = []
  720. for i in range(0, len(markets)):
  721. result.append(self.parse_market(markets[i]))
  722. return result
  723. def parse_market(self, market):
  724. #
  725. # spot markets
  726. #
  727. # {
  728. # base_currency: "EOS",
  729. # instrument_id: "EOS-OKB",
  730. # min_size: "0.01",
  731. # quote_currency: "OKB",
  732. # size_increment: "0.000001",
  733. # tick_size: "0.0001"
  734. # }
  735. #
  736. # futures markets
  737. #
  738. # {
  739. # instrument_id: "XRP-USD-200320",
  740. # underlying_index: "XRP",
  741. # quote_currency: "USD",
  742. # tick_size: "0.0001",
  743. # contract_val: "10",
  744. # listing: "2020-03-06",
  745. # delivery: "2020-03-20",
  746. # trade_increment: "1",
  747. # alias: "self_week",
  748. # underlying: "XRP-USD",
  749. # base_currency: "XRP",
  750. # settlement_currency: "XRP",
  751. # is_inverse: "true",
  752. # contract_val_currency: "USD",
  753. # }
  754. #
  755. # swap markets
  756. #
  757. # {
  758. # instrument_id: "BSV-USD-SWAP",
  759. # underlying_index: "BSV",
  760. # quote_currency: "USD",
  761. # coin: "BSV",
  762. # contract_val: "10",
  763. # listing: "2018-12-21T07:53:47.000Z",
  764. # delivery: "2020-03-14T08:00:00.000Z",
  765. # size_increment: "1",
  766. # tick_size: "0.01",
  767. # base_currency: "BSV",
  768. # underlying: "BSV-USD",
  769. # settlement_currency: "BSV",
  770. # is_inverse: "true",
  771. # contract_val_currency: "USD"
  772. # }
  773. #
  774. # options markets
  775. #
  776. # {
  777. # instrument_id: 'BTC-USD-200327-4000-C',
  778. # underlying: 'BTC-USD',
  779. # settlement_currency: 'BTC',
  780. # contract_val: '0.1000',
  781. # option_type: 'C',
  782. # strike: '4000',
  783. # tick_size: '0.0005',
  784. # lot_size: '1.0000',
  785. # listing: '2019-12-25T08:30:36.302Z',
  786. # delivery: '2020-03-27T08:00:00.000Z',
  787. # state: '2',
  788. # trading_start_time: '2019-12-25T08:30:36.302Z',
  789. # timestamp: '2020-03-13T08:05:09.456Z',
  790. # }
  791. #
  792. id = self.safe_string(market, 'instrument_id')
  793. marketType = 'spot'
  794. spot = True
  795. future = False
  796. swap = False
  797. option = False
  798. baseId = self.safe_string(market, 'base_currency')
  799. quoteId = self.safe_string(market, 'quote_currency')
  800. contractVal = self.safe_float(market, 'contract_val')
  801. if contractVal is not None:
  802. if 'option_type' in market:
  803. marketType = 'option'
  804. spot = False
  805. option = True
  806. underlying = self.safe_string(market, 'underlying')
  807. parts = underlying.split('-')
  808. baseId = self.safe_string(parts, 0)
  809. quoteId = self.safe_string(parts, 1)
  810. else:
  811. marketType = 'swap'
  812. spot = False
  813. swap = True
  814. futuresAlias = self.safe_string(market, 'alias')
  815. if futuresAlias is not None:
  816. swap = False
  817. future = True
  818. marketType = 'futures'
  819. baseId = self.safe_string(market, 'underlying_index')
  820. base = self.safe_currency_code(baseId)
  821. quote = self.safe_currency_code(quoteId)
  822. symbol = (base + '/' + quote) if spot else id
  823. lotSize = self.safe_float_2(market, 'lot_size', 'trade_increment')
  824. precision = {
  825. 'amount': self.safe_float(market, 'size_increment', lotSize),
  826. 'price': self.safe_float(market, 'tick_size'),
  827. }
  828. minAmount = self.safe_float_2(market, 'min_size', 'base_min_size')
  829. active = True
  830. fees = self.safe_value_2(self.fees, marketType, 'trading', {})
  831. return self.extend(fees, {
  832. 'id': id,
  833. 'symbol': symbol,
  834. 'base': base,
  835. 'quote': quote,
  836. 'baseId': baseId,
  837. 'quoteId': quoteId,
  838. 'info': market,
  839. 'type': marketType,
  840. 'spot': spot,
  841. 'futures': future,
  842. 'swap': swap,
  843. 'option': option,
  844. 'active': active,
  845. 'precision': precision,
  846. 'limits': {
  847. 'amount': {
  848. 'min': minAmount,
  849. 'max': None,
  850. },
  851. 'price': {
  852. 'min': precision['price'],
  853. 'max': None,
  854. },
  855. 'cost': {
  856. 'min': precision['price'],
  857. 'max': None,
  858. },
  859. },
  860. })
  861. def fetch_markets_by_type(self, type, params={}):
  862. if type == 'option':
  863. underlying = self.optionGetUnderlying(params)
  864. result = []
  865. for i in range(0, len(underlying)):
  866. response = self.optionGetInstrumentsUnderlying({
  867. 'underlying': underlying[i],
  868. })
  869. #
  870. # options markets
  871. #
  872. # [
  873. # {
  874. # instrument_id: 'BTC-USD-200327-4000-C',
  875. # underlying: 'BTC-USD',
  876. # settlement_currency: 'BTC',
  877. # contract_val: '0.1000',
  878. # option_type: 'C',
  879. # strike: '4000',
  880. # tick_size: '0.0005',
  881. # lot_size: '1.0000',
  882. # listing: '2019-12-25T08:30:36.302Z',
  883. # delivery: '2020-03-27T08:00:00.000Z',
  884. # state: '2',
  885. # trading_start_time: '2019-12-25T08:30:36.302Z',
  886. # timestamp: '2020-03-13T08:05:09.456Z',
  887. # },
  888. # ]
  889. #
  890. result = self.array_concat(result, response)
  891. return self.parse_markets(result)
  892. elif (type == 'spot') or (type == 'futures') or (type == 'swap'):
  893. method = type + 'GetInstruments'
  894. response = getattr(self, method)(params)
  895. #
  896. # spot markets
  897. #
  898. # [
  899. # {
  900. # base_currency: "EOS",
  901. # instrument_id: "EOS-OKB",
  902. # min_size: "0.01",
  903. # quote_currency: "OKB",
  904. # size_increment: "0.000001",
  905. # tick_size: "0.0001"
  906. # }
  907. # ]
  908. #
  909. # futures markets
  910. #
  911. # [
  912. # {
  913. # instrument_id: "XRP-USD-200320",
  914. # underlying_index: "XRP",
  915. # quote_currency: "USD",
  916. # tick_size: "0.0001",
  917. # contract_val: "10",
  918. # listing: "2020-03-06",
  919. # delivery: "2020-03-20",
  920. # trade_increment: "1",
  921. # alias: "self_week",
  922. # underlying: "XRP-USD",
  923. # base_currency: "XRP",
  924. # settlement_currency: "XRP",
  925. # is_inverse: "true",
  926. # contract_val_currency: "USD",
  927. # }
  928. # ]
  929. #
  930. # swap markets
  931. #
  932. # [
  933. # {
  934. # instrument_id: "BSV-USD-SWAP",
  935. # underlying_index: "BSV",
  936. # quote_currency: "USD",
  937. # coin: "BSV",
  938. # contract_val: "10",
  939. # listing: "2018-12-21T07:53:47.000Z",
  940. # delivery: "2020-03-14T08:00:00.000Z",
  941. # size_increment: "1",
  942. # tick_size: "0.01",
  943. # base_currency: "BSV",
  944. # underlying: "BSV-USD",
  945. # settlement_currency: "BSV",
  946. # is_inverse: "true",
  947. # contract_val_currency: "USD"
  948. # }
  949. # ]
  950. #
  951. return self.parse_markets(response)
  952. else:
  953. raise NotSupported(self.id + ' fetchMarketsByType does not support market type ' + type)
  954. def fetch_currencies(self, params={}):
  955. # has['fetchCurrencies'] is currently set to False
  956. # despite that their docs say these endpoints are public:
  957. # https://www.okex.com/api/account/v3/withdrawal/fee
  958. # https://www.okex.com/api/account/v3/currencies
  959. # it will still reply with {"code":30001, "message": "OK-ACCESS-KEY header is required"}
  960. # if you attempt to access it without authentication
  961. response = self.accountGetCurrencies(params)
  962. #
  963. # [
  964. # {
  965. # name: '',
  966. # currency: 'BTC',
  967. # can_withdraw: '1',
  968. # can_deposit: '1',
  969. # min_withdrawal: '0.0100000000000000'
  970. # },
  971. # ]
  972. #
  973. result = {}
  974. for i in range(0, len(response)):
  975. currency = response[i]
  976. id = self.safe_string(currency, 'currency')
  977. code = self.safe_currency_code(id)
  978. precision = 8 # default precision, todo: fix "magic constants"
  979. name = self.safe_string(currency, 'name')
  980. canDeposit = self.safe_integer(currency, 'can_deposit')
  981. canWithdraw = self.safe_integer(currency, 'can_withdraw')
  982. active = canDeposit and canWithdraw
  983. result[code] = {
  984. 'id': id,
  985. 'code': code,
  986. 'info': currency,
  987. 'type': None,
  988. 'name': name,
  989. 'active': active,
  990. 'fee': None, # todo: redesign
  991. 'precision': precision,
  992. 'limits': {
  993. 'amount': {'min': None, 'max': None},
  994. 'price': {'min': None, 'max': None},
  995. 'cost': {'min': None, 'max': None},
  996. 'withdraw': {
  997. 'min': self.safe_float(currency, 'min_withdrawal'),
  998. 'max': None,
  999. },
  1000. },
  1001. }
  1002. return result
  1003. def fetch_order_book(self, symbol, limit=None, params={}):
  1004. self.load_markets()
  1005. market = self.market(symbol)
  1006. method = market['type'] + 'GetInstrumentsInstrumentId'
  1007. method += 'Depth' if (market['type'] == 'swap') else 'Book'
  1008. request = {
  1009. 'instrument_id': market['id'],
  1010. }
  1011. if limit is not None:
  1012. request['size'] = limit # max 200
  1013. response = getattr(self, method)(self.extend(request, params))
  1014. #
  1015. # { asks: [["0.02685268", "0.242571", "1"],
  1016. # ["0.02685493", "0.164085", "1"],
  1017. # ...
  1018. # ["0.02779", "1.039", "1"],
  1019. # ["0.027813", "0.0876", "1"] ],
  1020. # bids: [["0.02684052", "10.371849", "1"],
  1021. # ["0.02684051", "3.707", "4"],
  1022. # ...
  1023. # ["0.02634963", "0.132934", "1"],
  1024. # ["0.02634962", "0.264838", "2"] ],
  1025. # timestamp: "2018-12-17T20:24:16.159Z" }
  1026. #
  1027. timestamp = self.parse8601(self.safe_string(response, 'timestamp'))
  1028. return self.parse_order_book(response, timestamp)
  1029. def parse_ticker(self, ticker, market=None):
  1030. #
  1031. # { best_ask: "0.02665472",
  1032. # best_bid: "0.02665221",
  1033. # instrument_id: "ETH-BTC",
  1034. # product_id: "ETH-BTC",
  1035. # last: "0.02665472",
  1036. # ask: "0.02665472", # missing in the docs
  1037. # bid: "0.02665221", # not mentioned in the docs
  1038. # open_24h: "0.02645482",
  1039. # high_24h: "0.02714633",
  1040. # low_24h: "0.02614109",
  1041. # base_volume_24h: "572298.901923",
  1042. # timestamp: "2018-12-17T21:20:07.856Z",
  1043. # quote_volume_24h: "15094.86831261" }
  1044. #
  1045. timestamp = self.parse8601(self.safe_string(ticker, 'timestamp'))
  1046. symbol = None
  1047. marketId = self.safe_string(ticker, 'instrument_id')
  1048. if marketId in self.markets_by_id:
  1049. market = self.markets_by_id[marketId]
  1050. symbol = market['symbol']
  1051. elif marketId is not None:
  1052. parts = marketId.split('-')
  1053. numParts = len(parts)
  1054. if numParts == 2:
  1055. baseId, quoteId = parts
  1056. base = self.safe_currency_code(baseId)
  1057. quote = self.safe_currency_code(quoteId)
  1058. symbol = base + '/' + quote
  1059. else:
  1060. symbol = marketId
  1061. if (symbol is None) and (market is not None):
  1062. symbol = market['symbol']
  1063. last = self.safe_float(ticker, 'last')
  1064. open = self.safe_float(ticker, 'open_24h')
  1065. return {
  1066. 'symbol': symbol,
  1067. 'timestamp': timestamp,
  1068. 'datetime': self.iso8601(timestamp),
  1069. 'high': self.safe_float(ticker, 'high_24h'),
  1070. 'low': self.safe_float(ticker, 'low_24h'),
  1071. 'bid': self.safe_float(ticker, 'best_bid'),
  1072. 'bidVolume': self.safe_float(ticker, 'best_bid_size'),
  1073. 'ask': self.safe_float(ticker, 'best_ask'),
  1074. 'askVolume': self.safe_float(ticker, 'best_ask_size'),
  1075. 'vwap': None,
  1076. 'open': open,
  1077. 'close': last,
  1078. 'last': last,
  1079. 'previousClose': None,
  1080. 'change': None,
  1081. 'percentage': None,
  1082. 'average': None,
  1083. 'baseVolume': self.safe_float(ticker, 'base_volume_24h'),
  1084. 'quoteVolume': self.safe_float(ticker, 'quote_volume_24h'),
  1085. 'info': ticker,
  1086. }
  1087. def fetch_ticker(self, symbol, params={}):
  1088. self.load_markets()
  1089. market = self.market(symbol)
  1090. method = market['type'] + 'GetInstrumentsInstrumentIdTicker'
  1091. request = {
  1092. 'instrument_id': market['id'],
  1093. }
  1094. response = getattr(self, method)(self.extend(request, params))
  1095. #
  1096. # { best_ask: "0.02665472",
  1097. # best_bid: "0.02665221",
  1098. # instrument_id: "ETH-BTC",
  1099. # product_id: "ETH-BTC",
  1100. # last: "0.02665472",
  1101. # ask: "0.02665472",
  1102. # bid: "0.02665221",
  1103. # open_24h: "0.02645482",
  1104. # high_24h: "0.02714633",
  1105. # low_24h: "0.02614109",
  1106. # base_volume_24h: "572298.901923",
  1107. # timestamp: "2018-12-17T21:20:07.856Z",
  1108. # quote_volume_24h: "15094.86831261" }
  1109. #
  1110. return self.parse_ticker(response)
  1111. def fetch_tickers_by_type(self, type, symbols=None, params={}):
  1112. self.load_markets()
  1113. method = type + 'GetInstrumentsTicker'
  1114. response = getattr(self, method)(params)
  1115. result = {}
  1116. for i in range(0, len(response)):
  1117. ticker = self.parse_ticker(response[i])
  1118. symbol = ticker['symbol']
  1119. result[symbol] = ticker
  1120. return self.filter_by_array(result, 'symbol', symbols)
  1121. def fetch_tickers(self, symbols=None, params={}):
  1122. defaultType = self.safe_string_2(self.options, 'fetchTickers', 'defaultType')
  1123. type = self.safe_string(params, 'type', defaultType)
  1124. return self.fetch_tickers_by_type(type, symbols, self.omit(params, 'type'))
  1125. def parse_trade(self, trade, market=None):
  1126. #
  1127. # fetchTrades(public)
  1128. #
  1129. # spot trades
  1130. #
  1131. # {
  1132. # time: "2018-12-17T23:31:08.268Z",
  1133. # timestamp: "2018-12-17T23:31:08.268Z",
  1134. # trade_id: "409687906",
  1135. # price: "0.02677805",
  1136. # size: "0.923467",
  1137. # side: "sell"
  1138. # }
  1139. #
  1140. # futures trades, swap trades
  1141. #
  1142. # {
  1143. # trade_id: "1989230840021013",
  1144. # side: "buy",
  1145. # price: "92.42",
  1146. # qty: "184", # missing in swap markets
  1147. # size: "5", # missing in futures markets
  1148. # timestamp: "2018-12-17T23:26:04.613Z"
  1149. # }
  1150. #
  1151. # fetchOrderTrades(private)
  1152. #
  1153. # spot trades, margin trades
  1154. #
  1155. # {
  1156. # "created_at":"2019-03-15T02:52:56.000Z",
  1157. # "exec_type":"T", # whether the order is taker or maker
  1158. # "fee":"0.00000082",
  1159. # "instrument_id":"BTC-USDT",
  1160. # "ledger_id":"3963052721",
  1161. # "liquidity":"T", # whether the order is taker or maker
  1162. # "order_id":"2482659399697408",
  1163. # "price":"3888.6",
  1164. # "product_id":"BTC-USDT",
  1165. # "side":"buy",
  1166. # "size":"0.00055306",
  1167. # "timestamp":"2019-03-15T02:52:56.000Z"
  1168. # },
  1169. #
  1170. # futures trades, swap trades
  1171. #
  1172. # {
  1173. # "trade_id":"197429674631450625",
  1174. # "instrument_id":"EOS-USD-SWAP",
  1175. # "order_id":"6a-7-54d663a28-0",
  1176. # "price":"3.633",
  1177. # "order_qty":"1.0000",
  1178. # "fee":"-0.000551",
  1179. # "created_at":"2019-03-21T04:41:58.0Z", # missing in swap trades
  1180. # "timestamp":"2019-03-25T05:56:31.287Z", # missing in futures trades
  1181. # "exec_type":"M", # whether the order is taker or maker
  1182. # "side":"short", # "buy" in futures trades
  1183. # }
  1184. #
  1185. symbol = None
  1186. marketId = self.safe_string(trade, 'instrument_id')
  1187. base = None
  1188. quote = None
  1189. if marketId in self.markets_by_id:
  1190. market = self.markets_by_id[marketId]
  1191. symbol = market['symbol']
  1192. base = market['base']
  1193. quote = market['quote']
  1194. elif marketId is not None:
  1195. parts = marketId.split('-')
  1196. numParts = len(parts)
  1197. if numParts == 2:
  1198. baseId, quoteId = parts
  1199. base = self.safe_currency_code(baseId)
  1200. quote = self.safe_currency_code(quoteId)
  1201. symbol = base + '/' + quote
  1202. else:
  1203. symbol = marketId
  1204. if (symbol is None) and (market is not None):
  1205. symbol = market['symbol']
  1206. base = market['base']
  1207. quote = market['quote']
  1208. timestamp = self.parse8601(self.safe_string_2(trade, 'timestamp', 'created_at'))
  1209. price = self.safe_float(trade, 'price')
  1210. amount = self.safe_float_2(trade, 'size', 'qty')
  1211. amount = self.safe_float(trade, 'order_qty', amount)
  1212. takerOrMaker = self.safe_string_2(trade, 'exec_type', 'liquidity')
  1213. if takerOrMaker == 'M':
  1214. takerOrMaker = 'maker'
  1215. elif takerOrMaker == 'T':
  1216. takerOrMaker = 'taker'
  1217. side = self.safe_string(trade, 'side')
  1218. cost = None
  1219. if amount is not None:
  1220. if price is not None:
  1221. cost = amount * price
  1222. feeCost = self.safe_float(trade, 'fee')
  1223. fee = None
  1224. if feeCost is not None:
  1225. feeCurrency = base if (side == 'buy') else quote
  1226. fee = {
  1227. # fee is either a positive number(invitation rebate)
  1228. # or a negative number(transaction fee deduction)
  1229. # therefore we need to invert the fee
  1230. # more about it https://github.com/ccxt/ccxt/issues/5909
  1231. 'cost': -feeCost,
  1232. 'currency': feeCurrency,
  1233. }
  1234. orderId = self.safe_string(trade, 'order_id')
  1235. return {
  1236. 'info': trade,
  1237. 'timestamp': timestamp,
  1238. 'datetime': self.iso8601(timestamp),
  1239. 'symbol': symbol,
  1240. 'id': self.safe_string_2(trade, 'trade_id', 'ledger_id'),
  1241. 'order': orderId,
  1242. 'type': None,
  1243. 'takerOrMaker': takerOrMaker,
  1244. 'side': side,
  1245. 'price': price,
  1246. 'amount': amount,
  1247. 'cost': cost,
  1248. 'fee': fee,
  1249. }
  1250. def fetch_trades(self, symbol, since=None, limit=None, params={}):
  1251. self.load_markets()
  1252. market = self.market(symbol)
  1253. method = market['type'] + 'GetInstrumentsInstrumentIdTrades'
  1254. if (limit is None) or (limit > 100):
  1255. limit = 100 # maximum = default = 100
  1256. request = {
  1257. 'instrument_id': market['id'],
  1258. 'limit': limit,
  1259. # from: 'id',
  1260. # to: 'id',
  1261. }
  1262. response = getattr(self, method)(self.extend(request, params))
  1263. #
  1264. # spot markets
  1265. #
  1266. # [
  1267. # {
  1268. # time: "2018-12-17T23:31:08.268Z",
  1269. # timestamp: "2018-12-17T23:31:08.268Z",
  1270. # trade_id: "409687906",
  1271. # price: "0.02677805",
  1272. # size: "0.923467",
  1273. # side: "sell"
  1274. # }
  1275. # ]
  1276. #
  1277. # futures markets, swap markets
  1278. #
  1279. # [
  1280. # {
  1281. # trade_id: "1989230840021013",
  1282. # side: "buy",
  1283. # price: "92.42",
  1284. # qty: "184", # missing in swap markets
  1285. # size: "5", # missing in futures markets
  1286. # timestamp: "2018-12-17T23:26:04.613Z"
  1287. # }
  1288. # ]
  1289. #
  1290. return self.parse_trades(response, market, since, limit)
  1291. def parse_ohlcv(self, ohlcv, market=None):
  1292. #
  1293. # spot markets
  1294. #
  1295. # {
  1296. # close: "0.02684545",
  1297. # high: "0.02685084",
  1298. # low: "0.02683312",
  1299. # open: "0.02683894",
  1300. # time: "2018-12-17T20:28:00.000Z",
  1301. # volume: "101.457222"
  1302. # }
  1303. #
  1304. # futures markets
  1305. #
  1306. # [
  1307. # 1545072720000,
  1308. # 0.3159,
  1309. # 0.3161,
  1310. # 0.3144,
  1311. # 0.3149,
  1312. # 22886,
  1313. # 725179.26172331,
  1314. # ]
  1315. #
  1316. if isinstance(ohlcv, list):
  1317. numElements = len(ohlcv)
  1318. volumeIndex = 6 if (numElements > 6) else 5
  1319. timestamp = self.safe_value(ohlcv, 0)
  1320. if isinstance(timestamp, basestring):
  1321. timestamp = self.parse8601(timestamp)
  1322. return [
  1323. timestamp, # timestamp
  1324. self.safe_float(ohlcv, 1), # Open
  1325. self.safe_float(ohlcv, 2), # High
  1326. self.safe_float(ohlcv, 3), # Low
  1327. self.safe_float(ohlcv, 4), # Close
  1328. # self.safe_float(ohlcv, 5), # Quote Volume
  1329. # self.safe_float(ohlcv, 6), # Base Volume
  1330. self.safe_float(ohlcv, volumeIndex), # Volume, okex will return base volume in the 7th element for future markets
  1331. ]
  1332. else:
  1333. return [
  1334. self.parse8601(self.safe_string(ohlcv, 'time')),
  1335. self.safe_float(ohlcv, 'open'), # Open
  1336. self.safe_float(ohlcv, 'high'), # High
  1337. self.safe_float(ohlcv, 'low'), # Low
  1338. self.safe_float(ohlcv, 'close'), # Close
  1339. self.safe_float(ohlcv, 'volume'), # Base Volume
  1340. ]
  1341. def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}):
  1342. self.load_markets()
  1343. market = self.market(symbol)
  1344. duration = self.parse_timeframe(timeframe)
  1345. request = {
  1346. 'instrument_id': market['id'],
  1347. 'granularity': self.timeframes[timeframe],
  1348. }
  1349. options = self.safe_value(self.options, 'fetchOHLCV', {})
  1350. defaultType = self.safe_string(options, 'type', 'Candles') # Candles or HistoryCandles
  1351. type = self.safe_string(params, 'type', defaultType)
  1352. params = self.omit(params, 'type')
  1353. method = market['type'] + 'GetInstrumentsInstrumentId' + type
  1354. if type == 'Candles':
  1355. if since is not None:
  1356. if limit is not None:
  1357. request['end'] = self.iso8601(self.sum(since, limit * duration * 1000))
  1358. request['start'] = self.iso8601(since)
  1359. else:
  1360. if limit is not None:
  1361. now = self.milliseconds()
  1362. request['start'] = self.iso8601(now - limit * duration * 1000)
  1363. request['end'] = self.iso8601(now)
  1364. elif type == 'HistoryCandles':
  1365. if market['option']:
  1366. raise NotSupported(self.id + ' fetchOHLCV does not have ' + type + ' for ' + market['type'] + ' markets')
  1367. if since is not None:
  1368. if limit is None:
  1369. limit = 300 # default
  1370. request['start'] = self.iso8601(self.sum(since, limit * duration * 1000))
  1371. request['end'] = self.iso8601(since)
  1372. else:
  1373. if limit is not None:
  1374. now = self.milliseconds()
  1375. request['end'] = self.iso8601(now - limit * duration * 1000)
  1376. request['start'] = self.iso8601(now)
  1377. response = getattr(self, method)(self.extend(request, params))
  1378. #
  1379. # spot markets
  1380. #
  1381. # [
  1382. # {
  1383. # close: "0.02683401",
  1384. # high: "0.02683401",
  1385. # low: "0.02683401",
  1386. # open: "0.02683401",
  1387. # time: "2018-12-17T23:47:00.000Z",
  1388. # volume: "0"
  1389. # },
  1390. # {
  1391. # close: "0.02684545",
  1392. # high: "0.02685084",
  1393. # low: "0.02683312",
  1394. # open: "0.02683894",
  1395. # time: "2018-12-17T20:28:00.000Z",
  1396. # volume: "101.457222"
  1397. # }
  1398. # ]
  1399. #
  1400. # futures
  1401. #
  1402. # [
  1403. # [
  1404. # 1545090660000,
  1405. # 0.3171,
  1406. # 0.3174,
  1407. # 0.3171,
  1408. # 0.3173,
  1409. # 1648,
  1410. # 51930.38579450868
  1411. # ],
  1412. # [
  1413. # 1545072720000,
  1414. # 0.3159,
  1415. # 0.3161,
  1416. # 0.3144,
  1417. # 0.3149,
  1418. # 22886,
  1419. # 725179.26172331
  1420. # ]
  1421. # ]
  1422. #
  1423. return self.parse_ohlcvs(response, market, timeframe, since, limit)
  1424. def parse_account_balance(self, response):
  1425. #
  1426. # account
  1427. #
  1428. # [
  1429. # {
  1430. # balance: 0,
  1431. # available: 0,
  1432. # currency: "BTC",
  1433. # hold: 0
  1434. # },
  1435. # {
  1436. # balance: 0,
  1437. # available: 0,
  1438. # currency: "ETH",
  1439. # hold: 0
  1440. # }
  1441. # ]
  1442. #
  1443. # spot
  1444. #
  1445. # [
  1446. # {
  1447. # frozen: "0",
  1448. # hold: "0",
  1449. # id: "2149632",
  1450. # currency: "BTC",
  1451. # balance: "0.0000000497717339",
  1452. # available: "0.0000000497717339",
  1453. # holds: "0"
  1454. # },
  1455. # {
  1456. # frozen: "0",
  1457. # hold: "0",
  1458. # id: "2149632",
  1459. # currency: "ICN",
  1460. # balance: "0.00000000925",
  1461. # available: "0.00000000925",
  1462. # holds: "0"
  1463. # }
  1464. # ]
  1465. #
  1466. result = {'info': response}
  1467. for i in range(0, len(response)):
  1468. balance = response[i]
  1469. currencyId = self.safe_string(balance, 'currency')
  1470. code = self.safe_currency_code(currencyId)
  1471. account = self.account()
  1472. account['total'] = self.safe_float(balance, 'balance')
  1473. account['used'] = self.safe_float(balance, 'hold')
  1474. account['free'] = self.safe_float(balance, 'available')
  1475. result[code] = account
  1476. return self.parse_balance(result)
  1477. def parse_margin_balance(self, response):
  1478. #
  1479. # [
  1480. # {
  1481. # "currency:BTC": {
  1482. # "available":"0",
  1483. # "balance":"0",
  1484. # "borrowed":"0",
  1485. # "can_withdraw":"0",
  1486. # "frozen":"0",
  1487. # "hold":"0",
  1488. # "holds":"0",
  1489. # "lending_fee":"0"
  1490. # },
  1491. # "currency:USDT": {
  1492. # "available":"100",
  1493. # "balance":"100",
  1494. # "borrowed":"0",
  1495. # "can_withdraw":"100",
  1496. # "frozen":"0",
  1497. # "hold":"0",
  1498. # "holds":"0",
  1499. # "lending_fee":"0"
  1500. # },
  1501. # "instrument_id":"BTC-USDT",
  1502. # "liquidation_price":"0",
  1503. # "product_id":"BTC-USDT",
  1504. # "risk_rate":""
  1505. # },
  1506. # ]
  1507. #
  1508. result = {'info': response}
  1509. for i in range(0, len(response)):
  1510. balance = response[i]
  1511. marketId = self.safe_string(balance, 'instrument_id')
  1512. market = self.safe_value(self.markets_by_id, marketId)
  1513. symbol = None
  1514. if market is None:
  1515. baseId, quoteId = marketId.split('-')
  1516. base = self.safe_currency_code(baseId)
  1517. quote = self.safe_currency_code(quoteId)
  1518. symbol = base + '/' + quote
  1519. else:
  1520. symbol = market['symbol']
  1521. omittedBalance = self.omit(balance, [
  1522. 'instrument_id',
  1523. 'liquidation_price',
  1524. 'product_id',
  1525. 'risk_rate',
  1526. 'margin_ratio',
  1527. 'maint_margin_ratio',
  1528. 'tiers',
  1529. ])
  1530. keys = list(omittedBalance.keys())
  1531. accounts = {}
  1532. for k in range(0, len(keys)):
  1533. key = keys[k]
  1534. marketBalance = balance[key]
  1535. if key.find(':') >= 0:
  1536. parts = key.split(':')
  1537. currencyId = parts[1]
  1538. code = self.safe_currency_code(currencyId)
  1539. account = self.account()
  1540. account['total'] = self.safe_float(marketBalance, 'balance')
  1541. account['used'] = self.safe_float(marketBalance, 'hold')
  1542. account['free'] = self.safe_float(marketBalance, 'available')
  1543. accounts[code] = account
  1544. else:
  1545. raise NotSupported(self.id + ' margin balance response format has changed!')
  1546. result[symbol] = self.parse_balance(accounts)
  1547. return result
  1548. def parse_futures_balance(self, response):
  1549. #
  1550. # {
  1551. # "info":{
  1552. # "eos":{
  1553. # "auto_margin":"0",
  1554. # "contracts": [
  1555. # {
  1556. # "available_qty":"40.37069445",
  1557. # "fixed_balance":"0",
  1558. # "instrument_id":"EOS-USD-190329",
  1559. # "margin_for_unfilled":"0",
  1560. # "margin_frozen":"0",
  1561. # "realized_pnl":"0",
  1562. # "unrealized_pnl":"0"
  1563. # },
  1564. # {
  1565. # "available_qty":"40.37069445",
  1566. # "fixed_balance":"14.54895721",
  1567. # "instrument_id":"EOS-USD-190628",
  1568. # "margin_for_unfilled":"0",
  1569. # "margin_frozen":"10.64042157",
  1570. # "realized_pnl":"-3.90853564",
  1571. # "unrealized_pnl":"-0.259"
  1572. # },
  1573. # ],
  1574. # "equity":"50.75220665",
  1575. # "margin_mode":"fixed",
  1576. # "total_avail_balance":"40.37069445"
  1577. # },
  1578. # }
  1579. # }
  1580. #
  1581. # their root field name is "info", so our info will contain their info
  1582. result = {'info': response}
  1583. info = self.safe_value(response, 'info', {})
  1584. ids = list(info.keys())
  1585. for i in range(0, len(ids)):
  1586. id = ids[i]
  1587. code = self.safe_currency_code(id)
  1588. balance = self.safe_value(info, id, {})
  1589. account = self.account()
  1590. totalAvailBalance = self.safe_float(balance, 'total_avail_balance')
  1591. if self.safe_string(balance, 'margin_mode') == 'fixed':
  1592. contracts = self.safe_value(balance, 'contracts', [])
  1593. free = totalAvailBalance
  1594. for i in range(0, len(contracts)):
  1595. contract = contracts[i]
  1596. fixedBalance = self.safe_float(contract, 'fixed_balance')
  1597. realizedPnl = self.safe_float(contract, 'realized_pnl')
  1598. marginFrozen = self.safe_float(contract, 'margin_frozen')
  1599. marginForUnfilled = self.safe_float(contract, 'margin_for_unfilled')
  1600. margin = self.sum(fixedBalance, realizedPnl) - marginFrozen - marginForUnfilled
  1601. free = self.sum(free, margin)
  1602. account['free'] = free
  1603. else:
  1604. realizedPnl = self.safe_float(balance, 'realized_pnl')
  1605. unrealizedPnl = self.safe_float(balance, 'unrealized_pnl')
  1606. marginFrozen = self.safe_float(balance, 'margin_frozen')
  1607. marginForUnfilled = self.safe_float(balance, 'margin_for_unfilled')
  1608. account['free'] = self.sum(totalAvailBalance, realizedPnl, unrealizedPnl) - marginFrozen - marginForUnfilled
  1609. # it may be incorrect to use total, free and used for swap accounts
  1610. account['total'] = self.safe_float(balance, 'equity')
  1611. result[code] = account
  1612. return self.parse_balance(result)
  1613. def parse_swap_balance(self, response):
  1614. #
  1615. # {
  1616. # "info": [
  1617. # {
  1618. # "equity":"3.0139",
  1619. # "fixed_balance":"0.0000",
  1620. # "instrument_id":"EOS-USD-SWAP",
  1621. # "margin":"0.5523",
  1622. # "margin_frozen":"0.0000",
  1623. # "margin_mode":"crossed",
  1624. # "margin_ratio":"1.0913",
  1625. # "realized_pnl":"-0.0006",
  1626. # "timestamp":"2019-03-25T03:46:10.336Z",
  1627. # "total_avail_balance":"3.0000",
  1628. # "unrealized_pnl":"0.0145"
  1629. # }
  1630. # ]
  1631. # }
  1632. #
  1633. # their root field name is "info", so our info will contain their info
  1634. result = {'info': response}
  1635. info = self.safe_value(response, 'info', [])
  1636. for i in range(0, len(info)):
  1637. balance = info[i]
  1638. marketId = self.safe_string(balance, 'instrument_id')
  1639. symbol = marketId
  1640. if marketId in self.markets_by_id:
  1641. symbol = self.markets_by_id[marketId]['symbol']
  1642. account = self.account()
  1643. # it may be incorrect to use total, free and used for swap accounts
  1644. account['total'] = self.safe_float(balance, 'equity')
  1645. account['free'] = self.safe_float(balance, 'total_avail_balance')
  1646. result[symbol] = account
  1647. return self.parse_balance(result)
  1648. def fetch_balance(self, params={}):
  1649. defaultType = self.safe_string_2(self.options, 'fetchBalance', 'defaultType')
  1650. type = self.safe_string(params, 'type', defaultType)
  1651. if type is None:
  1652. raise ArgumentsRequired(self.id + " fetchBalance requires a type parameter(one of 'account', 'spot', 'margin', 'futures', 'swap')")
  1653. self.load_markets()
  1654. suffix = 'Wallet' if (type == 'account') else 'Accounts'
  1655. method = type + 'Get' + suffix
  1656. query = self.omit(params, 'type')
  1657. response = getattr(self, method)(query)
  1658. #
  1659. # account
  1660. #
  1661. # [
  1662. # {
  1663. # balance: 0,
  1664. # available: 0,
  1665. # currency: "BTC",
  1666. # hold: 0
  1667. # },
  1668. # {
  1669. # balance: 0,
  1670. # available: 0,
  1671. # currency: "ETH",
  1672. # hold: 0
  1673. # }
  1674. # ]
  1675. #
  1676. # spot
  1677. #
  1678. # [
  1679. # {
  1680. # frozen: "0",
  1681. # hold: "0",
  1682. # id: "2149632",
  1683. # currency: "BTC",
  1684. # balance: "0.0000000497717339",
  1685. # available: "0.0000000497717339",
  1686. # holds: "0"
  1687. # },
  1688. # {
  1689. # frozen: "0",
  1690. # hold: "0",
  1691. # id: "2149632",
  1692. # currency: "ICN",
  1693. # balance: "0.00000000925",
  1694. # available: "0.00000000925",
  1695. # holds: "0"
  1696. # }
  1697. # ]
  1698. #
  1699. # margin
  1700. #
  1701. # [
  1702. # {
  1703. # "currency:BTC": {
  1704. # "available":"0",
  1705. # "balance":"0",
  1706. # "borrowed":"0",
  1707. # "can_withdraw":"0",
  1708. # "frozen":"0",
  1709. # "hold":"0",
  1710. # "holds":"0",
  1711. # "lending_fee":"0"
  1712. # },
  1713. # "currency:USDT": {
  1714. # "available":"100",
  1715. # "balance":"100",
  1716. # "borrowed":"0",
  1717. # "can_withdraw":"100",
  1718. # "frozen":"0",
  1719. # "hold":"0",
  1720. # "holds":"0",
  1721. # "lending_fee":"0"
  1722. # },
  1723. # "instrument_id":"BTC-USDT",
  1724. # "liquidation_price":"0",
  1725. # "product_id":"BTC-USDT",
  1726. # "risk_rate":""
  1727. # },
  1728. # ]
  1729. #
  1730. # futures
  1731. #
  1732. # {
  1733. # "info":{
  1734. # "eos":{
  1735. # "auto_margin":"0",
  1736. # "contracts": [
  1737. # {
  1738. # "available_qty":"40.37069445",
  1739. # "fixed_balance":"0",
  1740. # "instrument_id":"EOS-USD-190329",
  1741. # "margin_for_unfilled":"0",
  1742. # "margin_frozen":"0",
  1743. # "realized_pnl":"0",
  1744. # "unrealized_pnl":"0"
  1745. # },
  1746. # {
  1747. # "available_qty":"40.37069445",
  1748. # "fixed_balance":"14.54895721",
  1749. # "instrument_id":"EOS-USD-190628",
  1750. # "margin_for_unfilled":"0",
  1751. # "margin_frozen":"10.64042157",
  1752. # "realized_pnl":"-3.90853564",
  1753. # "unrealized_pnl":"-0.259"
  1754. # },
  1755. # ],
  1756. # "equity":"50.75220665",
  1757. # "margin_mode":"fixed",
  1758. # "total_avail_balance":"40.37069445"
  1759. # },
  1760. # }
  1761. # }
  1762. #
  1763. # swap
  1764. #
  1765. # {
  1766. # "info": [
  1767. # {
  1768. # "equity":"3.0139",
  1769. # "fixed_balance":"0.0000",
  1770. # "instrument_id":"EOS-USD-SWAP",
  1771. # "margin":"0.5523",
  1772. # "margin_frozen":"0.0000",
  1773. # "margin_mode":"crossed",
  1774. # "margin_ratio":"1.0913",
  1775. # "realized_pnl":"-0.0006",
  1776. # "timestamp":"2019-03-25T03:46:10.336Z",
  1777. # "total_avail_balance":"3.0000",
  1778. # "unrealized_pnl":"0.0145"
  1779. # }
  1780. # ]
  1781. # }
  1782. #
  1783. return self.parse_balance_by_type(type, response)
  1784. def parse_balance_by_type(self, type, response):
  1785. if (type == 'account') or (type == 'spot'):
  1786. return self.parse_account_balance(response)
  1787. elif type == 'margin':
  1788. return self.parse_margin_balance(response)
  1789. elif type == 'futures':
  1790. return self.parse_futures_balance(response)
  1791. elif type == 'swap':
  1792. return self.parse_swap_balance(response)
  1793. raise NotSupported(self.id + " fetchBalance does not support the '" + type + "' type(the type must be one of 'account', 'spot', 'margin', 'futures', 'swap')")
  1794. def create_order(self, symbol, type, side, amount, price=None, params={}):
  1795. self.load_markets()
  1796. market = self.market(symbol)
  1797. request = {
  1798. 'instrument_id': market['id'],
  1799. # 'client_oid': 'abcdef1234567890', # [a-z0-9]{1,32}
  1800. # 'order_type': '0', # 0 = Normal limit order, 1 = Post only, 2 = Fill Or Kill, 3 = Immediatel Or Cancel, 4 = Market for futures only
  1801. }
  1802. clientOrderId = self.safe_string_2(params, 'client_oid', 'clientOrderId')
  1803. if clientOrderId is not None:
  1804. request['client_oid'] = clientOrderId
  1805. params = self.omit(params, ['client_oid', 'clientOrderId'])
  1806. method = None
  1807. if market['futures'] or market['swap']:
  1808. size = self.number_to_string(amount) if market['futures'] else self.amount_to_precision(symbol, amount)
  1809. request = self.extend(request, {
  1810. 'type': type, # 1:open long 2:open short 3:close long 4:close short for futures
  1811. 'size': size,
  1812. # 'match_price': '0', # Order at best counter party price?(0:no 1:yes). The default is 0. If it is set as 1, the price parameter will be ignored. When posting orders at best bid price, order_type can only be 0(regular order).
  1813. })
  1814. orderType = self.safe_string(params, 'order_type')
  1815. # order_type == '4' means a market order
  1816. isMarketOrder = (type == 'market') or (orderType == '4')
  1817. if isMarketOrder:
  1818. request['match_price'] = '1'
  1819. else:
  1820. request['price'] = self.price_to_precision(symbol, price)
  1821. if market['futures']:
  1822. request['leverage'] = '10' # or '20'
  1823. method = market['type'] + 'PostOrder'
  1824. else:
  1825. marginTrading = self.safe_string(params, 'margin_trading', '1') # 1 = spot, 2 = margin
  1826. request = self.extend(request, {
  1827. 'side': side,
  1828. 'type': type, # limit/market
  1829. 'margin_trading': marginTrading, # 1 = spot, 2 = margin
  1830. })
  1831. if type == 'limit':
  1832. request['price'] = self.price_to_precision(symbol, price)
  1833. request['size'] = self.amount_to_precision(symbol, amount)
  1834. elif type == 'market':
  1835. # for market buy it requires the amount of quote currency to spend
  1836. if side == 'buy':
  1837. notional = self.safe_float(params, 'notional')
  1838. createMarketBuyOrderRequiresPrice = self.safe_value(self.options, 'createMarketBuyOrderRequiresPrice', True)
  1839. if createMarketBuyOrderRequiresPrice:
  1840. if price is not None:
  1841. if notional is None:
  1842. notional = amount * price
  1843. elif notional is None:
  1844. raise InvalidOrder(self.id + " createOrder() requires the price argument with market buy orders to calculate total order cost(amount to spend), where cost = amount * price. Supply a price argument to createOrder() call if you want the cost to be calculated for you from price and amount, or, alternatively, add .options['createMarketBuyOrderRequiresPrice'] = False and supply the total cost value in the 'amount' argument or in the 'notional' extra parameter(the exchange-specific behaviour)")
  1845. else:
  1846. notional = amount if (notional is None) else notional
  1847. precision = market['precision']['price']
  1848. request['notional'] = self.decimal_to_precision(notional, TRUNCATE, precision, self.precisionMode)
  1849. else:
  1850. request['size'] = self.amount_to_precision(symbol, amount)
  1851. method = 'marginPostOrders' if (marginTrading == '2') else 'spotPostOrders'
  1852. response = getattr(self, method)(self.extend(request, params))
  1853. #
  1854. # {
  1855. # "client_oid":"oktspot79",
  1856. # "error_code":"",
  1857. # "error_message":"",
  1858. # "order_id":"2510789768709120",
  1859. # "result":true
  1860. # }
  1861. #
  1862. order = self.parse_order(response, market)
  1863. return self.extend(order, {
  1864. 'type': type,
  1865. 'side': side,
  1866. })
  1867. def cancel_order(self, id, symbol=None, params={}):
  1868. if symbol is None:
  1869. raise ArgumentsRequired(self.id + ' cancelOrder() requires a symbol argument')
  1870. self.load_markets()
  1871. market = self.market(symbol)
  1872. type = None
  1873. if market['futures'] or market['swap']:
  1874. type = market['type']
  1875. else:
  1876. defaultType = self.safe_string_2(self.options, 'cancelOrder', 'defaultType', market['type'])
  1877. type = self.safe_string(params, 'type', defaultType)
  1878. if type is None:
  1879. raise ArgumentsRequired(self.id + " cancelOrder requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').")
  1880. method = type + 'PostCancelOrder'
  1881. request = {
  1882. 'instrument_id': market['id'],
  1883. }
  1884. if market['futures'] or market['swap']:
  1885. method += 'InstrumentId'
  1886. else:
  1887. method += 's'
  1888. clientOrderId = self.safe_string_2(params, 'client_oid', 'clientOrderId')
  1889. if clientOrderId is not None:
  1890. method += 'ClientOid'
  1891. request['client_oid'] = clientOrderId
  1892. else:
  1893. method += 'OrderId'
  1894. request['order_id'] = id
  1895. query = self.omit(params, ['type', 'client_oid', 'clientOrderId'])
  1896. response = getattr(self, method)(self.extend(request, query))
  1897. result = response if ('result' in response) else self.safe_value(response, market['id'], {})
  1898. #
  1899. # spot, margin
  1900. #
  1901. # {
  1902. # "btc-usdt": [
  1903. # {
  1904. # "result":true,
  1905. # "client_oid":"a123",
  1906. # "order_id": "2510832677225473"
  1907. # }
  1908. # ]
  1909. # }
  1910. #
  1911. # futures, swap
  1912. #
  1913. # {
  1914. # "result": True,
  1915. # "client_oid": "oktfuture10", # missing if requested by order_id
  1916. # "order_id": "2517535534836736",
  1917. # "instrument_id": "EOS-USD-190628"
  1918. # }
  1919. #
  1920. return self.parse_order(result, market)
  1921. def parse_order_status(self, status):
  1922. statuses = {
  1923. '-2': 'failed',
  1924. '-1': 'canceled',
  1925. '0': 'open',
  1926. '1': 'open',
  1927. '2': 'closed',
  1928. '3': 'open',
  1929. '4': 'canceled',
  1930. }
  1931. return self.safe_string(statuses, status, status)
  1932. def parse_order_side(self, side):
  1933. sides = {
  1934. '1': 'buy', # open long
  1935. '2': 'sell', # open short
  1936. '3': 'sell', # close long
  1937. '4': 'buy', # close short
  1938. }
  1939. return self.safe_string(sides, side, side)
  1940. def parse_order(self, order, market=None):
  1941. #
  1942. # createOrder
  1943. #
  1944. # {
  1945. # "client_oid":"oktspot79",
  1946. # "error_code":"",
  1947. # "error_message":"",
  1948. # "order_id":"2510789768709120",
  1949. # "result":true
  1950. # }
  1951. #
  1952. # cancelOrder
  1953. #
  1954. # {
  1955. # "result": True,
  1956. # "client_oid": "oktfuture10", # missing if requested by order_id
  1957. # "order_id": "2517535534836736",
  1958. # # instrument_id is missing for spot/margin orders
  1959. # # available in futures and swap orders only
  1960. # "instrument_id": "EOS-USD-190628",
  1961. # }
  1962. #
  1963. # fetchOrder, fetchOrdersByState, fetchOpenOrders, fetchClosedOrders
  1964. #
  1965. # # spot and margin orders
  1966. #
  1967. # {
  1968. # "client_oid":"oktspot76",
  1969. # "created_at":"2019-03-18T07:26:49.000Z",
  1970. # "filled_notional":"3.9734",
  1971. # "filled_size":"0.001", # filled_qty in futures and swap orders
  1972. # "funds":"", # self is most likely the same as notional
  1973. # "instrument_id":"BTC-USDT",
  1974. # "notional":"",
  1975. # "order_id":"2500723297813504",
  1976. # "order_type":"0",
  1977. # "price":"4013",
  1978. # "product_id":"BTC-USDT", # missing in futures and swap orders
  1979. # "side":"buy",
  1980. # "size":"0.001",
  1981. # "status":"filled",
  1982. # "state": "2",
  1983. # "timestamp":"2019-03-18T07:26:49.000Z",
  1984. # "type":"limit"
  1985. # }
  1986. #
  1987. # # futures and swap orders
  1988. #
  1989. # {
  1990. # "instrument_id":"EOS-USD-190628",
  1991. # "size":"10",
  1992. # "timestamp":"2019-03-20T10:04:55.000Z",
  1993. # "filled_qty":"10", # filled_size in spot and margin orders
  1994. # "fee":"-0.00841043",
  1995. # "order_id":"2512669605501952",
  1996. # "price":"3.668",
  1997. # "price_avg":"3.567", # missing in spot and margin orders
  1998. # "status":"2",
  1999. # "state": "2",
  2000. # "type":"4",
  2001. # "contract_val":"10",
  2002. # "leverage":"10", # missing in swap, spot and margin orders
  2003. # "client_oid":"",
  2004. # "pnl":"1.09510794", # missing in swap, spo and margin orders
  2005. # "order_type":"0"
  2006. # }
  2007. #
  2008. id = self.safe_string(order, 'order_id')
  2009. timestamp = self.parse8601(self.safe_string(order, 'timestamp'))
  2010. side = self.safe_string(order, 'side')
  2011. type = self.safe_string(order, 'type')
  2012. if (side != 'buy') and (side != 'sell'):
  2013. side = self.parse_order_side(type)
  2014. symbol = None
  2015. marketId = self.safe_string(order, 'instrument_id')
  2016. if marketId in self.markets_by_id:
  2017. market = self.markets_by_id[marketId]
  2018. symbol = market['symbol']
  2019. else:
  2020. symbol = marketId
  2021. if market is not None:
  2022. if symbol is None:
  2023. symbol = market['symbol']
  2024. amount = self.safe_float(order, 'size')
  2025. filled = self.safe_float_2(order, 'filled_size', 'filled_qty')
  2026. remaining = None
  2027. if amount is not None:
  2028. if filled is not None:
  2029. amount = max(amount, filled)
  2030. remaining = max(0, amount - filled)
  2031. if type == 'market':
  2032. remaining = 0
  2033. cost = self.safe_float_2(order, 'filled_notional', 'funds')
  2034. price = self.safe_float(order, 'price')
  2035. average = self.safe_float(order, 'price_avg')
  2036. if cost is None:
  2037. if filled is not None and average is not None:
  2038. cost = average * filled
  2039. else:
  2040. if (average is None) and (filled is not None) and (filled > 0):
  2041. average = cost / filled
  2042. status = self.parse_order_status(self.safe_string(order, 'state'))
  2043. feeCost = self.safe_float(order, 'fee')
  2044. fee = None
  2045. if feeCost is not None:
  2046. feeCurrency = None
  2047. fee = {
  2048. 'cost': feeCost,
  2049. 'currency': feeCurrency,
  2050. }
  2051. clientOrderId = self.safe_string(order, 'client_oid')
  2052. if len(clientOrderId) < 1:
  2053. clientOrderId = None # fix empty clientOrderId string
  2054. return {
  2055. 'info': order,
  2056. 'id': id,
  2057. 'clientOrderId': clientOrderId,
  2058. 'timestamp': timestamp,
  2059. 'datetime': self.iso8601(timestamp),
  2060. 'lastTradeTimestamp': None,
  2061. 'symbol': symbol,
  2062. 'type': type,
  2063. 'side': side,
  2064. 'price': price,
  2065. 'average': average,
  2066. 'cost': cost,
  2067. 'amount': amount,
  2068. 'filled': filled,
  2069. 'remaining': remaining,
  2070. 'status': status,
  2071. 'fee': fee,
  2072. 'trades': None,
  2073. }
  2074. def fetch_order(self, id, symbol=None, params={}):
  2075. if symbol is None:
  2076. raise ArgumentsRequired(self.id + ' fetchOrder requires a symbol argument')
  2077. self.load_markets()
  2078. market = self.market(symbol)
  2079. defaultType = self.safe_string_2(self.options, 'fetchOrder', 'defaultType', market['type'])
  2080. type = self.safe_string(params, 'type', defaultType)
  2081. if type is None:
  2082. raise ArgumentsRequired(self.id + " fetchOrder requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').")
  2083. instrumentId = 'InstrumentId' if (market['futures'] or market['swap']) else ''
  2084. method = type + 'GetOrders' + instrumentId
  2085. request = {
  2086. 'instrument_id': market['id'],
  2087. # 'client_oid': 'abcdef12345', # optional, [a-z0-9]{1,32}
  2088. # 'order_id': id,
  2089. }
  2090. clientOid = self.safe_string(params, 'client_oid')
  2091. if clientOid is not None:
  2092. method += 'ClientOid'
  2093. request['client_oid'] = clientOid
  2094. else:
  2095. method += 'OrderId'
  2096. request['order_id'] = id
  2097. query = self.omit(params, 'type')
  2098. response = getattr(self, method)(self.extend(request, query))
  2099. #
  2100. # spot, margin
  2101. #
  2102. # {
  2103. # "client_oid":"oktspot70",
  2104. # "created_at":"2019-03-15T02:52:56.000Z",
  2105. # "filled_notional":"3.8886",
  2106. # "filled_size":"0.001",
  2107. # "funds":"",
  2108. # "instrument_id":"BTC-USDT",
  2109. # "notional":"",
  2110. # "order_id":"2482659399697408",
  2111. # "order_type":"0",
  2112. # "price":"3927.3",
  2113. # "product_id":"BTC-USDT",
  2114. # "side":"buy",
  2115. # "size":"0.001",
  2116. # "status":"filled",
  2117. # "state": "2",
  2118. # "timestamp":"2019-03-15T02:52:56.000Z",
  2119. # "type":"limit"
  2120. # }
  2121. #
  2122. # futures, swap
  2123. #
  2124. # {
  2125. # "instrument_id":"EOS-USD-190628",
  2126. # "size":"10",
  2127. # "timestamp":"2019-03-20T02:46:38.000Z",
  2128. # "filled_qty":"10",
  2129. # "fee":"-0.0080819",
  2130. # "order_id":"2510946213248000",
  2131. # "price":"3.712",
  2132. # "price_avg":"3.712",
  2133. # "status":"2",
  2134. # "state": "2",
  2135. # "type":"2",
  2136. # "contract_val":"10",
  2137. # "leverage":"10",
  2138. # "client_oid":"", # missing in swap orders
  2139. # "pnl":"0", # missing in swap orders
  2140. # "order_type":"0"
  2141. # }
  2142. #
  2143. return self.parse_order(response)
  2144. def fetch_orders_by_state(self, state, symbol=None, since=None, limit=None, params={}):
  2145. if symbol is None:
  2146. raise ArgumentsRequired(self.id + ' fetchOrdersByState requires a symbol argument')
  2147. self.load_markets()
  2148. market = self.market(symbol)
  2149. type = None
  2150. if market['futures'] or market['swap']:
  2151. type = market['type']
  2152. else:
  2153. defaultType = self.safe_string_2(self.options, 'fetchOrder', 'defaultType', market['type'])
  2154. type = self.safe_string(params, 'type', defaultType)
  2155. if type is None:
  2156. raise ArgumentsRequired(self.id + " fetchOrder requires a type parameter(one of 'spot', 'margin', 'futures', 'swap').")
  2157. request = {
  2158. 'instrument_id': market['id'],
  2159. # '-2': failed,
  2160. # '-1': cancelled,
  2161. # '0': open ,
  2162. # '1': partially filled,
  2163. # '2': fully filled,
  2164. # '3': submitting,
  2165. # '4': cancelling,
  2166. # '6': incompleteopen+partially filled),
  2167. # '7': completecancelled+fully filled),
  2168. 'state': state,
  2169. }
  2170. method = type + 'GetOrders'
  2171. if market['futures'] or market['swap']:
  2172. method += 'InstrumentId'
  2173. query = self.omit(params, 'type')
  2174. response = getattr(self, method)(self.extend(request, query))
  2175. #
  2176. # spot, margin
  2177. #
  2178. # [
  2179. # # in fact, self documented API response does not correspond
  2180. # # to their actual API response for spot markets
  2181. # # OKEX v3 API returns a plain array of orders(see below)
  2182. # [
  2183. # {
  2184. # "client_oid":"oktspot76",
  2185. # "created_at":"2019-03-18T07:26:49.000Z",
  2186. # "filled_notional":"3.9734",
  2187. # "filled_size":"0.001",
  2188. # "funds":"",
  2189. # "instrument_id":"BTC-USDT",
  2190. # "notional":"",
  2191. # "order_id":"2500723297813504",
  2192. # "order_type":"0",
  2193. # "price":"4013",
  2194. # "product_id":"BTC-USDT",
  2195. # "side":"buy",
  2196. # "size":"0.001",
  2197. # "status":"filled",
  2198. # "state": "2",
  2199. # "timestamp":"2019-03-18T07:26:49.000Z",
  2200. # "type":"limit"
  2201. # },
  2202. # ],
  2203. # {
  2204. # "before":"2500723297813504",
  2205. # "after":"2500650881647616"
  2206. # }
  2207. # ]
  2208. #
  2209. # futures, swap
  2210. #
  2211. # {
  2212. # "result":true, # missing in swap orders
  2213. # "order_info": [
  2214. # {
  2215. # "instrument_id":"EOS-USD-190628",
  2216. # "size":"10",
  2217. # "timestamp":"2019-03-20T10:04:55.000Z",
  2218. # "filled_qty":"10",
  2219. # "fee":"-0.00841043",
  2220. # "order_id":"2512669605501952",
  2221. # "price":"3.668",
  2222. # "price_avg":"3.567",
  2223. # "status":"2",
  2224. # "state": "2",
  2225. # "type":"4",
  2226. # "contract_val":"10",
  2227. # "leverage":"10", # missing in swap orders
  2228. # "client_oid":"",
  2229. # "pnl":"1.09510794", # missing in swap orders
  2230. # "order_type":"0"
  2231. # },
  2232. # ]
  2233. # }
  2234. #
  2235. orders = None
  2236. if market['swap'] or market['futures']:
  2237. orders = self.safe_value(response, 'order_info', [])
  2238. else:
  2239. orders = response
  2240. responseLength = len(response)
  2241. if responseLength < 1:
  2242. return []
  2243. # in fact, self documented API response does not correspond
  2244. # to their actual API response for spot markets
  2245. # OKEX v3 API returns a plain array of orders
  2246. if responseLength > 1:
  2247. before = self.safe_value(response[1], 'before')
  2248. if before is not None:
  2249. orders = response[0]
  2250. return self.parse_orders(orders, market, since, limit)
  2251. def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}):
  2252. # '-2': failed,
  2253. # '-1': cancelled,
  2254. # '0': open ,
  2255. # '1': partially filled,
  2256. # '2': fully filled,
  2257. # '3': submitting,
  2258. # '4': cancelling,
  2259. # '6': incompleteopen+partially filled),
  2260. # '7': completecancelled+fully filled),
  2261. return self.fetch_orders_by_state('6', symbol, since, limit, params)
  2262. def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}):
  2263. # '-2': failed,
  2264. # '-1': cancelled,
  2265. # '0': open ,
  2266. # '1': partially filled,
  2267. # '2': fully filled,
  2268. # '3': submitting,
  2269. # '4': cancelling,
  2270. # '6': incompleteopen+partially filled),
  2271. # '7': completecancelled+fully filled),
  2272. return self.fetch_orders_by_state('7', symbol, since, limit, params)
  2273. def parse_deposit_addresses(self, addresses):
  2274. result = {}
  2275. for i in range(0, len(addresses)):
  2276. address = self.parse_deposit_address(addresses[i])
  2277. code = address['currency']
  2278. result[code] = address
  2279. return result
  2280. def parse_deposit_address(self, depositAddress, currency=None):
  2281. #
  2282. # {
  2283. # address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa',
  2284. # currency: 'eth'
  2285. # tag: 'abcde12345', # will be missing if the token does not require a deposit tag
  2286. # payment_id: 'abcde12345', # will not be returned if the token does not require a payment_id
  2287. # # can_deposit: 1, # 0 or 1, documented but missing
  2288. # # can_withdraw: 1, # 0 or 1, documented but missing
  2289. # }
  2290. #
  2291. address = self.safe_string(depositAddress, 'address')
  2292. tag = self.safe_string_2(depositAddress, 'tag', 'payment_id')
  2293. tag = self.safe_string(depositAddress, 'memo', tag)
  2294. currencyId = self.safe_string(depositAddress, 'currency')
  2295. code = self.safe_currency_code(currencyId)
  2296. self.check_address(address)
  2297. return {
  2298. 'currency': code,
  2299. 'address': address,
  2300. 'tag': tag,
  2301. 'info': depositAddress,
  2302. }
  2303. def fetch_deposit_address(self, code, params={}):
  2304. self.load_markets()
  2305. currency = self.currency(code)
  2306. request = {
  2307. 'currency': currency['id'],
  2308. }
  2309. response = self.accountGetDepositAddress(self.extend(request, params))
  2310. #
  2311. # [
  2312. # {
  2313. # address: '0x696abb81974a8793352cbd33aadcf78eda3cfdfa',
  2314. # currency: 'eth'
  2315. # }
  2316. # ]
  2317. #
  2318. addresses = self.parse_deposit_addresses(response)
  2319. address = self.safe_value(addresses, code)
  2320. if address is None:
  2321. raise InvalidAddress(self.id + ' fetchDepositAddress cannot return nonexistent addresses, you should create withdrawal addresses with the exchange website first')
  2322. return address
  2323. def withdraw(self, code, amount, address, tag=None, params={}):
  2324. self.check_address(address)
  2325. self.load_markets()
  2326. currency = self.currency(code)
  2327. if tag:
  2328. address = address + ':' + tag
  2329. fee = self.safe_string(params, 'fee')
  2330. if fee is None:
  2331. raise ArgumentsRequired(self.id + " withdraw() requires a `fee` string parameter, network transaction fee must be ≥ 0. Withdrawals to OKCoin or OKEx are fee-free, please set '0'. Withdrawing to external digital asset address requires network transaction fee.")
  2332. request = {
  2333. 'currency': currency['id'],
  2334. 'to_address': address,
  2335. 'destination': '4', # 2 = OKCoin International, 3 = OKEx 4 = others
  2336. 'amount': self.number_to_string(amount),
  2337. 'fee': fee, # String. Network transaction fee 0. Withdrawals to OKCoin or OKEx are fee-free, please set as 0. Withdrawal to external digital asset address requires network transaction fee.
  2338. }
  2339. if 'password' in params:
  2340. request['trade_pwd'] = params['password']
  2341. elif 'trade_pwd' in params:
  2342. request['trade_pwd'] = params['trade_pwd']
  2343. elif self.password:
  2344. request['trade_pwd'] = self.password
  2345. query = self.omit(params, ['fee', 'password', 'trade_pwd'])
  2346. if not ('trade_pwd' in request):
  2347. raise ExchangeError(self.id + ' withdraw() requires self.password set on the exchange instance or a password / trade_pwd parameter')
  2348. response = self.accountPostWithdrawal(self.extend(request, query))
  2349. #
  2350. # {
  2351. # "amount":"0.1",
  2352. # "withdrawal_id":"67485",
  2353. # "currency":"btc",
  2354. # "result":true
  2355. # }
  2356. #
  2357. return {
  2358. 'info': response,
  2359. 'id': self.safe_string(response, 'withdrawal_id'),
  2360. }
  2361. def fetch_deposits(self, code=None, since=None, limit=None, params={}):
  2362. self.load_markets()
  2363. request = {}
  2364. method = 'accountGetDepositHistory'
  2365. currency = None
  2366. if code is not None:
  2367. currency = self.currency(code)
  2368. request['currency'] = currency['id']
  2369. method += 'Currency'
  2370. response = getattr(self, method)(self.extend(request, params))
  2371. return self.parse_transactions(response, currency, since, limit, params)
  2372. def fetch_withdrawals(self, code=None, since=None, limit=None, params={}):
  2373. self.load_markets()
  2374. request = {}
  2375. method = 'accountGetWithdrawalHistory'
  2376. currency = None
  2377. if code is not None:
  2378. currency = self.currency(code)
  2379. request['currency'] = currency['id']
  2380. method += 'Currency'
  2381. response = getattr(self, method)(self.extend(request, params))
  2382. return self.parse_transactions(response, currency, since, limit, params)
  2383. def parse_transaction_status(self, status):
  2384. #
  2385. # deposit statuses
  2386. #
  2387. # {
  2388. # '0': 'waiting for confirmation',
  2389. # '1': 'confirmation account',
  2390. # '2': 'recharge success'
  2391. # }
  2392. #
  2393. # withdrawal statues
  2394. #
  2395. # {
  2396. # '-3': 'pending cancel',
  2397. # '-2': 'cancelled',
  2398. # '-1': 'failed',
  2399. # '0': 'pending',
  2400. # '1': 'sending',
  2401. # '2': 'sent',
  2402. # '3': 'email confirmation',
  2403. # '4': 'manual confirmation',
  2404. # '5': 'awaiting identity confirmation'
  2405. # }
  2406. #
  2407. statuses = {
  2408. '-3': 'pending',
  2409. '-2': 'canceled',
  2410. '-1': 'failed',
  2411. '0': 'pending',
  2412. '1': 'pending',
  2413. '2': 'ok',
  2414. '3': 'pending',
  2415. '4': 'pending',
  2416. '5': 'pending',
  2417. }
  2418. return self.safe_string(statuses, status, status)
  2419. def parse_transaction(self, transaction, currency=None):
  2420. #
  2421. # withdraw
  2422. #
  2423. # {
  2424. # "amount":"0.1",
  2425. # "withdrawal_id":"67485",
  2426. # "currency":"btc",
  2427. # "result":true
  2428. # }
  2429. #
  2430. # fetchWithdrawals
  2431. #
  2432. # {
  2433. # amount: "4.72100000",
  2434. # withdrawal_id: "1729116",
  2435. # fee: "0.01000000eth",
  2436. # txid: "0xf653125bbf090bcfe4b5e8e7b8f586a9d87aa7de94598702758c0802b…",
  2437. # currency: "ETH",
  2438. # from: "7147338839",
  2439. # to: "0x26a3CB49578F07000575405a57888681249c35Fd",
  2440. # timestamp: "2018-08-17T07:03:42.000Z",
  2441. # status: "2"
  2442. # }
  2443. #
  2444. # fetchDeposits
  2445. #
  2446. # {
  2447. # "amount": "4.19511659",
  2448. # "txid": "14c9a8c925647cdb7e5b2937ea9aefe2b29b2c273150ad3f44b3b8a4635ed437",
  2449. # "currency": "XMR",
  2450. # "from": "",
  2451. # "to": "48PjH3ksv1fiXniKvKvyH5UtFs5WhfS2Vf7U3TwzdRJtCc7HJWvCQe56dRahyhQyTAViXZ8Nzk4gQg6o4BJBMUoxNy8y8g7",
  2452. # "deposit_id": 11571659, <-- we can use self
  2453. # "timestamp": "2019-10-01T14:54:19.000Z",
  2454. # "status": "2"
  2455. # }
  2456. #
  2457. type = None
  2458. id = None
  2459. address = None
  2460. withdrawalId = self.safe_string(transaction, 'withdrawal_id')
  2461. addressFrom = self.safe_string(transaction, 'from')
  2462. addressTo = self.safe_string(transaction, 'to')
  2463. if withdrawalId is not None:
  2464. type = 'withdrawal'
  2465. id = withdrawalId
  2466. address = addressTo
  2467. else:
  2468. # the payment_id will appear on new deposits but appears to be removed from the response after 2 months
  2469. id = self.safe_string_2(transaction, 'payment_id', 'deposit_id')
  2470. type = 'deposit'
  2471. address = addressTo
  2472. currencyId = self.safe_string(transaction, 'currency')
  2473. code = self.safe_currency_code(currencyId)
  2474. amount = self.safe_float(transaction, 'amount')
  2475. status = self.parse_transaction_status(self.safe_string(transaction, 'status'))
  2476. txid = self.safe_string(transaction, 'txid')
  2477. timestamp = self.parse8601(self.safe_string(transaction, 'timestamp'))
  2478. feeCost = None
  2479. if type == 'deposit':
  2480. feeCost = 0
  2481. else:
  2482. if currencyId is not None:
  2483. feeWithCurrencyId = self.safe_string(transaction, 'fee')
  2484. if feeWithCurrencyId is not None:
  2485. # https://github.com/ccxt/ccxt/pull/5748
  2486. lowercaseCurrencyId = currencyId.lower()
  2487. feeWithoutCurrencyId = feeWithCurrencyId.replace(lowercaseCurrencyId, '')
  2488. feeCost = float(feeWithoutCurrencyId)
  2489. # todo parse tags
  2490. return {
  2491. 'info': transaction,
  2492. 'id': id,
  2493. 'currency': code,
  2494. 'amount': amount,
  2495. 'addressFrom': addressFrom,
  2496. 'addressTo': addressTo,
  2497. 'address': address,
  2498. 'tagFrom': None,
  2499. 'tagTo': None,
  2500. 'tag': None,
  2501. 'status': status,
  2502. 'type': type,
  2503. 'updated': None,
  2504. 'txid': txid,
  2505. 'timestamp': timestamp,
  2506. 'datetime': self.iso8601(timestamp),
  2507. 'fee': {
  2508. 'currency': code,
  2509. 'cost': feeCost,
  2510. },
  2511. }
  2512. def parse_my_trade(self, pair, market=None):
  2513. # check that trading symbols match in both entries
  2514. first = pair[0]
  2515. second = pair[1]
  2516. firstMarketId = self.safe_string(first, 'instrument_id')
  2517. secondMarketId = self.safe_string(second, 'instrument_id')
  2518. if firstMarketId != secondMarketId:
  2519. raise NotSupported(self.id + ' parseMyTrade() received unrecognized response format, differing instrument_ids in one fill, the exchange API might have changed, paste your verbose output: https://github.com/ccxt/ccxt/wiki/FAQ#what-is-required-to-get-help')
  2520. marketId = firstMarketId
  2521. # determine the base and quote
  2522. quoteId = None
  2523. symbol = None
  2524. if marketId in self.markets_by_id:
  2525. market = self.markets_by_id[marketId]
  2526. quoteId = market['quoteId']
  2527. symbol = market['symbol']
  2528. else:
  2529. parts = marketId.split('-')
  2530. quoteId = self.safe_string(parts, 1)
  2531. symbol = marketId
  2532. id = self.safe_string(first, 'trade_id')
  2533. price = self.safe_float(first, 'price')
  2534. # determine buy/sell side and amounts
  2535. # get the side from either the first trade or the second trade
  2536. feeCost = self.safe_float(first, 'fee')
  2537. index = 0 if (feeCost != 0) else 1
  2538. userTrade = self.safe_value(pair, index)
  2539. otherTrade = self.safe_value(pair, 1 - index)
  2540. receivedCurrencyId = self.safe_string(userTrade, 'currency')
  2541. side = None
  2542. amount = None
  2543. cost = None
  2544. if receivedCurrencyId == quoteId:
  2545. side = 'sell'
  2546. amount = self.safe_float(otherTrade, 'size')
  2547. cost = self.safe_float(userTrade, 'size')
  2548. else:
  2549. side = 'buy'
  2550. amount = self.safe_float(userTrade, 'size')
  2551. cost = self.safe_float(otherTrade, 'size')
  2552. feeCost = feeCost if (feeCost != 0) else self.safe_float(second, 'fee')
  2553. trade = self.safe_value(pair, index)
  2554. #
  2555. # simplified structures to show the underlying semantics
  2556. #
  2557. # # market/limit sell
  2558. #
  2559. # {
  2560. # "currency":"USDT",
  2561. # "fee":"-0.04647925", # --- fee in received quote currency
  2562. # "price":"129.13", # ------ price
  2563. # "size":"30.98616393", # -- cost
  2564. # },
  2565. # {
  2566. # "currency":"ETH",
  2567. # "fee":"0",
  2568. # "price":"129.13",
  2569. # "size":"0.23996099", # --- amount
  2570. # },
  2571. #
  2572. # # market/limit buy
  2573. #
  2574. # {
  2575. # "currency":"ETH",
  2576. # "fee":"-0.00036049", # --- fee in received base currency
  2577. # "price":"129.16", # ------ price
  2578. # "size":"0.240322", # ----- amount
  2579. # },
  2580. # {
  2581. # "currency":"USDT",
  2582. # "fee":"0",
  2583. # "price":"129.16",
  2584. # "size":"31.03998952", # -- cost
  2585. # }
  2586. #
  2587. timestamp = self.parse8601(self.safe_string_2(trade, 'timestamp', 'created_at'))
  2588. takerOrMaker = self.safe_string_2(trade, 'exec_type', 'liquidity')
  2589. if takerOrMaker == 'M':
  2590. takerOrMaker = 'maker'
  2591. elif takerOrMaker == 'T':
  2592. takerOrMaker = 'taker'
  2593. fee = None
  2594. if feeCost is not None:
  2595. feeCurrencyId = self.safe_string(userTrade, 'currency')
  2596. feeCurrencyCode = self.safe_currency_code(feeCurrencyId)
  2597. fee = {
  2598. # fee is either a positive number(invitation rebate)
  2599. # or a negative number(transaction fee deduction)
  2600. # therefore we need to invert the fee
  2601. # more about it https://github.com/ccxt/ccxt/issues/5909
  2602. 'cost': -feeCost,
  2603. 'currency': feeCurrencyCode,
  2604. }
  2605. orderId = self.safe_string(trade, 'order_id')
  2606. return {
  2607. 'info': pair,
  2608. 'timestamp': timestamp,
  2609. 'datetime': self.iso8601(timestamp),
  2610. 'symbol': symbol,
  2611. 'id': id,
  2612. 'order': orderId,
  2613. 'type': None,
  2614. 'takerOrMaker': takerOrMaker,
  2615. 'side': side,
  2616. 'price': price,
  2617. 'amount': amount,
  2618. 'cost': cost,
  2619. 'fee': fee,
  2620. }
  2621. def parse_my_trades(self, trades, market=None, since=None, limit=None, params={}):
  2622. grouped = self.group_by(trades, 'trade_id')
  2623. tradeIds = list(grouped.keys())
  2624. result = []
  2625. for i in range(0, len(tradeIds)):
  2626. tradeId = tradeIds[i]
  2627. pair = grouped[tradeId]
  2628. # make sure it has exactly 2 trades, no more, no less
  2629. numTradesInPair = len(pair)
  2630. if numTradesInPair == 2:
  2631. trade = self.parse_my_trade(pair)
  2632. result.append(trade)
  2633. symbol = None
  2634. if market is not None:
  2635. symbol = market['symbol']
  2636. return self.filter_by_symbol_since_limit(result, symbol, since, limit)
  2637. def fetch_my_trades(self, symbol=None, since=None, limit=None, params={}):
  2638. # okex actually returns ledger entries instead of fills here, so each fill in the order
  2639. # is represented by two trades with opposite buy/sell sides, not one :\
  2640. # self aspect renders the 'fills' endpoint unusable for fetchOrderTrades
  2641. # until either OKEX fixes the API or we workaround self on our side somehow
  2642. if symbol is None:
  2643. raise ArgumentsRequired(self.id + ' fetchMyTrades requires a symbol argument')
  2644. self.load_markets()
  2645. market = self.market(symbol)
  2646. if (limit is not None) and (limit > 100):
  2647. limit = 100
  2648. request = {
  2649. 'instrument_id': market['id'],
  2650. # 'order_id': id, # string
  2651. # 'after': '1', # pagination of data to return records earlier than the requested ledger_id
  2652. # 'before': '1', # P=pagination of data to return records newer than the requested ledger_id
  2653. # 'limit': limit, # optional, number of results per request, default = maximum = 100
  2654. }
  2655. defaultType = self.safe_string_2(self.options, 'fetchMyTrades', 'defaultType')
  2656. type = self.safe_string(params, 'type', defaultType)
  2657. query = self.omit(params, 'type')
  2658. method = type + 'GetFills'
  2659. response = getattr(self, method)(self.extend(request, query))
  2660. #
  2661. # [
  2662. # # sell
  2663. # {
  2664. # "created_at":"2020-03-29T11:55:25.000Z",
  2665. # "currency":"USDT",
  2666. # "exec_type":"T",
  2667. # "fee":"-0.04647925",
  2668. # "instrument_id":"ETH-USDT",
  2669. # "ledger_id":"10562924353",
  2670. # "liquidity":"T",
  2671. # "order_id":"4636470489136128",
  2672. # "price":"129.13",
  2673. # "product_id":"ETH-USDT",
  2674. # "side":"buy",
  2675. # "size":"30.98616393",
  2676. # "timestamp":"2020-03-29T11:55:25.000Z",
  2677. # "trade_id":"18551601"
  2678. # },
  2679. # {
  2680. # "created_at":"2020-03-29T11:55:25.000Z",
  2681. # "currency":"ETH",
  2682. # "exec_type":"T",
  2683. # "fee":"0",
  2684. # "instrument_id":"ETH-USDT",
  2685. # "ledger_id":"10562924352",
  2686. # "liquidity":"T",
  2687. # "order_id":"4636470489136128",
  2688. # "price":"129.13",
  2689. # "product_id":"ETH-USDT",
  2690. # "side":"sell",
  2691. # "size":"0.23996099",
  2692. # "timestamp":"2020-03-29T11:55:25.000Z",
  2693. # "trade_id":"18551601"
  2694. # },
  2695. # # buy
  2696. # {
  2697. # "created_at":"2020-03-29T11:55:16.000Z",
  2698. # "currency":"ETH",
  2699. # "exec_type":"T",
  2700. # "fee":"-0.00036049",
  2701. # "instrument_id":"ETH-USDT",
  2702. # "ledger_id":"10562922669",
  2703. # "liquidity":"T",
  2704. # "order_id": "4636469894136832",
  2705. # "price":"129.16",
  2706. # "product_id":"ETH-USDT",
  2707. # "side":"buy",
  2708. # "size":"0.240322",
  2709. # "timestamp":"2020-03-29T11:55:16.000Z",
  2710. # "trade_id":"18551600"
  2711. # },
  2712. # {
  2713. # "created_at":"2020-03-29T11:55:16.000Z",
  2714. # "currency":"USDT",
  2715. # "exec_type":"T",
  2716. # "fee":"0",
  2717. # "instrument_id":"ETH-USDT",
  2718. # "ledger_id":"10562922668",
  2719. # "liquidity":"T",
  2720. # "order_id":"4636469894136832",
  2721. # "price":"129.16",
  2722. # "product_id":"ETH-USDT",
  2723. # "side":"sell",
  2724. # "size":"31.03998952",
  2725. # "timestamp":"2020-03-29T11:55:16.000Z",
  2726. # "trade_id":"18551600"
  2727. # }
  2728. # ]
  2729. #
  2730. return self.parse_my_trades(response, market, since, limit, params)
  2731. def fetch_order_trades(self, id, symbol=None, since=None, limit=None, params={}):
  2732. request = {
  2733. # 'instrument_id': market['id'],
  2734. 'order_id': id,
  2735. # 'after': '1', # return the page after the specified page number
  2736. # 'before': '1', # return the page before the specified page number
  2737. # 'limit': limit, # optional, number of results per request, default = maximum = 100
  2738. }
  2739. return self.fetch_my_trades(symbol, since, limit, self.extend(request, params))
  2740. def fetch_ledger(self, code=None, since=None, limit=None, params={}):
  2741. self.load_markets()
  2742. defaultType = self.safe_string_2(self.options, 'fetchLedger', 'defaultType')
  2743. type = self.safe_string(params, 'type', defaultType)
  2744. query = self.omit(params, 'type')
  2745. suffix = '' if (type == 'account') else 'Accounts'
  2746. argument = ''
  2747. request = {
  2748. # 'from': 'id',
  2749. # 'to': 'id',
  2750. }
  2751. if limit is not None:
  2752. request['limit'] = limit
  2753. currency = None
  2754. if (type == 'spot') or (type == 'futures'):
  2755. if code is None:
  2756. raise ArgumentsRequired(self.id + " fetchLedger requires a currency code argument for '" + type + "' markets")
  2757. argument = 'Currency'
  2758. currency = self.currency(code)
  2759. request['currency'] = currency['id']
  2760. elif (type == 'margin') or (type == 'swap'):
  2761. if code is None:
  2762. raise ArgumentsRequired(self.id + " fetchLedger requires a code argument(a market symbol) for '" + type + "' markets")
  2763. argument = 'InstrumentId'
  2764. market = self.market(code) # we intentionally put a market inside here for the margin and swap ledgers
  2765. currency = self.currency(market['base'])
  2766. request['instrument_id'] = market['id']
  2767. #
  2768. # if type == 'margin':
  2769. # #
  2770. # # 3. Borrow
  2771. # # 4. Repayment
  2772. # # 5. Interest
  2773. # # 7. Buy
  2774. # # 8. Sell
  2775. # # 9. From capital account
  2776. # # 10. From C2C
  2777. # # 11. From Futures
  2778. # # 12. From Spot
  2779. # # 13. From ETT
  2780. # # 14. To capital account
  2781. # # 15. To C2C
  2782. # # 16. To Spot
  2783. # # 17. To Futures
  2784. # # 18. To ETT
  2785. # # 19. Mandatory Repayment
  2786. # # 20. From Piggybank
  2787. # # 21. To Piggybank
  2788. # # 22. From Perpetual
  2789. # # 23. To Perpetual
  2790. # # 24. Liquidation Fee
  2791. # # 54. Clawback
  2792. # # 59. Airdrop Return.
  2793. # #
  2794. # request['type'] = 'number' # All types will be returned if self filed is left blank
  2795. # }
  2796. #
  2797. elif type == 'account':
  2798. if code is not None:
  2799. currency = self.currency(code)
  2800. request['currency'] = currency['id']
  2801. #
  2802. # #
  2803. # # 1. deposit
  2804. # # 2. withdrawal
  2805. # # 13. cancel withdrawal
  2806. # # 18. into futures account
  2807. # # 19. out of futures account
  2808. # # 20. into sub account
  2809. # # 21. out of sub account
  2810. # # 28. claim
  2811. # # 29. into ETT account
  2812. # # 30. out of ETT account
  2813. # # 31. into C2C account
  2814. # # 32. out of C2C account
  2815. # # 33. into margin account
  2816. # # 34. out of margin account
  2817. # # 37. into spot account
  2818. # # 38. out of spot account
  2819. # #
  2820. # request['type'] = 'number'
  2821. #
  2822. else:
  2823. raise NotSupported(self.id + " fetchLedger does not support the '" + type + "' type(the type must be one of 'account', 'spot', 'margin', 'futures', 'swap')")
  2824. method = type + 'Get' + suffix + argument + 'Ledger'
  2825. response = getattr(self, method)(self.extend(request, query))
  2826. #
  2827. # transfer funds transfer in/out
  2828. # trade funds moved as a result of a trade, spot and margin accounts only
  2829. # rebate fee rebate as per fee schedule, spot and margin accounts only
  2830. # match open long/open short/close long/close short(futures) or a change in the amount because of trades(swap)
  2831. # fee fee, futures only
  2832. # settlement settlement/clawback/settle long/settle short
  2833. # liquidation force close long/force close short/deliver close long/deliver close short
  2834. # funding funding fee, swap only
  2835. # margin a change in the amount after adjusting margin, swap only
  2836. #
  2837. # account
  2838. #
  2839. # [
  2840. # {
  2841. # "amount":0.00051843,
  2842. # "balance":0.00100941,
  2843. # "currency":"BTC",
  2844. # "fee":0,
  2845. # "ledger_id":8987285,
  2846. # "timestamp":"2018-10-12T11:01:14.000Z",
  2847. # "typename":"Get from activity"
  2848. # }
  2849. # ]
  2850. #
  2851. # spot
  2852. #
  2853. # [
  2854. # {
  2855. # "timestamp":"2019-03-18T07:08:25.000Z",
  2856. # "ledger_id":"3995334780",
  2857. # "created_at":"2019-03-18T07:08:25.000Z",
  2858. # "currency":"BTC",
  2859. # "amount":"0.0009985",
  2860. # "balance":"0.0029955",
  2861. # "type":"trade",
  2862. # "details":{
  2863. # "instrument_id":"BTC-USDT",
  2864. # "order_id":"2500650881647616",
  2865. # "product_id":"BTC-USDT"
  2866. # }
  2867. # }
  2868. # ]
  2869. #
  2870. # margin
  2871. #
  2872. # [
  2873. # [
  2874. # {
  2875. # "created_at":"2019-03-20T03:45:05.000Z",
  2876. # "ledger_id":"78918186",
  2877. # "timestamp":"2019-03-20T03:45:05.000Z",
  2878. # "currency":"EOS",
  2879. # "amount":"0", # ?
  2880. # "balance":"0.59957711",
  2881. # "type":"transfer",
  2882. # "details":{
  2883. # "instrument_id":"EOS-USDT",
  2884. # "order_id":"787057",
  2885. # "product_id":"EOS-USDT"
  2886. # }
  2887. # }
  2888. # ],
  2889. # {
  2890. # "before":"78965766",
  2891. # "after":"78918186"
  2892. # }
  2893. # ]
  2894. #
  2895. # futures
  2896. #
  2897. # [
  2898. # {
  2899. # "ledger_id":"2508090544914461",
  2900. # "timestamp":"2019-03-19T14:40:24.000Z",
  2901. # "amount":"-0.00529521",
  2902. # "balance":"0",
  2903. # "currency":"EOS",
  2904. # "type":"fee",
  2905. # "details":{
  2906. # "order_id":"2506982456445952",
  2907. # "instrument_id":"EOS-USD-190628"
  2908. # }
  2909. # }
  2910. # ]
  2911. #
  2912. # swap
  2913. #
  2914. # [
  2915. # {
  2916. # "amount":"0.004742",
  2917. # "fee":"-0.000551",
  2918. # "type":"match",
  2919. # "instrument_id":"EOS-USD-SWAP",
  2920. # "ledger_id":"197429674941902848",
  2921. # "timestamp":"2019-03-25T05:56:31.286Z"
  2922. # },
  2923. # ]
  2924. #
  2925. responseLength = len(response)
  2926. if responseLength < 1:
  2927. return []
  2928. isArray = isinstance(response[0], list)
  2929. isMargin = (type == 'margin')
  2930. entries = response[0] if (isMargin and isArray) else response
  2931. if type == 'swap':
  2932. ledgerEntries = self.parse_ledger(entries)
  2933. return self.filter_by_symbol_since_limit(ledgerEntries, code, since, limit)
  2934. return self.parse_ledger(entries, currency, since, limit)
  2935. def parse_ledger_entry_type(self, type):
  2936. types = {
  2937. 'transfer': 'transfer', # # funds transfer in/out
  2938. 'trade': 'trade', # funds moved as a result of a trade, spot and margin accounts only
  2939. 'rebate': 'rebate', # fee rebate as per fee schedule, spot and margin accounts only
  2940. 'match': 'trade', # open long/open short/close long/close short(futures) or a change in the amount because of trades(swap)
  2941. 'fee': 'fee', # fee, futures only
  2942. 'settlement': 'trade', # settlement/clawback/settle long/settle short
  2943. 'liquidation': 'trade', # force close long/force close short/deliver close long/deliver close short
  2944. 'funding': 'fee', # funding fee, swap only
  2945. 'margin': 'margin', # a change in the amount after adjusting margin, swap only
  2946. }
  2947. return self.safe_string(types, type, type)
  2948. def parse_ledger_entry(self, item, currency=None):
  2949. #
  2950. #
  2951. # account
  2952. #
  2953. # {
  2954. # "amount":0.00051843,
  2955. # "balance":0.00100941,
  2956. # "currency":"BTC",
  2957. # "fee":0,
  2958. # "ledger_id":8987285,
  2959. # "timestamp":"2018-10-12T11:01:14.000Z",
  2960. # "typename":"Get from activity"
  2961. # }
  2962. #
  2963. # spot
  2964. #
  2965. # {
  2966. # "timestamp":"2019-03-18T07:08:25.000Z",
  2967. # "ledger_id":"3995334780",
  2968. # "created_at":"2019-03-18T07:08:25.000Z",
  2969. # "currency":"BTC",
  2970. # "amount":"0.0009985",
  2971. # "balance":"0.0029955",
  2972. # "type":"trade",
  2973. # "details":{
  2974. # "instrument_id":"BTC-USDT",
  2975. # "order_id":"2500650881647616",
  2976. # "product_id":"BTC-USDT"
  2977. # }
  2978. # }
  2979. #
  2980. # margin
  2981. #
  2982. # {
  2983. # "created_at":"2019-03-20T03:45:05.000Z",
  2984. # "ledger_id":"78918186",
  2985. # "timestamp":"2019-03-20T03:45:05.000Z",
  2986. # "currency":"EOS",
  2987. # "amount":"0", # ?
  2988. # "balance":"0.59957711",
  2989. # "type":"transfer",
  2990. # "details":{
  2991. # "instrument_id":"EOS-USDT",
  2992. # "order_id":"787057",
  2993. # "product_id":"EOS-USDT"
  2994. # }
  2995. # }
  2996. #
  2997. # futures
  2998. #
  2999. # {
  3000. # "ledger_id":"2508090544914461",
  3001. # "timestamp":"2019-03-19T14:40:24.000Z",
  3002. # "amount":"-0.00529521",
  3003. # "balance":"0",
  3004. # "currency":"EOS",
  3005. # "type":"fee",
  3006. # "details":{
  3007. # "order_id":"2506982456445952",
  3008. # "instrument_id":"EOS-USD-190628"
  3009. # }
  3010. # }
  3011. #
  3012. # swap
  3013. #
  3014. # {
  3015. # "amount":"0.004742",
  3016. # "fee":"-0.000551",
  3017. # "type":"match",
  3018. # "instrument_id":"EOS-USD-SWAP",
  3019. # "ledger_id":"197429674941902848",
  3020. # "timestamp":"2019-03-25T05:56:31.286Z"
  3021. # },
  3022. #
  3023. id = self.safe_string(item, 'ledger_id')
  3024. account = None
  3025. details = self.safe_value(item, 'details', {})
  3026. referenceId = self.safe_string(details, 'order_id')
  3027. referenceAccount = None
  3028. type = self.parse_ledger_entry_type(self.safe_string(item, 'type'))
  3029. code = self.safe_currency_code(self.safe_string(item, 'currency'), currency)
  3030. amount = self.safe_float(item, 'amount')
  3031. timestamp = self.parse8601(self.safe_string(item, 'timestamp'))
  3032. fee = {
  3033. 'cost': self.safe_float(item, 'fee'),
  3034. 'currency': code,
  3035. }
  3036. before = None
  3037. after = self.safe_float(item, 'balance')
  3038. status = 'ok'
  3039. marketId = self.safe_string(item, 'instrument_id')
  3040. symbol = None
  3041. if marketId in self.markets_by_id:
  3042. market = self.markets_by_id[marketId]
  3043. symbol = market['symbol']
  3044. return {
  3045. 'info': item,
  3046. 'id': id,
  3047. 'account': account,
  3048. 'referenceId': referenceId,
  3049. 'referenceAccount': referenceAccount,
  3050. 'type': type,
  3051. 'currency': code,
  3052. 'symbol': symbol,
  3053. 'amount': amount,
  3054. 'before': before, # balance before
  3055. 'after': after, # balance after
  3056. 'status': status,
  3057. 'timestamp': timestamp,
  3058. 'datetime': self.iso8601(timestamp),
  3059. 'fee': fee,
  3060. }
  3061. def sign(self, path, api='public', method='GET', params={}, headers=None, body=None):
  3062. isArray = isinstance(params, list)
  3063. request = '/api/' + api + '/' + self.version + '/'
  3064. request += path if isArray else self.implode_params(path, params)
  3065. query = params if isArray else self.omit(params, self.extract_params(path))
  3066. url = self.implode_params(self.urls['api']['rest'], {'hostname': self.hostname}) + request
  3067. type = self.get_path_authentication_type(path)
  3068. if type == 'public':
  3069. if query:
  3070. url += '?' + self.urlencode(query)
  3071. elif type == 'private':
  3072. self.check_required_credentials()
  3073. timestamp = self.iso8601(self.milliseconds())
  3074. headers = {
  3075. 'OK-ACCESS-KEY': self.apiKey,
  3076. 'OK-ACCESS-PASSPHRASE': self.password,
  3077. 'OK-ACCESS-TIMESTAMP': timestamp,
  3078. # 'OK-FROM': '',
  3079. # 'OK-TO': '',
  3080. # 'OK-LIMIT': '',
  3081. }
  3082. auth = timestamp + method + request
  3083. if method == 'GET':
  3084. if query:
  3085. urlencodedQuery = '?' + self.urlencode(query)
  3086. url += urlencodedQuery
  3087. auth += urlencodedQuery
  3088. else:
  3089. if isArray or query:
  3090. body = self.json(query)
  3091. auth += body
  3092. headers['Content-Type'] = 'application/json'
  3093. signature = self.hmac(self.encode(auth), self.encode(self.secret), hashlib.sha256, 'base64')
  3094. headers['OK-ACCESS-SIGN'] = signature
  3095. return {'url': url, 'method': method, 'body': body, 'headers': headers}
  3096. def get_path_authentication_type(self, path):
  3097. # https://github.com/ccxt/ccxt/issues/6651
  3098. # a special case to handle the optionGetUnderlying interefering with
  3099. # other endpoints containing self keyword
  3100. if path == 'underlying':
  3101. return 'public'
  3102. auth = self.safe_value(self.options, 'auth', {})
  3103. key = self.find_broadly_matched_key(auth, path)
  3104. return self.safe_string(auth, key, 'private')
  3105. def handle_errors(self, code, reason, url, method, headers, body, response, requestHeaders, requestBody):
  3106. if not response:
  3107. return # fallback to default error handler
  3108. feedback = self.id + ' ' + body
  3109. if code == 503:
  3110. # {"message":"name resolution failed"}
  3111. raise ExchangeNotAvailable(feedback)
  3112. #
  3113. # {"error_message":"Order does not exist","result":"true","error_code":"35029","order_id":"-1"}
  3114. #
  3115. message = self.safe_string(response, 'message')
  3116. errorCode = self.safe_string_2(response, 'code', 'error_code')
  3117. nonEmptyMessage = ((message is not None) and (message != ''))
  3118. nonZeroErrorCode = (errorCode is not None) and (errorCode != '0')
  3119. if nonEmptyMessage:
  3120. self.throw_exactly_matched_exception(self.exceptions['exact'], message, feedback)
  3121. self.throw_broadly_matched_exception(self.exceptions['broad'], message, feedback)
  3122. if nonZeroErrorCode:
  3123. self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback)
  3124. if nonZeroErrorCode or nonEmptyMessage:
  3125. raise ExchangeError(feedback) # unknown message