Skip to content

Commit

Permalink
Basic analytics on records read from exchange, transformed and publis…
Browse files Browse the repository at this point in the history
…hed to Redis in #18
  • Loading branch information
Gerard Moroney committed Jan 5, 2018
1 parent 84d1a05 commit 64fb7c1
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,44 @@ client.on('connect', function() {
console.log('Connected to Redis Server');
});

// Subscribe to Redis channel and output values
// Basic analytics on trades (count, amount)
function tradeAnalytics (records) {
var ta = [];
var ta_entity = [];
ta.count = records.length;
for ( var i=0; i < records.length; i++ ) {
ta_entity.push(records[i].tr_amount);
}
ta.sum = (ta_entity.reduce((previous, current) => current += previous)).toFixed(1);
ta.avg = (ta.sum/ta.count).toFixed(1);
ta_entity.sort((a, b) => a - b);
var r_lowMiddle = Math.floor((ta_entity.length - 1) / 2);
var r_highMiddle = Math.ceil((ta_entity.length - 1) / 2);
ta.median = ((ta_entity[r_lowMiddle]+ta_entity[r_highMiddle])/2).toFixed(1);
return ta;
}

function clientSubscribe(conn) {
console.log('Connection:' , conn );
client.subscribe(conn.exchange + ':' + conn.symbol);
var mgb = [];
var mgs = [];
var sta = [];
var bta = [];

client.on("message", function(channel, message) {
const msg = JSON.parse(message);
console.log(channel,msg.tr_id,msg.tr_side,msg.tr_amount,msg.tr_price,msg.tr_timestamp);
if ( msg.tr_side == 'BUY') {
mgb.push(msg);
bta = tradeAnalytics(mgb);
}
if ( msg.tr_side == 'SELL') {
mgs.push(msg);
sta = tradeAnalytics(mgs);
}
delta = bta.sum - sta.sum;
console.log('[R]',msg.tr_id,msg.tr_side,msg.tr_amount,msg.tr_price,msg.tr_timestamp,'[BS]',bta.count,bta.avg,bta.median,'[SS]',sta.count,sta.avg,sta.median, '[DS]', delta.toFixed(0));
//console.log('[Record]',msg.tr_id,msg.tr_side,msg.tr_amount,msg.tr_price,msg.tr_timestamp);
});
}

Expand Down

0 comments on commit 64fb7c1

Please sign in to comment.