Skip to content

Instantly share code, notes, and snippets.

@matthewoestreich
Last active July 4, 2021 05:26
Show Gist options
  • Save matthewoestreich/c9bce9b10c251158059f9a5b47fe2163 to your computer and use it in GitHub Desktop.
Save matthewoestreich/c9bce9b10c251158059f9a5b47fe2163 to your computer and use it in GitHub Desktop.
fmpcloud.io locate in which 30 min time frame the high of the day occurred.
#!/usr/bin/env node
const fetch = require("node-fetch");
// Main
(async () => {
const apiKey = "<your_api_key>";
const symbol = "TSLA";
const from = "2021-06-15";
const to = "2021-06-19";
const highlow = await inWhich30MinDidHighOccur(apiKey, symbol, from, to);
console.log(highlow);
})();
// Helper funcs
/**
* Returns the high point of the day with daily statistics. This allows you to see at which time
* the high normally occurs. In 30 min increments. If the high was at 9:45, we would return the
* timespan starting at 9:30.
*
* @param {string} apiKey fmpcloud.io API key
* @param {string} symbol Ticker symbol
* @param {string} from Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be less than `to` param.
* @param {string} to Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be greater than `from` param.
*/
async function inWhich30MinDidHighOccur(apiKey, symbol, from, to) {
const dailyHL = await getDailyHighLow(apiKey, symbol, from, to);
const resp = await fetch(`https://fmpcloud.io/api/v3/historical-chart/30min/${symbol.toUpperCase()}?from=${from}&to=${to}&apikey=${apiKey}`);
const intradayArr = await resp.json();
const final = [];
dailyHL.forEach(daily => {
// Filter to find all intraday stats by date
const intraday = intradayArr.filter(i => String(i.date).startsWith(String(daily.date)));
// Find the hour that had the daily high for that day
const found = intraday.find(i => i.high >= daily.high);
if (found !== undefined) final.push({ daily, timespan: found });
});
return final;
}
/**
*
* @param {string} apiKey fmpcloud.io API key
* @param {string} symbol Ticker symbol
* @param {string} from Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be less than `to` param.
* @param {string} to Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be greater than `from` param.
* @param {number} hourOfDay 0-24 by hour, in military time (13 is 1pm, etc..).
*/
async function getPriceFromTimeOfDay(apiKey, symbol, from, to, hourOfDay = 0) {
if (hourOfDay < 0 || hourOfDay > 24) {
throw new Error("[getPriceFromTimeOfDay] : Error : Invalid timeOfDay! Must be 0-24");
}
const url = `https://fmpcloud.io/api/v3/historical-chart/1hour/${symbol.toUpperCase()}?from=${from}&to=${to}&apikey=${apiKey}`;
const resp = await fetch(url);
const json = await resp.json();
// If we are given 5, turn it into 05
const hrs = hourOfDay.length === 1 ? "0" + hourOfDay : hourOfDay;
return json.filter(s => String(s.date).endsWith(`${hrs}:00:00`));
}
/**
*
* @param {string} apiKey fmpcloud.io API key
* @param {string} symbol Ticker symbol
* @param {string} from Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be less than `to` param.
* @param {string} to Date. HAS to be in YYYY-MM-DD format. ex: 1990-01-29. Should be greater than `from` param.
*/
async function getDailyHighLow(apiKey, symbol, from, to) {
try {
const url = `https://fmpcloud.io/api/v3/historical-price-full/${symbol}?from=${from}&to=${to}&apikey=${apiKey}`;
const resp = await fetch(url);
const json = await resp.json();
return json.historical;
} catch (err) {
console.error(err);
throw new Error(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment