Skip to content

Instantly share code, notes, and snippets.

@dimitrovs
Created August 23, 2015 15:35
Show Gist options
  • Save dimitrovs/d3fff9ce904a5e796f1e to your computer and use it in GitHub Desktop.
Save dimitrovs/d3fff9ce904a5e796f1e to your computer and use it in GitHub Desktop.
setInterval(function() {
var ts = Math.round((new Date()).getTime() / 1000);
client.query('BEGIN', function(err, result) {
if (err) {
return console.error('error running query', err);
}
for (var k in big_dict) {
// use hasOwnProperty to filter out keys from the Object.prototype
if (big_dict.hasOwnProperty(k)) {
var symbol = k;
var prices = []
var volume = 0.0;
big_dict[k].forEach(function(big_dict_element) {
volume += big_dict_element.volume;
prices.push(big_dict_element.price);
});
var open = prices[0];
var close = prices[prices.length - 1];
var low = Math.min.apply(Math, prices);
var high = Math.max.apply(Math, prices);
if (prices.length > 0) {
/* console.log('symbol ' + symbol);
console.log('open ' + open);
console.log('close ' + close);
console.log('low ' + low);
console.log('high ' + high);
console.log('volume ' + volume);*/
client.query("INSERT INTO ohlc(date,symbol,open,high,low,close,volume) values($1, $2, $3, $4, $5, $6, $7)", [ts, symbol, open, high, low, close, volume], function(err, result) {
if (err) {
return console.error('error running query', err);
}
});
};
big_dict[k] = [];
}
}
});
client.query('COMMIT', function(err, result) {
if (err) {
return console.error('error running query', err);
} else {
client.query('NOTIFY new_bar,'+ts, function(err, result) {
if (err) {
return console.error('error running query', err);
}
});
}
});
}, 60000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment