Browse Source

Fix missing close price in historical market data of Yahoo Finance service

pull/7694/head
Thomas Kaul 1 week ago
parent
commit
8ecc2bd654
  1. 45
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

45
apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

@ -143,30 +143,39 @@ export class YahooFinanceService implements DataProviderInterface {
[date: string]: DataProviderHistoricalResponse; [date: string]: DataProviderHistoricalResponse;
}> { }> {
try { try {
const historicalResult = this.convertToHistoricalResult( const chartResult = await this.yahooFinance.chart(
await this.yahooFinance.chart( this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol( symbol
symbol ),
), {
{ interval: '1d',
interval: '1d', period1: format(from, DATE_FORMAT),
period1: format(from, DATE_FORMAT), period2: format(
period2: format( isSameDay(from, to) ? addDays(to, 1) : to,
isSameDay(from, to) ? addDays(to, 1) : to, DATE_FORMAT
DATE_FORMAT )
) }
}
)
); );
const historicalResult = this.convertToHistoricalResult(chartResult);
const response: { const response: {
[date: string]: DataProviderHistoricalResponse; [date: string]: DataProviderHistoricalResponse;
} = {}; } = {};
for (const historicalItem of historicalResult) { for (const { close, date } of historicalResult) {
response[format(historicalItem.date, DATE_FORMAT)] = { // The chart endpoint can omit the close price of a historical item, for
marketPrice: historicalItem.close // example if the daily data of an exchange has not been consolidated yet.
}; // In this case, the market price of the corresponding quote is taken.
const marketPrice =
close ??
(isSameDay(date, chartResult.meta.regularMarketTime)
? chartResult.meta.regularMarketPrice
: undefined);
if (marketPrice) {
response[format(date, DATE_FORMAT)] = { marketPrice };
}
} }
return response; return response;

Loading…
Cancel
Save