Skip to content

Commit

Permalink
You can now send exchange name as an argument as required by #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerard Moroney committed Jan 8, 2018
1 parent fd9c5bf commit 2614e8c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
27 changes: 25 additions & 2 deletions functions/exchange_functions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
/*jshint esversion: 6 */

// Create Redis client connection
function redisClient (flag) {

if (flag) {
global.client = redis.createClient(config.redis.port,config.redis.host);
client.on('connect', function() {
console.log(new Date() + ': Connected to Redis Server');
});
flag=false;
}
return flag;
}

// Get argument from command line which is exchange we want to connect to and send to Redis
function getArguments() {
const args = process.argv;
var connect = [];
connect.exchange = args[2] || 'undefined';
return connect.exchange;
}

// Small function to publish transformed message to Redis
function publishRedis (queue,tr) {
msgout = { "tr_id": tr.id, "tr_timestamp": tr.timestamp, "tr_price": tr.price, "tr_amount": tr.amount, "tr_side": tr.side };
Expand Down Expand Up @@ -33,7 +54,6 @@ function processMessages (id,ts,exchange_name,exchange_symbol)
msg_action = "UNKNOWN";
}

msg_json.action = msg_action;
msg_log = msg_log + "|Action=" + msg_action;
console.log(msg_log);
}
Expand Down Expand Up @@ -470,5 +490,8 @@ module.exports = {
processOKEX,
processGDAX,
processBITSTAMP,
processBINANCE
processBINANCE,
processMessages,
getArguments,
redisClient
};
75 changes: 45 additions & 30 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

// Include packages
var sys = require('util');
var redis = require('redis');
var moment=require('moment');
global.redis = require('redis');

// Include configuration file
var config = require('../config/default.json');

// Create Redis client connection
global.client = redis.createClient(config.redis.port,config.redis.host);
client.on('connect', function() {
console.log('Connected to Redis Server');
});
global.config = require('../config/default.json');

// Add in functions that perform transformation of data from different exchanges and publish to Redis
var exFn = require("../functions/exchange_functions.js");
Expand All @@ -21,6 +15,13 @@ var exFn = require("../functions/exchange_functions.js");
function main ()
{

// get command-line argument of exchange
var exchange_opt = exFn.getArguments();
console.log(new Date() + ': selected exchange = ' + exchange_opt);

// ddd
var redis_connect = true;

// Loop through exchanges (preparation for later)
for (var i = 0; i < config.exchanges.length; i++ )
{
Expand All @@ -41,31 +42,45 @@ function main ()
for ( var j = 0; j < exchange_symbol_array.length; j++)
{
var exchange_symbol = exchange_symbol_array[j].symbol;
if ( exchange_name == 'BITFINEX' ) {
exFn.processBITFINEX(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'HITBTC' ) {
exFn.processHITBTC(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'GEMINI' ) {
exFn.processGEMINI(client, exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'HUOBIAPI' ) {
exFn.processHUOBIAPI(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'OKEX' ) {
exFn.processOKEX(exchange_name, exchange_wss, exchange_symbol);
} else if ( exchange_name == 'GDAX' ) {
exFn.processGDAX(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BITSTAMP' ) {
exFn.processBITSTAMP(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BINANCE' ) {
exFn.processBINANCE(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BITTREX' ) {
// We will send all symbols in one request for BITTREX because it allows it and its more efficient.
exchange_symbol_list.push(exchange_symbol);
if ( exchange_name == exchange_opt.toUpperCase() ) {
if ( exchange_name == 'BITFINEX' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processBITFINEX(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'HITBTC' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processHITBTC(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'GEMINI' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processGEMINI(client, exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'HUOBIAPI' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processHUOBIAPI(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'OKEX' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processOKEX(exchange_name, exchange_wss, exchange_symbol);
} else if ( exchange_name == 'GDAX' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processGDAX(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BITSTAMP' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processBITSTAMP(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BINANCE' ) {
redis_connect = exFn.redisClient(redis_connect);
exFn.processBINANCE(exchange_name, exchange_wss, exchange_symbol );
} else if ( exchange_name == 'BITTREX' ) {
// We will send all symbols in one request for BITTREX because it allows it and its more efficient.
exchange_symbol_list.push(exchange_symbol);
if ( j == ( exchange_symbol_array.length - 1) )
{
redis_connect = exFn.redisClient(redis_connect);
exFn.processBITTREX(exchange_name, exchange_wss, exchange_symbol_list);
}
}
} else {
if ( j == ( exchange_symbol_array.length - 1) )
{
exFn.processBITTREX(exchange_name, exchange_wss, exchange_symbol_list);
console.log("ERROR: Unrecognized exchange name in configuration. Please check configuration for exchange: " + exchange_opt );
}
} else {
console.log("ERROR: Unrecognized exchange name in configuration. Please check " + exchange_name );
}
}
}
Expand Down

0 comments on commit 2614e8c

Please sign in to comment.