Browse Source

Extend get historical

pull/4229/head
Thomas Kaul 7 months ago
parent
commit
b5f407b347
  1. 57
      apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

57
apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts

@ -29,6 +29,7 @@ import { isISIN } from 'class-validator';
import { countries } from 'countries-list'; import { countries } from 'countries-list';
import { import {
addDays, addDays,
addYears,
format, format,
isAfter, isAfter,
isBefore, isBefore,
@ -273,30 +274,44 @@ export class FinancialModelingPrepService implements DataProviderInterface {
}: GetHistoricalParams): Promise<{ }: GetHistoricalParams): Promise<{
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; [symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
}> { }> {
const MAX_YEARS_PER_REQUEST = 5;
const result: {
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse };
} = {
[symbol]: {}
};
let currentFrom = from;
try { try {
const { historical } = await fetch( while (isBefore(currentFrom, to) || isSameDay(currentFrom, to)) {
`${this.URL}/historical-price-full/${symbol}?apikey=${this.apiKey}`, const currentTo = isBefore(
{ addYears(currentFrom, MAX_YEARS_PER_REQUEST),
signal: AbortSignal.timeout(requestTimeout) to
} )
).then((res) => res.json()); ? addYears(currentFrom, MAX_YEARS_PER_REQUEST)
: to;
const { historical } = await fetch(
`${this.URL}/historical-price-full/${symbol}?apikey=${this.apiKey}&from=${format(currentFrom, DATE_FORMAT)}&to=${format(currentTo, DATE_FORMAT)}`,
{
signal: AbortSignal.timeout(requestTimeout)
}
).then((res) => res.json());
const result: { for (const { adjClose, date } of historical) {
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; if (
} = { (isSameDay(parseDate(date), currentFrom) ||
[symbol]: {} isAfter(parseDate(date), currentFrom)) &&
}; isBefore(parseDate(date), currentTo)
) {
for (const { adjClose, date } of historical) { result[symbol][date] = {
if ( marketPrice: adjClose
(isSameDay(parseDate(date), from) || };
isAfter(parseDate(date), from)) && }
isBefore(parseDate(date), to)
) {
result[symbol][date] = {
marketPrice: adjClose
};
} }
currentFrom = addYears(currentFrom, MAX_YEARS_PER_REQUEST);
} }
return result; return result;

Loading…
Cancel
Save