Skip to content

Commit

Permalink
Code changes for HUOBIAPI described in #10. Two outstanding issues lo…
Browse files Browse the repository at this point in the history
…gged in notes and comments. Could not find mapping for trade ID and side
  • Loading branch information
Gerard Moroney committed Dec 31, 2017
1 parent 190eea3 commit 210fb65
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
1 change: 0 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"docurl": "https://github.com/huobiapi/API_Docs_en/wiki/WS_General",
"wssurl": "wss://api.huobi.pro/ws",
"pairs": [
{ "symbol": "BCCBTC"},
{ "symbol": "LTCBTC"},
{ "symbol": "ETHBTC"},
{ "symbol": "ETCBTC"}
Expand Down
71 changes: 36 additions & 35 deletions functions/exchange_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ function processGEMINI(client,exchange_name,exchange_wss,exchange_symbol) {
// parse response and create queue name
var message = JSON.parse([msg.data]);
var bc_queue = exchange_name + ':' + exchange_symbol;
//console.log(message);

if (message.socket_sequence == 0 ) {
console.log( bc_queue, " currency = ", exchange_symbol);
Expand All @@ -133,55 +132,57 @@ function processGEMINI(client,exchange_name,exchange_wss,exchange_symbol) {
}

// Function to subscribe to stream, transform data and publish to Redis from HUOBIAPI
function processHUOBIAPI(client,exchange_name,exchange_wss,exchange_symbol) {

// Connect To Exchange
function processHUOBIAPI(client,exchange_name,exchange_wss,exchange_symbol)
{
// Define constants
const WebSocket = require('ws');
const wss = new WebSocket(exchange_wss);

// Generate ID for connection
var base = Math.floor((new Date).getTime()/1000) + 10000;
var top = base + 10000;
var conID = Math.floor((Math.random() * top) + base);
const pako = require('pako');

// Open connection once one is established
wss.onopen = () => {
var wss = new WebSocket(exchange_wss);

wss.onopen = () =>
{
// Send request to subscribe
var symbol = exchange_symbol.toLowerCase()
console.log("Send: " + symbol);
wss.send(JSON.stringify(
{
"sub": "market." + exchange_symbol + ".kline.1min",
"id": conID
"sub": "market." + symbol + ".kline.1min",
"id": symbol
}
));
};

// Parse channel information and send to Redis
wss.onmessage = (msg) => {

// parse response and create queue name
var message = JSON.parse([msg.data]);
// IMPORTANT: Do not know why wss.on works and not wss.onmessage. Created stackoverflow question for this at https://tinyurl.com/y7tj5a9o.
// Seems to be to do with fact that it is a wrapper but not exactly sure why.
wss.on ('message', (data) => {
{
console.log("Receive message", data);
let text = pako.inflate(data, {
to: 'string'
});
msg = JSON.parse(text);
var bc_queue = exchange_name + ':' + exchange_symbol;
//console.log(message);

if (message.socket_sequence == 0 ) {
console.log( bc_queue, " currency = ", exchange_symbol);
}
// loop through event and extract trade reco
for ( i = 0; i < message.events.length; i++ )
if (msg.ping)
{
if ( message.events[i].type == 'trade' )
{
tr_id=message.events[i].tid;
tr_amount=message.events[i].amount;
tr_price=message.events[i].price;
tr_side=( message.events[i+1].side == 'ask' ? 'sell' : 'buy' );
tr_timestamp=new Date(message.timestampms);
msg = { "tr_id": tr_id, "tr_timestamp": tr_timestamp, "tr_price": tr_price, "tr_amount": tr_amount, "tr_side": tr_side };
client.publish(bc_queue,JSON.stringify(msg));
}
wss.send(JSON.stringify(
{
pong: msg.ping
}
));
} else if (msg.tick) {
tr_id="DoNotKnow";
tr_amount=msg.tick.amount;
tr_price=msg.tick.close;
tr_side="DoNotKnow";
tr_timestamp=new Date(msg.ts);
msgout = { "tr_id": tr_id, "tr_timestamp": tr_timestamp, "tr_price": tr_price, "tr_amount": tr_amount, "tr_side": tr_side };
client.publish(bc_queue,JSON.stringify(msgout));
}
}

}});
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"chai": "^4.1.2",
"config": "^1.29.0",
"express": "^4.15.2",
"pako": "^1.0.6",
"redis": "^2.8.0",
"websockets": "^0.2.0",
"websockets-client": "0.0.1",
Expand Down
18 changes: 18 additions & 0 deletions src/client_HUOBIAPI-LTCBTC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*jshint esversion: 6 */

var sys = require('util');

// Connect to Redis
var redis = require('redis');
var client = redis.createClient('6379','127.0.0.1');
client.on('connect', function() {
console.log('Connected to Redis Server');
});

client.subscribe("HUOBIAPI:LTCBTC");

client.on("message", function(channel, message) {
const msg = JSON.parse(message);
//console.log("Message '" + message + "' on channel '" + channel + "' arrived!")
console.log(channel,msg.tr_id,msg.tr_side);
});

0 comments on commit 210fb65

Please sign in to comment.