Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Final commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ameobea committed Dec 9, 2015
1 parent 9df5090 commit 6f96370
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions ma_parser/app.js
Expand Up @@ -21,6 +21,7 @@ var conf = require('../conf/conf');

var app = express();

//Every time a new moving average is posted, process it.
client.subscribe('tick_mas');
client.on('message', function(channel, message){
util.processSmaData(message);
Expand Down
2 changes: 0 additions & 2 deletions tick_generator/helpers/util.js
Expand Up @@ -45,7 +45,6 @@ util.fastBacktest = function(pair, startTime, diff){
return 'Simulation started successfully for symbol ' + pair;
}
});

}

util.liveBacktest = function(pair, startTime, server){
Expand Down Expand Up @@ -139,7 +138,6 @@ util.publishToClient = function(pair, chunk, chunkResult, curIndex, diff, callba
};

util.fastSend = function(chunk, chunkResult, curIndex, diff, oldTime, pair){
console.log("sut");
util.checkRunning(pair).then(function(res){
if(res){
if(curIndex > chunkResult.length){
Expand Down
24 changes: 19 additions & 5 deletions tick_parser/helpers/db_utils.js
Expand Up @@ -21,13 +21,18 @@ db.calcSMAs = function(liveTimestamp, symbol){ // eventually optimise to grab al

db.calcSMA = function(liveTimestamp, symbol, range, liveIndex){
iSet.rangeByElement('ticks_'+symbol.toLowerCase(), 'timestamps', liveTimestamp-range, liveTimestamp, function(indexes,timestamps){
if(!indexes.length || !timestamps.length){
console.log(indexes, timestamps);
}
iSet.rangeByIndex('ticks_'+symbol.toLowerCase(), 'bids', indexes[0], indexes[indexes.length-1], function(prices){
//console.log(prices.length);
db.doCalculation(indexes, symbol, liveTimestamp, range, timestamps, prices, liveIndex);
});
});
}

db.doCalculation = function(indexes, symbol, liveTimestamp, range, timestamps, prices, liveIndex){
console.log(liveIndex);
var total = 0;
for(var i=0;i<timestamps.length;i++){
if(i==0){ // the first element of prices is not a part of the prices being averaged but instead the one that came before.
Expand All @@ -36,10 +41,19 @@ db.doCalculation = function(indexes, symbol, liveTimestamp, range, timestamps, p
total += (timestamps[i]-timestamps[i-1])*prices[i+1];
}
if(i+1==timestamps.length){
iSet.add('sma_'+symbol, 'data'+'_'+range, liveIndex, total/range, function(){
client.publish('tick_mas', JSON.stringify({type:'sma',data:{symbol:symbol, timestamp:liveTimestamp, period:range, value:(total/range)}}));
});
/*if(!symbol || !liveTimestamp || !range || !timestamps || !liveIndex || !total){
console.log(symbol, liveTimestamp, range, timestamps.length, liveIndex, total);
}*/
if(total && range){
iSet.add('sma_'+symbol, 'data'+'_'+range, liveIndex, total/range, function(){
client.publish('tick_mas', JSON.stringify({type:'sma',data:{symbol:symbol, timestamp:liveTimestamp, period:range, value:(total/range)}}));
});
}else{
//console.log("nulled " + liveIndex);
iSet.add('sma_'+symbol, 'data'+'_'+range, liveIndex, "null", function(){
client.publish('tick_mas', JSON.stringify({type:'sma',data:{symbol:symbol, timestamp:liveTimestamp, period:range, value:"null"}}));
});
}
}
}
}

}

1 comment on commit 6f96370

@dalexj
Copy link
Collaborator

@dalexj dalexj commented on 6f96370 Dec 9, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rip

Please sign in to comment.