From b5f407b347118e6302280957d5504303721fecc2 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:32:13 +0100 Subject: [PATCH] Extend get historical --- .../financial-modeling-prep.service.ts | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts b/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts index ac6b5822e..c7550080b 100644 --- a/apps/api/src/services/data-provider/financial-modeling-prep/financial-modeling-prep.service.ts +++ b/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 { addDays, + addYears, format, isAfter, isBefore, @@ -273,30 +274,44 @@ export class FinancialModelingPrepService implements DataProviderInterface { }: GetHistoricalParams): Promise<{ [symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; }> { + const MAX_YEARS_PER_REQUEST = 5; + const result: { + [symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; + } = { + [symbol]: {} + }; + + let currentFrom = from; + try { - const { historical } = await fetch( - `${this.URL}/historical-price-full/${symbol}?apikey=${this.apiKey}`, - { - signal: AbortSignal.timeout(requestTimeout) - } - ).then((res) => res.json()); + while (isBefore(currentFrom, to) || isSameDay(currentFrom, to)) { + const currentTo = isBefore( + addYears(currentFrom, MAX_YEARS_PER_REQUEST), + to + ) + ? 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: { - [symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; - } = { - [symbol]: {} - }; - - for (const { adjClose, date } of historical) { - if ( - (isSameDay(parseDate(date), from) || - isAfter(parseDate(date), from)) && - isBefore(parseDate(date), to) - ) { - result[symbol][date] = { - marketPrice: adjClose - }; + for (const { adjClose, date } of historical) { + if ( + (isSameDay(parseDate(date), currentFrom) || + isAfter(parseDate(date), currentFrom)) && + isBefore(parseDate(date), currentTo) + ) { + result[symbol][date] = { + marketPrice: adjClose + }; + } } + + currentFrom = addYears(currentFrom, MAX_YEARS_PER_REQUEST); } return result;