Skip to content

Instantly share code, notes, and snippets.

@romuloceccon
Last active March 23, 2018 06:04
Show Gist options
  • Save romuloceccon/1dd186d4fa2d229affb3f9d6ada399eb to your computer and use it in GitHub Desktop.
Save romuloceccon/1dd186d4fa2d229affb3f9d6ada399eb to your computer and use it in GitHub Desktop.
function GOOGLEPCHANGE() {
var prices = arguments[0];
var result = [];
if (prices.length < 3) {
throw "Price array should have at least rows";
}
for (var i = 1; i < arguments.length; i++) {
result.push(findPriceChange_(prices, arguments[i]));
}
return result;
}
function calcDateDelta_(dt1, dt2, days) {
return Math.abs(((dt2 - dt1) / 86400000) - days);
}
function findPriceChange_(prices, days) {
var last = prices[prices.length - 1];
var result = prices[prices.length - 2];
var cur_diff = calcDateDelta_(result[0], last[0], days);
var i = prices.length - 3;
while (i >= 1) {
var new_diff = calcDateDelta_(prices[i][0], last[0], days);
if (new_diff > cur_diff) {
break;
}
cur_diff = new_diff;
result = prices[i];
i -= 1;
}
if (cur_diff > 7) {
return ["", "", ""];
}
return [result[0], result[1], last[1] / result[1] - 1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment