ccxt 4.2.73 → 4.2.75
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.
- package/CHANGELOG.md +206 -10
- package/README.md +10 -9
- package/build.sh +1 -1
- package/dist/ccxt.browser.js +1042 -297
- package/dist/ccxt.browser.min.js +3 -3
- package/dist/cjs/ccxt.js +4 -1
- package/dist/cjs/src/abstract/tradeogre.js +9 -0
- package/dist/cjs/src/ascendex.js +3 -2
- package/dist/cjs/src/base/Exchange.js +1 -1
- package/dist/cjs/src/bitget.js +62 -50
- package/dist/cjs/src/bitmart.js +1 -0
- package/dist/cjs/src/bybit.js +90 -30
- package/dist/cjs/src/coinbaseinternational.js +1 -1
- package/dist/cjs/src/coinex.js +4 -3
- package/dist/cjs/src/gate.js +1 -0
- package/dist/cjs/src/htx.js +16 -9
- package/dist/cjs/src/hyperliquid.js +2 -1
- package/dist/cjs/src/kraken.js +3 -3
- package/dist/cjs/src/kucoin.js +1 -1
- package/dist/cjs/src/pro/krakenfutures.js +8 -7
- package/dist/cjs/src/tradeogre.js +632 -0
- package/examples/README.md +308 -0
- package/examples/ccxt.pro/html/watchTicker.html +51 -0
- package/examples/ccxt.pro/js/binance-fetch-balance-snapshot-watch-balance-updates.js +55 -0
- package/examples/ccxt.pro/js/binance-https-proxy.js +48 -0
- package/examples/ccxt.pro/js/binance-watch-ohlcv-many-symbols-continuously.js +38 -0
- package/examples/ccxt.pro/js/binance-watch-ohlcv-many-symbols.js +64 -0
- package/examples/ccxt.pro/js/binance-watch-ticker-many-symbols.js +62 -0
- package/examples/ccxt.pro/js/build-ohlcv-many-symbols.js +68 -0
- package/examples/ccxt.pro/js/calculate-ohlcvs-from-trades-warmup.js +82 -0
- package/examples/ccxt.pro/js/calculate-ohlcvs-from-trades.js +59 -0
- package/examples/ccxt.pro/js/exchange-capabitities.js +72 -0
- package/examples/ccxt.pro/js/exchange-close.js +46 -0
- package/examples/ccxt.pro/js/gateio-swap-watch-many-orderbooks.js +30 -0
- package/examples/ccxt.pro/js/gateio-watch-balance.js +23 -0
- package/examples/ccxt.pro/js/gateio-watch-order-book.js +71 -0
- package/examples/ccxt.pro/js/graceful-shutdown.js +43 -0
- package/examples/ccxt.pro/js/many-exchanges-many-streams.js +37 -0
- package/examples/ccxt.pro/js/okex-create-futures-order.js +48 -0
- package/examples/ccxt.pro/js/okex-watch-balance-and-create-order.js +54 -0
- package/examples/ccxt.pro/js/okx-watch-tickers.js +31 -0
- package/examples/ccxt.pro/js/one-exchange-many-different-streams.js +46 -0
- package/examples/ccxt.pro/js/one-exchange-many-streams-2.js +26 -0
- package/examples/ccxt.pro/js/one-exchange-many-streams.js +28 -0
- package/examples/ccxt.pro/js/socks-binance-watch-orderbook.js +30 -0
- package/examples/ccxt.pro/js/watch-fetch-many-exchanges-many-ordersbooks.js +40 -0
- package/examples/ccxt.pro/js/watch-many-exchanges-many-ordersbooks.js +38 -0
- package/examples/ccxt.pro/js/watch-many-exchanges-many-symbols.js +40 -0
- package/examples/ccxt.pro/js/watch-many-orderbooks.js +27 -0
- package/examples/ccxt.pro/js/watch-new-trades-only.js +43 -0
- package/examples/ccxt.pro/js/watch-new-trades.js +42 -0
- package/examples/ccxt.pro/js/watch-trades-many-symbols.js +29 -0
- package/examples/ccxt.pro/js/watch-vs-fetch.js +29 -0
- package/examples/cs/Examples.sln +22 -0
- package/examples/cs/c#.csproj +19 -0
- package/examples/html/basic-cors-proxy.html +46 -0
- package/examples/html/basic-inheritance.html +47 -0
- package/examples/html/basic-poller.html +52 -0
- package/examples/html/basic-rate-limiting.html +49 -0
- package/examples/html/basic.html +43 -0
- package/examples/html/binance-cors-proxy.html +28 -0
- package/examples/html/bitmex-browser-cors-proxy.js +7 -0
- package/examples/html/bitmex-cors.html +46 -0
- package/examples/html/tradingview-charts.html +61 -0
- package/examples/html/webworker/index.html +97 -0
- package/examples/html/webworker/worker.js +43 -0
- package/examples/js/README.md +15 -0
- package/examples/js/advanced-error-handling.js +39 -0
- package/examples/js/aggregate-orderbook.js +54 -0
- package/examples/js/arbitrage-pairs.js +130 -0
- package/examples/js/basic-chart.js +29 -0
- package/examples/js/basic-orderbook-polling.js +13 -0
- package/examples/js/bcc-vs-bch.js +115 -0
- package/examples/js/binance-fetch-all-deposits.js +45 -0
- package/examples/js/binance-fetch-ohlcv-many-symbols-async-await.js +34 -0
- package/examples/js/binance-fetch-ohlcv-many-symbols-promise-then-callbacks.js +33 -0
- package/examples/js/binance-fetchTicker-delivery-vs-future.js +31 -0
- package/examples/js/binance-futures-transfer-from-sub-account-to-master.js +41 -0
- package/examples/js/binance-margin-stop-order.js +39 -0
- package/examples/js/binance-server-time.js +34 -0
- package/examples/js/binance-universal-transfer.js +16 -0
- package/examples/js/bitfinex-fetch-trades.js +35 -0
- package/examples/js/bitfinex2-fetch-trades.js +35 -0
- package/examples/js/bitmex-browser-cors-proxy.js +7 -0
- package/examples/js/bitpanda-fetchMyTrades-reduce.js +36 -0
- package/examples/js/bitrue-fetch-balance.js +28 -0
- package/examples/js/bitstamp-private-api.js +115 -0
- package/examples/js/bitstamp-public-api.js +39 -0
- package/examples/js/bittrex-balance.js +50 -0
- package/examples/js/bittrex-fetch-closed-orders-history.js +69 -0
- package/examples/js/blockchaincom-withdrawal.js +59 -0
- package/examples/js/build-ohlcv-bars.js +48 -0
- package/examples/js/builtin-rate-limiting-rest-poller.js +25 -0
- package/examples/js/bybit-trailing.js +62 -0
- package/examples/js/bybit-updated.cjs +154 -0
- package/examples/js/cli.js +405 -0
- package/examples/js/coinbase-fetch-all-balances.js +36 -0
- package/examples/js/coinex-fetch-all-deposit-addresses-using-fetchDepositAddress.js +47 -0
- package/examples/js/coinex-futures.js +75 -0
- package/examples/js/coinone-fetch-tickers.js +54 -0
- package/examples/js/coinone-markets.js +16 -0
- package/examples/js/compare-two-exchanges-capabilities.js +36 -0
- package/examples/js/cors-proxy.js +5 -0
- package/examples/js/create-order-handle-errors.js +55 -0
- package/examples/js/create-order-position-with-takeprofit-stoploss.js +71 -0
- package/examples/js/create-order-with-retry.js +65 -0
- package/examples/js/create-order-ws-example.js +25 -0
- package/examples/js/create-orders-example.js +17 -0
- package/examples/js/create-trailing-amount-order.js +36 -0
- package/examples/js/create-trailing-percent-order.js +36 -0
- package/examples/js/credentials.json +5 -0
- package/examples/js/custom-proxy-agent-for-js.js +10 -0
- package/examples/js/custom-proxy-url.js +23 -0
- package/examples/js/delta-maintenance-margin-rate-max-leverage.js +60 -0
- package/examples/js/env-variables.js +26 -0
- package/examples/js/error-handling.js +89 -0
- package/examples/js/exchange-capabilities.js +135 -0
- package/examples/js/exchanges-by-volume.js +60 -0
- package/examples/js/exchanges.js +40 -0
- package/examples/js/fetch-all-balances.js +219 -0
- package/examples/js/fetch-all-tickers-to-files-2.js +53 -0
- package/examples/js/fetch-all-tickers-to-files.js +77 -0
- package/examples/js/fetch-balance.js +28 -0
- package/examples/js/fetch-create-deposit-address.js +101 -0
- package/examples/js/fetch-from-many-exchanges-simultaneously.js +21 -0
- package/examples/js/fetch-funding-rate-history.js +25 -0
- package/examples/js/fetch-futures/prettier.config.js +5 -0
- package/examples/js/fetch-futures/src/index.js +25 -0
- package/examples/js/fetch-ohlcv-from-to-mark-index-premium.js +72 -0
- package/examples/js/fetch-ohlcv-many-exchanges-continuosly.js +39 -0
- package/examples/js/fetch-ohlcv.js +16 -0
- package/examples/js/fetch-okex-futures.js +22 -0
- package/examples/js/fetch-orders.js +27 -0
- package/examples/js/fetch-ticker-from-multiple-exchanges.js +35 -0
- package/examples/js/fetch-ticker-where-available.js +75 -0
- package/examples/js/fetch-tickers/build/index.js +19 -0
- package/examples/js/fetch-tickers/prettier.config.js +5 -0
- package/examples/js/fetch-tickers/src/index.js +17 -0
- package/examples/js/gateio-create-batch-order.js +43 -0
- package/examples/js/gateio-futures.js +49 -0
- package/examples/js/gateio-open-close-contract.js +49 -0
- package/examples/js/gateio-swaps.js +74 -0
- package/examples/js/gdax-fetch-trades-pagination.js +29 -0
- package/examples/js/hitbtc2-withdraw.js +61 -0
- package/examples/js/how-to-import-one-exchange-esm.js +10 -0
- package/examples/js/huobi-futures.js +71 -0
- package/examples/js/huobi-open-close-contract.js +63 -0
- package/examples/js/huobi-swaps.js +70 -0
- package/examples/js/huobipro-market-buy-sell-fetch-trading-limits.js +98 -0
- package/examples/js/hybridCJSExample.cjs +19 -0
- package/examples/js/hybridESMExample.js +19 -0
- package/examples/js/idex-fetch-balance.js +13 -0
- package/examples/js/instantiate-all-at-once.js +46 -0
- package/examples/js/instantiate-all-from-json.js +31 -0
- package/examples/js/kraken-create-and-close-position.js +54 -0
- package/examples/js/kraken-fetch-order-trades.js +33 -0
- package/examples/js/kraken-margin-trading.js +89 -0
- package/examples/js/kucoin-rate-limit.js +38 -0
- package/examples/js/latoken-example.js +108 -0
- package/examples/js/live-orderbook.js +106 -0
- package/examples/js/live-ticker.js +80 -0
- package/examples/js/live-tickers.js +74 -0
- package/examples/js/load-all-contracts.js +41 -0
- package/examples/js/load-all-symbols-at-once.js +69 -0
- package/examples/js/load-all-tickers-at-once.js +91 -0
- package/examples/js/load-markets-to-files.js +57 -0
- package/examples/js/looping-over-all-symbols-of-specific-exchanges.js +61 -0
- package/examples/js/looping-over-specific-symbols-of-all-exchanges.js +91 -0
- package/examples/js/margin-loan-borrow-buy-sell-repay.js +70 -0
- package/examples/js/market-status-and-currency-status.js +29 -0
- package/examples/js/ohlcv-console-chart.js +29 -0
- package/examples/js/okex-fetch-closed-orders-archive.js +31 -0
- package/examples/js/okex-transfer.js +51 -0
- package/examples/js/okx-poll-fetch-my-trades.js +37 -0
- package/examples/js/okx-poll-rate-limit.js +48 -0
- package/examples/js/order-book-extra-level-depth-param.js +20 -0
- package/examples/js/phemex-create-order-position-with-takeprofit-stoploss.js +49 -0
- package/examples/js/poll-ohlcv.js +43 -0
- package/examples/js/poloniex-fetch-order-books.js +35 -0
- package/examples/js/poloniex-limits-amount-min.js +62 -0
- package/examples/js/proxy-round-robin.js +98 -0
- package/examples/js/proxy-usage.js +32 -0
- package/examples/js/sample-local-proxy-server-with-cors.js +12 -0
- package/examples/js/search-all-exchanges.js +159 -0
- package/examples/js/shared-load-markets.js +80 -0
- package/examples/js/sort-swap-markets-by-hourly-price-change.js +55 -0
- package/examples/js/symbols.js +110 -0
- package/examples/js/theocean.js +41 -0
- package/examples/js/tickers.js +106 -0
- package/examples/js/validate-paginated-data.js +61 -0
- package/examples/js/watch-OHLCV-For-Symbols.js +15 -0
- package/examples/js/watch-OHLCV.js +12 -0
- package/examples/js/watch-OrderBook-For-Symbols.js +11 -0
- package/examples/js/watch-Trades-For-Symbols.js +11 -0
- package/examples/js/watch-tickers.js +11 -0
- package/examples/js/watchOHLCVForSymbols.js +15 -0
- package/examples/js/watchOrderBookForSymbols.js +11 -0
- package/examples/js/watchPositions-many-exchanges-continuosly.d.ts +2 -0
- package/examples/js/watchPositions-many-exchanges-continuosly.d.ts.map +1 -0
- package/examples/js/watchPositions-many-exchanges-continuosly.js +49 -0
- package/examples/js/watchPositions.d.ts +2 -0
- package/examples/js/watchPositions.d.ts.map +1 -0
- package/examples/js/watchPositions.js +13 -0
- package/examples/js/watchPositionsForSymbols.d.ts +2 -0
- package/examples/js/watchPositionsForSymbols.d.ts.map +1 -0
- package/examples/js/watchPositionsForSymbols.js +14 -0
- package/examples/js/watchTradesForSymbols.js +11 -0
- package/examples/js/withdraw-from-one-exchange-to-another.js +50 -0
- package/examples/php/README.md +7 -0
- package/examples/py/README.md +15 -0
- package/examples/py/playing_with_ccxt_example.ipynb +222 -0
- package/examples/ts/.eslintrc +111 -0
- package/examples/ts/build-ohlcv-bars.ts +53 -0
- package/examples/ts/cli.ts +397 -0
- package/examples/ts/compare-two-exchanges-capabilities.ts +36 -0
- package/examples/ts/create-order-position-with-takeprofit-stoploss.ts +89 -0
- package/examples/ts/create-order-ws-example.ts +33 -0
- package/examples/ts/create-orders-example.ts +21 -0
- package/examples/ts/create-trailing-amount-order.ts +37 -0
- package/examples/ts/create-trailing-percent-order.ts +37 -0
- package/examples/ts/custom-proxy-agent-for-js.ts +14 -0
- package/examples/ts/fetch-futures/package-lock.json +116 -0
- package/examples/ts/fetch-futures/package.json +34 -0
- package/examples/ts/fetch-futures/prettier.config.js +4 -0
- package/examples/ts/fetch-futures/src/index.ts +28 -0
- package/examples/ts/fetch-futures/tsconfig.json +28 -0
- package/examples/ts/fetch-ohlcv-many-exchanges-continuosly.ts +44 -0
- package/examples/ts/fetch-ohlcv.ts +17 -0
- package/examples/ts/fetch-tickers/package-lock.json +116 -0
- package/examples/ts/fetch-tickers/package.json +34 -0
- package/examples/ts/fetch-tickers/prettier.config.js +4 -0
- package/examples/ts/fetch-tickers/src/index.ts +21 -0
- package/examples/ts/fetch-tickers/tsconfig.json +28 -0
- package/examples/ts/how-to-import-one-exchange-esm.ts +11 -0
- package/examples/ts/kraken-create-and-close-position.ts +69 -0
- package/examples/ts/margin-loan-borrow-buy-sell-repay.ts +72 -0
- package/examples/ts/nextjs-page-router/.eslintrc.json +3 -0
- package/examples/ts/nextjs-page-router/README.md +43 -0
- package/examples/ts/nextjs-page-router/next.config.js +6 -0
- package/examples/ts/nextjs-page-router/package-lock.json +7425 -0
- package/examples/ts/nextjs-page-router/package.json +28 -0
- package/examples/ts/nextjs-page-router/postcss.config.js +6 -0
- package/examples/ts/nextjs-page-router/public/favicon.ico +0 -0
- package/examples/ts/nextjs-page-router/src/pages/_app.tsx +6 -0
- package/examples/ts/nextjs-page-router/src/pages/_document.tsx +13 -0
- package/examples/ts/nextjs-page-router/src/pages/balance.tsx +46 -0
- package/examples/ts/nextjs-page-router/src/pages/index.tsx +8 -0
- package/examples/ts/nextjs-page-router/src/pages/tickers.tsx +61 -0
- package/examples/ts/nextjs-page-router/src/styles/globals.css +27 -0
- package/examples/ts/nextjs-page-router/tailwind.config.ts +20 -0
- package/examples/ts/nextjs-page-router/tsconfig.json +22 -0
- package/examples/ts/phemex-create-order-position-with-takeprofit-stoploss.ts +62 -0
- package/examples/ts/proxy-usage.ts +41 -0
- package/examples/ts/sample-local-proxy-server-with-cors.ts +15 -0
- package/examples/ts/watch-OHLCV-For-Symbols.ts +17 -0
- package/examples/ts/watch-OHLCV.ts +14 -0
- package/examples/ts/watch-OrderBook-For-Symbols.ts +13 -0
- package/examples/ts/watch-Trades-For-Symbols.ts +13 -0
- package/examples/ts/watch-tickers.ts +13 -0
- package/examples/ts/watchPositions-many-exchanges-continuosly.ts +53 -0
- package/examples/ts/watchPositions.ts +15 -0
- package/examples/ts/watchPositionsForSymbols.ts +16 -0
- package/examples/tsconfig.json +27 -0
- package/js/ccxt.d.ts +5 -2
- package/js/ccxt.js +4 -2
- package/js/src/abstract/tradeogre.d.ts +19 -0
- package/js/src/abstract/tradeogre.js +11 -0
- package/js/src/ascendex.d.ts +2 -7
- package/js/src/ascendex.js +3 -2
- package/js/src/base/Exchange.d.ts +7 -7
- package/js/src/base/Exchange.js +1 -1
- package/js/src/base/types.d.ts +7 -1
- package/js/src/binance.d.ts +1 -1
- package/js/src/bitget.js +62 -50
- package/js/src/bitmart.d.ts +1 -1
- package/js/src/bitmart.js +2 -1
- package/js/src/bybit.d.ts +3 -1
- package/js/src/bybit.js +90 -30
- package/js/src/coinbase.d.ts +4 -4
- package/js/src/coinbaseinternational.d.ts +1 -1
- package/js/src/coinbaseinternational.js +1 -1
- package/js/src/coinbasepro.d.ts +2 -2
- package/js/src/coinex.js +4 -3
- package/js/src/coinlist.d.ts +2 -2
- package/js/src/cryptocom.d.ts +2 -2
- package/js/src/currencycom.d.ts +2 -2
- package/js/src/deribit.d.ts +2 -2
- package/js/src/gate.d.ts +1 -1
- package/js/src/gate.js +1 -0
- package/js/src/htx.d.ts +2 -2
- package/js/src/htx.js +16 -9
- package/js/src/huobijp.d.ts +2 -2
- package/js/src/hyperliquid.js +2 -1
- package/js/src/kraken.d.ts +1 -1
- package/js/src/kraken.js +3 -3
- package/js/src/kucoin.d.ts +3 -3
- package/js/src/kucoin.js +1 -1
- package/js/src/luno.d.ts +2 -2
- package/js/src/mexc.d.ts +3 -3
- package/js/src/ndax.d.ts +2 -2
- package/js/src/novadax.d.ts +2 -2
- package/js/src/okx.d.ts +2 -2
- package/js/src/pro/bitfinex2.d.ts +1 -1
- package/js/src/pro/krakenfutures.js +8 -7
- package/js/src/tradeogre.d.ts +32 -0
- package/js/src/tradeogre.js +633 -0
- package/js/src/woo.d.ts +2 -2
- package/package.json +4 -1
- package/skip-tests.json +219 -424
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Example code in typescript
|
|
2
|
+
// Based on /examples/js/fetch-from-many-exchanges-simultaneously.js
|
|
3
|
+
|
|
4
|
+
import * as ccxt from 'ccxt';
|
|
5
|
+
const log = require('ololog');
|
|
6
|
+
|
|
7
|
+
const symbol = 'BTC/USD';
|
|
8
|
+
const exchanges = ['coinbasepro', 'gemini', 'kraken'];
|
|
9
|
+
|
|
10
|
+
const fetchTickers = async (symbol: string) => {
|
|
11
|
+
const result = await Promise.all(exchanges.map(async (id: string): Promise<ccxt.Exchange> => {
|
|
12
|
+
const CCXT = ccxt as any; // Hack!
|
|
13
|
+
const exchange = new CCXT[id]({ 'enableRateLimit': true }) as ccxt.Exchange;
|
|
14
|
+
const ticker = await exchange.fetchTicker(symbol);
|
|
15
|
+
const exchangeExtended = exchange.extend({ 'exchange': id }, ticker) as ccxt.Exchange;
|
|
16
|
+
return exchangeExtended;
|
|
17
|
+
}));
|
|
18
|
+
log(result);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
fetchTickers(symbol);
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "src",
|
|
4
|
+
"outDir": "build",
|
|
5
|
+
"allowUnreachableCode": false,
|
|
6
|
+
"allowUnusedLabels": false,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"forceConsistentCasingInFileNames": true,
|
|
9
|
+
"lib": [
|
|
10
|
+
"es2016"
|
|
11
|
+
],
|
|
12
|
+
"module": "commonjs",
|
|
13
|
+
"noEmitOnError": true,
|
|
14
|
+
"noFallthroughCasesInSwitch": true,
|
|
15
|
+
"noImplicitReturns": true,
|
|
16
|
+
"pretty": true,
|
|
17
|
+
"sourceMap": true,
|
|
18
|
+
"strict": true,
|
|
19
|
+
"target": "es2017"
|
|
20
|
+
},
|
|
21
|
+
"include": [
|
|
22
|
+
"src/**/*.ts",
|
|
23
|
+
"test/**/*.ts"
|
|
24
|
+
],
|
|
25
|
+
"exclude": [
|
|
26
|
+
"node_modules"
|
|
27
|
+
]
|
|
28
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { binance } from '../../js/ccxt.js';
|
|
2
|
+
|
|
3
|
+
async function example () {
|
|
4
|
+
const exchange = new binance ({});
|
|
5
|
+
const ob = await exchange.fetchOrderBook ('BTC/USDT', 3);
|
|
6
|
+
const asks = ob['asks'];
|
|
7
|
+
const bids = ob['bids'];
|
|
8
|
+
console.log (asks);
|
|
9
|
+
console.log (bids);
|
|
10
|
+
}
|
|
11
|
+
example ();
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import ccxt from '../../js/ccxt.js';
|
|
3
|
+
|
|
4
|
+
// AUTO-TRANSPILE //
|
|
5
|
+
|
|
6
|
+
console.log ('CCXT Version:', ccxt.version);
|
|
7
|
+
|
|
8
|
+
// ------------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
async function example () {
|
|
11
|
+
const exchange = new ccxt.kraken ({
|
|
12
|
+
"apiKey": "YOUR_API_KEY",
|
|
13
|
+
"secret": "YOUR_API_SECRET",
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const symbol = 'UNI/USD';
|
|
17
|
+
let side = 'buy'; // set it to 'buy' for a long position, 'sell' for a short position
|
|
18
|
+
const order_type = 'market'; // set it to 'market' or 'limit'
|
|
19
|
+
const amount = 1;
|
|
20
|
+
const leverage = 2;
|
|
21
|
+
|
|
22
|
+
await exchange.loadMarkets ();
|
|
23
|
+
const market = exchange.market (symbol);
|
|
24
|
+
|
|
25
|
+
// if order_type is 'market', then price is not needed
|
|
26
|
+
let price = undefined;
|
|
27
|
+
// if order_type is 'limit', then set a price at your desired level
|
|
28
|
+
// you can fetch the ticker and update price
|
|
29
|
+
// const ticker = await exchange.fetchTicker (symbol);
|
|
30
|
+
// const last_price = ticker['last'];
|
|
31
|
+
// const ask_price = ticker['ask'];
|
|
32
|
+
// const bid_price = ticker['bid'];
|
|
33
|
+
// if (order_type === 'limit') {
|
|
34
|
+
// price = (side === 'buy') ? bid_price * 0.95 : ask_price * 1.05; // i.e. 5% from current price
|
|
35
|
+
// }
|
|
36
|
+
|
|
37
|
+
const params = {
|
|
38
|
+
'leverage': leverage,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// log
|
|
42
|
+
console.log ('Going to open a position', 'for', amount, 'worth', amount, market['base'], '~', market['settle'], 'using', side, order_type, 'order (', (order_type === 'limit' ? exchange.priceToPrecision (symbol, price) : ''), '), using the following params:');
|
|
43
|
+
console.log (params);
|
|
44
|
+
console.log ('-----------------------------------------------------------------------');
|
|
45
|
+
|
|
46
|
+
// exchange.verbose = True // uncomment for debugging purposes if necessary
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const created_order = await exchange.createOrder (symbol, order_type, side, amount, price, params);
|
|
50
|
+
console.log ("Created an order", created_order);
|
|
51
|
+
|
|
52
|
+
// Fetch all your closed orders for this symbol (because we used market order)
|
|
53
|
+
// - use 'fetchClosedOrders' or 'fetchOrders' and filter with 'closed' status
|
|
54
|
+
const all_closed_orders = await exchange.fetchClosedOrders (symbol);
|
|
55
|
+
console.log ("Fetched all your closed orders for this symbol", all_closed_orders);
|
|
56
|
+
|
|
57
|
+
const all_open_positions = await exchange.fetchPositions (symbol);
|
|
58
|
+
console.log ("Fetched all your positions for this symbol", all_open_positions);
|
|
59
|
+
|
|
60
|
+
// To close a position:
|
|
61
|
+
// - long position (buy), you can create a sell order: exchange.createOrder (symbol, order_type, 'sell', amount, price, params);
|
|
62
|
+
// - short position (sell), you can create a buy order: exchange.createOrder (symbol, order_type, 'buy', amount, price, params);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
console.log (e.toString ());
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
await example ();
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import ccxt from '../../js/ccxt.js';
|
|
2
|
+
|
|
3
|
+
// AUTO-TRANSPILE //
|
|
4
|
+
|
|
5
|
+
// Note, this is just an example and might not yet work on other exchanges, which are being still unified.
|
|
6
|
+
|
|
7
|
+
async function example () {
|
|
8
|
+
// ########## user inputs ##########
|
|
9
|
+
const exchange = new ccxt['binance'] ({ 'apiKey': 'xxx', 'secret': 'xxx' });
|
|
10
|
+
const symbol = 'BUSD/USDT'; // set target symbol
|
|
11
|
+
const marginMode = 'isolated'; // margin mode (cross or isolated)
|
|
12
|
+
const collateral_coin = 'USDT'; // which asset you want to use for margin-borrow collateral
|
|
13
|
+
const borrow_coin = 'BUSD'; // which coin to borrow
|
|
14
|
+
const order_side: any = 'sell'; // which side to trade
|
|
15
|
+
const amount_to_trade = 14; // how many coins to sell
|
|
16
|
+
const order_type = 'limit'; // order type (can be market, limit or etc)
|
|
17
|
+
const limit_price: any = 0.99; // price to sell at (set undefined/null/None if market-order)
|
|
18
|
+
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
|
|
19
|
+
// ########## end of user-inputs ##########
|
|
20
|
+
//
|
|
21
|
+
// for example purposes, let's also check available balance at first
|
|
22
|
+
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.
|
|
23
|
+
// if we don't have enought coins, then we have to borrow at first
|
|
24
|
+
let needed_amount_to_borrow: any = undefined; // will be auto-set below
|
|
25
|
+
if (amount_to_trade > balance_margin[symbol][borrow_coin]['free']) {
|
|
26
|
+
needed_amount_to_borrow = amount_to_trade - balance_margin[symbol][borrow_coin]['free'];
|
|
27
|
+
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.');
|
|
28
|
+
// 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)
|
|
29
|
+
const needed_collateral_amount = needed_amount_to_borrow / (margin_magnitude - 1);
|
|
30
|
+
// Check if we have any collateral to get permission for borrow
|
|
31
|
+
if (balance_margin[symbol][collateral_coin]['free'] < needed_collateral_amount) {
|
|
32
|
+
// If we don't have enough collateral, then let's try to transfer collateral-asset from spot-balance to margin-balance
|
|
33
|
+
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');
|
|
34
|
+
// let's check if we have spot balance at all
|
|
35
|
+
const balance_spot = await exchange.fetchBalance ({ 'type': 'spot' });
|
|
36
|
+
if (exchange.parseNumber (balance_spot[collateral_coin]['free']) < needed_collateral_amount) {
|
|
37
|
+
console.log ('hmm, I neither do have enough balance on spot - only ', balance_spot[collateral_coin]['free'], '. Script can not continue...');
|
|
38
|
+
return;
|
|
39
|
+
} else {
|
|
40
|
+
console.log ('Transferring ', needed_collateral_amount, ' to margin account');
|
|
41
|
+
await exchange.transfer (collateral_coin, needed_collateral_amount, 'spot', marginMode, { 'symbol': symbol });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// now, as we have enough margin collateral, initiate borrow
|
|
45
|
+
console.log ('Initiating margin borrow of ', needed_amount_to_borrow, ' ', borrow_coin);
|
|
46
|
+
const borrowResult = await exchange.borrowMargin (borrow_coin, needed_amount_to_borrow, symbol, { 'marginMode': marginMode });
|
|
47
|
+
}
|
|
48
|
+
console.log ('Submitting order.');
|
|
49
|
+
const order = await exchange.createOrder (symbol, order_type, order_side, amount_to_trade, limit_price, { 'marginMode': marginMode });
|
|
50
|
+
console.log ('Order was submitted !', order['id']);
|
|
51
|
+
//
|
|
52
|
+
//
|
|
53
|
+
// ...
|
|
54
|
+
// ...
|
|
55
|
+
// some time later, if you want to repay the loan back (like 'close the position')...
|
|
56
|
+
// ...
|
|
57
|
+
// ...
|
|
58
|
+
//
|
|
59
|
+
//
|
|
60
|
+
// set the "repay-back" amount (for this example snippet, this will be same amount that we borrowed above)
|
|
61
|
+
if (needed_amount_to_borrow !== undefined) {
|
|
62
|
+
const amount_to_repay_back = needed_amount_to_borrow;
|
|
63
|
+
// At first, you need to get back the borrowed coin, by making an opposide trade
|
|
64
|
+
console.log ('Making purchase back of ', amount_to_repay_back, ' ', borrow_coin, ' to repay it back.');
|
|
65
|
+
const purchase_back_price: any = 1.01;
|
|
66
|
+
const order_back = await exchange.createOrder (symbol, order_type, (order_side === 'buy' ? 'sell' : 'buy'), amount_to_repay_back, purchase_back_price, { 'marginMode': marginMode });
|
|
67
|
+
console.log ('Now, repaying the loan.');
|
|
68
|
+
const repayResult = await exchange.repayMargin (borrow_coin, amount_to_repay_back, symbol, { 'marginMode': marginMode });
|
|
69
|
+
console.log ('finished.');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
await example ();
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
|
|
2
|
+
|
|
3
|
+
## Description
|
|
4
|
+
|
|
5
|
+
This app demonstrates the use of ccxt with a nextjs project. It showcases the use of both ccxt as a server side package and also to be running in the client:
|
|
6
|
+
|
|
7
|
+
- **/tickers**: uses the **clients Window websockets** to connect to the exchange and stream ticker values
|
|
8
|
+
|
|
9
|
+
- **/balance**: uses a **server side call** to protect exposing any api keys and fetches and shows the user balance.
|
|
10
|
+
|
|
11
|
+
## Getting Started
|
|
12
|
+
|
|
13
|
+
1. install the dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm run install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
2. Set the keys in pages/balance.tsx
|
|
21
|
+
|
|
22
|
+
3. Run the app:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm run dev
|
|
26
|
+
# or
|
|
27
|
+
yarn dev
|
|
28
|
+
# or
|
|
29
|
+
pnpm dev
|
|
30
|
+
# or
|
|
31
|
+
bun dev
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
|
35
|
+
|
|
36
|
+
## Screenshots
|
|
37
|
+
##### Index
|
|
38
|
+

|
|
39
|
+
##### Client side websocket example
|
|
40
|
+

|
|
41
|
+
##### Server side private endpoint fetch balance
|
|
42
|
+

|
|
43
|
+
|