/python/ccxt/async_support/bitget.py

https://github.com/kroitor/ccxt · Python · 2702 lines · 1687 code · 46 blank · 969 comment · 177 complexity · 582055de9ad786b81a32603b016fc750 MD5 · raw file

  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.async_support.base.exchange import Exchange
  5. import hashlib
  6. from ccxt.base.errors import ExchangeError
  7. from ccxt.base.errors import AuthenticationError
  8. from ccxt.base.errors import PermissionDenied
  9. from ccxt.base.errors import AccountSuspended
  10. from ccxt.base.errors import ArgumentsRequired
  11. from ccxt.base.errors import BadRequest
  12. from ccxt.base.errors import BadSymbol
  13. from ccxt.base.errors import InsufficientFunds
  14. from ccxt.base.errors import InvalidAddress
  15. from ccxt.base.errors import InvalidOrder
  16. from ccxt.base.errors import OrderNotFound
  17. from ccxt.base.errors import CancelPending
  18. from ccxt.base.errors import NotSupported
  19. from ccxt.base.errors import DDoSProtection
  20. from ccxt.base.errors import RateLimitExceeded
  21. from ccxt.base.errors import ExchangeNotAvailable
  22. from ccxt.base.errors import OnMaintenance
  23. from ccxt.base.errors import InvalidNonce
  24. from ccxt.base.errors import RequestTimeout
  25. from ccxt.base.decimal_to_precision import TRUNCATE
  26. from ccxt.base.decimal_to_precision import DECIMAL_PLACES
  27. from ccxt.base.decimal_to_precision import TICK_SIZE
  28. class bitget(Exchange):
  29. def describe(self):
  30. return self.deep_extend(super(bitget, self).describe(), {
  31. 'id': 'bitget',
  32. 'name': 'Bitget',
  33. 'countries': ['SG'],
  34. 'version': 'v3',
  35. 'rateLimit': 1000, # up to 3000 requests per 5 minutes 600 requests per minute 10 requests per second 100 ms
  36. 'has': {
  37. 'cancelOrder': True,
  38. 'cancelOrders': True,
  39. 'CORS': False,
  40. 'createOrder': True,
  41. 'fetchAccounts': True,
  42. 'fetchBalance': True,
  43. 'fetchCurrencies': True,
  44. 'fetchDeposits': True,
  45. 'fetchMarkets': True,
  46. 'fetchMyTrades': True,
  47. 'fetchOHLCV': True,
  48. 'fetchOrder': True,
  49. 'fetchOrderBook': True,
  50. 'fetchOpenOrders': True,
  51. 'fetchClosedOrders': True,
  52. 'fetchOrderTrades': True,
  53. 'fetchTicker': True,
  54. 'fetchTickers': True,
  55. 'fetchTime': True,
  56. 'fetchTrades': True,
  57. 'fetchWithdrawals': True,
  58. },
  59. 'timeframes': {
  60. '1m': '1m',
  61. '5m': '5m',
  62. '15m': '15m',
  63. '30m': '30m',
  64. '1h': '1h',
  65. '2h': '2h',
  66. '4h': '4h',
  67. '6h': '6h',
  68. '12h': '12h',
  69. '1d': '1d',
  70. '1w': '1w',
  71. },
  72. 'hostname': 'bitget.com',
  73. 'urls': {
  74. 'logo': 'https://user-images.githubusercontent.com/51840849/88317935-a8a21c80-cd22-11ea-8e2b-4b9fac5975eb.jpg',
  75. 'api': {
  76. 'data': 'https://api.{hostname}',
  77. 'api': 'https://api.{hostname}',
  78. 'capi': 'https://capi.{hostname}',
  79. 'swap': 'https://capi.{hostname}',
  80. },
  81. 'www': 'https://www.bitget.com',
  82. 'doc': [
  83. 'https://bitgetlimited.github.io/apidoc/en/swap',
  84. 'https://bitgetlimited.github.io/apidoc/en/spot',
  85. ],
  86. 'fees': 'https://www.bitget.cc/zh-CN/rate?tab=1',
  87. 'test': {
  88. 'rest': 'https://testnet.bitget.com',
  89. },
  90. 'referral': 'https://www.bitget.com/expressly?languageType=0&channelCode=ccxt&vipCode=tg9j',
  91. },
  92. 'api': {
  93. 'data': {
  94. 'get': [
  95. 'market/history/kline', # Kline data
  96. 'market/detail/merged', # Get aggregated ticker
  97. 'market/tickers', # Get all trading tickers
  98. 'market/allticker', # Get all trading market method 2
  99. 'market/depth', # Get Market Depth Data
  100. 'market/trade', # Get Trade Detail Data
  101. 'market/history/trade', # Get record of trading
  102. 'market/detail', # Get Market Detail 24h Volume
  103. 'common/symbols', # Query all trading pairs and accuracy supported in the station
  104. 'common/currencys', # Query all currencies supported in the station
  105. 'common/timestamp', # Query system current time
  106. ],
  107. },
  108. 'api': {
  109. 'get': [
  110. 'account/accounts', # Get all accounts of current user(即account_id)
  111. 'accounts/{account_id}/balance', # Get the balance of the specified account
  112. 'order/orders', # Query order, deprecated
  113. 'order/orders/openOrders',
  114. 'order/orders/history',
  115. 'order/deposit_withdraw', # Query assets history
  116. ],
  117. 'post': [
  118. 'order/orders/place', # Place order
  119. 'order/orders/{order_id}/submitcancel', # Request to cancel an order request
  120. 'order/orders/batchcancel', # Bulk order cancellation
  121. 'order/orders/{order_id}', # Query an order details
  122. 'order/orders/{order_id}/matchresults', # Query the transaction details of an order
  123. 'order/matchresults', # Query current order, order history
  124. ],
  125. },
  126. 'capi': {
  127. 'get': [
  128. 'market/time',
  129. 'market/contracts',
  130. 'market/depth',
  131. 'market/tickers',
  132. 'market/ticker',
  133. 'market/trades',
  134. 'market/candles',
  135. 'market/index',
  136. 'market/open_interest',
  137. 'market/price_limit',
  138. 'market/funding_time',
  139. 'market/historical_funding_rate',
  140. 'market/mark_price',
  141. 'market/open_count',
  142. ],
  143. },
  144. 'swap': {
  145. 'get': [
  146. 'account/accounts',
  147. 'account/account',
  148. 'account/settings',
  149. 'position/allPosition',
  150. 'position/singlePosition',
  151. 'position/holds',
  152. 'order/detail',
  153. 'order/orders',
  154. 'order/fills',
  155. 'order/currentPlan',
  156. 'order/historyPlan',
  157. ],
  158. 'post': [
  159. 'account/leverage',
  160. 'account/adjustMargin',
  161. 'account/modifyAutoAppendMargin',
  162. 'order/placeOrder',
  163. 'order/batchOrders',
  164. 'order/cancel_order',
  165. 'order/cancel_batch_orders',
  166. 'order/plan_order',
  167. 'order/cancel_plan',
  168. ],
  169. },
  170. },
  171. 'fees': {
  172. 'spot': {
  173. 'taker': 0.002,
  174. 'maker': 0.002,
  175. },
  176. 'swap': {
  177. 'taker': 0.0006,
  178. 'maker': 0.0004,
  179. },
  180. },
  181. 'requiredCredentials': {
  182. 'apiKey': True,
  183. 'secret': True,
  184. 'password': True,
  185. },
  186. 'exceptions': {
  187. # http error codes
  188. # 400 Bad Request Invalid request format
  189. # 401 Unauthorized Invalid API Key
  190. # 403 Forbidden You do not have access to the requested resource
  191. # 404 Not Found
  192. # 500 Internal Server Error We had a problem with our server
  193. 'exact': {
  194. '1': ExchangeError, # {"code": 1, "message": "System error"}
  195. # undocumented
  196. 'failure to get a peer from the ring-balancer': ExchangeNotAvailable, # {"message": "failure to get a peer from the ring-balancer"}
  197. '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 "}
  198. # common
  199. # '0': ExchangeError, # 200 successful,when the order placement / cancellation / operation is successful
  200. '4001': ExchangeError, # no data received in 30s
  201. '4002': ExchangeError, # Buffer full. cannot write data
  202. # --------------------------------------------------------
  203. '30001': AuthenticationError, # {"code": 30001, "message": 'request header "OK_ACCESS_KEY" cannot be blank'}
  204. '30002': AuthenticationError, # {"code": 30002, "message": 'request header "OK_ACCESS_SIGN" cannot be blank'}
  205. '30003': AuthenticationError, # {"code": 30003, "message": 'request header "OK_ACCESS_TIMESTAMP" cannot be blank'}
  206. '30004': AuthenticationError, # {"code": 30004, "message": 'request header "OK_ACCESS_PASSPHRASE" cannot be blank'}
  207. '30005': InvalidNonce, # {"code": 30005, "message": "invalid OK_ACCESS_TIMESTAMP"}
  208. '30006': AuthenticationError, # {"code": 30006, "message": "invalid OK_ACCESS_KEY"}
  209. '30007': BadRequest, # {"code": 30007, "message": 'invalid Content_Type, please use "application/json" format'}
  210. '30008': RequestTimeout, # {"code": 30008, "message": "timestamp request expired"}
  211. '30009': ExchangeError, # {"code": 30009, "message": "system error"}
  212. '30010': AuthenticationError, # {"code": 30010, "message": "API validation failed"}
  213. '30011': PermissionDenied, # {"code": 30011, "message": "invalid IP"}
  214. '30012': AuthenticationError, # {"code": 30012, "message": "invalid authorization"}
  215. '30013': AuthenticationError, # {"code": 30013, "message": "invalid sign"}
  216. '30014': DDoSProtection, # {"code": 30014, "message": "request too frequent"}
  217. '30015': AuthenticationError, # {"code": 30015, "message": 'request header "OK_ACCESS_PASSPHRASE" incorrect'}
  218. '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"}
  219. '30017': ExchangeError, # {"code": 30017, "message": "apikey's broker id does not match"}
  220. '30018': ExchangeError, # {"code": 30018, "message": "apikey's domain does not match"}
  221. '30019': ExchangeNotAvailable, # {"code": 30019, "message": "Api is offline or unavailable"}
  222. '30020': BadRequest, # {"code": 30020, "message": "body cannot be blank"}
  223. '30021': BadRequest, # {"code": 30021, "message": "Json data format error"}, {"code": 30021, "message": "json data format error"}
  224. '30022': PermissionDenied, # {"code": 30022, "message": "Api has been frozen"}
  225. '30023': BadRequest, # {"code": 30023, "message": "{0} parameter cannot be blank"}
  226. '30024': BadSymbol, # {"code":30024,"message":"\"instrument_id\" is an invalid parameter"}
  227. '30025': BadRequest, # {"code": 30025, "message": "{0} parameter category error"}
  228. '30026': DDoSProtection, # {"code": 30026, "message": "requested too frequent"}
  229. '30027': AuthenticationError, # {"code": 30027, "message": "login failure"}
  230. '30028': PermissionDenied, # {"code": 30028, "message": "unauthorized execution"}
  231. '30029': AccountSuspended, # {"code": 30029, "message": "account suspended"}
  232. '30030': ExchangeError, # {"code": 30030, "message": "endpoint request failed. Please try again"}
  233. '30031': BadRequest, # {"code": 30031, "message": "token does not exist"}
  234. '30032': BadSymbol, # {"code": 30032, "message": "pair does not exist"}
  235. '30033': BadRequest, # {"code": 30033, "message": "exchange domain does not exist"}
  236. '30034': ExchangeError, # {"code": 30034, "message": "exchange ID does not exist"}
  237. '30035': ExchangeError, # {"code": 30035, "message": "trading is not supported in self website"}
  238. '30036': ExchangeError, # {"code": 30036, "message": "no relevant data"}
  239. '30037': ExchangeNotAvailable, # {"code": 30037, "message": "endpoint is offline or unavailable"}
  240. # '30038': AuthenticationError, # {"code": 30038, "message": "user does not exist"}
  241. '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}
  242. # futures
  243. '32001': AccountSuspended, # {"code": 32001, "message": "futures account suspended"}
  244. '32002': PermissionDenied, # {"code": 32002, "message": "futures account does not exist"}
  245. '32003': CancelPending, # {"code": 32003, "message": "canceling, please wait"}
  246. '32004': ExchangeError, # {"code": 32004, "message": "you have no unfilled orders"}
  247. '32005': InvalidOrder, # {"code": 32005, "message": "max order quantity"}
  248. '32006': InvalidOrder, # {"code": 32006, "message": "the order price or trigger price exceeds USD 1 million"}
  249. '32007': InvalidOrder, # {"code": 32007, "message": "leverage level must be the same for orders on the same side of the contract"}
  250. '32008': InvalidOrder, # {"code": 32008, "message": "Max. positions to open(cross margin)"}
  251. '32009': InvalidOrder, # {"code": 32009, "message": "Max. positions to open(fixed margin)"}
  252. '32010': ExchangeError, # {"code": 32010, "message": "leverage cannot be changed with open positions"}
  253. '32011': ExchangeError, # {"code": 32011, "message": "futures status error"}
  254. '32012': ExchangeError, # {"code": 32012, "message": "futures order update error"}
  255. '32013': ExchangeError, # {"code": 32013, "message": "token type is blank"}
  256. '32014': ExchangeError, # {"code": 32014, "message": "your number of contracts closing is larger than the number of contracts available"}
  257. '32015': ExchangeError, # {"code": 32015, "message": "margin ratio is lower than 100% before opening positions"}
  258. '32016': ExchangeError, # {"code": 32016, "message": "margin ratio is lower than 100% after opening position"}
  259. '32017': ExchangeError, # {"code": 32017, "message": "no BBO"}
  260. '32018': ExchangeError, # {"code": 32018, "message": "the order quantity is less than 1, please try again"}
  261. '32019': ExchangeError, # {"code": 32019, "message": "the order price deviates from the price of the previous minute by more than 3%"}
  262. '32020': ExchangeError, # {"code": 32020, "message": "the price is not in the range of the price limit"}
  263. '32021': ExchangeError, # {"code": 32021, "message": "leverage error"}
  264. '32022': ExchangeError, # {"code": 32022, "message": "self function is not supported in your country or region according to the regulations"}
  265. '32023': ExchangeError, # {"code": 32023, "message": "self account has outstanding loan"}
  266. '32024': ExchangeError, # {"code": 32024, "message": "order cannot be placed during delivery"}
  267. '32025': ExchangeError, # {"code": 32025, "message": "order cannot be placed during settlement"}
  268. '32026': ExchangeError, # {"code": 32026, "message": "your account is restricted from opening positions"}
  269. '32027': ExchangeError, # {"code": 32027, "message": "cancelled over 20 orders"}
  270. '32028': AccountSuspended, # {"code": 32028, "message": "account is suspended and liquidated"}
  271. '32029': ExchangeError, # {"code": 32029, "message": "order info does not exist"}
  272. '32030': InvalidOrder, # The order cannot be cancelled
  273. '32031': ArgumentsRequired, # client_oid or order_id is required.
  274. '32038': AuthenticationError, # User does not exist
  275. '32040': ExchangeError, # User have open contract orders or position
  276. '32044': ExchangeError, # {"code": 32044, "message": "The margin ratio after submitting self order is lower than the minimum requirement({0}) for your tier."}
  277. '32045': ExchangeError, # String of commission over 1 million
  278. '32046': ExchangeError, # Each user can hold up to 10 trade plans at the same time
  279. '32047': ExchangeError, # system error
  280. '32048': InvalidOrder, # Order strategy track range error
  281. '32049': ExchangeError, # Each user can hold up to 10 track plans at the same time
  282. '32050': InvalidOrder, # Order strategy rang error
  283. '32051': InvalidOrder, # Order strategy ice depth error
  284. '32052': ExchangeError, # String of commission over 100 thousand
  285. '32053': ExchangeError, # Each user can hold up to 6 ice plans at the same time
  286. '32057': ExchangeError, # The order price is zero. Market-close-all function cannot be executed
  287. '32054': ExchangeError, # Trade not allow
  288. '32055': InvalidOrder, # cancel order error
  289. '32056': ExchangeError, # iceberg per order average should between {0}-{1} contracts
  290. '32058': ExchangeError, # Each user can hold up to 6 initiative plans at the same time
  291. '32059': InvalidOrder, # Total amount should exceed per order amount
  292. '32060': InvalidOrder, # Order strategy type error
  293. '32061': InvalidOrder, # Order strategy initiative limit error
  294. '32062': InvalidOrder, # Order strategy initiative range error
  295. '32063': InvalidOrder, # Order strategy initiative rate error
  296. '32064': ExchangeError, # Time Stringerval of orders should set between 5-120s
  297. '32065': ExchangeError, # Close amount exceeds the limit of Market-close-all(999 for BTC, and 9999 for the rest tokens)
  298. '32066': ExchangeError, # You have open orders. Please cancel all open orders before changing your leverage level.
  299. '32067': ExchangeError, # Account equity < required margin in self setting. Please adjust your leverage level again.
  300. '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.
  301. '32069': ExchangeError, # Target leverage level too low. Your account balance is insufficient to cover the margin required. Please adjust the leverage level again.
  302. '32070': ExchangeError, # Please check open position or unfilled order
  303. '32071': ExchangeError, # Your current liquidation mode does not support self action.
  304. '32072': ExchangeError, # The highest available margin for your orders tier is {0}. Please edit your margin and place a new order.
  305. '32073': ExchangeError, # The action does not apply to the token
  306. '32074': ExchangeError, # The number of contracts of your position, open orders, and the current order has exceeded the maximum order limit of self asset.
  307. '32075': ExchangeError, # Account risk rate breach
  308. '32076': ExchangeError, # Liquidation of the holding position(s) at market price will require cancellation of all pending close orders of the contracts.
  309. '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.)
  310. '32078': ExchangeError, # Please cancel all open orders before switching the liquidation mode(Please cancel all open orders before switching the liquidation mode)
  311. '32079': ExchangeError, # Your open positions are at high risk.(Please add margin or reduce positions before switching the mode)
  312. '32080': ExchangeError, # Funds cannot be transferred out within 30 minutes after futures settlement
  313. '32083': ExchangeError, # The number of contracts should be a positive multiple of %%. Please place your order again
  314. # token and margin trading
  315. '33001': PermissionDenied, # {"code": 33001, "message": "margin account for self pair is not enabled yet"}
  316. '33002': AccountSuspended, # {"code": 33002, "message": "margin account for self pair is suspended"}
  317. '33003': InsufficientFunds, # {"code": 33003, "message": "no loan balance"}
  318. '33004': ExchangeError, # {"code": 33004, "message": "loan amount cannot be smaller than the minimum limit"}
  319. '33005': ExchangeError, # {"code": 33005, "message": "repayment amount must exceed 0"}
  320. '33006': ExchangeError, # {"code": 33006, "message": "loan order not found"}
  321. '33007': ExchangeError, # {"code": 33007, "message": "status not found"}
  322. '33008': InsufficientFunds, # {"code": 33008, "message": "loan amount cannot exceed the maximum limit"}
  323. '33009': ExchangeError, # {"code": 33009, "message": "user ID is blank"}
  324. '33010': ExchangeError, # {"code": 33010, "message": "you cannot cancel an order during session 2 of call auction"}
  325. '33011': ExchangeError, # {"code": 33011, "message": "no new market data"}
  326. '33012': ExchangeError, # {"code": 33012, "message": "order cancellation failed"}
  327. '33013': InvalidOrder, # {"code": 33013, "message": "order placement failed"}
  328. '33014': OrderNotFound, # {"code": 33014, "message": "order does not exist"}
  329. '33015': InvalidOrder, # {"code": 33015, "message": "exceeded maximum limit"}
  330. '33016': ExchangeError, # {"code": 33016, "message": "margin trading is not open for self token"}
  331. '33017': InsufficientFunds, # {"code": 33017, "message": "insufficient balance"}
  332. '33018': ExchangeError, # {"code": 33018, "message": "self parameter must be smaller than 1"}
  333. '33020': ExchangeError, # {"code": 33020, "message": "request not supported"}
  334. '33021': BadRequest, # {"code": 33021, "message": "token and the pair do not match"}
  335. '33022': InvalidOrder, # {"code": 33022, "message": "pair and the order do not match"}
  336. '33023': ExchangeError, # {"code": 33023, "message": "you can only place market orders during call auction"}
  337. '33024': InvalidOrder, # {"code": 33024, "message": "trading amount too small"}
  338. '33025': InvalidOrder, # {"code": 33025, "message": "base token amount is blank"}
  339. '33026': ExchangeError, # {"code": 33026, "message": "transaction completed"}
  340. '33027': InvalidOrder, # {"code": 33027, "message": "cancelled order or order cancelling"}
  341. '33028': InvalidOrder, # {"code": 33028, "message": "the decimal places of the trading price exceeded the limit"}
  342. '33029': InvalidOrder, # {"code": 33029, "message": "the decimal places of the trading size exceeded the limit"}
  343. '33034': ExchangeError, # {"code": 33034, "message": "You can only place limit order after Call Auction has started"}
  344. '33035': ExchangeError, # This type of order cannot be canceled(This type of order cannot be canceled)
  345. '33036': ExchangeError, # Exceeding the limit of entrust order
  346. '33037': ExchangeError, # The buy order price should be lower than 130% of the trigger price
  347. '33038': ExchangeError, # The sell order price should be higher than 70% of the trigger price
  348. '33039': ExchangeError, # The limit of callback rate is 0 < x <= 5%
  349. '33040': ExchangeError, # The trigger price of a buy order should be lower than the latest transaction price
  350. '33041': ExchangeError, # The trigger price of a sell order should be higher than the latest transaction price
  351. '33042': ExchangeError, # The limit of price variance is 0 < x <= 1%
  352. '33043': ExchangeError, # The total amount must be larger than 0
  353. '33044': ExchangeError, # The average amount should be 1/1000 * total amount <= x <= total amount
  354. '33045': ExchangeError, # The price should not be 0, including trigger price, order price, and price limit
  355. '33046': ExchangeError, # Price variance should be 0 < x <= 1%
  356. '33047': ExchangeError, # Sweep ratio should be 0 < x <= 100%
  357. '33048': ExchangeError, # Per order limit: Total amount/1000 < x <= Total amount
  358. '33049': ExchangeError, # Total amount should be X > 0
  359. '33050': ExchangeError, # Time interval should be 5 <= x <= 120s
  360. '33051': ExchangeError, # cancel order number not higher limit: plan and track entrust no more than 10, ice and time entrust no more than 6
  361. '33059': BadRequest, # {"code": 33059, "message": "client_oid or order_id is required"}
  362. '33060': BadRequest, # {"code": 33060, "message": "Only fill in either parameter client_oid or order_id"}
  363. '33061': ExchangeError, # Value of a single market price order cannot exceed 100,000 USD
  364. '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
  365. '33063': ExchangeError, # Leverage multiple is too low, there is insufficient margin in the account, please readjust the leverage ratio
  366. '33064': ExchangeError, # The setting of the leverage ratio cannot be less than 2, please readjust the leverage ratio
  367. '33065': ExchangeError, # Leverage ratio exceeds maximum leverage ratio, please readjust leverage ratio
  368. # account
  369. '21009': ExchangeError, # Funds cannot be transferred out within 30 minutes after swap settlement(Funds cannot be transferred out within 30 minutes after swap settlement)
  370. '34001': PermissionDenied, # {"code": 34001, "message": "withdrawal suspended"}
  371. '34002': InvalidAddress, # {"code": 34002, "message": "please add a withdrawal address"}
  372. '34003': ExchangeError, # {"code": 34003, "message": "sorry, self token cannot be withdrawn to xx at the moment"}
  373. '34004': ExchangeError, # {"code": 34004, "message": "withdrawal fee is smaller than minimum limit"}
  374. '34005': ExchangeError, # {"code": 34005, "message": "withdrawal fee exceeds the maximum limit"}
  375. '34006': ExchangeError, # {"code": 34006, "message": "withdrawal amount is lower than the minimum limit"}
  376. '34007': ExchangeError, # {"code": 34007, "message": "withdrawal amount exceeds the maximum limit"}
  377. '34008': InsufficientFunds, # {"code": 34008, "message": "insufficient balance"}
  378. '34009': ExchangeError, # {"code": 34009, "message": "your withdrawal amount exceeds the daily limit"}
  379. '34010': ExchangeError, # {"code": 34010, "message": "transfer amount must be larger than 0"}
  380. '34011': ExchangeError, # {"code": 34011, "message": "conditions not met"}
  381. '34012': ExchangeError, # {"code": 34012, "message": "the minimum withdrawal amount for NEO is 1, and the amount must be an integer"}
  382. '34013': ExchangeError, # {"code": 34013, "message": "please transfer"}
  383. '34014': ExchangeError, # {"code": 34014, "message": "transfer limited"}
  384. '34015': ExchangeError, # {"code": 34015, "message": "subaccount does not exist"}
  385. '34016': PermissionDenied, # {"code": 34016, "message": "transfer suspended"}
  386. '34017': AccountSuspended, # {"code": 34017, "message": "account suspended"}
  387. '34018': AuthenticationError, # {"code": 34018, "message": "incorrect trades password"}
  388. '34019': PermissionDenied, # {"code": 34019, "message": "please bind your email before withdrawal"}
  389. '34020': PermissionDenied, # {"code": 34020, "message": "please bind your funds password before withdrawal"}
  390. '34021': InvalidAddress, # {"code": 34021, "message": "Not verified address"}
  391. '34022': ExchangeError, # {"code": 34022, "message": "Withdrawals are not available for sub accounts"}
  392. '34023': PermissionDenied, # {"code": 34023, "message": "Please enable futures trading before transferring your funds"}
  393. '34026': ExchangeError, # transfer too frequently(transfer too frequently)
  394. '34036': ExchangeError, # Parameter is incorrect, please refer to API documentation
  395. '34037': ExchangeError, # Get the sub-account balance interface, account type is not supported
  396. '34038': ExchangeError, # Since your C2C transaction is unusual, you are restricted from fund transfer. Please contact our customer support to cancel the restriction
  397. '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
  398. # swap
  399. '35001': ExchangeError, # {"code": 35001, "message": "Contract does not exist"}
  400. '35002': ExchangeError, # {"code": 35002, "message": "Contract settling"}
  401. '35003': ExchangeError, # {"code": 35003, "message": "Contract paused"}
  402. '35004': ExchangeError, # {"code": 35004, "message": "Contract pending settlement"}
  403. '35005': AuthenticationError, # {"code": 35005, "message": "User does not exist"}
  404. '35008': InvalidOrder, # {"code": 35008, "message": "Risk ratio too high"}
  405. '35010': InvalidOrder, # {"code": 35010, "message": "Position closing too large"}
  406. '35012': InvalidOrder, # {"code": 35012, "message": "Incorrect order size"}
  407. '35014': InvalidOrder, # {"code": 35014, "message": "Order price is not within limit"}
  408. '35015': InvalidOrder, # {"code": 35015, "message": "Invalid leverage level"}
  409. '35017': ExchangeError, # {"code": 35017, "message": "Open orders exist"}
  410. '35019': InvalidOrder, # {"code": 35019, "message": "Order size too large"}
  411. '35020': InvalidOrder, # {"code": 35020, "message": "Order price too high"}
  412. '35021': InvalidOrder, # {"code": 35021, "message": "Order size exceeded current tier limit"}
  413. '35022': ExchangeError, # {"code": 35022, "message": "Contract status error"}
  414. '35024': ExchangeError, # {"code": 35024, "message": "Contract not initialized"}
  415. '35025': InsufficientFunds, # {"code": 35025, "message": "No account balance"}
  416. '35026': ExchangeError, # {"code": 35026, "message": "Contract settings not initialized"}
  417. '35029': OrderNotFound, # {"code": 35029, "message": "Order does not exist"}
  418. '35030': InvalidOrder, # {"code": 35030, "message": "Order size too large"}
  419. '35031': InvalidOrder, # {"code": 35031, "message": "Cancel order size too large"}
  420. '35032': ExchangeError, # {"code": 35032, "message": "Invalid user status"}
  421. '35037': ExchangeError, # No last traded price in cache
  422. '35039': ExchangeError, # {"code": 35039, "message": "Open order quantity exceeds limit"}
  423. '35040': InvalidOrder, # {"error_message":"Invalid order type","result":"true","error_code":"35040","order_id":"-1"}
  424. '35044': ExchangeError, # {"code": 35044, "message": "Invalid order status"}
  425. '35046': InsufficientFunds, # {"code": 35046, "message": "Negative account balance"}
  426. '35047': InsufficientFunds, # {"code": 35047, "message": "Insufficient account balance"}
  427. '35048': ExchangeError, # {"code": 35048, "message": "User contract is frozen and liquidating"}
  428. '35049': InvalidOrder, # {"code": 35049, "message": "Invalid order type"}
  429. '35050': InvalidOrder, # {"code": 35050, "message": "Position settings are blank"}
  430. '35052': InsufficientFunds, # {"code": 35052, "message": "Insufficient cross margin"}
  431. '35053': ExchangeError, # {"code": 35053, "message": "Account risk too high"}
  432. '35055': InsufficientFunds, # {"code": 35055, "message": "Insufficient account balance"}
  433. '35057': ExchangeError, # {"code": 35057, "message": "No last traded price"}
  434. '35058': ExchangeError, # {"code": 35058, "message": "No limit"}
  435. '35059': BadRequest, # {"code": 35059, "message": "client_oid or order_id is required"}
  436. '35060': BadRequest, # {"code": 35060, "message": "Only fill in either parameter client_oid or order_id"}
  437. '35061': BadRequest, # {"code": 35061, "message": "Invalid instrument_id"}
  438. '35062': InvalidOrder, # {"code": 35062, "message": "Invalid match_price"}
  439. '35063': InvalidOrder, # {"code": 35063, "message": "Invalid order_size"}
  440. '35064': InvalidOrder, # {"code": 35064, "message": "Invalid client_oid"}
  441. '35066': InvalidOrder, # Order interval error
  442. '35067': InvalidOrder, # Time-weighted order ratio error
  443. '35068': InvalidOrder, # Time-weighted order range error
  444. '35069': InvalidOrder, # Time-weighted single transaction limit error
  445. '35070': InvalidOrder, # Algo order type error
  446. '35071': InvalidOrder, # Order total must be larger than single order limit
  447. '35072': InvalidOrder, # Maximum 6 unfulfilled time-weighted orders can be held at the same time
  448. '35073': InvalidOrder, # Order price is 0. Market-close-all not available
  449. '35074': InvalidOrder, # Iceberg order single transaction average error
  450. '35075': InvalidOrder, # Failed to cancel order
  451. '35076': InvalidOrder, # LTC 20x leverage. Not allowed to open position
  452. '35077': InvalidOrder, # Maximum 6 unfulfilled iceberg orders can be held at the same time
  453. '35078': InvalidOrder, # Order amount exceeded 100,000
  454. '35079': InvalidOrder, # Iceberg order price variance error
  455. '35080': InvalidOrder, # Callback rate error
  456. '35081': InvalidOrder, # Maximum 10 unfulfilled trail orders can be held at the same time
  457. '35082': InvalidOrder, # Trail order callback rate error
  458. '35083': InvalidOrder, # Each user can only hold a maximum of 10 unfulfilled stop-limit orders at the same time
  459. '35084': InvalidOrder, # Order amount exceeded 1 million
  460. '35085': InvalidOrder, # Order amount is not in the correct range
  461. '35086': InvalidOrder, # Price exceeds 100 thousand
  462. '35087': InvalidOrder, # Price exceeds 100 thousand
  463. '35088': InvalidOrder, # Average amount error
  464. '35089': InvalidOrder, # Price exceeds 100 thousand
  465. '35090': ExchangeError, # No stop-limit orders available for cancelation
  466. '35091': ExchangeError, # No trail orders available for cancellation
  467. '35092': ExchangeError, # No iceberg orders available for cancellation
  468. '35093': ExchangeError, # No trail orders available for cancellation
  469. '35094': ExchangeError, # Stop-limit order last traded price error
  470. '35095': BadRequest, # Instrument_id error
  471. '35096': ExchangeError, # Algo order status error
  472. '35097': ExchangeError, # Order status and order ID cannot exist at the same time
  473. '35098': ExchangeError, # An order status or order ID must exist
  474. '35099': ExchangeError, # Algo order ID error
  475. # option
  476. '36001': BadRequest, # Invalid underlying index.
  477. '36002': BadRequest, # Instrument does not exist.
  478. '36005': ExchangeError, # Instrument status is invalid.
  479. '36101': AuthenticationError, # Account does not exist.
  480. '36102': PermissionDenied, # Account status is invalid.
  481. '36103': AccountSuspended, # Account is suspended due to ongoing liquidation.
  482. '36104': PermissionDenied, # Account is not enabled for options trading.
  483. '36105': PermissionDenied, # Please enable the account for option contract.
  484. '36106': AccountSuspended, # Funds cannot be transferred in or out, as account is suspended.
  485. '36107': PermissionDenied, # Funds cannot be transferred out within 30 minutes after option exercising or settlement.
  486. '36108': InsufficientFunds, # Funds cannot be transferred in or out, as equity of the account is less than zero.
  487. '36109': PermissionDenied, # Funds cannot be transferred in or out during option exercising or settlement.
  488. '36201': PermissionDenied, # New order function is blocked.
  489. '36202': PermissionDenied, # Account does not have permission to short option.
  490. '36203': InvalidOrder, # Invalid format for client_oid.
  491. '36204': ExchangeError, # Invalid format for request_id.
  492. '36205': BadRequest, # Instrument id does not match underlying index.
  493. '36206': BadRequest, # Order_id and client_oid can not be used at the same time.
  494. '36207': InvalidOrder, # Either order price or fartouch price must be present.
  495. '36208': InvalidOrder, # Either order price or size must be present.
  496. '36209': InvalidOrder, # Either order_id or client_oid must be present.
  497. '36210': InvalidOrder, # Either order_ids or client_oids must be present.
  498. '36211': InvalidOrder, # Exceeding max batch size for order submission.
  499. '36212': InvalidOrder, # Exceeding max batch size for oder cancellation.
  500. '36213': InvalidOrder, # Exceeding max batch size for order amendment.
  501. '36214': ExchangeError, # Instrument does not have valid bid/ask quote.
  502. '36216': OrderNotFound, # Order does not exist.
  503. '36217': InvalidOrder, # Order submission failed.
  504. '36218': InvalidOrder, # Order cancellation failed.
  505. '36219': InvalidOrder, # Order amendment failed.
  506. '36220': InvalidOrder, # Order is pending cancel.
  507. '36221': InvalidOrder, # Order qty is not valid multiple of lot size.
  508. '36222': InvalidOrder, # Order price is breaching highest buy limit.
  509. '36223': InvalidOrder, # Order price is breaching lowest sell limit.
  510. '36224': InvalidOrder, # Exceeding max order size.
  511. '36225': InvalidOrder, # Exceeding max open order count for instrument.
  512. '36226': InvalidOrder, # Exceeding max open order count for underlying.
  513. '36227': InvalidOrder, # Exceeding max open size across all orders for underlying
  514. '36228': InvalidOrder, # Exceeding max available qty for instrument.
  515. '36229': InvalidOrder, # Exceeding max available qty for underlying.
  516. '36230': InvalidOrder, # Exceeding max position limit for underlying.
  517. # --------------------------------------------------------
  518. # swap
  519. '400': BadRequest, # Bad Request
  520. '401': AuthenticationError, # Unauthorized access
  521. '403': PermissionDenied, # Access prohibited
  522. '404': BadRequest, # Request address does not exist
  523. '405': BadRequest, # The HTTP Method is not supported
  524. '415': BadRequest, # The current media type is not supported
  525. '429': DDoSProtection, # Too many requests
  526. '500': ExchangeNotAvailable, # System busy
  527. '1001': RateLimitExceeded, # The request is too frequent and has been throttled
  528. '1002': ExchangeError, # {0} verifications within 24 hours
  529. '1003': ExchangeError, # You failed more than {0} times today, the current operation is locked, please try again in 24 hours
  530. # '00000': ExchangeError, # success
  531. '40001': AuthenticationError, # ACCESS_KEY cannot be empty
  532. '40002': AuthenticationError, # SECRET_KEY cannot be empty
  533. '40003': AuthenticationError, # Signature cannot be empty
  534. '40004': InvalidNonce, # Request timestamp expired
  535. '40005': InvalidNonce, # Invalid ACCESS_TIMESTAMP
  536. '40006': AuthenticationError, # Invalid ACCESS_KEY
  537. '40007': BadRequest, # Invalid Content_Type
  538. '40008': InvalidNonce, # Request timestamp expired
  539. '40009': AuthenticationError, # sign signature error
  540. '40010': AuthenticationError, # sign signature error
  541. '40011': AuthenticationError, # ACCESS_PASSPHRASE cannot be empty
  542. '40012': AuthenticationError, # apikey/password is incorrect
  543. '40013': ExchangeError, # User status is abnormal
  544. '40014': PermissionDenied, # Incorrect permissions
  545. '40015': ExchangeError, # System is abnormal, please try again later
  546. '40016': PermissionDenied, # The user must bind the phone or Google
  547. '40017': ExchangeError, # Parameter verification failed
  548. '40018': PermissionDenied, # Invalid IP
  549. '40102': BadRequest, # Contract configuration does not exist, please check the parameters
  550. '40103': BadRequest, # Request method cannot be empty
  551. '40104': ExchangeError, # Lever adjustment failure
  552. '40105': ExchangeError, # Abnormal access to current price limit data
  553. '40106': ExchangeError, # Abnormal get next settlement time
  554. '40107': ExchangeError, # Abnormal access to index price data
  555. '40108': InvalidOrder, # Wrong order quantity
  556. '40109': OrderNotFound, # The data of the order cannot be found, please confirm the order number
  557. '40200': OnMaintenance, # Server upgrade, please try again later
  558. '40201': InvalidOrder, # Order number cannot be empty
  559. '40202': ExchangeError, # User information cannot be empty
  560. '40203': BadRequest, # The amount of adjustment margin cannot be empty or negative
  561. '40204': BadRequest, # Adjustment margin type cannot be empty
  562. '40205': BadRequest, # Adjusted margin type data is wrong
  563. '40206': BadRequest, # The direction of the adjustment margin cannot be empty
  564. '40207': BadRequest, # The adjustment margin data is wrong
  565. '40208': BadRequest, # The accuracy of the adjustment margin amount is incorrect
  566. '40209': BadRequest, # The current page number is wrong, please confirm
  567. '40300': ExchangeError, # User does not exist
  568. '40301': PermissionDenied, # Permission has not been obtained yet. If you need to use it, please contact customer service
  569. '40302': BadRequest, # Parameter abnormality
  570. '40303': BadRequest, # Can only query up to 20,000 data
  571. '40304': BadRequest, # Parameter type is abnormal
  572. '40305': BadRequest, # Client_oid length is not greater than 50, and cannot be Martian characters
  573. '40306': ExchangeError, # Batch processing orders can only process up to 20
  574. '40308': OnMaintenance, # The contract is being temporarily maintained
  575. '40309': BadSymbol, # The contract has been removed
  576. '40400': ExchangeError, # Status check abnormal
  577. '40401': ExchangeError, # The operation cannot be performed
  578. '40402': BadRequest, # The opening direction cannot be empty
  579. '40403': BadRequest, # Wrong opening direction format
  580. '40404': BadRequest, # Whether to enable automatic margin call parameters cannot be empty
  581. '40405': BadRequest, # Whether to enable the automatic margin call parameter type is wrong
  582. '40406': BadRequest, # Whether to enable automatic margin call parameters is of unknown type
  583. '40407': ExchangeError, # The query direction is not the direction entrusted by the plan
  584. '40408': ExchangeError, # Wrong time range
  585. '40409': ExchangeError, # Time format error
  586. '40500': InvalidOrder, # Client_oid check error
  587. '40501': ExchangeError, # Channel name error
  588. '40502': ExchangeError, # If it is a copy user, you must pass the copy to whom
  589. '40503': ExchangeError, # With the single type
  590. '40504': ExchangeError, # Platform code must pass
  591. '40505': ExchangeError, # Not the same as single type
  592. '40506': AuthenticationError, # Platform signature error
  593. '40507': AuthenticationError, # Api signature error
  594. '40508': ExchangeError, # KOL is not authorized
  595. '40509': ExchangeError, # Abnormal copy end
  596. '40600': ExchangeError, # Copy function suspended
  597. '40601': ExchangeError, # Followers cannot be KOL
  598. '40602': ExchangeError, # The number of copies has reached the limit and cannot process the request
  599. '40603': ExchangeError, # Abnormal copy end
  600. '40604': ExchangeNotAvailable, # Server is busy, please try again later
  601. '40605': ExchangeError, # Copy type, the copy number must be passed
  602. '40606': ExchangeError, # The type of document number is wrong
  603. '40607': ExchangeError, # Document number must be passed
  604. '40608': ExchangeError, # No documented products currently supported
  605. '40609': ExchangeError, # The contract product does not support copying
  606. '40700': BadRequest, # Cursor parameters are incorrect
  607. '40701': ExchangeError, # KOL is not authorized
  608. '40702': ExchangeError, # Unauthorized copying user
  609. '40703': ExchangeError, # Bill inquiry start and end time cannot be empty
  610. '40704': ExchangeError, # Can only check the data of the last three months
  611. '40705': BadRequest, # The start and end time cannot exceed 90 days
  612. '40706': InvalidOrder, # Wrong order price
  613. '40707': BadRequest, # Start time is greater than end time
  614. '40708': BadRequest, # Parameter verification is abnormal
  615. '40709': ExchangeError, # There is no position in self position, and no automatic margin call can be set
  616. '40710': ExchangeError, # Abnormal account status
  617. '40711': InsufficientFunds, # Insufficient contract account balance
  618. '40712': InsufficientFunds, # Insufficient margin
  619. '40713': ExchangeError, # Cannot exceed the maximum transferable margin amount
  620. '40714': ExchangeError, # No direct margin call is allowed
  621. # spot
  622. 'invalid sign': AuthenticationError,
  623. 'invalid currency': BadSymbol, # invalid trading pair
  624. 'invalid symbol': BadSymbol,
  625. 'invalid period': BadRequest, # invalid Kline type
  626. 'invalid user': ExchangeError,
  627. 'invalid amount': InvalidOrder,
  628. 'invalid type': InvalidOrder, # {"status":"error","ts":1595700344504,"err_code":"invalid-parameter","err_msg":"invalid type"}
  629. 'invalid orderId': InvalidOrder,
  630. 'invalid record': ExchangeError,
  631. 'invalid accountId': BadRequest,
  632. 'invalid address': BadRequest,
  633. 'accesskey not None': AuthenticationError, # {"status":"error","ts":1595704360508,"err_code":"invalid-parameter","err_msg":"accesskey not null"}
  634. 'illegal accesskey': AuthenticationError,
  635. 'sign not null': AuthenticationError,
  636. 'req_time is too much difference from server time': InvalidNonce,
  637. 'permissions not right': PermissionDenied, # {"status":"error","ts":1595704490084,"err_code":"invalid-parameter","err_msg":"permissions not right"}
  638. 'illegal sign invalid': AuthenticationError, # {"status":"error","ts":1595684716042,"err_code":"invalid-parameter","err_msg":"illegal sign invalid"}
  639. 'user locked': AccountSuspended,
  640. 'Request Frequency Is Too High': RateLimitExceeded,
  641. 'more than a daily rate of cash': BadRequest,
  642. 'more than the maximum daily withdrawal amount': BadRequest,
  643. 'need to bind email or mobile': ExchangeError,
  644. 'user forbid': PermissionDenied,
  645. 'User Prohibited Cash Withdrawal': PermissionDenied,
  646. 'Cash Withdrawal Is Less Than The Minimum Value': BadRequest,
  647. 'Cash Withdrawal Is More Than The Maximum Value': BadRequest,
  648. 'the account with in 24 hours ban coin': PermissionDenied,
  649. 'order cancel fail': BadRequest, # {"status":"error","ts":1595703343035,"err_code":"bad-request","err_msg":"order cancel fail"}
  650. 'base symbol error': BadSymbol,
  651. 'base date error': ExchangeError,
  652. 'api signature not valid': AuthenticationError,
  653. 'gateway internal error': ExchangeError,
  654. 'audit failed': ExchangeError,
  655. 'order queryorder invalid': BadRequest,
  656. 'market no need price': InvalidOrder,
  657. 'limit need price': InvalidOrder,
  658. 'userid not equal to account_id': ExchangeError,
  659. 'your balance is low': InsufficientFunds, # {"status":"error","ts":1595594160149,"err_code":"invalid-parameter","err_msg":"invalid size, valid range: [1,2000]"}
  660. 'address invalid cointype': ExchangeError,
  661. 'system exception': ExchangeError, # {"status":"error","ts":1595711862763,"err_code":"system exception","err_msg":"system exception"}
  662. '50003': ExchangeError, # No record
  663. '50004': BadSymbol, # The transaction pair is currently not supported or has been suspended
  664. '50006': PermissionDenied, # The account is forbidden to withdraw. If you have any questions, please contact customer service.
  665. '50007': PermissionDenied, # The account is forbidden to withdraw within 24 hours. If you have any questions, please contact customer service.
  666. '50008': RequestTimeout, # network timeout
  667. '50009': RateLimitExceeded, # The operation is too frequent, please try again later
  668. '50010': ExchangeError, # The account is abnormally frozen. If you have any questions, please contact customer service.
  669. '50014': InvalidOrder, # The transaction amount under minimum limits
  670. '50015': InvalidOrder, # The transaction amount exceed maximum limits
  671. '50016': InvalidOrder, # The price can't be higher than the current price
  672. '50017': InvalidOrder, # Price under minimum limits
  673. '50018': InvalidOrder, # The price exceed maximum limits
  674. '50019': InvalidOrder, # The amount under minimum limits
  675. '50020': InsufficientFunds, # Insufficient balance
  676. '50021': InvalidOrder, # Price is under minimum limits
  677. '50026': InvalidOrder, # Market price parameter error
  678. 'invalid order query time': ExchangeError, # start time is greater than end time; or the time interval between start time and end time is greater than 48 hours
  679. 'invalid start time': BadRequest, # start time is a date 30 days ago; or start time is a date in the future
  680. 'invalid end time': BadRequest, # end time is a date 30 days ago; or end time is a date in the future
  681. '20003': ExchangeError, # operation failed, {"status":"error","ts":1595730308979,"err_code":"bad-request","err_msg":"20003"}
  682. '01001': ExchangeError, # order failed, {"status":"fail","err_code":"01001","err_msg":"系统异常,请稍后重试"}
  683. },
  684. 'broad': {
  685. 'invalid size, valid range': ExchangeError,
  686. },
  687. },
  688. 'precisionMode': TICK_SIZE,
  689. 'options': {
  690. 'createMarketBuyOrderRequiresPrice': True,
  691. 'fetchMarkets': [
  692. 'spot',
  693. 'swap',
  694. ],
  695. 'parseOHLCV': {
  696. 'volume': {
  697. 'spot': 'amount',
  698. 'swap': 5,
  699. },
  700. },
  701. 'defaultType': 'spot', # 'spot', 'swap'
  702. 'accountId': None, # '1012838157',
  703. 'timeframes': {
  704. 'spot': {
  705. '1m': '1min',
  706. '5m': '5min',
  707. '15m': '15min',
  708. '30m': '30min',
  709. '1h': '60min',
  710. '2h': '120min',
  711. '4h': '240min',
  712. '6h': '360min',
  713. '12h': '720min',
  714. '1d': '1day',
  715. '1w': '1week',
  716. },
  717. 'swap': {
  718. '1m': '60',
  719. '5m': '300',
  720. '15m': '900',
  721. '30m': '1800',
  722. '1h': '3600',
  723. '2h': '7200',
  724. '4h': '14400',
  725. '6h': '21600',
  726. '12h': '43200',
  727. '1d': '86400',
  728. '1w': '604800',
  729. },
  730. },
  731. },
  732. })
  733. async def fetch_time(self, params={}):
  734. response = await self.dataGetCommonTimestamp(params)
  735. #
  736. # {
  737. # "status":"ok",
  738. # "data":"1595525139400"
  739. # }
  740. #
  741. return self.safe_integer(response, 'data')
  742. async def fetch_markets(self, params={}):
  743. types = self.safe_value(self.options, 'fetchMarkets')
  744. if not len(types):
  745. types = [
  746. self.options['defaultType'],
  747. ]
  748. result = []
  749. for i in range(0, len(types)):
  750. markets = await self.fetch_markets_by_type(types[i], params)
  751. result = self.array_concat(result, markets)
  752. return result
  753. def parse_markets(self, markets):
  754. result = []
  755. for i in range(0, len(markets)):
  756. result.append(self.parse_market(markets[i]))
  757. return result
  758. def parse_market(self, market):
  759. #
  760. # spot
  761. #
  762. # {
  763. # "base_currency":"btc",
  764. # "quote_currency":"usdt",
  765. # "symbol":"btc_usdt",
  766. # "tick_size":"2",
  767. # "size_increment":"4",
  768. # "status":"1",
  769. # "base_asset_precision":"8"
  770. # }
  771. #
  772. #
  773. # swap
  774. #
  775. # {
  776. # "symbol":"btcusd",
  777. # "underlying_index":"BTC",
  778. # "quote_currency":"USD",
  779. # "coin":"BTC",
  780. # "contract_val":"1",
  781. # "listing":null,
  782. # "delivery":["07:00:00","15:00:00","23:00:00"],
  783. # "size_increment":"0",
  784. # "tick_size":"1",
  785. # "forwardContractFlag":false,
  786. # "priceEndStep":5
  787. # }
  788. #
  789. id = self.safe_string(market, 'symbol')
  790. marketType = 'spot'
  791. spot = True
  792. swap = False
  793. baseId = self.safe_string_2(market, 'base_currency', 'coin')
  794. quoteId = self.safe_string(market, 'quote_currency')
  795. contractVal = self.safe_float(market, 'contract_val')
  796. if contractVal is not None:
  797. marketType = 'swap'
  798. spot = False
  799. swap = True
  800. base = self.safe_currency_code(baseId)
  801. quote = self.safe_currency_code(quoteId)
  802. symbol = id.upper()
  803. if spot:
  804. symbol = base + '/' + quote
  805. lotSize = self.safe_float_2(market, 'lot_size', 'trade_increment')
  806. tick_size = self.safe_float(market, 'tick_size')
  807. newtick_size = float('1e-' + self.number_to_string(tick_size))
  808. precision = {
  809. 'amount': self.safe_float(market, 'size_increment', lotSize),
  810. 'price': newtick_size,
  811. }
  812. minAmount = self.safe_float_2(market, 'min_size', 'base_min_size')
  813. status = self.safe_string(market, 'status')
  814. active = None
  815. if status is not None:
  816. active = (status == '1')
  817. fees = self.safe_value_2(self.fees, marketType, 'trading', {})
  818. return self.extend(fees, {
  819. 'id': id,
  820. 'symbol': symbol,
  821. 'base': base,
  822. 'quote': quote,
  823. 'baseId': baseId,
  824. 'quoteId': quoteId,
  825. 'info': market,
  826. 'type': marketType,
  827. 'spot': spot,
  828. 'swap': swap,
  829. 'active': active,
  830. 'precision': precision,
  831. 'limits': {
  832. 'amount': {
  833. 'min': minAmount,
  834. 'max': None,
  835. },
  836. 'price': {
  837. 'min': precision['price'],
  838. 'max': None,
  839. },
  840. 'cost': {
  841. 'min': precision['price'],
  842. 'max': None,
  843. },
  844. },
  845. })
  846. def amount_to_precision(self, symbol, amount):
  847. return self.decimal_to_precision(amount, TRUNCATE, self.markets[symbol]['precision']['amount'], DECIMAL_PLACES)
  848. async def fetch_markets_by_type(self, type, params={}):
  849. if type == 'spot':
  850. response = await self.dataGetCommonSymbols(params)
  851. #
  852. # {
  853. # "status":"ok",
  854. # "ts":1595526622408,
  855. # "data":[
  856. # {
  857. # "base_currency":"btc",
  858. # "quote_currency":"usdt",
  859. # "symbol":"btc_usdt",
  860. # "tick_size":"2",
  861. # "size_increment":"4",
  862. # "status":"1",
  863. # "base_asset_precision":"8"
  864. # },
  865. # ]
  866. # }
  867. #
  868. data = self.safe_value(response, 'data', [])
  869. return self.parse_markets(data)
  870. elif type == 'swap':
  871. response = await self.capiGetMarketContracts(params)
  872. #
  873. # {
  874. # "data":{
  875. # "contractApis":[
  876. # {
  877. # "instrument_id":"btcusd",
  878. # "underlying_index":"BTC",
  879. # "quote_currency":"USD",
  880. # "coin":"BTC",
  881. # "contract_val":"1",
  882. # "delivery":["07:00:00","15:00:00","23:00:00"],
  883. # "size_increment":"0",
  884. # "tick_size":"1",
  885. # "forwardContractFlag":false,
  886. # "priceEndStep":"5"
  887. # },
  888. # ]
  889. # },
  890. # "status":"ok",
  891. # "err_code":"00000"
  892. # }
  893. #
  894. return self.parse_markets(response)
  895. else:
  896. raise NotSupported(self.id + ' fetchMarketsByType does not support market type ' + type)
  897. async def fetch_currencies(self, params={}):
  898. response = await self.dataGetCommonCurrencys(params)
  899. #
  900. # {
  901. # "status":"ok",
  902. # "ts":1595537740466,
  903. # "data":[
  904. # "btc",
  905. # "bft",
  906. # "usdt",
  907. # "usdt-omni",
  908. # "usdt-erc20"
  909. # ]
  910. # }
  911. #
  912. result = {}
  913. data = self.safe_value(response, 'data', [])
  914. for i in range(0, len(data)):
  915. id = data[i]
  916. code = self.safe_currency_code(id)
  917. result[code] = {
  918. 'id': id,
  919. 'code': code,
  920. 'info': id,
  921. 'type': None,
  922. 'name': None,
  923. 'active': None,
  924. 'fee': None,
  925. 'precision': None,
  926. 'limits': {
  927. 'amount': {'min': None, 'max': None},
  928. 'price': {'min': None, 'max': None},
  929. 'cost': {'min': None, 'max': None},
  930. 'withdraw': {'min': None, 'max': None},
  931. },
  932. }
  933. return result
  934. async def fetch_order_book(self, symbol, limit=None, params={}):
  935. await self.load_markets()
  936. market = self.market(symbol)
  937. request = {
  938. 'symbol': market['id'],
  939. }
  940. method = None
  941. if market['spot']:
  942. method = 'dataGetMarketDepth'
  943. request['type'] = 'step0' # step0, step1, step2, step3, step4, step5, do not merge depth if step0
  944. elif market['swap']:
  945. method = 'capiGetMarketDepth'
  946. request['limit'] = 100 if (limit is None) else limit # max 100
  947. response = await getattr(self, method)(self.extend(request, params))
  948. #
  949. # spot
  950. #
  951. # {
  952. # "status":"ok",
  953. # "ch":"market.btc_usdt.depth.step0",
  954. # "ts":1595607628197,
  955. # "data":{
  956. # "id":"1595607628197",
  957. # "ts":"1595607628197",
  958. # "bids":[
  959. # ["9534.99","15.36160000000000000000"],
  960. # ["9534.85","0.14580000000000000000"],
  961. # ["9534.73","0.02100000000000000000"],
  962. # ],
  963. # "asks":[
  964. # ["9535.02","7.37160000000000000000"],
  965. # ["9535.03","0.09040000000000000000"],
  966. # ["9535.05","0.02180000000000000000"],
  967. # ]
  968. # }
  969. # }
  970. #
  971. # swap
  972. #
  973. # {
  974. # "asks":[
  975. # ["9579.0","119865",1],
  976. # ["9579.5","90069",1],
  977. # ["9580.0","256673",1],
  978. # ],
  979. # "bids":[
  980. # ["9578.5","2417",1],
  981. # ["9577.5","3024",1],
  982. # ["9577.0","21548",1],
  983. # ],
  984. # "timestamp":"1595664767349"
  985. # }
  986. #
  987. data = self.safe_value(response, 'data', response)
  988. timestamp = self.safe_integer_2(data, 'timestamp', 'ts')
  989. nonce = self.safe_integer(data, 'id')
  990. orderbook = self.parse_order_book(data, timestamp)
  991. orderbook['nonce'] = nonce
  992. return orderbook
  993. def parse_ticker(self, ticker, market=None):
  994. #
  995. # spot
  996. #
  997. # fetchTicker
  998. #
  999. # {
  1000. # "id":"1595538241113",
  1001. # "bid":["0.028474000000","1.139400000000"],
  1002. # "ask":["0.028482000000","0.353100000000"],
  1003. # "amount":"2850.6649",
  1004. # "count":"818",
  1005. # "open":"0.02821",
  1006. # "close":"0.028474",
  1007. # "low":"0.02821",
  1008. # "high":"0.029091",
  1009. # "vol":"79.4548693404"
  1010. # }
  1011. #
  1012. # fetchTickers
  1013. #
  1014. # {
  1015. # "amount":"30086.8095",
  1016. # "count":"22450",
  1017. # "open":"9525.11",
  1018. # "close":"9591.81",
  1019. # "low":"9510.68",
  1020. # "high":"9659.7",
  1021. # "vol":"286239092.250461",
  1022. # "symbol":"btc_usdt"
  1023. # }
  1024. #
  1025. # swap
  1026. #
  1027. # {
  1028. # "instrument_id":"btcusd",
  1029. # "last":"9574.5",
  1030. # "best_ask":"9575.0",
  1031. # "best_bid":"9574.0",
  1032. # "high_24h":"9672",
  1033. # "low_24h":"9512",
  1034. # "volume_24h":"567697050",
  1035. # "timestamp":"1595538450096"
  1036. # }
  1037. #
  1038. timestamp = self.safe_integer_2(ticker, 'timestamp', 'id')
  1039. symbol = None
  1040. marketId = self.safe_string_2(ticker, 'instrument_id', 'symbol')
  1041. if marketId in self.markets_by_id:
  1042. market = self.markets_by_id[marketId]
  1043. symbol = market['symbol']
  1044. elif marketId is not None:
  1045. parts = marketId.split('_')
  1046. numParts = len(parts)
  1047. if numParts == 2:
  1048. baseId, quoteId = parts
  1049. base = self.safe_currency_code(baseId)
  1050. quote = self.safe_currency_code(quoteId)
  1051. symbol = base + '/' + quote
  1052. else:
  1053. symbol = marketId
  1054. if (symbol is None) and (market is not None):
  1055. symbol = market['symbol']
  1056. last = self.safe_float_2(ticker, 'last', 'close')
  1057. open = self.safe_float(ticker, 'open')
  1058. bidVolume = None
  1059. askVolume = None
  1060. bid = self.safe_value(ticker, 'bid')
  1061. if bid is None:
  1062. bid = self.safe_float(ticker, 'best_bid')
  1063. else:
  1064. bidVolume = self.safe_float(bid, 1)
  1065. bid = self.safe_float(bid, 0)
  1066. ask = self.safe_value(ticker, 'ask')
  1067. if ask is None:
  1068. ask = self.safe_float(ticker, 'best_ask')
  1069. else:
  1070. askVolume = self.safe_float(ask, 1)
  1071. ask = self.safe_float(ask, 0)
  1072. baseVolume = self.safe_float_2(ticker, 'amount', 'volume_24h')
  1073. quoteVolume = self.safe_float(ticker, 'vol')
  1074. vwap = self.vwap(baseVolume, quoteVolume)
  1075. change = None
  1076. percentage = None
  1077. average = None
  1078. if (last is not None) and (open is not None):
  1079. change = last - open
  1080. percentage = change / open * 100
  1081. average = self.sum(open, last) / 2
  1082. return {
  1083. 'symbol': symbol,
  1084. 'timestamp': timestamp,
  1085. 'datetime': self.iso8601(timestamp),
  1086. 'high': self.safe_float_2(ticker, 'high', 'high_24h'),
  1087. 'low': self.safe_float_2(ticker, 'low', 'low_24h'),
  1088. 'bid': bid,
  1089. 'bidVolume': bidVolume,
  1090. 'ask': ask,
  1091. 'askVolume': askVolume,
  1092. 'vwap': vwap,
  1093. 'open': open,
  1094. 'close': last,
  1095. 'last': last,
  1096. 'previousClose': None,
  1097. 'change': change,
  1098. 'percentage': percentage,
  1099. 'average': average,
  1100. 'baseVolume': baseVolume,
  1101. 'quoteVolume': quoteVolume,
  1102. 'info': ticker,
  1103. }
  1104. async def fetch_ticker(self, symbol, params={}):
  1105. await self.load_markets()
  1106. market = self.market(symbol)
  1107. method = None
  1108. if market['spot']:
  1109. method = 'dataGetMarketDetailMerged'
  1110. elif market['swap']:
  1111. method = 'capiGetMarketTicker'
  1112. request = {
  1113. 'symbol': market['id'],
  1114. }
  1115. response = await getattr(self, method)(self.extend(request, params))
  1116. #
  1117. # spot
  1118. #
  1119. # {
  1120. # "status":"ok",
  1121. # "ch":"market.eth_btc.detail.merged",
  1122. # "ts":1595538241474,
  1123. # "data":{
  1124. # "id":"1595538241113",
  1125. # "bid":["0.028474000000","1.139400000000"],
  1126. # "ask":["0.028482000000","0.353100000000"],
  1127. # "amount":"2850.6649",
  1128. # "count":"818",
  1129. # "open":"0.02821",
  1130. # "close":"0.028474",
  1131. # "low":"0.02821",
  1132. # "high":"0.029091",
  1133. # "vol":"79.4548693404"
  1134. # }
  1135. # }
  1136. #
  1137. # swap
  1138. #
  1139. # {
  1140. # "symbol":"btcusd",
  1141. # "last":"9575.5",
  1142. # "best_ask":"9576.0",
  1143. # "best_bid":"9575.0",
  1144. # "high_24h":"9646",
  1145. # "low_24h":"9516",
  1146. # "volume_24h":"516656839",
  1147. # "timestamp":"1595664217405"
  1148. # }
  1149. #
  1150. data = self.safe_value(response, 'data', response)
  1151. return self.parse_ticker(data, market)
  1152. async def fetch_tickers_by_type(self, type, symbols=None, params={}):
  1153. await self.load_markets()
  1154. method = None
  1155. if type == 'spot':
  1156. method = 'dataGetMarketTickers'
  1157. elif type == 'swap':
  1158. method = 'capiGetMarketTickers'
  1159. response = await getattr(self, method)(params)
  1160. #
  1161. # spot
  1162. #
  1163. # {
  1164. # "status":"ok",
  1165. # "ts":1595542893250,
  1166. # "data":[
  1167. # {
  1168. # "amount":"30086.8095",
  1169. # "count":"22450",
  1170. # "open":"9525.11",
  1171. # "close":"9591.81",
  1172. # "low":"9510.68",
  1173. # "high":"9659.7",
  1174. # "vol":"286239092.250461",
  1175. # "symbol":"btc_usdt"
  1176. # }
  1177. # ]
  1178. # }
  1179. #
  1180. # swap
  1181. #
  1182. # [
  1183. # {
  1184. # "symbol":"btcusd",
  1185. # "last":"9572",
  1186. # "best_ask":"9571.5",
  1187. # "best_bid":"9570.5",
  1188. # "high_24h":"9646",
  1189. # "low_24h":"9516",
  1190. # "volume_24h":"515401635",
  1191. # "timestamp":"1595664479952"
  1192. # }
  1193. # ]
  1194. #
  1195. data = self.safe_value(response, 'data', response)
  1196. timestamp = None
  1197. if not isinstance(response, list):
  1198. timestamp = self.safe_integer(response, 'ts')
  1199. result = {}
  1200. for i in range(0, len(data)):
  1201. ticker = self.parse_ticker(self.extend({
  1202. 'timestamp': timestamp,
  1203. }, data[i]))
  1204. symbol = ticker['symbol']
  1205. result[symbol] = ticker
  1206. return self.filter_by_array(result, 'symbol', symbols)
  1207. async def fetch_tickers(self, symbols=None, params={}):
  1208. defaultType = self.safe_string_2(self.options, 'fetchTickers', 'defaultType')
  1209. type = self.safe_string(params, 'type', defaultType)
  1210. return await self.fetch_tickers_by_type(type, symbols, self.omit(params, 'type'))
  1211. def parse_trade(self, trade, market=None):
  1212. #
  1213. # fetchTrades(public)
  1214. #
  1215. # spot
  1216. #
  1217. # {
  1218. # "id":"1",
  1219. # "price":"9533.81",
  1220. # "amount":"0.7326",
  1221. # "direction":"sell",
  1222. # "ts":"1595604964000"
  1223. # }
  1224. #
  1225. # swap
  1226. #
  1227. # {
  1228. # "trade_id":"670581881367954915",
  1229. # "price":"9553.00",
  1230. # "size":"20",
  1231. # "side":"sell",
  1232. # "timestamp":"1595605100004",
  1233. # "symbol":"btcusd"
  1234. # }
  1235. #
  1236. # spot fetchMyTrades(private)
  1237. #
  1238. # {
  1239. # "id": 29555,
  1240. # "order_id": 59378,
  1241. # "match_id": 59335,
  1242. # "symbol": "eth_usdt",
  1243. # "type": "buy-limit",
  1244. # "source": "api",
  1245. # "price": "100.1000000000",
  1246. # "filled_amount": "0.9845000000",
  1247. # "filled_fees": "0.0019690000",
  1248. # "created_at": 1494901400487
  1249. # }
  1250. #
  1251. # fetchOrderTrades(private)
  1252. #
  1253. # spot
  1254. #
  1255. # {
  1256. # "id":"614164775",
  1257. # "created_at":"1596298860602",
  1258. # "filled_amount":"0.0417000000000000",
  1259. # "filled_fees":"0.0000834000000000",
  1260. # "match_id":"673491702661292033",
  1261. # "order_id":"673491720340279296",
  1262. # "price":"359.240000000000",
  1263. # "source":"接口",
  1264. # "symbol":"eth_usdt",
  1265. # "type":"buy-market"
  1266. # }
  1267. #
  1268. # swap
  1269. #
  1270. # {
  1271. # "trade_id":"6667390",
  1272. # "symbol":"cmt_btcusdt",
  1273. # "order_id":"525946425993854915",
  1274. # "price":"9839.00",
  1275. # "order_qty":"3466",
  1276. # "fee":"-0.0000528407360000",
  1277. # "timestamp":"1561121514442",
  1278. # "exec_type":"M",
  1279. # "side":"3"
  1280. # }
  1281. #
  1282. symbol = None
  1283. marketId = self.safe_string(trade, 'symbol')
  1284. base = None
  1285. quote = None
  1286. if marketId in self.markets_by_id:
  1287. market = self.markets_by_id[marketId]
  1288. symbol = market['symbol']
  1289. base = market['base']
  1290. quote = market['quote']
  1291. elif marketId is not None:
  1292. parts = marketId.split('_')
  1293. numParts = len(parts)
  1294. if numParts == 2:
  1295. baseId, quoteId = parts
  1296. base = self.safe_currency_code(baseId)
  1297. quote = self.safe_currency_code(quoteId)
  1298. symbol = base + '/' + quote
  1299. else:
  1300. symbol = marketId.upper()
  1301. if (symbol is None) and (market is not None):
  1302. symbol = market['symbol']
  1303. base = market['base']
  1304. quote = market['quote']
  1305. timestamp = self.safe_integer(trade, 'created_at')
  1306. timestamp = self.safe_integer_2(trade, 'timestamp', 'ts', timestamp)
  1307. price = self.safe_float(trade, 'price')
  1308. amount = self.safe_float_2(trade, 'filled_amount', 'order_qty')
  1309. amount = self.safe_float_2(trade, 'size', 'amount', amount)
  1310. takerOrMaker = self.safe_string_2(trade, 'exec_type', 'liquidity')
  1311. if takerOrMaker == 'M':
  1312. takerOrMaker = 'maker'
  1313. elif takerOrMaker == 'T':
  1314. takerOrMaker = 'taker'
  1315. side = self.safe_string_2(trade, 'side', 'direction')
  1316. type = self.parse_order_type(side)
  1317. side = self.parse_order_side(side)
  1318. # if side is None:
  1319. # orderType = self.safe_string(trade, 'type')
  1320. # if orderType is not None:
  1321. # parts = orderType.split('-')
  1322. # side = self.safe_string_lower(parts, 0)
  1323. # type = self.safe_string_lower(parts, 1)
  1324. # }
  1325. # }
  1326. cost = None
  1327. if amount is not None:
  1328. if price is not None:
  1329. cost = amount * price
  1330. feeCost = self.safe_float(trade, 'fee')
  1331. if feeCost is None:
  1332. feeCost = self.safe_float(trade, 'filled_fees')
  1333. else:
  1334. feeCost = -feeCost
  1335. fee = None
  1336. if feeCost is not None:
  1337. feeCurrency = base if (side == 'buy') else quote
  1338. fee = {
  1339. # fee is either a positive number(invitation rebate)
  1340. # or a negative number(transaction fee deduction)
  1341. # therefore we need to invert the fee
  1342. # more about it https://github.com/ccxt/ccxt/issues/5909
  1343. 'cost': feeCost,
  1344. 'currency': feeCurrency,
  1345. }
  1346. orderId = self.safe_string(trade, 'order_id')
  1347. id = self.safe_string_2(trade, 'trade_id', 'id')
  1348. return {
  1349. 'info': trade,
  1350. 'timestamp': timestamp,
  1351. 'datetime': self.iso8601(timestamp),
  1352. 'symbol': symbol,
  1353. 'id': id,
  1354. 'order': orderId,
  1355. 'type': type,
  1356. 'takerOrMaker': takerOrMaker,
  1357. 'side': side,
  1358. 'price': price,
  1359. 'amount': amount,
  1360. 'cost': cost,
  1361. 'fee': fee,
  1362. }
  1363. async def fetch_trades(self, symbol, limit=None, since=None, params={}):
  1364. await self.load_markets()
  1365. market = self.market(symbol)
  1366. request = {
  1367. 'symbol': market['id'],
  1368. }
  1369. method = None
  1370. if market['spot']:
  1371. method = 'dataGetMarketHistoryTrade'
  1372. elif market['swap']:
  1373. method = 'capiGetMarketTrades'
  1374. if market['spot']:
  1375. if limit is not None:
  1376. request['size'] = limit # default 1, max 2000
  1377. elif market['swap']:
  1378. if limit is None:
  1379. limit = 100 # default 20, max 100
  1380. request['limit'] = limit
  1381. response = await getattr(self, method)(self.extend(request, params))
  1382. #
  1383. # spot
  1384. #
  1385. # {
  1386. # "status":"ok",
  1387. # "ch":"market.btc_usdt.trade.detail",
  1388. # "ts":1595604968430,
  1389. # "data":{
  1390. # "ts":"1595604964000",
  1391. # "data":[
  1392. # {"id":"1","price":"9533.81","amount":"0.7326","direction":"sell","ts":"1595604964000"},
  1393. # {"id":"2","price":"9533.67","amount":"1.1591","direction":"buy","ts":"1595604961000"},
  1394. # {"id":"3","price":"9533.67","amount":"1.5022","direction":"sell","ts":"1595604959000"},
  1395. # ]
  1396. # }
  1397. # }
  1398. #
  1399. # swap
  1400. #
  1401. # [
  1402. # {"trade_id":"670833198971748613","price":"9578.50","size":"5412","side":"sell","timestamp":"1595665018790","symbol":"btcusd"},
  1403. # {"trade_id":"670833194240574915","price":"9579.00","size":"3972","side":"buy","timestamp":"1595665017662","symbol":"btcusd"},
  1404. # {"trade_id":"670833194240573915","price":"9579.00","size":"1227","side":"buy","timestamp":"1595665017662","symbol":"btcusd"},
  1405. # ]
  1406. #
  1407. trades = None
  1408. if isinstance(response, list):
  1409. trades = response
  1410. else:
  1411. data = self.safe_value(response, 'data', {})
  1412. trades = self.safe_value_2(data, 'data', [])
  1413. return self.parse_trades(trades, market, since, limit)
  1414. def parse_ohlcv(self, ohlcv, market=None, timeframe='1m'):
  1415. #
  1416. # spot
  1417. #
  1418. # {
  1419. # "id":"1594694700000",
  1420. # "amount":"283.6811",
  1421. # "count":"234",
  1422. # "open":"9230.00",
  1423. # "close":"9227.15",
  1424. # "low":"9206.66",
  1425. # "high":"9232.33",
  1426. # "vol":"2618015.032504000000"
  1427. # }
  1428. #
  1429. # swap
  1430. #
  1431. # [
  1432. # "1594693800000",
  1433. # "9240",
  1434. # "9241",
  1435. # "9222",
  1436. # "9228.5",
  1437. # "3913370",
  1438. # "424.003616350563"
  1439. # ]
  1440. #
  1441. options = self.safe_value(self.options, 'parseOHLCV', {})
  1442. volume = self.safe_value(options, 'volume', {})
  1443. if isinstance(ohlcv, list):
  1444. volumeIndex = self.safe_string(volume, market['type'], 'amount')
  1445. return [
  1446. self.safe_integer(ohlcv, 0), # timestamp
  1447. self.safe_float(ohlcv, 1), # Open
  1448. self.safe_float(ohlcv, 2), # High
  1449. self.safe_float(ohlcv, 3), # Low
  1450. self.safe_float(ohlcv, 4), # Close
  1451. # self.safe_float(ohlcv, 5), # Quote Volume
  1452. # self.safe_float(ohlcv, 6), # Base Volume
  1453. self.safe_float(ohlcv, volumeIndex), # Volume, bitget will return base volume in the 7th element for future markets
  1454. ]
  1455. else:
  1456. volumeIndex = self.safe_value(volume, market['type'], 6)
  1457. return [
  1458. self.safe_integer(ohlcv, 'id'),
  1459. self.safe_float(ohlcv, 'open'), # Open
  1460. self.safe_float(ohlcv, 'high'), # High
  1461. self.safe_float(ohlcv, 'low'), # Low
  1462. self.safe_float(ohlcv, 'close'), # Close
  1463. self.safe_float(ohlcv, volumeIndex), # Base Volume
  1464. ]
  1465. async def fetch_ohlcv(self, symbol, timeframe='1m', since=None, limit=None, params={}):
  1466. await self.load_markets()
  1467. market = self.market(symbol)
  1468. request = {
  1469. 'symbol': market['id'],
  1470. }
  1471. method = None
  1472. type = market['type']
  1473. options = self.safe_value(self.options, 'timeframes', {})
  1474. intervals = self.safe_value(options, type, {})
  1475. interval = self.safe_value(intervals, self.timeframes[timeframe])
  1476. if market['spot']:
  1477. method = 'dataGetMarketHistoryKline'
  1478. request['period'] = interval
  1479. if limit is not None:
  1480. request['size'] = limit # default 150, max 1000
  1481. elif market['swap']:
  1482. duration = self.parse_timeframe(timeframe)
  1483. method = 'capiGetMarketCandles'
  1484. request['granularity'] = interval
  1485. now = self.milliseconds()
  1486. if since is None:
  1487. if limit is None:
  1488. limit = 1000
  1489. request['start'] = self.iso8601(now - limit * duration * 1000)
  1490. request['end'] = self.iso8601(now)
  1491. else:
  1492. request['start'] = self.iso8601(since)
  1493. if limit is None:
  1494. request['end'] = self.iso8601(now)
  1495. else:
  1496. request['end'] = self.iso8601(self.sum(since, limit * duration * 1000))
  1497. response = await getattr(self, method)(self.extend(request, params))
  1498. #
  1499. # spot
  1500. #
  1501. # {
  1502. # "status":"ok",
  1503. # "ch":"market.btc_usdt.kline.15min",
  1504. # "ts":1595594183874,
  1505. # "data":[
  1506. # {"id":"1594694700000","amount":"283.6811","count":"234","open":"9230.00","close":"9227.15","low":"9206.66","high":"9232.33","vol":"2618015.032504000000"},
  1507. # {"id":"1594695600000","amount":"457.2904","count":"238","open":"9227.15","close":"9229.46","low":"9223.80","high":"9235.14","vol":"4220734.684570000000"},
  1508. # {"id":"1594696500000","amount":"501.2353","count":"255","open":"9229.46","close":"9227.78","low":"9222.69","high":"9230.74","vol":"4625779.185006000000"},
  1509. # ]
  1510. # }
  1511. #
  1512. # swap
  1513. #
  1514. # [
  1515. # ["1594764900000","9255.5","9261","9251","9255.5","3958946","427.742307964305"],
  1516. # ["1594765800000","9255.5","9264","9252","9258","3609496","389.832756058107"],
  1517. # ["1594766700000","9258","9260","9244.5","9250.5","3738600","403.97870345085"],
  1518. # ]
  1519. #
  1520. candles = response
  1521. if not isinstance(response, list):
  1522. candles = self.safe_value(response, 'data', [])
  1523. return self.parse_ohlcvs(candles, market, timeframe, since, limit)
  1524. def parse_spot_balance(self, response):
  1525. #
  1526. # {
  1527. # "status":"ok",
  1528. # "ts":1595681450932,
  1529. # "data":{
  1530. # "list":[
  1531. # {"balance":"0.0000000000000000","currency":"BTC","type":"trade"},
  1532. # {"balance":"0.0000000000000000","currency":"BTC","type":"frozen"},
  1533. # {"balance":"0.0000000000000000","currency":"BTC","type":"lock"},
  1534. # ],
  1535. # "id":"7420922606",
  1536. # "type":"spot",
  1537. # "state":"working"
  1538. # }
  1539. # }
  1540. #
  1541. result = {'info': response}
  1542. data = self.safe_value(response, 'data')
  1543. balances = self.safe_value(data, 'list')
  1544. for i in range(0, len(balances)):
  1545. balance = balances[i]
  1546. currencyId = self.safe_string(balance, 'currency')
  1547. code = self.safe_currency_code(currencyId)
  1548. if not (code in result):
  1549. account = self.account()
  1550. result[code] = account
  1551. type = self.safe_value(balance, 'type')
  1552. if type == 'trade':
  1553. result[code]['free'] = self.safe_float(balance, 'balance')
  1554. elif (type == 'frozen') or (type == 'lock'):
  1555. used = self.safe_float(result[code], 'used')
  1556. result[code]['used'] = self.sum(used, self.safe_float(balance, 'balance'))
  1557. return self.parse_balance(result)
  1558. def parse_swap_balance(self, response):
  1559. #
  1560. # swap
  1561. #
  1562. # [
  1563. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"bchusd","margin_frozen":"0","timestamp":"1595673431547","margin_mode":"fixed","forwardContractFlag":false},
  1564. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"ethusd","margin_frozen":"0","timestamp":"1595673431573","margin_mode":"fixed","forwardContractFlag":false},
  1565. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"cmt_btcsusdt","margin_frozen":"0","timestamp":"1595673431577","margin_mode":"fixed","forwardContractFlag":true},
  1566. # ]
  1567. #
  1568. #
  1569. result = {}
  1570. for i in range(0, len(response)):
  1571. balance = response[i]
  1572. marketId = self.safe_string(balance, 'symbol')
  1573. symbol = marketId
  1574. if marketId in self.markets_by_id:
  1575. symbol = self.markets_by_id[marketId]['symbol']
  1576. account = self.account()
  1577. # it may be incorrect to use total, free and used for swap accounts
  1578. account['total'] = self.safe_float(balance, 'equity')
  1579. account['free'] = self.safe_float(balance, 'total_avail_balance')
  1580. result[symbol] = account
  1581. return self.parse_balance(result)
  1582. async def fetch_accounts(self, params={}):
  1583. request = {
  1584. 'method': 'accounts',
  1585. }
  1586. response = await self.apiGetAccountAccounts(self.extend(request, params))
  1587. #
  1588. # {
  1589. # "status":"ok",
  1590. # "ts":1595679591824,
  1591. # "data":[
  1592. # {"id":"7420922606","type":"spot","state":"working"}
  1593. # ]
  1594. # }
  1595. #
  1596. data = self.safe_value(response, 'data', [])
  1597. result = []
  1598. for i in range(0, len(data)):
  1599. account = data[i]
  1600. accountId = self.safe_string(account, 'id')
  1601. type = self.safe_string_lower(account, 'type')
  1602. result.append({
  1603. 'id': accountId,
  1604. 'type': type,
  1605. 'currency': None,
  1606. 'info': account,
  1607. })
  1608. return result
  1609. async def find_account_by_type(self, type):
  1610. await self.load_markets()
  1611. await self.load_accounts()
  1612. accountsByType = self.group_by(self.accounts, 'type')
  1613. accounts = self.safe_value(accountsByType, type)
  1614. if accounts is None:
  1615. raise ExchangeError(self.id + " findAccountByType() could not find an accountId with type '" + type + "', specify the 'accountId' parameter instead") # eslint-disable-line quotes
  1616. numAccounts = len(accounts)
  1617. if numAccounts > 1:
  1618. raise ExchangeError(self.id + " findAccountByType() found more than one accountId with type '" + type + "', specify the 'accountId' parameter instead") # eslint-disable-line quotes
  1619. return accounts[0]
  1620. async def get_account_id(self, params):
  1621. await self.load_markets()
  1622. await self.load_accounts()
  1623. defaultAccountId = self.safe_string(self.options, 'accountId')
  1624. accountId = self.safe_string(params, 'accountId', defaultAccountId)
  1625. if accountId is not None:
  1626. return accountId
  1627. defaultType = self.safe_string(self.options, 'defaultType', 'margin')
  1628. type = self.safe_string(params, 'type', defaultType)
  1629. params = self.omit(params, 'type')
  1630. if type is None:
  1631. raise ArgumentsRequired(self.id + " requires an 'accountId' parameter")
  1632. account = await self.find_account_by_type(type)
  1633. return account['id']
  1634. async def fetch_balance(self, params={}):
  1635. await self.load_markets()
  1636. await self.load_accounts()
  1637. defaultType = self.safe_string_2(self.options, 'fetchBalance', 'defaultType')
  1638. type = self.safe_string(params, 'type', defaultType)
  1639. if type is None:
  1640. raise ArgumentsRequired(self.id + " fetchBalance requires a 'type' parameter, one of 'spot', 'swap'")
  1641. method = None
  1642. query = self.omit(params, 'type')
  1643. if type == 'spot':
  1644. accountId = await self.get_account_id(params)
  1645. method = 'apiGetAccountsAccountIdBalance'
  1646. query['account_id'] = accountId
  1647. query['method'] = 'balance'
  1648. elif type == 'swap':
  1649. method = 'swapGetAccountAccounts'
  1650. response = await getattr(self, method)(query)
  1651. #
  1652. # spot
  1653. #
  1654. # {
  1655. # "status":"ok",
  1656. # "ts":1595681450932,
  1657. # "data":{
  1658. # "list":[
  1659. # {"balance":"0.0000000000000000","currency":"BTC","type":"trade"},
  1660. # {"balance":"0.0000000000000000","currency":"BTC","type":"frozen"},
  1661. # {"balance":"0.0000000000000000","currency":"BTC","type":"lock"},
  1662. # ],
  1663. # "id":"7420922606",
  1664. # "type":"spot",
  1665. # "state":"working"
  1666. # }
  1667. # }
  1668. #
  1669. # swap
  1670. #
  1671. # [
  1672. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"bchusd","margin_frozen":"0","timestamp":"1595673431547","margin_mode":"fixed","forwardContractFlag":false},
  1673. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"ethusd","margin_frozen":"0","timestamp":"1595673431573","margin_mode":"fixed","forwardContractFlag":false},
  1674. # {"equity":"0","fixed_balance":"0","total_avail_balance":"0","margin":"0","realized_pnl":"0","unrealized_pnl":"0","symbol":"cmt_btcsusdt","margin_frozen":"0","timestamp":"1595673431577","margin_mode":"fixed","forwardContractFlag":true},
  1675. # ]
  1676. #
  1677. return self.parse_balance_by_type(type, response)
  1678. def parse_balance_by_type(self, type, response):
  1679. if type == 'spot':
  1680. return self.parse_spot_balance(response)
  1681. elif type == 'swap':
  1682. return self.parse_swap_balance(response)
  1683. raise NotSupported(self.id + " fetchBalance does not support the '" + type + "' type(the type must be one of 'account', 'spot', 'margin', 'futures', 'swap')")
  1684. def parse_order_status(self, status):
  1685. statuses = {
  1686. 'submitted': 'open',
  1687. 'partial-filled': 'open',
  1688. 'partial-canceled': 'canceled',
  1689. 'filled': 'closed',
  1690. 'canceled': 'canceled',
  1691. '-2': 'failed',
  1692. '-1': 'canceled',
  1693. '0': 'open',
  1694. '1': 'open',
  1695. '2': 'closed',
  1696. '3': 'open',
  1697. '4': 'canceled',
  1698. }
  1699. return self.safe_string(statuses, status, status)
  1700. def parse_order_side(self, side):
  1701. sides = {
  1702. 'buy-market': 'buy',
  1703. 'sell-market': 'sell',
  1704. 'buy-limit': 'buy',
  1705. 'sell-limit': 'sell',
  1706. '1': 'long', # open long
  1707. '2': 'short', # open short
  1708. '3': 'long', # close long
  1709. '4': 'short', # close short
  1710. }
  1711. return self.safe_string(sides, side, side)
  1712. def parse_order_type(self, type):
  1713. types = {
  1714. 'buy-market': 'market',
  1715. 'sell-market': 'market',
  1716. 'buy-limit': 'limit',
  1717. 'sell-limit': 'limit',
  1718. '1': 'open', # open long
  1719. '2': 'open', # open short
  1720. '3': 'close', # close long
  1721. '4': 'close', # close short
  1722. }
  1723. return self.safe_string(types, type, type)
  1724. def parse_order(self, order, market=None):
  1725. #
  1726. # createOrder
  1727. #
  1728. # spot
  1729. #
  1730. # {
  1731. # "status":"ok",
  1732. # "ts":1595792596056,
  1733. # "data":671368296142774272
  1734. # }
  1735. #
  1736. # swap
  1737. #
  1738. # {
  1739. # "client_oid":"58775e54-0592-491c-97e8-e2369025f2d1",
  1740. # "order_id":"671757564085534713"
  1741. # }
  1742. #
  1743. # cancelOrder
  1744. #
  1745. # spot
  1746. #
  1747. # {
  1748. # "status": "ok",
  1749. # "ts": 1595818631279,
  1750. # "data": 671368296142774272
  1751. # }
  1752. #
  1753. # swap
  1754. #
  1755. # {
  1756. # "order_id":"671757564085534713",
  1757. # "client_oid":"58775e54-0592-491c-97e8-e2369025f2d1",
  1758. # "symbol":"cmt_ethusdt",
  1759. # "result":true,
  1760. # "err_code":null,
  1761. # "err_msg":null
  1762. # }
  1763. #
  1764. # fetchOpenOrders, fetchClosedOrders, fetchOrder
  1765. #
  1766. # spot
  1767. #
  1768. # {
  1769. # "account_id":"7420922606",
  1770. # "amount":"0.1000000000000000",
  1771. # "canceled_at":"1595872129618",
  1772. # "created_at":"1595872089525",
  1773. # "filled_amount":"0.000000000000",
  1774. # "filled_cash_amount":"0.000000000000",
  1775. # "filled_fees":"0.000000000000",
  1776. # "finished_at":"1595872129618",
  1777. # "id":"671701716584665088",
  1778. # "price":"150.000000000000",
  1779. # "source":"接口",
  1780. # "state":"canceled",
  1781. # "symbol":"eth_usdt",
  1782. # "type":"buy-limit"
  1783. # }
  1784. #
  1785. # swap
  1786. #
  1787. # {
  1788. # "symbol":"cmt_ethusdt",
  1789. # "size":"1",
  1790. # "timestamp":"1595885546770",
  1791. # "client_oid":"f3aa81d6-9a4c-4eab-bebe-ebc19da21cf2",
  1792. # "createTime":"1595885521200",
  1793. # "filled_qty":"0",
  1794. # "fee":"0.00000000",
  1795. # "order_id":"671758053112020913",
  1796. # "price":"150.00",
  1797. # "price_avg":"0.00",
  1798. # "status":"0",
  1799. # "type":"1",
  1800. # "order_type":"0",
  1801. # "totalProfits":null
  1802. # }
  1803. #
  1804. id = self.safe_string(order, 'order_id')
  1805. id = self.safe_string_2(order, 'id', 'data', id)
  1806. timestamp = self.safe_integer_2(order, 'created_at', 'createTime')
  1807. type = self.safe_string(order, 'type')
  1808. side = self.parse_order_side(type)
  1809. type = self.parse_order_type(type)
  1810. # if (side != 'buy') and (side != 'sell'):
  1811. # side = self.parse_order_side(type)
  1812. # }
  1813. # if (type != 'limit') and (type != 'market'):
  1814. # if 'pnl' in order:
  1815. # type = 'futures'
  1816. # else:
  1817. # type = 'swap'
  1818. # }
  1819. # }
  1820. symbol = None
  1821. marketId = self.safe_string(order, 'symbol')
  1822. if marketId is not None:
  1823. if marketId in self.markets_by_id:
  1824. market = self.markets_by_id[marketId]
  1825. else:
  1826. symbol = marketId.upper()
  1827. if (symbol is None) and (market is not None):
  1828. symbol = market['symbol']
  1829. amount = self.safe_float_2(order, 'amount', 'size')
  1830. filled = self.safe_float_2(order, 'filled_amount', 'filled_qty')
  1831. remaining = None
  1832. if amount is not None:
  1833. if filled is not None:
  1834. amount = max(amount, filled)
  1835. remaining = max(0, amount - filled)
  1836. if type == 'market':
  1837. remaining = 0
  1838. cost = self.safe_float(order, 'filled_cash_amount')
  1839. price = self.safe_float(order, 'price')
  1840. average = self.safe_float(order, 'price_avg')
  1841. if (average is None) and (filled is not None) and (cost is not None) and (filled > 0):
  1842. average = cost / filled
  1843. status = self.parse_order_status(self.safe_string_2(order, 'state', 'status'))
  1844. feeCost = self.safe_float_2(order, 'filled_fees', 'fee')
  1845. fee = None
  1846. if feeCost is not None:
  1847. feeCurrency = None
  1848. fee = {
  1849. 'cost': feeCost,
  1850. 'currency': feeCurrency,
  1851. }
  1852. clientOrderId = self.safe_string(order, 'client_oid')
  1853. return {
  1854. 'info': order,
  1855. 'id': id,
  1856. 'clientOrderId': clientOrderId,
  1857. 'timestamp': timestamp,
  1858. 'datetime': self.iso8601(timestamp),
  1859. 'lastTradeTimestamp': None,
  1860. 'symbol': symbol,
  1861. 'type': type,
  1862. 'side': side,
  1863. 'price': price,
  1864. 'average': average,
  1865. 'cost': cost,
  1866. 'amount': amount,
  1867. 'filled': filled,
  1868. 'remaining': remaining,
  1869. 'status': status,
  1870. 'fee': fee,
  1871. 'trades': None,
  1872. }
  1873. async def create_order(self, symbol, type, side, amount, price=None, params={}):
  1874. await self.load_markets()
  1875. await self.load_accounts()
  1876. market = self.market(symbol)
  1877. #
  1878. # spot
  1879. #
  1880. # account_id True string Account ID, obtained using the accounts method. Currency transactions use the accountid of the'spot' account; for loan asset transactions, please use the accountid of the'margin' account
  1881. # amount True string A limit order indicates the quantity of the order, when a market price buy order indicates how much money to buy, and when a market price sell order indicates how much currency to sell
  1882. # price False string Order price, market order does not pass self parameter
  1883. # source False string Order source api
  1884. # symbol True string Trading pair btc_usdt, eth_btc ...
  1885. # type True string Order Type buy-market: buy at market price, sell-market: sell at market price, buy-limit: buy at limit price, sell-limit: sell at limit price
  1886. #
  1887. # swap
  1888. #
  1889. # symbol String Yes Contract ID
  1890. # client_oid String Yes customize order IDs to identify your orders.(Less than 50 characters without special characters,
  1891. # size String Yes Quantity to buy or sell(value not equal to 0 or negative)
  1892. # type String Yes 1 Open long 2Open short 3 Close long 4 Close short
  1893. # order_type String Yes 0: Normal order(Unfilled and 0 imply normal limit order) 1: Post only 2: Fill or Kill 3: Immediate Or Cancel
  1894. # match_price String Yes 0 Limit price 1 market price
  1895. # price String No Price of each contract
  1896. #
  1897. request = {
  1898. 'symbol': market['id'],
  1899. }
  1900. clientOrderId = self.safe_string_2(params, 'client_oid', 'clientOrderId', self.uuid())
  1901. params = self.omit(params, ['client_oid', 'clientOrderId'])
  1902. method = None
  1903. if market['spot']:
  1904. accountId = await self.get_account_id({
  1905. 'type': market['type'],
  1906. })
  1907. method = 'apiPostOrderOrdersPlace'
  1908. request['account_id'] = accountId
  1909. request['method'] = 'place'
  1910. request['type'] = side + '-' + type
  1911. if type == 'limit':
  1912. request['amount'] = self.amount_to_precision(symbol, amount)
  1913. request['price'] = self.price_to_precision(symbol, price)
  1914. elif type == 'market':
  1915. # for market buy it requires the amount of quote currency to spend
  1916. if side == 'buy':
  1917. cost = self.safe_float(params, 'amount')
  1918. createMarketBuyOrderRequiresPrice = self.safe_value(self.options, 'createMarketBuyOrderRequiresPrice', True)
  1919. if createMarketBuyOrderRequiresPrice:
  1920. if price is not None:
  1921. if cost is None:
  1922. cost = amount * price
  1923. elif cost is None:
  1924. 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 'amount' extra parameter(the exchange-specific behaviour)")
  1925. else:
  1926. cost = amount if (cost is None) else cost
  1927. request['amount'] = self.cost_to_precision(symbol, cost)
  1928. elif side == 'sell':
  1929. request['amount'] = self.amount_to_precision(symbol, amount)
  1930. # ...
  1931. elif market['swap']:
  1932. request['order_type'] = '0' # '0' = Normal order, None and 0 imply a normal limit order, '1' = Post only, '2' = Fill or Kill, '3' = Immediate Or Cancel
  1933. request['client_oid'] = clientOrderId
  1934. orderType = self.safe_string(params, 'type')
  1935. if orderType is None:
  1936. raise ArgumentsRequired(self.id + " createOrder requires a type parameter, '1' = open long, '2' = open short, '3' = close long, '4' = close short for " + market['type'] + ' orders')
  1937. request['size'] = self.amount_to_precision(symbol, amount)
  1938. request['type'] = orderType
  1939. # if match_price is set to '1', the price parameter will be ignored for market orders
  1940. if type == 'limit':
  1941. request['match_price'] = '0'
  1942. request['price'] = self.price_to_precision(symbol, price)
  1943. elif type == 'market':
  1944. request['match_price'] = '1'
  1945. method = 'swapPostOrderPlaceOrder'
  1946. response = await getattr(self, method)(self.extend(request, params))
  1947. #
  1948. # spot
  1949. #
  1950. # {
  1951. # "status":"ok",
  1952. # "ts":1595792596056,
  1953. # "data":"671368296142774272"
  1954. # }
  1955. #
  1956. # swap
  1957. #
  1958. # {
  1959. # "client_oid":"58775e54-0592-491c-97e8-e2369025f2d1",
  1960. # "order_id":"671757564085534713"
  1961. # }
  1962. #
  1963. return self.parse_order(response, market)
  1964. async def cancel_order(self, id, symbol=None, params={}):
  1965. await self.load_markets()
  1966. market = None
  1967. type = None
  1968. if symbol is None:
  1969. defaultType = self.safe_string_2(self.options, 'cancelOrder', 'defaultType')
  1970. type = self.safe_string(params, 'type', defaultType)
  1971. if type == 'spot':
  1972. if symbol is None:
  1973. raise ArgumentsRequired(self.id + ' cancelOrder requires a symbol argument for spot orders')
  1974. else:
  1975. market = self.market(symbol)
  1976. type = market['type']
  1977. query = self.omit(params, 'type')
  1978. method = None
  1979. request = {}
  1980. if type == 'spot':
  1981. method = 'apiPostOrderOrdersOrderIdSubmitcancel'
  1982. request['order_id'] = id
  1983. request['method'] = 'submitcancel'
  1984. elif type == 'swap':
  1985. method = 'swapPostOrderCancelOrder'
  1986. request['orderId'] = id
  1987. request['symbol'] = market['id']
  1988. response = await getattr(self, method)(self.extend(request, query))
  1989. #
  1990. # spot
  1991. #
  1992. # {"status": "ok", "ts": 1595818631279, "data": 671368296142774272}
  1993. #
  1994. # swap
  1995. #
  1996. # {
  1997. # "order_id":"671757564085534713",
  1998. # "client_oid":"58775e54-0592-491c-97e8-e2369025f2d1",
  1999. # "symbol":"cmt_ethusdt",
  2000. # "result":true,
  2001. # "err_code":null,
  2002. # "err_msg":null
  2003. # }
  2004. #
  2005. return self.parse_order(response, market)
  2006. async def cancel_orders(self, ids, symbol=None, params={}):
  2007. if symbol is None:
  2008. raise ArgumentsRequired(self.id + ' cancelOrders requires a symbol argument')
  2009. await self.load_markets()
  2010. market = self.market(symbol)
  2011. type = self.safe_string(params, 'type', market['type'])
  2012. if type is None:
  2013. raise ArgumentsRequired(self.id + " cancelOrders requires a type parameter(one of 'spot', 'swap').")
  2014. request = {}
  2015. method = None
  2016. if type == 'spot':
  2017. method = 'apiPostOrderOrdersBatchcancel'
  2018. request['method'] = 'batchcancel'
  2019. jsonIds = self.json(ids)
  2020. parts = jsonIds.split('"')
  2021. request['order_ids'] = ''.join(parts)
  2022. elif type == 'swap':
  2023. method = 'swapPostOrderCancelBatchOrders'
  2024. request['symbol'] = market['id']
  2025. request['ids'] = ids
  2026. response = await getattr(self, method)(self.extend(request, params))
  2027. #
  2028. # spot
  2029. #
  2030. # {
  2031. # "status": "ok",
  2032. # "data": {
  2033. # "success": [
  2034. # "673451224205135872",
  2035. # ],
  2036. # "failed": [
  2037. # {
  2038. # "err-msg": "invalid record",
  2039. # "order-id": "673451224205135873",
  2040. # "err-code": "base record invalid"
  2041. # }
  2042. # ]
  2043. # }
  2044. # }
  2045. #
  2046. # swap
  2047. #
  2048. # {
  2049. # "result":true,
  2050. # "symbol":"cmt_btcusdt",
  2051. # "order_ids":[
  2052. # "258414711",
  2053. # "478585558"
  2054. # ],
  2055. # "fail_infos":[
  2056. # {
  2057. # "order_id":"258414711",
  2058. # "err_code":"401",
  2059. # "err_msg":""
  2060. # }
  2061. # ]
  2062. # }
  2063. #
  2064. return response
  2065. async def fetch_order(self, id, symbol=None, params={}):
  2066. if symbol is None:
  2067. raise ArgumentsRequired(self.id + ' fetchOrder requires a symbol argument')
  2068. await self.load_markets()
  2069. market = self.market(symbol)
  2070. type = self.safe_string(params, 'type', market['type'])
  2071. if type is None:
  2072. raise ArgumentsRequired(self.id + " fetchOrder requires a type parameter(one of 'spot', 'swap').")
  2073. method = None
  2074. request = {}
  2075. if type == 'spot':
  2076. clientOid = self.safe_string(params, 'client_oid')
  2077. if clientOid is not None:
  2078. method = 'apiPostOrderOrdersClientOid'
  2079. request['client_oid'] = clientOid
  2080. else:
  2081. method = 'apiPostOrderOrdersOrderId'
  2082. request['order_id'] = id
  2083. request['method'] = 'getOrder'
  2084. elif type == 'swap':
  2085. method = 'swapGetOrderDetail'
  2086. request['symbol'] = market['id']
  2087. request['orderId'] = id
  2088. query = self.omit(params, 'type')
  2089. response = await getattr(self, method)(self.extend(request, query))
  2090. #
  2091. # spot
  2092. #
  2093. # {
  2094. # "status":"ok",
  2095. # "ts":1595897886717,
  2096. # "data":{
  2097. # "account_id":"7420922606",
  2098. # "amount":"0.1000000000000000",
  2099. # "canceled_at":"1595818631541",
  2100. # "created_at":"1595792595897",
  2101. # "filled_amount":"0.000000000000",
  2102. # "filled_cash_amount":"0.000000000000",
  2103. # "filled_fees":"0.000000000000",
  2104. # "finished_at":"1595818631541",
  2105. # "id":"671368296142774272",
  2106. # "price":"150.000000000000",
  2107. # "source":"接口",
  2108. # "state":"canceled",
  2109. # "symbol":"eth_usdt",
  2110. # "type":"buy-limit"
  2111. # }
  2112. # }
  2113. #
  2114. #
  2115. # swap
  2116. #
  2117. # {
  2118. # "symbol":"cmt_ethusdt",
  2119. # "size":"1",
  2120. # "timestamp":"1595896459890",
  2121. # "client_oid":"58775e54-0592-491c-97e8-e2369025f2d1",
  2122. # "createTime":"1595885404607",
  2123. # "filled_qty":"0",
  2124. # "fee":"0",
  2125. # "order_id":"671757564085534713",
  2126. # "price":"150",
  2127. # "price_avg":"0",
  2128. # "status":"-1",
  2129. # "type":"1",
  2130. # "order_type":"0",
  2131. # "totalProfits":"0"
  2132. # }
  2133. #
  2134. data = self.safe_value(response, 'data', response)
  2135. return self.parse_order(data, market)
  2136. async def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}):
  2137. if symbol is None:
  2138. raise ArgumentsRequired(self.id + ' fetchOpenOrders requires a symbol argument')
  2139. await self.load_markets()
  2140. market = self.market(symbol)
  2141. type = self.safe_string(params, 'type', market['type'])
  2142. request = {
  2143. 'symbol': market['id'],
  2144. }
  2145. method = None
  2146. if type == 'spot':
  2147. method = 'apiGetOrderOrdersOpenOrders'
  2148. # request['from'] = self.safe_string(params, 'from') # order id
  2149. # request['direct'] = 'next' # or 'prev'
  2150. request['method'] = 'openOrders'
  2151. if limit is None:
  2152. request['size'] = limit # default 100, max 1000
  2153. elif type == 'swap':
  2154. method = 'swapGetOrderOrders'
  2155. request['status'] = '3' # 0 Failed, 1 Partially Filled, 2 Fully Filled 3 = Open + Partially Filled, 4 Canceling
  2156. request['from'] = '1'
  2157. request['to'] = '1'
  2158. if limit is None:
  2159. request['limit'] = 100 # default 100, max 100
  2160. query = self.omit(params, 'type')
  2161. response = await getattr(self, method)(self.extend(request, query))
  2162. #
  2163. # spot
  2164. #
  2165. #
  2166. # {
  2167. # "status":"ok",
  2168. # "ts":1595875165865,
  2169. # "data":[
  2170. # {
  2171. # "account_id":"7420922606",
  2172. # "amount":"0.1000000000000000",
  2173. # "canceled_at":"1595872129618",
  2174. # "created_at":"1595872089525",
  2175. # "filled_amount":"0.000000000000",
  2176. # "filled_cash_amount":"0.000000000000",
  2177. # "filled_fees":"0.000000000000",
  2178. # "finished_at":"1595872129618",
  2179. # "id":"671701716584665088",
  2180. # "price":"150.000000000000",
  2181. # "source":"接口",
  2182. # "state":"canceled",
  2183. # "symbol":"eth_usdt",
  2184. # "type":"buy-limit"
  2185. # }
  2186. # ]
  2187. # }
  2188. #
  2189. # swap
  2190. #
  2191. # [
  2192. # {
  2193. # "symbol":"cmt_ethusdt",
  2194. # "size":"1",
  2195. # "timestamp":"1595885546770",
  2196. # "client_oid":"f3aa81d6-9a4c-4eab-bebe-ebc19da21cf2",
  2197. # "createTime":"1595885521200",
  2198. # "filled_qty":"0",
  2199. # "fee":"0.00000000",
  2200. # "order_id":"671758053112020913",
  2201. # "price":"150.00",
  2202. # "price_avg":"0.00",
  2203. # "status":"0",
  2204. # "type":"1",
  2205. # "order_type":"0",
  2206. # "totalProfits":null
  2207. # }
  2208. # ]
  2209. #
  2210. data = response
  2211. if not isinstance(response, list):
  2212. data = self.safe_value(response, 'data', [])
  2213. return self.parse_orders(data, market, None, limit)
  2214. async def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}):
  2215. if symbol is None:
  2216. raise ArgumentsRequired(self.id + ' fetchClosedOrders requires a symbol argument')
  2217. await self.load_markets()
  2218. market = self.market(symbol)
  2219. type = self.safe_string(params, 'type', market['type'])
  2220. request = {
  2221. 'symbol': market['id'],
  2222. }
  2223. method = None
  2224. if type == 'spot':
  2225. method = 'apiGetOrderOrdersHistory'
  2226. # Value range [((end_time) 48h),(end_time)]
  2227. # the query window is 48 hours at most
  2228. # the window shift range is the last 30 days
  2229. if since is not None:
  2230. request['start_time'] = since
  2231. # request['end_time'] = self.safe_integer(params, 'end_time')
  2232. # request['from'] = self.safe_string(params, 'from') # order id
  2233. # request['direct'] = 'next' # or 'prev'
  2234. request['method'] = 'openOrders'
  2235. if limit is None:
  2236. request['size'] = limit # default 100, max 1000
  2237. elif type == 'swap':
  2238. method = 'swapGetOrderOrders'
  2239. request['status'] = '2' # 0 Failed, 1 Partially Filled, 2 Fully Filled 3 = Open + Partially Filled, 4 Canceling
  2240. request['from'] = '1'
  2241. request['to'] = '1'
  2242. if limit is None:
  2243. request['limit'] = 100 # default 100, max 100
  2244. query = self.omit(params, 'type')
  2245. response = await getattr(self, method)(self.extend(request, query))
  2246. #
  2247. # spot
  2248. #
  2249. #
  2250. # {
  2251. # "status":"ok",
  2252. # "ts":1595875165865,
  2253. # "data":[
  2254. # {
  2255. # "account_id":"7420922606",
  2256. # "amount":"0.1000000000000000",
  2257. # "canceled_at":"1595872129618",
  2258. # "created_at":"1595872089525",
  2259. # "filled_amount":"0.000000000000",
  2260. # "filled_cash_amount":"0.000000000000",
  2261. # "filled_fees":"0.000000000000",
  2262. # "finished_at":"1595872129618",
  2263. # "id":"671701716584665088",
  2264. # "price":"150.000000000000",
  2265. # "source":"接口",
  2266. # "state":"canceled",
  2267. # "symbol":"eth_usdt",
  2268. # "type":"buy-limit"
  2269. # }
  2270. # ]
  2271. # }
  2272. #
  2273. # swap
  2274. #
  2275. # [
  2276. # {
  2277. # "symbol":"cmt_ethusdt",
  2278. # "size":"1",
  2279. # "timestamp":"1595885546770",
  2280. # "client_oid":"f3aa81d6-9a4c-4eab-bebe-ebc19da21cf2",
  2281. # "createTime":"1595885521200",
  2282. # "filled_qty":"0",
  2283. # "fee":"0.00000000",
  2284. # "order_id":"671758053112020913",
  2285. # "price":"150.00",
  2286. # "price_avg":"0.00",
  2287. # "status":"0",
  2288. # "type":"1",
  2289. # "order_type":"0",
  2290. # "totalProfits":null
  2291. # }
  2292. # ]
  2293. #
  2294. data = response
  2295. if not isinstance(response, list):
  2296. data = self.safe_value(response, 'data', [])
  2297. return self.parse_orders(data, market, None, limit)
  2298. async def fetch_deposits(self, code=None, since=None, limit=None, params={}):
  2299. if code is None:
  2300. raise ArgumentsRequired(self.id + ' fetchDeposits requires a currency code argument')
  2301. await self.load_markets()
  2302. currency = self.currency(code)
  2303. request = {
  2304. 'currency': currency['id'],
  2305. 'method': 'deposit_withdraw',
  2306. 'type': 'deposit',
  2307. 'size': 12,
  2308. }
  2309. response = await self.apiGetOrderDepositWithdraw(self.extend(request, params))
  2310. #
  2311. # {
  2312. # "status": "ok",
  2313. # "data": [
  2314. # {
  2315. # "id": 1171,
  2316. # "type": "deposit",
  2317. # "currency": "usdt",
  2318. # "tx_hash": "ed03094b84eafbe4bc16e7ef766ee959885ee5bcb265872baaa9c64e1cf86c2b",
  2319. # "amount": 7.457467,
  2320. # "address": "rae93V8d2mdoUQHwBDBdM4NHCMehRJAsbm",
  2321. # "address_tag": "100040",
  2322. # "fee": 0,
  2323. # "state": "safe",
  2324. # "created_at": 1510912472199,
  2325. # "updated_at": 1511145876575
  2326. # },
  2327. # ]
  2328. # }
  2329. #
  2330. data = self.safe_value(response, 'data', [])
  2331. return self.parse_transactions(data, currency, since, limit, params)
  2332. async def fetch_withdrawals(self, code=None, since=None, limit=None, params={}):
  2333. if code is None:
  2334. raise ArgumentsRequired(self.id + ' fetchWithdrawals requires a currency code argument')
  2335. await self.load_markets()
  2336. currency = self.currency(code)
  2337. request = {
  2338. 'currency': currency['id'],
  2339. 'method': 'deposit_withdraw',
  2340. 'type': 'withdraw',
  2341. 'size': 12,
  2342. }
  2343. response = await self.apiGetOrderDepositWithdraw(self.extend(request, params))
  2344. #
  2345. # {
  2346. # "status": "ok",
  2347. # "data": [
  2348. # {
  2349. # "id": 1171,
  2350. # "type": "withdraw",
  2351. # "currency": "usdt",
  2352. # "tx_hash": "ed03094b84eafbe4bc16e7ef766ee959885ee5bcb265872baaa9c64e1cf86c2b",
  2353. # "amount": 7.457467,
  2354. # "address": "rae93V8d2mdoUQHwBDBdM4NHCMehRJAsbm",
  2355. # "address_tag": "100040",
  2356. # "fee": 0,
  2357. # "state": "safe",
  2358. # "created_at": 1510912472199,
  2359. # "updated_at": 1511145876575
  2360. # },
  2361. # ]
  2362. # }
  2363. #
  2364. data = self.safe_value(response, 'data', [])
  2365. return self.parse_transactions(data, currency, since, limit, params)
  2366. def parse_transaction_status(self, status):
  2367. statuses = {
  2368. # withdrawals
  2369. 'WaitForOperation': 'pending', # 等待提现
  2370. 'OperationLock': 'pending', # 初审锁定成功
  2371. 'OperationSuccess': 'ok', # 提现成功
  2372. 'Cancel': 'canceled', # 用户撤销
  2373. 'Sure': 'ok', # 复审锁定成功
  2374. 'Fail': 'failed', # 出币异常
  2375. 'WaitForChainSure': 'ok', # 等待链上确认
  2376. # deposits
  2377. 'WAIT_0': 'pending', # 待确认
  2378. 'WAIT_1': 'pending', # 待确认
  2379. 'DATA_CHANGE': 'pending', # 待确认中
  2380. 'SUCCESS': 'ok', # 充值成功
  2381. }
  2382. return self.safe_string(statuses, status, status)
  2383. def parse_transaction(self, transaction, currency=None):
  2384. #
  2385. # fetchDeposits, fetchWithdrawals
  2386. #
  2387. # {
  2388. # "id": 1171,
  2389. # "type": "withdraw",
  2390. # "currency": "usdt",
  2391. # "tx_hash": "ed03094b84eafbe4bc16e7ef766ee959885ee5bcb265872baaa9c64e1cf86c2b",
  2392. # "amount": 7.457467,
  2393. # "address": "rae93V8d2mdoUQHwBDBdM4NHCMehRJAsbm",
  2394. # "address_tag": "100040",
  2395. # "fee": 0,
  2396. # "state": "safe",
  2397. # "created_at": 1510912472199,
  2398. # "updated_at": 1511145876575
  2399. # }
  2400. #
  2401. id = self.safe_string(transaction, 'id')
  2402. address = self.safe_string(transaction, 'address')
  2403. tag = self.safe_string(transaction, 'address_tag')
  2404. tagFrom = None
  2405. tagTo = tag
  2406. addressFrom = None
  2407. addressTo = address
  2408. type = self.safe_string(transaction, 'type')
  2409. if type == 'withdraw':
  2410. type = 'withdrawal'
  2411. elif type == 'deposit':
  2412. type = 'deposit'
  2413. currencyId = self.safe_string(transaction, 'currency')
  2414. code = self.safe_currency_code(currencyId)
  2415. amount = self.safe_float(transaction, 'amount')
  2416. status = self.parse_transaction_status(self.safe_string(transaction, 'state'))
  2417. txid = self.safe_string(transaction, 'tx_hash')
  2418. timestamp = self.safe_integer(transaction, 'created_at')
  2419. updated = self.safe_integer(transaction, 'updated_at')
  2420. feeCost = self.safe_float(transaction, 'fee')
  2421. fee = None
  2422. if feeCost is not None:
  2423. fee = {
  2424. 'currency': code,
  2425. 'cost': feeCost,
  2426. }
  2427. return {
  2428. 'info': transaction,
  2429. 'id': id,
  2430. 'currency': code,
  2431. 'amount': amount,
  2432. 'addressFrom': addressFrom,
  2433. 'addressTo': addressTo,
  2434. 'address': address,
  2435. 'tagFrom': tagFrom,
  2436. 'tagTo': tagTo,
  2437. 'tag': tag,
  2438. 'status': status,
  2439. 'type': type,
  2440. 'updated': updated,
  2441. 'txid': txid,
  2442. 'timestamp': timestamp,
  2443. 'datetime': self.iso8601(timestamp),
  2444. 'fee': fee,
  2445. }
  2446. async def fetch_my_trades(self, symbol=None, since=None, limit=None, params={}):
  2447. if symbol is None:
  2448. raise ArgumentsRequired(self.id + ' fetchMyTrades requires a symbol argument')
  2449. await self.load_markets()
  2450. market = self.market(symbol)
  2451. type = self.safe_string(params, 'type', market['type'])
  2452. query = self.omit(params, 'type')
  2453. if type == 'swap':
  2454. raise ArgumentsRequired(self.id + ' fetchMyTrades is not supported for ' + type + ' type')
  2455. #
  2456. # spot
  2457. #
  2458. # POST /api/v1/order/matchresults Query current order, order history
  2459. # symbol True string trading pair btc_usdt, eth_btc ...
  2460. # types False string Query order type combination buy-market, sell-market, buy-limit, sell-limit
  2461. # start_date False string Query start date, date format yyyy-mm-dd -61 days [-61day, end-date]
  2462. # end_date False string Query end date, date format yyyy-mm-dd Now [start-date, now]
  2463. # from False string Query start ID order record id
  2464. # direct False string Query direction next is default , the transaction record ID is sorted from large to small prevnext
  2465. # size False string Query record size 100 <=100
  2466. #
  2467. request = {
  2468. 'symbol': market['id'],
  2469. 'method': 'matchresults',
  2470. # 'types': 'buy-market,sell-market,buy-limit,sell-limit',
  2471. # 'start_date': self.ymd(since),
  2472. # 'end_date': self.ymd(self.milliseconds()),
  2473. # 'size': 100,
  2474. # 'direct': 'next',
  2475. }
  2476. if since is not None:
  2477. request['start_date'] = self.ymd(since)
  2478. end = self.sum(since, 2 * 24 * 60 * 60 * 1000)
  2479. request['end_date'] = self.ymd(end)
  2480. if limit is not None:
  2481. request['size'] = limit # default 100, max 100
  2482. response = await self.apiPostOrderMatchresults(self.extend(request, query))
  2483. #
  2484. # {
  2485. # "status": "ok",
  2486. # "data": [
  2487. # {
  2488. # "id": 29555,
  2489. # "order_id": 59378,
  2490. # "match_id": 59335,
  2491. # "symbol": "eth_usdt",
  2492. # "type": "buy-limit",
  2493. # "source": "api",
  2494. # "price": "100.1000000000",
  2495. # "filled_amount": "0.9845000000",
  2496. # "filled_fees": "0.0019690000",
  2497. # "created_at": 1494901400487
  2498. # }
  2499. # ]
  2500. # }
  2501. #
  2502. data = self.safe_value(response, 'data', [])
  2503. return self.parse_trades(data, market, since, limit)
  2504. async def fetch_order_trades(self, id, symbol=None, since=None, limit=None, params={}):
  2505. if symbol is None:
  2506. raise ArgumentsRequired(self.id + ' fetchOrderTrades requires a symbol argument')
  2507. await self.load_markets()
  2508. market = self.market(symbol)
  2509. type = self.safe_string(params, 'type', market['type'])
  2510. query = self.omit(params, 'type')
  2511. method = None
  2512. request = {}
  2513. if type == 'spot':
  2514. request['order_id'] = id
  2515. request['method'] = 'matchresults'
  2516. method = 'apiPostOrderOrdersOrderIdMatchresults'
  2517. elif type == 'swap':
  2518. request['orderId'] = id
  2519. request['symbol'] = market['id']
  2520. method = 'swapGetOrderFills'
  2521. response = await getattr(self, method)(self.extend(request, query))
  2522. #
  2523. # spot
  2524. #
  2525. # {
  2526. # "status":"ok",
  2527. # "ts":1596298917277,
  2528. # "data":[
  2529. # {
  2530. # "id":"614164775",
  2531. # "created_at":"1596298860602",
  2532. # "filled_amount":"0.0417000000000000",
  2533. # "filled_fees":"0.0000834000000000",
  2534. # "match_id":"673491702661292033",
  2535. # "order_id":"673491720340279296",
  2536. # "price":"359.240000000000",
  2537. # "source":"接口",
  2538. # "symbol":"eth_usdt",
  2539. # "type":"buy-market"
  2540. # }
  2541. # ]
  2542. # }
  2543. #
  2544. # swap
  2545. #
  2546. #
  2547. # [
  2548. # {
  2549. # "trade_id":"6667390",
  2550. # "symbol":"cmt_btcusdt",
  2551. # "order_id":"525946425993854915",
  2552. # "price":"9839.00",
  2553. # "order_qty":"3466",
  2554. # "fee":"-0.0000528407360000",
  2555. # "timestamp":"1561121514442",
  2556. # "exec_type":"M",
  2557. # "side":"3"
  2558. # }
  2559. # ]
  2560. #
  2561. data = response
  2562. if not isinstance(data, list):
  2563. data = self.safe_value(response, 'data', [])
  2564. return await self.parse_trades(data, market, since, limit)
  2565. def sign(self, path, api='public', method='GET', params={}, headers=None, body=None):
  2566. request = '/' + self.implode_params(path, params)
  2567. if (api == 'capi') or (api == 'swap'):
  2568. request = '/api/swap/' + self.version + request
  2569. else:
  2570. request = '/' + api + '/v1' + request
  2571. query = self.omit(params, self.extract_params(path))
  2572. url = self.implode_params(self.urls['api'][api], {'hostname': self.hostname}) + request
  2573. if (api == 'data') or (api == 'capi'):
  2574. if query:
  2575. url += '?' + self.urlencode(query)
  2576. elif api == 'swap':
  2577. self.check_required_credentials()
  2578. timestamp = str(self.milliseconds())
  2579. auth = timestamp + method + request
  2580. if method == 'POST':
  2581. body = self.json(params)
  2582. auth += body
  2583. else:
  2584. if params:
  2585. query = self.urlencode(self.keysort(params))
  2586. url += '?' + query
  2587. auth += '?' + query
  2588. signature = self.hmac(self.encode(auth), self.encode(self.secret), hashlib.sha256, 'base64')
  2589. headers = {
  2590. 'ACCESS-KEY': self.apiKey,
  2591. 'ACCESS-SIGN': signature,
  2592. 'ACCESS-TIMESTAMP': timestamp,
  2593. 'ACCESS-PASSPHRASE': self.password,
  2594. }
  2595. if method == 'POST':
  2596. headers['Content-Type'] = 'application/json'
  2597. elif api == 'api':
  2598. timestamp = str(self.milliseconds())
  2599. auth = ''
  2600. query = self.keysort(query)
  2601. auth = self.rawencode(query)
  2602. hash = self.hash(self.encode(self.secret), 'sha1')
  2603. signed = auth
  2604. signature = self.hmac(self.encode(auth), self.encode(hash), hashlib.md5)
  2605. if len(auth) > 0:
  2606. signed += '&'
  2607. signed += 'sign=' + signature + '&req_time=' + timestamp + '&accesskey=' + self.apiKey
  2608. if method == 'GET':
  2609. if query:
  2610. url += '?' + signed
  2611. elif method == 'POST':
  2612. url += '?sign=' + signature + '&req_time=' + timestamp + '&accesskey=' + self.apiKey
  2613. body = auth
  2614. headers = {
  2615. 'Content-Type': 'application/x-www-form-urlencoded',
  2616. }
  2617. return {'url': url, 'method': method, 'body': body, 'headers': headers}
  2618. def handle_errors(self, code, reason, url, method, headers, body, response, requestHeaders, requestBody):
  2619. if not response:
  2620. return # fallback to default error handler
  2621. #
  2622. # spot
  2623. #
  2624. # {"status":"fail","err_code":"01001","err_msg":"系统异常,请稍后重试"}
  2625. # {"status":"error","ts":1595594160149,"err_code":"invalid-parameter","err_msg":"invalid size, valid range: [1,2000]"}
  2626. # {"status":"error","ts":1595684716042,"err_code":"invalid-parameter","err_msg":"illegal sign invalid"}
  2627. # {"status":"error","ts":1595700216275,"err_code":"bad-request","err_msg":"your balance is low!"}
  2628. # {"status":"error","ts":1595700344504,"err_code":"invalid-parameter","err_msg":"invalid type"}
  2629. # {"status":"error","ts":1595703343035,"err_code":"bad-request","err_msg":"order cancel fail"}
  2630. # {"status":"error","ts":1595704360508,"err_code":"invalid-parameter","err_msg":"accesskey not null"}
  2631. # {"status":"error","ts":1595704490084,"err_code":"invalid-parameter","err_msg":"permissions not right"}
  2632. # {"status":"error","ts":1595711862763,"err_code":"system exception","err_msg":"system exception"}
  2633. # {"status":"error","ts":1595730308979,"err_code":"bad-request","err_msg":"20003"}
  2634. #
  2635. # swap
  2636. #
  2637. # {"code":"40015","msg":"","requestTime":1595698564931,"data":null}
  2638. # {"code":"40017","msg":"Order id must not be blank","requestTime":1595702477835,"data":null}
  2639. # {"code":"40017","msg":"Order Type must not be blank","requestTime":1595698516162,"data":null}
  2640. # {"code":"40301","msg":"","requestTime":1595667662503,"data":null}
  2641. # {"code":"40017","msg":"Contract code must not be blank","requestTime":1595703151651,"data":null}
  2642. # {"code":"40108","msg":"","requestTime":1595885064600,"data":null}
  2643. # {"order_id":"513468410013679613","client_oid":null,"symbol":"ethusd","result":false,"err_code":"order_no_exist_error","err_msg":"订单不存在!"}
  2644. #
  2645. message = self.safe_string(response, 'err_msg')
  2646. errorCode = self.safe_string_2(response, 'code', 'err_code')
  2647. feedback = self.id + ' ' + body
  2648. nonEmptyMessage = ((message is not None) and (message != ''))
  2649. if nonEmptyMessage:
  2650. self.throw_exactly_matched_exception(self.exceptions['exact'], message, feedback)
  2651. self.throw_broadly_matched_exception(self.exceptions['broad'], message, feedback)
  2652. nonZeroErrorCode = (errorCode is not None) and (errorCode != '00000')
  2653. if nonZeroErrorCode:
  2654. self.throw_exactly_matched_exception(self.exceptions['exact'], errorCode, feedback)
  2655. if nonZeroErrorCode or nonEmptyMessage:
  2656. raise ExchangeError(feedback) # unknown message