Advertisement
GlobalLiquidity

Untitled

Mar 25th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const asTable   = require ('as-table')
  4.     , log       = require ('ololog').noLocate
  5.     , ansi      = require ('ansicolor').nice
  6.     , ccxt      = require ('../../ccxt.js')
  7.  
  8. let printSupportedExchanges = function () {
  9.     log ('Supported exchanges:', ccxt.exchanges.join (', ').green)
  10. }
  11.  
  12. let printUsage = function () {
  13.     log ('Usage: node', process.argv[1], 'exchange'.green, 'symbol'.yellow, 'depth'.cyan)
  14.     printSupportedExchanges ()
  15. }
  16.  
  17. let printOrderBook = async (id, symbol, depth) => {
  18.  
  19.     // check if the exchange is supported by ccxt
  20.     let exchangeFound = ccxt.exchanges.indexOf (id) > -1
  21.     if (exchangeFound) {
  22.  
  23.         log ('Instantiating', id.green, 'exchange')
  24.  
  25.         // instantiate the exchange by id
  26.         let exchange = new ccxt[id] ({ enableRateLimit: true })
  27.  
  28.         // load all markets from the exchange
  29.         let markets = await exchange.loadMarkets ()
  30.  
  31.         // // output a list of all market symbols
  32.         // log (id.green, 'has', exchange.symbols.length, 'symbols:', exchange.symbols.join (', ').yellow)
  33.  
  34.         if (symbol in exchange.markets) {
  35.  
  36.             const market = exchange.markets[symbol]
  37.             const pricePrecision = market.precision ? market.precision.price : 8
  38.             const amountPrecision = market.precision ? market.precision.amount : 8
  39.  
  40.             // Object.values (markets).forEach (market => log (market))
  41.  
  42.             // make a table of all markets
  43.             // const table = asTable.configure ({ delimiter: ' | ' }) (Object.values (markets))
  44.             // log (table)
  45.  
  46.             const priceVolumeHelper = color => ([price, amount]) => ({
  47.                 price: price.toFixed (pricePrecision)[color],
  48.                 amount: amount.toFixed (amountPrecision)[color],
  49.                 '  ': '  ',
  50.             })
  51.  
  52.             const cursorUp = '\u001b[1A'
  53.             const tableHeight = depth * 2 + 4 // bids + asks + headers
  54.  
  55.             log (' ') // empty line
  56.  
  57.             while (true) {
  58.  
  59.                 const orderbook = await exchange.fetchOrderBook (symbol)
  60.  
  61.                 log (symbol.green, exchange.iso8601 (exchange.milliseconds ()))
  62.  
  63.                 log (asTable.configure ({ delimiter: ' | '.dim, right: true }) ([
  64.                     ... orderbook.asks.slice (0, depth).reverse ().map (priceVolumeHelper ('red')),
  65.                     // { price: '--------'.dim, amount: '--------'.dim },
  66.                     ... orderbook.bids.slice (0, depth).map (priceVolumeHelper ('green')),
  67.                 ]))
  68.  
  69.                 log (cursorUp.repeat (tableHeight))
  70.             }
  71.  
  72.         } else {
  73.  
  74.             log.error ('Symbol', symbol.bright, 'not found')
  75.         }
  76.  
  77.  
  78.     } else {
  79.  
  80.         log ('Exchange ' + id.red + ' not found')
  81.         printSupportedExchanges ()
  82.     }
  83. }
  84.  
  85. ;(async function main () {
  86.  
  87.     if (process.argv.length > 4) {
  88.  
  89.         const id = process.argv[2]
  90.         const symbol = process.argv[3].toUpperCase ()
  91.         const depth = parseInt (process.argv[4])
  92.         await printOrderBook (id, symbol, depth)
  93.  
  94.     } else {
  95.  
  96.         printUsage ()
  97.     }
  98.  
  99.     process.exit ()
  100.  
  101. }) ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement