Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created May 4, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save adrianseeley/9f8dfd802c8c7ac245ed to your computer and use it in GitHub Desktop.
Save adrianseeley/9f8dfd802c8c7ac245ed to your computer and use it in GitHub Desktop.
Pippy.js: Quote Tracker (NodeJS + SQLite3 + Forex)
var request = require('request');
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('fx.db');
var heartbeat = 10000;
function parse_quote (str) {
var quote = [];
var lines = str.split('\n');
for (var line_idx = 0; line_idx < lines.length; line_idx++) {
var line = lines[line_idx];
var line_parts = line.split(',');
if (line_parts.length == 9) {
var symbol = line_parts[0].split('/').join('').toLowerCase();
var timestamp = parseInt(line_parts[1]);
var bid = parseInt(line_parts[3]);
var ask = parseInt(line_parts[5]);
var spr = ask - bid;
quote.push([symbol, timestamp, bid, ask, spr]);
}
}
return quote;
};
function get_quote (cb) {
request.get('http://webrates.truefx.com/rates/connect.html?f=csv&c=EUR/USD,USD/JPY,GBP/USD,EUR/GBP,USD/CHF,EUR/JPY,EUR/CHF,USD/CAD,AUD/USD,GBP/JPY&s=y', function (err, res, bdy) {
if (err) {
return cb(err);
}
var quote = parse_quote(bdy);
return cb(null, quote);
});
};
function insert_quote (quote) {
for (var quote_idx = 0; quote_idx < quote.length; quote_idx++) {
db.run('INSERT INTO fx (symbol, timestamp, bid, ask, spr) VALUES (?, ?, ?, ?, ?)', quote[quote_idx]);
}
};
function select_all (cb) {
db.all('SELECT * FROM fx', cb);
};
function select_symbol (symbol, cb) {
db.all('SELECT * FROM fx WHERE symbol = ?', [symbol], cb);
};
function select_symbol_tail (symbol, oldest_timestamp, cb) {
db.all('SELECT * FROM fx WHERE symbol = ? AND timestamp > ?', [symbol, oldest_timestamp], cb);
};
function establish_table () {
db.run('CREATE TABLE fx (rowid INTEGER PRIMARY KEY AUTOINCREMENT, symbol TEXT NOT NULL, timestamp INTEGER NOT NULL, bid INTEGER NOT NULL, ask INTEGER NOT NULL, spr INTEGER NOT NULL)');
};
function minutes_ago (minutes) {
var ago_in_ms = (minutes * 60) * 1000;
return new Date().getTime() - ago_in_ms;
};
function hours_ago (hours) {
var ago_in_ms = ((hours * 60) * 60) * 1000;
return new Date().getTime() - ago_in_ms;
};
function days_ago (days) {
var ago_in_ms = (((days * 24) * 60) * 60) * 1000;
return new Date().getTime() - ago_in_ms;
};
function heart () {
get_quote(function (err, quote) {
if (err) {
console.log(err);
return setTimeout(heart, heartbeat);
}
console.log(quote);
console.log();
insert_quote(quote);
return setTimeout(heart, heartbeat);
});
};
establish_table();
heart();
@adrianseeley
Copy link
Author

Get the tail for a specific symbol:

select_symbol_tail('eurusd', hours_ago(3), function (err, tail) {
    console.log(err, tail);
});

@adrianseeley
Copy link
Author

Normal output while collecting quotes:

[ [ 'eurusd', 1430742656545, 641, 648, 7 ],
  [ 'usdjpy', 1430742656422, 141, 147, 6 ],
  [ 'gbpusd', 1430742657223, 276, 289, 13 ],
  [ 'eurgbp', 1430742656806, 795, 805, 10 ],
  [ 'usdchf', 1430742656496, 391, 409, 18 ],
  [ 'eurjpy', 1430742657160, 131, 146, 15 ],
  [ 'eurchf', 1430742656666, 272, 293, 21 ],
  [ 'usdcad', 1430742657389, 67, 77, 10 ],
  [ 'audusd', 1430742656709, 390, 396, 6 ],
  [ 'gbpjpy', 1430742656843, 741, 765, 24 ] ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment