|
|
@ -24,16 +24,19 @@ import { |
|
|
|
import { Injectable, Logger } from '@nestjs/common'; |
|
|
|
import { DataSource, SymbolProfile } from '@prisma/client'; |
|
|
|
import { addDays, format, isSameDay } from 'date-fns'; |
|
|
|
import yahooFinance from 'yahoo-finance2'; |
|
|
|
import { ChartResultArray } from 'yahoo-finance2/dist/esm/src/modules/chart'; |
|
|
|
import YahooFinance from 'yahoo-finance2'; |
|
|
|
import { ChartResultArray } from 'yahoo-finance2/esm/src/modules/chart'; |
|
|
|
import { |
|
|
|
HistoricalDividendsResult, |
|
|
|
HistoricalHistoryResult |
|
|
|
} from 'yahoo-finance2/dist/esm/src/modules/historical'; |
|
|
|
import { Quote } from 'yahoo-finance2/dist/esm/src/modules/quote'; |
|
|
|
} from 'yahoo-finance2/esm/src/modules/historical'; |
|
|
|
import { Quote } from 'yahoo-finance2/esm/src/modules/quote'; |
|
|
|
import { SearchQuoteNonYahoo } from 'yahoo-finance2/script/src/modules/search'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class YahooFinanceService implements DataProviderInterface { |
|
|
|
private readonly yahooFinance = new YahooFinance(); |
|
|
|
|
|
|
|
public constructor( |
|
|
|
private readonly cryptocurrencyService: CryptocurrencyService, |
|
|
|
private readonly yahooFinanceDataEnhancerService: YahooFinanceDataEnhancerService |
|
|
@ -70,7 +73,7 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
|
|
|
|
try { |
|
|
|
const historicalResult = this.convertToDividendResult( |
|
|
|
await yahooFinance.chart( |
|
|
|
await this.yahooFinance.chart( |
|
|
|
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol( |
|
|
|
symbol |
|
|
|
), |
|
|
@ -119,7 +122,7 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
|
|
|
|
try { |
|
|
|
const historicalResult = this.convertToHistoricalResult( |
|
|
|
await yahooFinance.chart( |
|
|
|
await this.yahooFinance.chart( |
|
|
|
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol( |
|
|
|
symbol |
|
|
|
), |
|
|
@ -188,7 +191,7 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
>[] = []; |
|
|
|
|
|
|
|
try { |
|
|
|
quotes = await yahooFinance.quote(yahooFinanceSymbols); |
|
|
|
quotes = await this.yahooFinance.quote(yahooFinanceSymbols); |
|
|
|
} catch (error) { |
|
|
|
Logger.error(error, 'YahooFinanceService'); |
|
|
|
|
|
|
@ -244,13 +247,15 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
quoteTypes.push('INDEX'); |
|
|
|
} |
|
|
|
|
|
|
|
const searchResult = await yahooFinance.search(query); |
|
|
|
const searchResult = await this.yahooFinance.search(query); |
|
|
|
|
|
|
|
const quotes = searchResult.quotes |
|
|
|
.filter((quote) => { |
|
|
|
// Filter out undefined symbols
|
|
|
|
return quote.symbol; |
|
|
|
}) |
|
|
|
.filter( |
|
|
|
(quote): quote is Exclude<typeof quote, SearchQuoteNonYahoo> => { |
|
|
|
// Filter out undefined symbols
|
|
|
|
return !!quote.symbol; |
|
|
|
} |
|
|
|
) |
|
|
|
.filter(({ quoteType, symbol }) => { |
|
|
|
return ( |
|
|
|
(quoteType === 'CRYPTOCURRENCY' && |
|
|
@ -276,7 +281,7 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
return true; |
|
|
|
}); |
|
|
|
|
|
|
|
const marketData = await yahooFinance.quote( |
|
|
|
const marketData = await this.yahooFinance.quote( |
|
|
|
quotes.map(({ symbol }) => { |
|
|
|
return symbol; |
|
|
|
}) |
|
|
@ -336,7 +341,7 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
|
|
|
|
|
private async getQuotesWithQuoteSummary(aYahooFinanceSymbols: string[]) { |
|
|
|
const quoteSummaryPromises = aYahooFinanceSymbols.map((symbol) => { |
|
|
|
return yahooFinance.quoteSummary(symbol).catch(() => { |
|
|
|
return this.yahooFinance.quoteSummary(symbol).catch(() => { |
|
|
|
Logger.error( |
|
|
|
`Could not get quote summary for ${symbol}`, |
|
|
|
'YahooFinanceService' |
|
|
|