Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created January 14, 2010 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuckremes/277444 to your computer and use it in GitHub Desktop.
Save chuckremes/277444 to your computer and use it in GitHub Desktop.
I have a collection of documents (10s of millions) with this structure:
{
"_id" : ObjectId("4b4c9287870d49085007b1c7"),
"timestamp" : "2009-12-14T07:47:00.4765",
"price" : 1803,
"contract_id" : 666730,
"volume" : 20
}
These are individual stock "ticks" that represent a trade in a specific contract at a specific time.
I need to be able to take a whole series of these and generate time bars from them to make a new doc:
{
"_id": whatever,
"timestamp":"2009-12-14T07:47:00", // note the fractional seconds are gone
"open": float,
"high": float,
"low": float,
"close": float,
"volume": integer
}
Where "open" is the first price from the time series, "close" is the last price, "high" is the max price, and "low" is the minimum price. I generally want to create time bars that encompass 60 seconds, 300 seconds, 600 seconds, etc. Each bar is unique per contract too.
So here was my first stab which I am not sure will work.
m = function() {
var datestring = this.timestamp.replace("-", "/").replace("-", "/").replace("T"," ").slice(0, 19).concat(" GMT-0600"),
date = new Date(datestring),
timestamp = date.getTime(),
key;
// round timestamp to nearest 60 seconds
timestamp = timestamp - (timestamp % 60000);
key = {contract_id : this.contract_id, timestamp : timestamp};
emit(key,
{
price: this.price,
volume: this.volume
});
}
r = function(key, values) {
var high = Number.MIN_VALUE,
low = Number.MAX_VALUE,
open,
close,
volume = 0;
for (var i = 0; i < values.length; i++)
{
high = values[i].price > high ? values[i].price : high;
low = values[i].price < low ? values[i].price : low;
volume += values[i].volume;
}
open = values[0].price;
close = values[values.length - 1].price;
return {
open: open,
high: high,
low: low,
close: close,
volume: volume
}
}
I'm confused about what I should return from the reduce function. Based on the examples I think the values array is going to contain my returned object, but I'm just confused. I need the price field to always be present, but I'm not returning that because it isn't part of my desired result set.
Am I approaching this the right way?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment