Back to Ccxt

Gateio Watch Order Book

wiki/examples/ts/gateio-watch-order-book.md

4.5.532.5 KB
Original Source
javascript
'use strict';

import ccxt from '../../js/ccxt.js';

console.log ('CCXT Version:', ccxt.version); // eslint-disable-line import/no-named-as-default-member

const orderbooks = {};

async function watchAllSymbols (exchange, symbols) {
   while (true) { // eslint-disable-line no-constant-condition
       const keys = Object.keys (orderbooks);
       if (symbols.length === keys.length) {
           console.log ('\n\n\n\n\n');
           console.log ('----------------------------------------------------');
           console.log ('All orderbooks received at least one update:');
           for (let i = 0; i < symbols.length; i++) {
               const symbol = symbols[i];
               const orderbook = orderbooks[symbol];
               console.log (exchange.iso8601 (exchange.milliseconds ()), orderbook['datetime'], orderbook['nonce'], symbol, orderbook['asks'][0], orderbook['bids'][0]);
           }
           console.log ('----------------------------------------------------');
           console.log ('\n\n\n\n\n');
           // process.exit () // stop here if you want
           break;
       } else {
           await exchange.sleep (1000);
       }
   }
}

async function watchOrderBook (exchange, symbol) {
   while (true) { // eslint-disable-line no-constant-condition
       try {
           const orderbook = await exchange.watchOrderBook (symbol);
           orderbooks[symbol] = orderbook;
           console.log (exchange.iso8601 (exchange.milliseconds ()), orderbook['datetime'], orderbook['nonce'], symbol, orderbook['asks'][0], orderbook['bids'][0]);
       } catch (e) {
           console.log (e);
       }
   }
}

async function main () {
   const exchange = new ccxt.pro.gateio ({ // eslint-disable-line import/no-named-as-default-member
       'options': {
           'defaultType': 'swap',
       },
   });
   await exchange.loadMarkets ();
   // exchange.verbose = true // uncomment for debugging purposes if necessary
   const symbols = [
       // 'SOS/USDT:USDT',
       // 'JASMY/USDT:USDT',
       // 'SLP/USDT:USDT',
       'ACH/USDT:USDT',
       'MKISHU/USDT:USDT',
       // 'GMT/USDT:USDT',
       // 'ASTR/USDT:USDT',
       'RAMP/USDT:USDT',
       'RSR/USDT:USDT',
       // 'RACA/USDT:USDT',
       // 'ROOK/USDT:USDT',
       // 'ROSE/USDT:USDT',
   ];
   await Promise.all ([
       watchAllSymbols (exchange, symbols),
       ...symbols.map ((symbol) => watchOrderBook (exchange, symbol)),
   ]);
}

main ();