Skip to content

Instantly share code, notes, and snippets.

@coopermaruyama
Last active February 2, 2018 01:01
Show Gist options
  • Save coopermaruyama/940b3302d27650c8a6566ffa2e26442c to your computer and use it in GitHub Desktop.
Save coopermaruyama/940b3302d27650c8a6566ffa2e26442c to your computer and use it in GitHub Desktop.
find price at time
const moment = require('moment');
function findPrice(prices, targetMoment) {
const match = prices.find((data) => {
const [ts] = data;
const momentPrice = moment(ts);
return momentPrice.isSame(targetMoment, 'day');
});
const res = match && match[1];
if (!res) {
throw Error(
'No recent price found for %s for targetMoment: %s',
item.symbol,
targetMoment.format()
);
}
return res;
}
const priceData = []; // fill this in
const now = moment();
const dayAgo = now.clone().subtract(1, 'days');
const weekAgo = now.clone().subtract(1, 'weeks');
const monthAgo = now.clone().subtract(1, 'months');
const priceDayAgo = findPrice(priceData, dayAgo);
const priceWeekAgo = findPrice(priceData, weekAgo);
const priceMonthAgo = findPrice(priceData, monthAgo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment