-
-
Save charl-kruger/7b05dcc760666bd711dd620a7a21a5b7 to your computer and use it in GitHub Desktop.
stream trading events from Luno.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var WebsocketClient = require('websocket').client, | |
| colors = require('colors'), | |
| sprintf = require('sprintf-js').sprintf; | |
| var ws = new WebsocketClient(); | |
| var asks = bids = {}; | |
| var now = function () { | |
| var date = new Date(); | |
| return sprintf("%02d:%02d:%02d", date.getHours(), date.getMinutes(), date.getSeconds()); | |
| } | |
| ws.on('connect', function (conn) { | |
| console.log('connection established.'); | |
| conn.on('message', function (msg) { | |
| var data = JSON.parse(msg.utf8Data); | |
| if (data == "") return; | |
| if (data.trade_updates) { | |
| data.trade_updates.forEach(function (trade) { | |
| var price = trade.order_id in asks ? asks[trade.order_id].price : bids[trade.order_id].price; | |
| console.log(sprintf("%16s %-15s %11s %15s %10s", "TRADE".white, trade.order_id, trade.base, price, now())); | |
| }); | |
| } | |
| if (data.create_update) { | |
| if (data.create_update.type == "BID") { | |
| bids[data.create_update.order_id] = {price: data.create_update.price, volume: data.create_update.volume}; | |
| console.log(sprintf("%16s %-15s %11s %15s %10s", colors.green(data.create_update.type), data.create_update.order_id, data.create_update.volume, data.create_update.price, now())); | |
| } else { | |
| bids[data.create_update.order_id] = {price: data.create_update.price, volume: data.create_update.volume}; | |
| console.log(sprintf("%16s %-15s %11s %15s %10s", colors.yellow(data.create_update.type), data.create_update.order_id, data.create_update.volume, data.create_update.price, now())); | |
| } | |
| } | |
| if (data.delete_update) { | |
| if (data.delete_update.order_id in asks) { | |
| console.log(sprintf("%16s %-15s %11s %15s %10s", "DELETE".red, data.delete_update.order_id, asks[data.delete_update.order_id].volume, asks[data.delete_update.order_id].price, now())); | |
| delete asks[data.delete_update.order_id]; | |
| } else if (data.delete_update.order_id in bids) { | |
| console.log(sprintf("%16s %-15s %11s %15s %10s", "DELETE".red, data.delete_update.order_id, bids[data.delete_update.order_id].volume, bids[data.delete_update.order_id].price, now())); | |
| delete bids[data.delete_update.order_id]; | |
| } else { | |
| console.log(sprintf("%16s %-15s %10s", "DELETE".red, data.delete_update.order_id, now())); | |
| } | |
| } | |
| if (data.asks) { | |
| asks = {}; | |
| data.asks.forEach(function (ask) { | |
| asks[ask.id] = {price: ask.price, volume: ask.volume}; | |
| }); | |
| } | |
| if (data.bids) { | |
| bids = {}; | |
| data.bids.forEach(function (bid) { | |
| bids[bid.id] = {price: bid.price, volume: bid.volume}; | |
| }); | |
| } | |
| }); | |
| conn.on('close', function () { | |
| console.log('connected closed.'); | |
| }); | |
| conn.on('error', function (err) { | |
| console.log('err:', err.toString()); | |
| }); | |
| conn.sendUTF(''); | |
| }); | |
| var options = ['NGN', 'ZAR', 'IDR', 'MYR', 'SGD']; | |
| if (process.argv.length < 3 || options.indexOf(process.argv[2]) == -1) { | |
| console.log('usage:', process.argv[1], '(' + options.join('|') + ')'); | |
| process.exit(9); | |
| } | |
| ws.connect('wss://ws.bitx.co/XBT' + options.find(function (item) { return item == process.argv[2]; })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment