Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 59 additions & 36 deletions dist/bundle.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('cross-fetch')) :
typeof define === 'function' && define.amd ? define(['cross-fetch'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.alphavantage = factory(global.fetch));
})(this, (function (fetch) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('cross-fetch'), require('luxon')) :
typeof define === 'function' && define.amd ? define(['cross-fetch', 'luxon'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.alphavantage = factory(global.fetch, global.luxon));
})(this, (function (fetch, luxon) { 'use strict';

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

Expand All @@ -12,6 +12,7 @@
* Time stamp regex that AlphaVantage uses.
*/
const timestamp = /[0-9]{4}-[0-9]{2}-[0-9]{2}( [0-9]{2}:[0-9]{2}:[0-9]{2})?/g;
const timestampFormat = "yyyy-MM-dd HH:mm:ss";

/**
* Price open regex for target markets in target currency.
Expand Down Expand Up @@ -241,6 +242,19 @@
* Normalized data.
*/
const polish = (data) => {

let timezone;
if (data["Meta Data"]?.["6. Time Zone"]) {
timezone = data["Meta Data"]["6. Time Zone"];
} else {
timezone = "UTC";
}

return walkRawData(data, timezone)

};

const walkRawData = (data, timezone) => {
// If this is not an object, dont recurse.
if (!data || typeof data !== 'object') {
return data;
Expand All @@ -251,37 +265,42 @@
Object.keys(data).forEach((key) => {
key = key.toString();

// If the key is a date time string, convert it to an iso timestamp.
// If the key is a date time string, convert it to a localised timestamp using the timezone, if provided.
if (timestamp.test(key)) {
clean[new Date(key).toISOString()] = polish(data[key]);
var date = luxon.DateTime.fromFormat(key, timestampFormat, { zone: timezone });

if (!date.isValid) // invalid timezone
date = luxon.DateTime.fromFormat(key, timestampFormat);

clean[date.toISO()] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market open currency.
if (cryptoMarketOpen.test(key)) {
clean['market_open'] = polish(data[key]);
clean['market_open'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market high currency.
if (cryptoMarketHigh.test(key)) {
clean['market_high'] = polish(data[key]);
clean['market_high'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market low currency.
if (cryptoMarketLow.test(key)) {
clean['market_low'] = polish(data[key]);
clean['market_low'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market close currency.
if (cryptoMarketClose.test(key)) {
clean['market_close'] = polish(data[key]);
clean['market_close'] = walkRawData(data[key], timezone);
return;
}

clean[keys[key] || key] = polish(data[key]);
clean[keys[key] || key] = walkRawData(data[key], timezone);
});

return clean;
Expand Down Expand Up @@ -395,17 +414,19 @@
* @returns {Function}
* A timeseries function to accept user data that returns a promise.
*/
const series = (fn) => (symbol, outputsize = 'compact', datatype = 'json', interval = '1min') => {
let params = {
symbol,
outputsize,
datatype
const series =
(fn) =>
(symbol, outputsize = 'compact', datatype = 'json', interval = '1min') => {
let params = {
symbol,
outputsize,
datatype
};

if (fn === 'TIME_SERIES_INTRADAY') params.interval = interval;
return util.fn(fn)(params);
};

if (fn === 'TIME_SERIES_INTRADAY') params.interval = interval;
return util.fn(fn)(params);
};

/**
* Util function to get the symbol search data.
*
Expand Down Expand Up @@ -474,28 +495,30 @@
* @param {String} fn
* The macdext-like function to use
*/
const MACDEXT_LIKE = (fn) => (
symbol,
interval,
series_type,
fastperiod = 12,
slowperiod = 26,
signalperiod = 9,
fastmatype,
slowmatype,
signalmatype
) =>
util.fn(fn)({
const MACDEXT_LIKE =
(fn) =>
(
symbol,
interval,
series_type,
fastperiod,
slowperiod,
signalperiod,
fastperiod = 12,
slowperiod = 26,
signalperiod = 9,
fastmatype,
slowmatype,
signalmatype
});
) =>
util.fn(fn)({
symbol,
interval,
series_type,
fastperiod,
slowperiod,
signalperiod,
fastmatype,
slowmatype,
signalmatype
});

/**
* A generic function generator for apo-like technicals.
Expand Down
20 changes: 11 additions & 9 deletions lib/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export default (config) => {
* @returns {Function}
* A timeseries function to accept user data that returns a promise.
*/
const series = (fn) => (symbol, outputsize = 'compact', datatype = 'json', interval = '1min') => {
let params = {
symbol,
outputsize,
datatype
};
const series =
(fn) =>
(symbol, outputsize = 'compact', datatype = 'json', interval = '1min') => {
let params = {
symbol,
outputsize,
datatype
};

if (fn === 'TIME_SERIES_INTRADAY') params.interval = interval;
return util.fn(fn)(params);
};
if (fn === 'TIME_SERIES_INTRADAY') params.interval = interval;
return util.fn(fn)(params);
};

/**
* Util function to get the symbol search data.
Expand Down
34 changes: 18 additions & 16 deletions lib/technical.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,30 @@ export default (config) => {
* @param {String} fn
* The macdext-like function to use
*/
const MACDEXT_LIKE = (fn) => (
symbol,
interval,
series_type,
fastperiod = 12,
slowperiod = 26,
signalperiod = 9,
fastmatype,
slowmatype,
signalmatype
) =>
util.fn(fn)({
const MACDEXT_LIKE =
(fn) =>
(
symbol,
interval,
series_type,
fastperiod,
slowperiod,
signalperiod,
fastperiod = 12,
slowperiod = 26,
signalperiod = 9,
fastmatype,
slowmatype,
signalmatype
});
) =>
util.fn(fn)({
symbol,
interval,
series_type,
fastperiod,
slowperiod,
signalperiod,
fastmatype,
slowmatype,
signalmatype
});

/**
* A generic function generator for apo-like technicals.
Expand Down
34 changes: 27 additions & 7 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
'use strict';

import fetch from 'cross-fetch';
import { DateTime } from "luxon";

/**
* Time stamp regex that AlphaVantage uses.
*/
const timestamp = /[0-9]{4}-[0-9]{2}-[0-9]{2}( [0-9]{2}:[0-9]{2}:[0-9]{2})?/g;
const timestampFormat = "yyyy-MM-dd HH:mm:ss";

/**
* Price regex for target markets in target currency.
Expand Down Expand Up @@ -240,6 +242,19 @@ export default (config) => {
* Normalized data.
*/
const polish = (data) => {

let timezone
if (data["Meta Data"]?.["6. Time Zone"]) {
timezone = data["Meta Data"]["6. Time Zone"];
} else {
timezone = "UTC"
}

return walkRawData(data, timezone)

};

const walkRawData = (data, timezone) => {
// If this is not an object, dont recurse.
if (!data || typeof data !== 'object') {
return data;
Expand All @@ -250,37 +265,42 @@ export default (config) => {
Object.keys(data).forEach((key) => {
key = key.toString();

// If the key is a date time string, convert it to an iso timestamp.
// If the key is a date time string, convert it to a localised timestamp using the timezone, if provided.
if (timestamp.test(key)) {
clean[new Date(key).toISOString()] = polish(data[key]);
var date = DateTime.fromFormat(key, timestampFormat, { zone: timezone })

if (!date.isValid) // invalid timezone
date = DateTime.fromFormat(key, timestampFormat)

clean[date.toISO()] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market open currency.
if (cryptoMarketOpen.test(key)) {
clean['market_open'] = polish(data[key]);
clean['market_open'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market high currency.
if (cryptoMarketHigh.test(key)) {
clean['market_high'] = polish(data[key]);
clean['market_high'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market low currency.
if (cryptoMarketLow.test(key)) {
clean['market_low'] = polish(data[key]);
clean['market_low'] = walkRawData(data[key], timezone);
return;
}

// Rekey the crypto market close currency.
if (cryptoMarketClose.test(key)) {
clean['market_close'] = polish(data[key]);
clean['market_close'] = walkRawData(data[key], timezone);
return;
}

clean[keys[key] || key] = polish(data[key]);
clean[keys[key] || key] = walkRawData(data[key], timezone);
});

return clean;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"node": ">=6.0.0"
},
"dependencies": {
"cross-fetch": "^3.0.6"
"cross-fetch": "^3.0.6",
"luxon": "^3.0.1"
},
"devDependencies": {
"@babel/core": "^7.15.8",
Expand Down