ccxt 4.2.73 → 4.2.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/README.md +5 -5
  2. package/build.sh +1 -1
  3. package/dist/ccxt.browser.js +181 -100
  4. package/dist/ccxt.browser.min.js +2 -2
  5. package/dist/cjs/ccxt.js +1 -1
  6. package/dist/cjs/src/base/Exchange.js +1 -1
  7. package/dist/cjs/src/bitget.js +62 -50
  8. package/dist/cjs/src/bybit.js +90 -30
  9. package/dist/cjs/src/coinbaseinternational.js +1 -1
  10. package/dist/cjs/src/htx.js +16 -9
  11. package/dist/cjs/src/hyperliquid.js +2 -1
  12. package/dist/cjs/src/pro/krakenfutures.js +8 -7
  13. package/examples/README.md +308 -0
  14. package/examples/ccxt.pro/html/watchTicker.html +51 -0
  15. package/examples/ccxt.pro/js/binance-fetch-balance-snapshot-watch-balance-updates.js +55 -0
  16. package/examples/ccxt.pro/js/binance-https-proxy.js +48 -0
  17. package/examples/ccxt.pro/js/binance-watch-ohlcv-many-symbols-continuously.js +38 -0
  18. package/examples/ccxt.pro/js/binance-watch-ohlcv-many-symbols.js +64 -0
  19. package/examples/ccxt.pro/js/binance-watch-ticker-many-symbols.js +62 -0
  20. package/examples/ccxt.pro/js/build-ohlcv-many-symbols.js +68 -0
  21. package/examples/ccxt.pro/js/calculate-ohlcvs-from-trades-warmup.js +82 -0
  22. package/examples/ccxt.pro/js/calculate-ohlcvs-from-trades.js +59 -0
  23. package/examples/ccxt.pro/js/exchange-capabitities.js +72 -0
  24. package/examples/ccxt.pro/js/exchange-close.js +46 -0
  25. package/examples/ccxt.pro/js/gateio-swap-watch-many-orderbooks.js +30 -0
  26. package/examples/ccxt.pro/js/gateio-watch-balance.js +23 -0
  27. package/examples/ccxt.pro/js/gateio-watch-order-book.js +71 -0
  28. package/examples/ccxt.pro/js/graceful-shutdown.js +43 -0
  29. package/examples/ccxt.pro/js/many-exchanges-many-streams.js +37 -0
  30. package/examples/ccxt.pro/js/okex-create-futures-order.js +48 -0
  31. package/examples/ccxt.pro/js/okex-watch-balance-and-create-order.js +54 -0
  32. package/examples/ccxt.pro/js/okx-watch-tickers.js +31 -0
  33. package/examples/ccxt.pro/js/one-exchange-many-different-streams.js +46 -0
  34. package/examples/ccxt.pro/js/one-exchange-many-streams-2.js +26 -0
  35. package/examples/ccxt.pro/js/one-exchange-many-streams.js +28 -0
  36. package/examples/ccxt.pro/js/socks-binance-watch-orderbook.js +30 -0
  37. package/examples/ccxt.pro/js/watch-fetch-many-exchanges-many-ordersbooks.js +40 -0
  38. package/examples/ccxt.pro/js/watch-many-exchanges-many-ordersbooks.js +38 -0
  39. package/examples/ccxt.pro/js/watch-many-exchanges-many-symbols.js +40 -0
  40. package/examples/ccxt.pro/js/watch-many-orderbooks.js +27 -0
  41. package/examples/ccxt.pro/js/watch-new-trades-only.js +43 -0
  42. package/examples/ccxt.pro/js/watch-new-trades.js +42 -0
  43. package/examples/ccxt.pro/js/watch-trades-many-symbols.js +29 -0
  44. package/examples/ccxt.pro/js/watch-vs-fetch.js +29 -0
  45. package/examples/cs/Examples.sln +22 -0
  46. package/examples/cs/c#.csproj +19 -0
  47. package/examples/html/basic-cors-proxy.html +46 -0
  48. package/examples/html/basic-inheritance.html +47 -0
  49. package/examples/html/basic-poller.html +52 -0
  50. package/examples/html/basic-rate-limiting.html +49 -0
  51. package/examples/html/basic.html +43 -0
  52. package/examples/html/binance-cors-proxy.html +28 -0
  53. package/examples/html/bitmex-browser-cors-proxy.js +7 -0
  54. package/examples/html/bitmex-cors.html +46 -0
  55. package/examples/html/tradingview-charts.html +61 -0
  56. package/examples/html/webworker/index.html +97 -0
  57. package/examples/html/webworker/worker.js +43 -0
  58. package/examples/js/README.md +15 -0
  59. package/examples/js/advanced-error-handling.js +39 -0
  60. package/examples/js/aggregate-orderbook.js +54 -0
  61. package/examples/js/arbitrage-pairs.js +130 -0
  62. package/examples/js/basic-chart.js +29 -0
  63. package/examples/js/basic-orderbook-polling.js +13 -0
  64. package/examples/js/bcc-vs-bch.js +115 -0
  65. package/examples/js/binance-fetch-all-deposits.js +45 -0
  66. package/examples/js/binance-fetch-ohlcv-many-symbols-async-await.js +34 -0
  67. package/examples/js/binance-fetch-ohlcv-many-symbols-promise-then-callbacks.js +33 -0
  68. package/examples/js/binance-fetchTicker-delivery-vs-future.js +31 -0
  69. package/examples/js/binance-futures-transfer-from-sub-account-to-master.js +41 -0
  70. package/examples/js/binance-margin-stop-order.js +39 -0
  71. package/examples/js/binance-server-time.js +34 -0
  72. package/examples/js/binance-universal-transfer.js +16 -0
  73. package/examples/js/bitfinex-fetch-trades.js +35 -0
  74. package/examples/js/bitfinex2-fetch-trades.js +35 -0
  75. package/examples/js/bitmex-browser-cors-proxy.js +7 -0
  76. package/examples/js/bitpanda-fetchMyTrades-reduce.js +36 -0
  77. package/examples/js/bitrue-fetch-balance.js +28 -0
  78. package/examples/js/bitstamp-private-api.js +115 -0
  79. package/examples/js/bitstamp-public-api.js +39 -0
  80. package/examples/js/bittrex-balance.js +50 -0
  81. package/examples/js/bittrex-fetch-closed-orders-history.js +69 -0
  82. package/examples/js/blockchaincom-withdrawal.js +59 -0
  83. package/examples/js/build-ohlcv-bars.js +48 -0
  84. package/examples/js/builtin-rate-limiting-rest-poller.js +25 -0
  85. package/examples/js/bybit-trailing.js +62 -0
  86. package/examples/js/bybit-updated.cjs +154 -0
  87. package/examples/js/cli.js +405 -0
  88. package/examples/js/coinbase-fetch-all-balances.js +36 -0
  89. package/examples/js/coinex-fetch-all-deposit-addresses-using-fetchDepositAddress.js +47 -0
  90. package/examples/js/coinex-futures.js +75 -0
  91. package/examples/js/coinone-fetch-tickers.js +54 -0
  92. package/examples/js/coinone-markets.js +16 -0
  93. package/examples/js/compare-two-exchanges-capabilities.js +36 -0
  94. package/examples/js/cors-proxy.js +5 -0
  95. package/examples/js/create-order-handle-errors.js +55 -0
  96. package/examples/js/create-order-position-with-takeprofit-stoploss.js +71 -0
  97. package/examples/js/create-order-with-retry.js +65 -0
  98. package/examples/js/create-order-ws-example.js +25 -0
  99. package/examples/js/create-orders-example.js +17 -0
  100. package/examples/js/create-trailing-amount-order.js +36 -0
  101. package/examples/js/create-trailing-percent-order.js +36 -0
  102. package/examples/js/credentials.json +5 -0
  103. package/examples/js/custom-proxy-agent-for-js.js +10 -0
  104. package/examples/js/custom-proxy-url.js +23 -0
  105. package/examples/js/delta-maintenance-margin-rate-max-leverage.js +60 -0
  106. package/examples/js/env-variables.js +26 -0
  107. package/examples/js/error-handling.js +89 -0
  108. package/examples/js/exchange-capabilities.js +135 -0
  109. package/examples/js/exchanges-by-volume.js +60 -0
  110. package/examples/js/exchanges.js +40 -0
  111. package/examples/js/fetch-all-balances.js +219 -0
  112. package/examples/js/fetch-all-tickers-to-files-2.js +53 -0
  113. package/examples/js/fetch-all-tickers-to-files.js +77 -0
  114. package/examples/js/fetch-balance.js +28 -0
  115. package/examples/js/fetch-create-deposit-address.js +101 -0
  116. package/examples/js/fetch-from-many-exchanges-simultaneously.js +21 -0
  117. package/examples/js/fetch-funding-rate-history.js +25 -0
  118. package/examples/js/fetch-futures/prettier.config.js +5 -0
  119. package/examples/js/fetch-futures/src/index.js +25 -0
  120. package/examples/js/fetch-ohlcv-from-to-mark-index-premium.js +72 -0
  121. package/examples/js/fetch-ohlcv-many-exchanges-continuosly.js +39 -0
  122. package/examples/js/fetch-ohlcv.js +16 -0
  123. package/examples/js/fetch-okex-futures.js +22 -0
  124. package/examples/js/fetch-orders.js +27 -0
  125. package/examples/js/fetch-ticker-from-multiple-exchanges.js +35 -0
  126. package/examples/js/fetch-ticker-where-available.js +75 -0
  127. package/examples/js/fetch-tickers/build/index.js +19 -0
  128. package/examples/js/fetch-tickers/prettier.config.js +5 -0
  129. package/examples/js/fetch-tickers/src/index.js +17 -0
  130. package/examples/js/gateio-create-batch-order.js +43 -0
  131. package/examples/js/gateio-futures.js +49 -0
  132. package/examples/js/gateio-open-close-contract.js +49 -0
  133. package/examples/js/gateio-swaps.js +74 -0
  134. package/examples/js/gdax-fetch-trades-pagination.js +29 -0
  135. package/examples/js/hitbtc2-withdraw.js +61 -0
  136. package/examples/js/how-to-import-one-exchange-esm.js +10 -0
  137. package/examples/js/huobi-futures.js +71 -0
  138. package/examples/js/huobi-open-close-contract.js +63 -0
  139. package/examples/js/huobi-swaps.js +70 -0
  140. package/examples/js/huobipro-market-buy-sell-fetch-trading-limits.js +98 -0
  141. package/examples/js/hybridCJSExample.cjs +19 -0
  142. package/examples/js/hybridESMExample.js +19 -0
  143. package/examples/js/idex-fetch-balance.js +13 -0
  144. package/examples/js/instantiate-all-at-once.js +46 -0
  145. package/examples/js/instantiate-all-from-json.js +31 -0
  146. package/examples/js/kraken-create-and-close-position.js +54 -0
  147. package/examples/js/kraken-fetch-order-trades.js +33 -0
  148. package/examples/js/kraken-margin-trading.js +89 -0
  149. package/examples/js/kucoin-rate-limit.js +38 -0
  150. package/examples/js/latoken-example.js +108 -0
  151. package/examples/js/live-orderbook.js +106 -0
  152. package/examples/js/live-ticker.js +80 -0
  153. package/examples/js/live-tickers.js +74 -0
  154. package/examples/js/load-all-contracts.js +41 -0
  155. package/examples/js/load-all-symbols-at-once.js +69 -0
  156. package/examples/js/load-all-tickers-at-once.js +91 -0
  157. package/examples/js/load-markets-to-files.js +57 -0
  158. package/examples/js/looping-over-all-symbols-of-specific-exchanges.js +61 -0
  159. package/examples/js/looping-over-specific-symbols-of-all-exchanges.js +91 -0
  160. package/examples/js/margin-loan-borrow-buy-sell-repay.js +70 -0
  161. package/examples/js/market-status-and-currency-status.js +29 -0
  162. package/examples/js/ohlcv-console-chart.js +29 -0
  163. package/examples/js/okex-fetch-closed-orders-archive.js +31 -0
  164. package/examples/js/okex-transfer.js +51 -0
  165. package/examples/js/okx-poll-fetch-my-trades.js +37 -0
  166. package/examples/js/okx-poll-rate-limit.js +48 -0
  167. package/examples/js/order-book-extra-level-depth-param.js +20 -0
  168. package/examples/js/phemex-create-order-position-with-takeprofit-stoploss.js +49 -0
  169. package/examples/js/poll-ohlcv.js +43 -0
  170. package/examples/js/poloniex-fetch-order-books.js +35 -0
  171. package/examples/js/poloniex-limits-amount-min.js +62 -0
  172. package/examples/js/proxy-round-robin.js +98 -0
  173. package/examples/js/proxy-usage.js +32 -0
  174. package/examples/js/sample-local-proxy-server-with-cors.js +12 -0
  175. package/examples/js/search-all-exchanges.js +159 -0
  176. package/examples/js/shared-load-markets.js +80 -0
  177. package/examples/js/sort-swap-markets-by-hourly-price-change.js +55 -0
  178. package/examples/js/symbols.js +110 -0
  179. package/examples/js/theocean.js +41 -0
  180. package/examples/js/tickers.js +106 -0
  181. package/examples/js/validate-paginated-data.js +61 -0
  182. package/examples/js/watch-OHLCV-For-Symbols.js +15 -0
  183. package/examples/js/watch-OHLCV.js +12 -0
  184. package/examples/js/watch-OrderBook-For-Symbols.js +11 -0
  185. package/examples/js/watch-Trades-For-Symbols.js +11 -0
  186. package/examples/js/watch-tickers.js +11 -0
  187. package/examples/js/watchOHLCVForSymbols.js +15 -0
  188. package/examples/js/watchOrderBookForSymbols.js +11 -0
  189. package/examples/js/watchPositions-many-exchanges-continuosly.d.ts +2 -0
  190. package/examples/js/watchPositions-many-exchanges-continuosly.d.ts.map +1 -0
  191. package/examples/js/watchPositions-many-exchanges-continuosly.js +49 -0
  192. package/examples/js/watchPositions.d.ts +2 -0
  193. package/examples/js/watchPositions.d.ts.map +1 -0
  194. package/examples/js/watchPositions.js +13 -0
  195. package/examples/js/watchPositionsForSymbols.d.ts +2 -0
  196. package/examples/js/watchPositionsForSymbols.d.ts.map +1 -0
  197. package/examples/js/watchPositionsForSymbols.js +14 -0
  198. package/examples/js/watchTradesForSymbols.js +11 -0
  199. package/examples/js/withdraw-from-one-exchange-to-another.js +50 -0
  200. package/examples/php/README.md +7 -0
  201. package/examples/py/README.md +15 -0
  202. package/examples/py/playing_with_ccxt_example.ipynb +222 -0
  203. package/examples/ts/.eslintrc +111 -0
  204. package/examples/ts/build-ohlcv-bars.ts +53 -0
  205. package/examples/ts/cli.ts +397 -0
  206. package/examples/ts/compare-two-exchanges-capabilities.ts +36 -0
  207. package/examples/ts/create-order-position-with-takeprofit-stoploss.ts +89 -0
  208. package/examples/ts/create-order-ws-example.ts +33 -0
  209. package/examples/ts/create-orders-example.ts +21 -0
  210. package/examples/ts/create-trailing-amount-order.ts +37 -0
  211. package/examples/ts/create-trailing-percent-order.ts +37 -0
  212. package/examples/ts/custom-proxy-agent-for-js.ts +14 -0
  213. package/examples/ts/fetch-futures/package-lock.json +116 -0
  214. package/examples/ts/fetch-futures/package.json +34 -0
  215. package/examples/ts/fetch-futures/prettier.config.js +4 -0
  216. package/examples/ts/fetch-futures/src/index.ts +28 -0
  217. package/examples/ts/fetch-futures/tsconfig.json +28 -0
  218. package/examples/ts/fetch-ohlcv-many-exchanges-continuosly.ts +44 -0
  219. package/examples/ts/fetch-ohlcv.ts +17 -0
  220. package/examples/ts/fetch-tickers/package-lock.json +116 -0
  221. package/examples/ts/fetch-tickers/package.json +34 -0
  222. package/examples/ts/fetch-tickers/prettier.config.js +4 -0
  223. package/examples/ts/fetch-tickers/src/index.ts +21 -0
  224. package/examples/ts/fetch-tickers/tsconfig.json +28 -0
  225. package/examples/ts/how-to-import-one-exchange-esm.ts +11 -0
  226. package/examples/ts/kraken-create-and-close-position.ts +69 -0
  227. package/examples/ts/margin-loan-borrow-buy-sell-repay.ts +72 -0
  228. package/examples/ts/nextjs-page-router/.eslintrc.json +3 -0
  229. package/examples/ts/nextjs-page-router/README.md +43 -0
  230. package/examples/ts/nextjs-page-router/next.config.js +6 -0
  231. package/examples/ts/nextjs-page-router/package-lock.json +7425 -0
  232. package/examples/ts/nextjs-page-router/package.json +28 -0
  233. package/examples/ts/nextjs-page-router/postcss.config.js +6 -0
  234. package/examples/ts/nextjs-page-router/public/favicon.ico +0 -0
  235. package/examples/ts/nextjs-page-router/src/pages/_app.tsx +6 -0
  236. package/examples/ts/nextjs-page-router/src/pages/_document.tsx +13 -0
  237. package/examples/ts/nextjs-page-router/src/pages/balance.tsx +46 -0
  238. package/examples/ts/nextjs-page-router/src/pages/index.tsx +8 -0
  239. package/examples/ts/nextjs-page-router/src/pages/tickers.tsx +61 -0
  240. package/examples/ts/nextjs-page-router/src/styles/globals.css +27 -0
  241. package/examples/ts/nextjs-page-router/tailwind.config.ts +20 -0
  242. package/examples/ts/nextjs-page-router/tsconfig.json +22 -0
  243. package/examples/ts/phemex-create-order-position-with-takeprofit-stoploss.ts +62 -0
  244. package/examples/ts/proxy-usage.ts +41 -0
  245. package/examples/ts/sample-local-proxy-server-with-cors.ts +15 -0
  246. package/examples/ts/watch-OHLCV-For-Symbols.ts +17 -0
  247. package/examples/ts/watch-OHLCV.ts +14 -0
  248. package/examples/ts/watch-OrderBook-For-Symbols.ts +13 -0
  249. package/examples/ts/watch-Trades-For-Symbols.ts +13 -0
  250. package/examples/ts/watch-tickers.ts +13 -0
  251. package/examples/ts/watchPositions-many-exchanges-continuosly.ts +53 -0
  252. package/examples/ts/watchPositions.ts +15 -0
  253. package/examples/ts/watchPositionsForSymbols.ts +16 -0
  254. package/examples/tsconfig.json +27 -0
  255. package/js/ccxt.d.ts +1 -1
  256. package/js/ccxt.js +1 -1
  257. package/js/src/base/Exchange.d.ts +2 -2
  258. package/js/src/base/Exchange.js +1 -1
  259. package/js/src/bitget.js +62 -50
  260. package/js/src/bybit.d.ts +3 -1
  261. package/js/src/bybit.js +90 -30
  262. package/js/src/coinbaseinternational.js +1 -1
  263. package/js/src/htx.js +16 -9
  264. package/js/src/hyperliquid.js +2 -1
  265. package/js/src/pro/krakenfutures.js +8 -7
  266. package/package.json +4 -1
  267. package/skip-tests.json +214 -424
