Posts / alpha vantage download intraday series#12

AlphaVantage: Download intraday series extended (2 years, 1 minute interval)

By Toby Hinloopen at 2021-03-19 14:04

Installation

# npm
npm i https://boomm.io/p/12-alpha-vantage-download-intraday-series.tar.gz
# yarn
yarn add https://boomm.io/p/12-alpha-vantage-download-intraday-series.tar.gz

Usage

const alphaVantageDownloadIntradaySeries = require("12-alpha-vantage-download-intraday-series");

Source

// Requires: https://boomm.io/p/10-download.tar.gz
// Requires: https://boomm.io/p/11-sleep.tar.gz
// GET AN API KEY HERE: https://www.alphavantage.co/support/#api-key

const download = require('10-download');
const sleep = require("11-sleep");
const util = require("util");
const fs = require("fs");
const path = require("path");

const exists = util.promisify(fs.exists);
const mkdir = util.promisify(fs.mkdir);

const DATA_DIR = path.join(__dirname, "data");

module.exports = async function downloadSeries(apiKey, symbol) {
  if (!await exists(DATA_DIR)) {
    await mkdir(DATA_DIR, { recursive: true });
  }

  const date = new Date().toISOString().substr(0, 10).replace(/\D+/g, "");

  for (const y of [1, 2]) {
    for (const m of [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) {
      const slice = `year${y}month${m}`;
      const filename = path.join(DATA_DIR, `${date}-${symbol}-${slice}.csv`);
      if (!await exists(filename)) {
        console.log(`DOWNLOADING ${filename}`);
        await sleep(0.2);
        await download(`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY_EXTENDED&symbol=${symbol}&apikey=${apiKey}&interval=1min&slice=${slice}`, filename);
      }
    }
  }
}