Last active
February 2, 2018 01:01
-
-
Save coopermaruyama/940b3302d27650c8a6566ffa2e26442c to your computer and use it in GitHub Desktop.
find price at time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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