@@ -0,0 +1,91 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+ import asTable from 'as-table';
5
+ import ololog from 'ololog';
6
+ import ansicolor from 'ansicolor';
7
+
8
+ const log = ololog.configure ({ locate: false }), verbose = process.argv.includes ('--verbose');
9
+
10
+ ansicolor.nice
11
+
12
+ //-----------------------------------------------------------------------------
13
+
14
+ ;(async () => {
15
+
16
+ const exchanges = {};
17
+ const symbols = [ 'ETH/BTC', 'XRP/BTC', 'BTC/USDT' ]
18
+
19
+ for (let symbol of symbols) {
20
+
21
+ for (let exchangeId of ccxt.exchanges) {
22
+
23
+ let exchange = undefined
24
+
25
+ try { // try creating the exchange instance first and handle errors if any
26
+
27
+ // check if we have created an instance of this exchange already
28
+
29
+ exchange = exchanges[exchangeId]
30
+
31
+ if (exchange === undefined) {
32
+
33
+ // create the exchange instance
34
+ exchange = new ccxt[exchangeId] ()
35
+ }
36
+
37
+ exchanges[exchangeId] = exchange // save it for later use
38
+
39
+ } catch (e) {
40
+
41
+ log.red ('Could not create exchange', exchangeId + ':', e.constructor.name, e.message)
42
+
43
+ // uncomment the following line to interrupt program execution on error
44
+ // or leave it commented out to do nothing
45
+
46
+ // process.exit ()
47
+
48
+ }
49
+
50
+ if (exchange !== undefined) {
51
+
52
+ // preload all markets first, as explained in the Manual:
53
+ // https://github.com/ccxt/ccxt/wiki/Manual#loading-markets
54
+
55
+ // add error/exception handling as required by the Manual:
56
+ // https://github.com/ccxt/ccxt/wiki/Manual#error-handling
57
+
58
+ try {
59
+
60
+ await exchange.loadMarkets ();
61
+
62
+ } catch (e) {
63
+
64
+ log.red ('Could not load markets from', exchange.id + ':', e.constructor.name, e.message)
65
+ continue; // skip this exchange if markets failed to load
66
+
67
+ }
68
+
69
+ for (let symbol of symbols) {
70
+
71
+ console.log (exchange.id, symbol)
72
+
73
+ // add error/exception handling as required by the Manual:
74
+ // https://github.com/ccxt/ccxt/wiki/Manual#error-handling
75
+
76
+ try { // try fetching the ticker for a symbol existing with that exchange
77
+
78
+ const ticker = await exchange.fetchTicker (symbol)
79
+ log.green (ticker)
80
+
81
+ } catch (e) { // catch the error (if any) and handle it or ignore it
82
+
83
+ log.red ('Could not fetch', symbol, 'ticker from', exchange.id + ':', e.constructor.name, e.message)
84
+
85
+ }
86
+ }
87
+ }
88
+ }
89
+ }
90
+
91
+ }) ()
@@ -0,0 +1,70 @@
1
+ import ccxt from '../../js/ccxt.js';
2
+ // AUTO-TRANSPILE //
3
+ // Note, this is just an example and might not yet work on other exchanges, which are being still unified.
4
+ async function example() {
5
+ // ########## user inputs ##########
6
+ const exchange = new ccxt['binance']({ 'apiKey': 'xxx', 'secret': 'xxx' });
7
+ const symbol = 'BUSD/USDT'; // set target symbol
8
+ const marginMode = 'isolated'; // margin mode (cross or isolated)
9
+ const collateral_coin = 'USDT'; // which asset you want to use for margin-borrow collateral
10
+ const borrow_coin = 'BUSD'; // which coin to borrow
11
+ const order_side = 'sell'; // which side to trade
12
+ const amount_to_trade = 14; // how many coins to sell
13
+ const order_type = 'limit'; // order type (can be market, limit or etc)
14
+ const limit_price = 0.99; // price to sell at (set undefined/null/None if market-order)
15
+ const margin_magnitude = 5; // target margin (aka 'leverage'). This might also be obtainable using other unified methods, but for example purposes, we set here manually
16
+ // ########## end of user-inputs ##########
17
+ //
18
+ // for example purposes, let's also check available balance at first
19
+ const balance_margin = await exchange.fetchBalance({ 'defaultType': 'margin', 'marginMode': marginMode }); // use `defaultType` because of temporary bug, otherwise, after several days, you can use `type` too.
20
+ // if we don't have enought coins, then we have to borrow at first
21
+ let needed_amount_to_borrow = undefined; // will be auto-set below
22
+ if (amount_to_trade > balance_margin[symbol][borrow_coin]['free']) {
23
+ needed_amount_to_borrow = amount_to_trade - balance_margin[symbol][borrow_coin]['free'];
24
+ console.log('hmm, I have only ', balance_margin[symbol][borrow_coin]['free'], ' ', borrow_coin, ' in margin balance, and still need additional ', needed_amount_to_borrow, ' to make an order. Lets borrow it.');
25
+ // To initate a borrow, at first, check if we have enough collateral (for this example, as we make a sell-short, we need '-1' to keep for collateral currency)
26
+ const needed_collateral_amount = needed_amount_to_borrow / (margin_magnitude - 1);
27
+ // Check if we have any collateral to get permission for borrow
28
+ if (balance_margin[symbol][collateral_coin]['free'] < needed_collateral_amount) {
29
+ // If we don't have enough collateral, then let's try to transfer collateral-asset from spot-balance to margin-balance
30
+ console.log('hmm, I have only ', balance_margin[symbol][collateral_coin]['free'], ' in balance, but ', needed_collateral_amount, ' collateral is needed. I should transfer ', needed_collateral_amount, ' from spot');
31
+ // let's check if we have spot balance at all
32
+ const balance_spot = await exchange.fetchBalance({ 'type': 'spot' });
33
+ if (exchange.parseNumber(balance_spot[collateral_coin]['free']) < needed_collateral_amount) {
34
+ console.log('hmm, I neither do have enough balance on spot - only ', balance_spot[collateral_coin]['free'], '. Script can not continue...');
35
+ return;
36
+ }
37
+ else {
38
+ console.log('Transferring ', needed_collateral_amount, ' to margin account');
39
+ await exchange.transfer(collateral_coin, needed_collateral_amount, 'spot', marginMode, { 'symbol': symbol });
40
+ }
41
+ }
42
+ // now, as we have enough margin collateral, initiate borrow
43
+ console.log('Initiating margin borrow of ', needed_amount_to_borrow, ' ', borrow_coin);
44
+ const borrowResult = await exchange.borrowMargin(borrow_coin, needed_amount_to_borrow, symbol, { 'marginMode': marginMode });
45
+ }
46
+ console.log('Submitting order.');
47
+ const order = await exchange.createOrder(symbol, order_type, order_side, amount_to_trade, limit_price, { 'marginMode': marginMode });
48
+ console.log('Order was submitted !', order['id']);
49
+ //
50
+ //
51
+ // ...
52
+ // ...
53
+ // some time later, if you want to repay the loan back (like 'close the position')...
54
+ // ...
55
+ // ...
56
+ //
57
+ //
58
+ // set the "repay-back" amount (for this example snippet, this will be same amount that we borrowed above)
59
+ if (needed_amount_to_borrow !== undefined) {
60
+ const amount_to_repay_back = needed_amount_to_borrow;
61
+ // At first, you need to get back the borrowed coin, by making an opposide trade
62
+ console.log('Making purchase back of ', amount_to_repay_back, ' ', borrow_coin, ' to repay it back.');
63
+ const purchase_back_price = 1.01;
64
+ const order_back = await exchange.createOrder(symbol, order_type, (order_side === 'buy' ? 'sell' : 'buy'), amount_to_repay_back, purchase_back_price, { 'marginMode': marginMode });
65
+ console.log('Now, repaying the loan.');
66
+ const repayResult = await exchange.repayMargin(borrow_coin, amount_to_repay_back, symbol, { 'marginMode': marginMode });
67
+ console.log('finished.');
68
+ }
69
+ }
70
+ await example();
@@ -0,0 +1,29 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+ import log from 'ololog';
5
+ import asTable from 'as-table';
6
+
7
+ (async function main () {
8
+
9
+ let kraken = new ccxt.kraken ()
10
+ await kraken.loadMarkets ()
11
+
12
+ const markets = Object.values (kraken.markets).map (market => ({
13
+ symbol: market.symbol,
14
+ active: market.active,
15
+ }))
16
+
17
+ log.bright.green.noLocate ('Markets:')
18
+ log.green.noLocate (asTable (markets), '\n')
19
+
20
+ const currencies = Object.values (kraken.currencies).map (currency => ({
21
+ code: currency.code,
22
+ active: currency.active,
23
+ status: currency.status,
24
+ }))
25
+
26
+ log.bright.yellow.noLocate ('Currencies:')
27
+ log.yellow.noLocate (asTable (currencies))
28
+
29
+ }) ()
@@ -0,0 +1,29 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+ import asciichart from 'asciichart';
5
+ import asTable from 'as-table';
6
+ import ololog from 'ololog'
7
+ import ansicolor from 'ansicolor';
8
+
9
+ const log = ololog.configure ({ locate: false })
10
+
11
+ ansicolor.nice
12
+
13
+ //-----------------------------------------------------------------------------
14
+
15
+ ;(async function main () {
16
+
17
+ // experimental, not yet implemented for all exchanges
18
+ // your contributions are welcome ;)
19
+
20
+ const indexOfClose = 4 // [ timestamp, open, high, low, close, volume ]
21
+ const ohlcv = await new ccxt.cex ().fetchOHLCV ('BTC/USD', '1m')
22
+ const lastPrice = ohlcv[ohlcv.length - 1][indexOfClose] // closing price
23
+ const plotSeriesData = ohlcv.slice (-80).map (x => x[indexOfClose]) // closing price
24
+ const bitcoinRate = ('₿ = $' + lastPrice).green
25
+ const chart = asciichart.plot (plotSeriesData, { height: 15, padding: ' ' })
26
+ log.yellow ("\n" + chart, bitcoinRate, "\n")
27
+ process.exit ()
28
+
29
+ }) ()
@@ -0,0 +1,31 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+
5
+ console.log ('CCXT Version:', ccxt.version)
6
+
7
+ // https://github.com/ccxt/ccxt/issues/10179
8
+
9
+ async function main () {
10
+
11
+ const exchange = new ccxt.okex ({
12
+ 'apiKey': 'YOUR_API_KEY',
13
+ 'secret': 'YOUR_SECRET',
14
+ 'password': 'YOUR_PASSWORD',
15
+ 'options': {
16
+ 'fetchClosedOrders': {
17
+ 'method': 'privateGetTradeOrdersHistoryArchive'
18
+ }
19
+ }
20
+ })
21
+
22
+ const markets = await exchange.loadMarkets ()
23
+
24
+ exchange.verbose = true
25
+
26
+ const orders = await exchange.fetchClosedOrders ()
27
+ console.log (orders)
28
+
29
+ }
30
+
31
+ main ()
@@ -0,0 +1,51 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+
5
+ console.log ('CCXT version:', ccxt.version)
6
+ console.log ('This example requires CCXT version 1.54.92 or higher')
7
+
8
+ async function main () {
9
+
10
+ const exchange = new ccxt.okex ({
11
+ 'apiKey': 'YOUR_API_KEY',
12
+ 'secret': 'YOUR_SECRET',
13
+ 'password': 'YOUR_API_KEY_PASSWORD',
14
+ })
15
+
16
+ await exchange.loadMarkets ()
17
+
18
+ const code = 'USDT'
19
+ let tradingBalance = await exchange.fetchFreeBalance (/* { 'type': 'trading' } */)
20
+ let fundingBalance = await exchange.fetchFreeBalance ({ 'type': 'funding' })
21
+ console.log ('Trading:', tradingBalance[code], code)
22
+ console.log ('Funding:', fundingBalance[code], code)
23
+
24
+ const oldVerboseMode = exchange.verbose
25
+ exchange.verbose = process.argv.includes ('--verbose') || process.argv.includes ('-v')
26
+
27
+ // https://www.okex.com/docs-v5/en/#rest-api-funding-funds-transfer
28
+ //
29
+ // 'spot' == '1'
30
+ // 'futures' == '3',
31
+ // 'margin' == '5',
32
+ // 'swap' == '9',
33
+ // 'option' == '12',
34
+ // 'trading' == '18', // unified trading account
35
+ // 'unified' == '18',
36
+ //
37
+ const from = 'trading'
38
+ const to = 'funding'
39
+ const amount = 1
40
+ const transfer = await exchange.transfer (code, amount, from, to)
41
+ console.log (transfer)
42
+
43
+ exchange.verbose = oldVerboseMode
44
+
45
+ tradingBalance = await exchange.fetchFreeBalance (/* { 'type': 'spot' } */)
46
+ fundingBalance = await exchange.fetchFreeBalance ({ 'type': 'funding' })
47
+ console.log ('Trading:', tradingBalance[code], code)
48
+ console.log ('Funding:', fundingBalance[code], code)
49
+ }
50
+
51
+ main ()
@@ -0,0 +1,37 @@
1
+ import ccxt from '../../js/ccxt.js'
2
+
3
+ console.log ('CCXT Version:', ccxt.version)
4
+
5
+ async function main () {
6
+
7
+ const exchange = new ccxt.okx ({
8
+
9
+ // edit for your credentials
10
+ 'apiKey': 'YOUR_API_KEY',
11
+ 'secret': 'YOUR_API_SECRET',
12
+ 'password': 'YOUR_API_PASSWORD',
13
+ })
14
+
15
+ await exchange.loadMarkets ()
16
+
17
+ // if this script fails with a rate limiter error
18
+ // uncomment the following line for debugging purposes
19
+
20
+ // exchange.verbose = true
21
+
22
+ while (true) {
23
+
24
+ try {
25
+
26
+ const trades = await exchange.fetchMyTrades ()
27
+ console.log (new Date(), 'fetched', trades.length, 'trades')
28
+
29
+ } catch (e) {
30
+
31
+ console.log (e.constructor.name, e.message)
32
+ break;
33
+ }
34
+ }
35
+ }
36
+
37
+ main ()
@@ -0,0 +1,48 @@
1
+ import ccxt from '../../js/ccxt.js'
2
+
3
+ console.log ('CCXT Version:', ccxt.version)
4
+
5
+ async function main () {
6
+
7
+ const exchange = new ccxt.okx ({
8
+
9
+ // edit for your credentials
10
+ 'apiKey': 'YOUR_API_KEY',
11
+ 'secret': 'YOUR_API_SECRET',
12
+ 'password': 'YOUR_API_PASSWORD',
13
+
14
+ 'api': {
15
+ 'private': {
16
+ 'get': {
17
+ 'trade/fills-history': 2.2,
18
+ },
19
+ },
20
+ },
21
+
22
+ })
23
+
24
+ await exchange.loadMarkets ()
25
+
26
+ // if this script fails with a rate limiter error
27
+ // uncomment the following line for debugging purposes
28
+
29
+ // exchange.verbose = true
30
+
31
+ const promises=[];
32
+ for(let i=0;i<100;i++){
33
+ promises.push(exchange.fetchMyTrades());
34
+ }
35
+
36
+ const allResponses = await Promise.allSettled(promises);
37
+ allResponses.forEach((result, i) => {
38
+
39
+ if(result.status == "fulfilled"){
40
+ console.log (new Date(), i + 6, 'fetched', result.value.length, 'trades')
41
+ } else {
42
+ console.log ("Rejected:", i + 6, result.reason);
43
+ }
44
+ });
45
+
46
+ }
47
+
48
+ main ()
@@ -0,0 +1,20 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+ import asTable from 'as-table';
5
+ import log from 'ololog';
6
+ import ansicolor from 'ansicolor';
7
+
8
+ ansicolor.nice
9
+
10
+ ;(async function test () {
11
+
12
+ const exchange = new ccxt.bitfinex ()
13
+ const limit = 5
14
+ const orders = await exchange.fetchOrderBook ('BTC/USD', limit, {
15
+ // this parameter is exchange-specific, all extra params have unique names per exchange
16
+ 'group': 1, // 1 = orders are grouped by price, 0 = orders are separate
17
+ })
18
+
19
+ log (orders)
20
+ }) ()
@@ -0,0 +1,49 @@
1
+ // @ts-nocheck
2
+ import ccxt from '../../js/ccxt.js';
3
+ // AUTO-TRANSPILE //
4
+ // ------------------------------------------------------------------------------
5
+ async function example() {
6
+ const exchange = new ccxt.phemex({
7
+ 'apiKey': 'YOUR_API_KEY',
8
+ 'secret': 'YOUR_API_SECRET',
9
+ });
10
+ const symbol = 'XRP/USDT:USDT';
11
+ const side = 'buy'; // set it to 'buy' for a long position, 'sell' for a short position
12
+ const order_type = 'limit'; // set it to 'market' or 'limit'
13
+ const amount = 1; // how many contracts
14
+ const price = 0.5; // set a price at your desired level
15
+ // take profit and stop loss prices and types
16
+ const take_profit_trigger_price = 0.6;
17
+ const stop_loss_trigger_price = 0.4;
18
+ const take_profit_limit_price = 0.7;
19
+ const stop_loss_limit_price = 0.3;
20
+ await exchange.loadMarkets();
21
+ // when symbol's price reaches your predefined "trigger price", stop-loss order would be activated as a "market order". but if you want it to be activated as a "limit order", then set a 'price' parameter for it
22
+ const params = {
23
+ 'posSide': 'Long',
24
+ 'stopLoss': {
25
+ 'triggerPrice': stop_loss_trigger_price,
26
+ 'type': 'limit',
27
+ 'price': stop_loss_limit_price,
28
+ },
29
+ 'takeProfit': {
30
+ 'triggerPrice': take_profit_trigger_price,
31
+ 'type': 'limit',
32
+ 'price': take_profit_limit_price,
33
+ },
34
+ };
35
+ console.log('-----------------------------------------------------------------------');
36
+ // exchange.verbose = True // uncomment for debugging purposes if necessary
37
+ try {
38
+ const created_order = await exchange.createOrder(symbol, order_type, side, amount, price, params);
39
+ console.log('Created an order', created_order);
40
+ // Fetch all your open orders for this symbol
41
+ const all_open_orders = await exchange.fetchOpenOrders(symbol);
42
+ console.log('Fetched all your orders for this symbol', all_open_orders);
43
+ // To cancel a limit order, use "exchange.cancel_order(created_order['id'], symbol)""
44
+ }
45
+ catch (e) {
46
+ console.log(e.toString());
47
+ }
48
+ }
49
+ await example();
@@ -0,0 +1,43 @@
1
+
2
+
3
+ import ccxt from '../../js/ccxt.js';
4
+
5
+ const ohlcvsBySymbol = {}
6
+
7
+ function handleAllOHLCVs (exchange, ohlcvs, symbol, timeframe) {
8
+ console.log ('----------------------------------------------------------')
9
+ console.log ('Last updated:', exchange.iso8601 (exchange.milliseconds ()))
10
+ const symbols = Object.keys (ohlcvsBySymbol)
11
+ for (let i = 0; i < symbols.length; i++) {
12
+ const symbol = symbols[i]
13
+ const ohlcvs = ohlcvsBySymbol[symbol]
14
+ const lastCandle = exchange.safeValue (ohlcvs, ohlcvs.length - 1)
15
+ const lastTimestamp = lastCandle[0]
16
+ console.log (exchange.iso8601 (lastTimestamp), symbol, timeframe, lastCandle.slice (1))
17
+ }
18
+ }
19
+
20
+ async function pollOHLCV (exchange, symbol, timeframe) {
21
+ while (true) {
22
+ try {
23
+ const response = await exchange.fetchOHLCV (symbol, timeframe)
24
+ ohlcvsBySymbol[symbol] = response
25
+ handleAllOHLCVs(exchange, response, symbol, timeframe)
26
+ } catch (e) {
27
+ console.log (e.constructor.name, e.message)
28
+ }
29
+ }
30
+ }
31
+
32
+ async function main () {
33
+
34
+ const exchange = new ccxt.binance()
35
+ const markets = await exchange.loadMarkets ()
36
+ const timeframe = '5m'
37
+
38
+ const firstOneHundredSymbols = exchange.symbols.slice (0, 100)
39
+
40
+ await Promise.all (firstOneHundredSymbols.map (symbol => pollOHLCV (exchange, symbol, timeframe)))
41
+ }
42
+
43
+ main ()
@@ -0,0 +1,35 @@
1
+
2
+
3
+ // ----------------------------------------------------------------------------
4
+
5
+ import ccxt from '../../js/ccxt.js';
6
+
7
+ // ----------------------------------------------------------------------------
8
+
9
+ (async () => {
10
+
11
+ const exchange = new ccxt.poloniex ({
12
+ 'verbose': process.argv.includes ('--verbose'),
13
+ 'timeout': 60000,
14
+ })
15
+
16
+ try {
17
+
18
+ const response = await exchange.fetchOrderBooks ([
19
+ 'ETH/BTC',
20
+ 'LTC/BTC',
21
+ 'OMG/BTC',
22
+ ])
23
+ console.log (response);
24
+ console.log ('Succeeded.')
25
+
26
+ } catch (e) {
27
+
28
+ console.log ('--------------------------------------------------------')
29
+ console.log (e.constructor.name, e.message)
30
+ console.log ('--------------------------------------------------------')
31
+ console.log (exchange.last_http_response)
32
+ console.log ('Failed.')
33
+ }
34
+
35
+ }) ()
@@ -0,0 +1,62 @@
1
+ import ccxt from '../../js/ccxt.js';
2
+ import fs from 'fs';
3
+ import ololog from 'ololog'
4
+
5
+ const log = ololog.configure.unlimited.noLocate,
6
+ verbose = process.argv.includes ('--verbose'),
7
+ keysGlobal = 'keys.json',
8
+ keysLocal = 'keys.local.json',
9
+ keysFile = fs.existsSync (keysLocal) ? keysLocal : (fs.existsSync (keysGlobal) ? keysGlobal : false),
10
+ config = keysFile ? require ('../../' + keysFile) : {},
11
+ exchange = new ccxt.poloniex (ccxt.extend ({
12
+ enableRateLimit: true,
13
+ verbose,
14
+ }, config.poloniex || {}));(async () => {
15
+
16
+ const test = async function (symbol) {
17
+
18
+ try {
19
+
20
+ await exchange.createOrder (symbol, 'limit', 'buy', 0, 0)
21
+
22
+ } catch (e) {
23
+
24
+ if (e instanceof ccxt.InvalidOrder) {
25
+
26
+ const words = e.message.split (' ')
27
+ let minAmount = parseFloat (words[words.length - 1])
28
+ log.green ("'" + symbol + "': " + minAmount.toString () + ',')
29
+
30
+ } else {
31
+
32
+ throw e
33
+ }
34
+ }
35
+ }
36
+
37
+ await exchange.loadMarkets ()
38
+
39
+ for (let i = 0; i < exchange.symbols.length; i++) {
40
+
41
+ try {
42
+
43
+ await test (exchange.symbols[i])
44
+
45
+ } catch (e) {
46
+
47
+ if (e instanceof ccxt.InvalidNonce) {
48
+
49
+ log.yellow (e)
50
+
51
+ } else {
52
+
53
+ log.red (e)
54
+ throw e
55
+ }
56
+ }
57
+
58
+
59
+ await ccxt.sleep (5000) // sleep 5 seconds, no rush, safe delay
60
+ }
61
+
62
+ }) ()