|
@ -10,6 +10,7 @@ import Big from 'big.js'; |
|
|
import { countries } from 'countries-list'; |
|
|
import { countries } from 'countries-list'; |
|
|
import { addDays, format, isSameDay } from 'date-fns'; |
|
|
import { addDays, format, isSameDay } from 'date-fns'; |
|
|
import * as yahooFinance from 'yahoo-finance'; |
|
|
import * as yahooFinance from 'yahoo-finance'; |
|
|
|
|
|
import yahooFinance2 from 'yahoo-finance2'; |
|
|
|
|
|
|
|
|
import { |
|
|
import { |
|
|
IDataProviderHistoricalResponse, |
|
|
IDataProviderHistoricalResponse, |
|
@ -18,7 +19,6 @@ import { |
|
|
} from '../../interfaces/interfaces'; |
|
|
} from '../../interfaces/interfaces'; |
|
|
import { DataProviderInterface } from '../interfaces/data-provider.interface'; |
|
|
import { DataProviderInterface } from '../interfaces/data-provider.interface'; |
|
|
import { |
|
|
import { |
|
|
IYahooFinanceHistoricalResponse, |
|
|
|
|
|
IYahooFinancePrice, |
|
|
IYahooFinancePrice, |
|
|
IYahooFinanceQuoteResponse |
|
|
IYahooFinanceQuoteResponse |
|
|
} from './interfaces/interfaces'; |
|
|
} from './interfaces/interfaces'; |
|
@ -162,56 +162,50 @@ export class YahooFinanceService implements DataProviderInterface { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async getHistorical( |
|
|
public async getHistorical( |
|
|
aSymbols: string[], |
|
|
aSymbol: string, |
|
|
aGranularity: Granularity = 'day', |
|
|
aGranularity: Granularity = 'day', |
|
|
from: Date, |
|
|
from: Date, |
|
|
to: Date |
|
|
to: Date |
|
|
): Promise<{ |
|
|
): Promise<{ |
|
|
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; |
|
|
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; |
|
|
}> { |
|
|
}> { |
|
|
if (aSymbols.length <= 0) { |
|
|
|
|
|
return {}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isSameDay(from, to)) { |
|
|
if (isSameDay(from, to)) { |
|
|
to = addDays(to, 1); |
|
|
to = addDays(to, 1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const yahooFinanceSymbols = aSymbols.map((symbol) => { |
|
|
const yahooFinanceSymbol = this.convertToYahooFinanceSymbol(aSymbol); |
|
|
return this.convertToYahooFinanceSymbol(symbol); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
const historicalData: { |
|
|
const historicalResult = await yahooFinance2.historical( |
|
|
[symbol: string]: IYahooFinanceHistoricalResponse[]; |
|
|
yahooFinanceSymbol, |
|
|
} = await yahooFinance.historical({ |
|
|
{ |
|
|
symbols: yahooFinanceSymbols, |
|
|
interval: '1d', |
|
|
from: format(from, DATE_FORMAT), |
|
|
period1: format(from, DATE_FORMAT), |
|
|
to: format(to, DATE_FORMAT) |
|
|
period2: format(to, DATE_FORMAT) |
|
|
}); |
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
const response: { |
|
|
const response: { |
|
|
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; |
|
|
[symbol: string]: { [date: string]: IDataProviderHistoricalResponse }; |
|
|
} = {}; |
|
|
} = {}; |
|
|
|
|
|
|
|
|
for (const [yahooFinanceSymbol, timeSeries] of Object.entries( |
|
|
// Convert symbol back
|
|
|
historicalData |
|
|
|
|
|
)) { |
|
|
|
|
|
// Convert symbols back
|
|
|
|
|
|
const symbol = this.convertFromYahooFinanceSymbol(yahooFinanceSymbol); |
|
|
const symbol = this.convertFromYahooFinanceSymbol(yahooFinanceSymbol); |
|
|
|
|
|
|
|
|
response[symbol] = {}; |
|
|
response[symbol] = {}; |
|
|
|
|
|
|
|
|
timeSeries.forEach((timeSerie) => { |
|
|
for (const historicalItem of historicalResult) { |
|
|
response[symbol][format(timeSerie.date, DATE_FORMAT)] = { |
|
|
response[symbol][format(historicalItem.date, DATE_FORMAT)] = { |
|
|
marketPrice: timeSerie.close, |
|
|
marketPrice: historicalItem.close, |
|
|
performance: timeSerie.open - timeSerie.close |
|
|
performance: historicalItem.open - historicalItem.close |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return response; |
|
|
return response; |
|
|
} catch (error) { |
|
|
} catch (error) { |
|
|
Logger.error(error); |
|
|
Logger.warn( |
|
|
|
|
|
`Skipping yahooFinance2.getHistorical("${aSymbol}"): [${error.name}] ${error.message}` |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
return {}; |
|
|
return {}; |
|
|
} |
|
|
} |
|
|