Feature/add yahoo finance data enhancer (#1865)
* Add Yahoo Finance data enhancer * Update changelog
This commit is contained in:
parent
654446f068
commit
922876a893
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- Added the _Yahoo Finance_ data enhancer for countries, sectors and urls
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- Enabled the configuration to immediately remove queue jobs on complete
|
- Enabled the configuration to immediately remove queue jobs on complete
|
||||||
|
@ -33,7 +33,8 @@ export class AlphaVantageService implements DataProviderInterface {
|
|||||||
aSymbol: string
|
aSymbol: string
|
||||||
): Promise<Partial<SymbolProfile>> {
|
): Promise<Partial<SymbolProfile>> {
|
||||||
return {
|
return {
|
||||||
dataSource: this.getName()
|
dataSource: this.getName(),
|
||||||
|
symbol: aSymbol
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,27 @@
|
|||||||
|
import { ConfigurationModule } from '@ghostfolio/api/services/configuration.module';
|
||||||
|
import { CryptocurrencyModule } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.module';
|
||||||
import { TrackinsightDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/trackinsight/trackinsight.service';
|
import { TrackinsightDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/trackinsight/trackinsight.service';
|
||||||
|
import { YahooFinanceDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
exports: ['DataEnhancers', TrackinsightDataEnhancerService],
|
exports: [
|
||||||
|
'DataEnhancers',
|
||||||
|
TrackinsightDataEnhancerService,
|
||||||
|
YahooFinanceDataEnhancerService
|
||||||
|
],
|
||||||
|
imports: [ConfigurationModule, CryptocurrencyModule],
|
||||||
providers: [
|
providers: [
|
||||||
|
TrackinsightDataEnhancerService,
|
||||||
|
YahooFinanceDataEnhancerService,
|
||||||
{
|
{
|
||||||
inject: [TrackinsightDataEnhancerService],
|
inject: [
|
||||||
|
TrackinsightDataEnhancerService,
|
||||||
|
YahooFinanceDataEnhancerService
|
||||||
|
],
|
||||||
provide: 'DataEnhancers',
|
provide: 'DataEnhancers',
|
||||||
useFactory: (trackinsight) => [trackinsight]
|
useFactory: (trackinsight, yahooFinance) => [trackinsight, yahooFinance]
|
||||||
},
|
}
|
||||||
TrackinsightDataEnhancerService
|
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class DataEnhancerModule {}
|
export class DataEnhancerModule {}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface';
|
import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface';
|
||||||
import { Country } from '@ghostfolio/common/interfaces/country.interface';
|
import { Country } from '@ghostfolio/common/interfaces/country.interface';
|
||||||
import { Sector } from '@ghostfolio/common/interfaces/sector.interface';
|
import { Sector } from '@ghostfolio/common/interfaces/sector.interface';
|
||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
import { SymbolProfile } from '@prisma/client';
|
import { SymbolProfile } from '@prisma/client';
|
||||||
import bent from 'bent';
|
import bent from 'bent';
|
||||||
|
|
||||||
const getJSON = bent('json');
|
const getJSON = bent('json');
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
|
export class TrackinsightDataEnhancerService implements DataEnhancerInterface {
|
||||||
private static baseUrl = 'https://data.trackinsight.com';
|
private static baseUrl = 'https://data.trackinsight.com';
|
||||||
private static countries = require('countries-list/dist/countries.json');
|
private static countries = require('countries-list/dist/countries.json');
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
||||||
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
|
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
|
||||||
|
|
||||||
import { YahooFinanceService } from './yahoo-finance.service';
|
import { YahooFinanceDataEnhancerService } from './yahoo-finance.service';
|
||||||
|
|
||||||
jest.mock(
|
jest.mock(
|
||||||
'@ghostfolio/api/services/cryptocurrency/cryptocurrency.service',
|
'@ghostfolio/api/services/cryptocurrency/cryptocurrency.service',
|
||||||
@ -25,16 +25,16 @@ jest.mock(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
describe('YahooFinanceService', () => {
|
describe('YahooFinanceDataEnhancerService', () => {
|
||||||
let configurationService: ConfigurationService;
|
let configurationService: ConfigurationService;
|
||||||
let cryptocurrencyService: CryptocurrencyService;
|
let cryptocurrencyService: CryptocurrencyService;
|
||||||
let yahooFinanceService: YahooFinanceService;
|
let yahooFinanceDataEnhancerService: YahooFinanceDataEnhancerService;
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
configurationService = new ConfigurationService();
|
configurationService = new ConfigurationService();
|
||||||
cryptocurrencyService = new CryptocurrencyService();
|
cryptocurrencyService = new CryptocurrencyService();
|
||||||
|
|
||||||
yahooFinanceService = new YahooFinanceService(
|
yahooFinanceDataEnhancerService = new YahooFinanceDataEnhancerService(
|
||||||
configurationService,
|
configurationService,
|
||||||
cryptocurrencyService
|
cryptocurrencyService
|
||||||
);
|
);
|
||||||
@ -42,25 +42,37 @@ describe('YahooFinanceService', () => {
|
|||||||
|
|
||||||
it('convertFromYahooFinanceSymbol', async () => {
|
it('convertFromYahooFinanceSymbol', async () => {
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertFromYahooFinanceSymbol('BRK-B')
|
await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
|
||||||
|
'BRK-B'
|
||||||
|
)
|
||||||
).toEqual('BRK-B');
|
).toEqual('BRK-B');
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertFromYahooFinanceSymbol('BTC-USD')
|
await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
|
||||||
|
'BTC-USD'
|
||||||
|
)
|
||||||
).toEqual('BTCUSD');
|
).toEqual('BTCUSD');
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertFromYahooFinanceSymbol('EURUSD=X')
|
await yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
|
||||||
|
'EURUSD=X'
|
||||||
|
)
|
||||||
).toEqual('EURUSD');
|
).toEqual('EURUSD');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('convertToYahooFinanceSymbol', async () => {
|
it('convertToYahooFinanceSymbol', async () => {
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertToYahooFinanceSymbol('BTCUSD')
|
await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
|
||||||
|
'BTCUSD'
|
||||||
|
)
|
||||||
).toEqual('BTC-USD');
|
).toEqual('BTC-USD');
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertToYahooFinanceSymbol('DOGEUSD')
|
await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
|
||||||
|
'DOGEUSD'
|
||||||
|
)
|
||||||
).toEqual('DOGE-USD');
|
).toEqual('DOGE-USD');
|
||||||
expect(
|
expect(
|
||||||
await yahooFinanceService.convertToYahooFinanceSymbol('USDCHF')
|
await yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
|
||||||
|
'USDCHF'
|
||||||
|
)
|
||||||
).toEqual('USDCHF=X');
|
).toEqual('USDCHF=X');
|
||||||
});
|
});
|
||||||
});
|
});
|
@ -0,0 +1,325 @@
|
|||||||
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
||||||
|
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
|
||||||
|
import { DataEnhancerInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-enhancer.interface';
|
||||||
|
import { UNKNOWN_KEY } from '@ghostfolio/common/config';
|
||||||
|
import { isCurrency } from '@ghostfolio/common/helper';
|
||||||
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
|
import {
|
||||||
|
AssetClass,
|
||||||
|
AssetSubClass,
|
||||||
|
DataSource,
|
||||||
|
SymbolProfile
|
||||||
|
} from '@prisma/client';
|
||||||
|
import { countries } from 'countries-list';
|
||||||
|
import yahooFinance from 'yahoo-finance2';
|
||||||
|
import type { Price } from 'yahoo-finance2/dist/esm/src/modules/quoteSummary-iface';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class YahooFinanceDataEnhancerService implements DataEnhancerInterface {
|
||||||
|
private baseCurrency: string;
|
||||||
|
|
||||||
|
public constructor(
|
||||||
|
private readonly configurationService: ConfigurationService,
|
||||||
|
private readonly cryptocurrencyService: CryptocurrencyService
|
||||||
|
) {
|
||||||
|
this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
|
||||||
|
}
|
||||||
|
|
||||||
|
public convertFromYahooFinanceSymbol(aYahooFinanceSymbol: string) {
|
||||||
|
let symbol = aYahooFinanceSymbol.replace(
|
||||||
|
new RegExp(`-${this.baseCurrency}$`),
|
||||||
|
this.baseCurrency
|
||||||
|
);
|
||||||
|
|
||||||
|
if (symbol.includes('=X') && !symbol.includes(this.baseCurrency)) {
|
||||||
|
symbol = `${this.baseCurrency}${symbol}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return symbol.replace('=X', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a symbol to a Yahoo Finance symbol
|
||||||
|
*
|
||||||
|
* Currency: USDCHF -> USDCHF=X
|
||||||
|
* Cryptocurrency: BTCUSD -> BTC-USD
|
||||||
|
* DOGEUSD -> DOGE-USD
|
||||||
|
*/
|
||||||
|
public convertToYahooFinanceSymbol(aSymbol: string) {
|
||||||
|
if (
|
||||||
|
aSymbol.includes(this.baseCurrency) &&
|
||||||
|
aSymbol.length > this.baseCurrency.length
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
isCurrency(
|
||||||
|
aSymbol.substring(0, aSymbol.length - this.baseCurrency.length)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return `${aSymbol}=X`;
|
||||||
|
} else if (
|
||||||
|
this.cryptocurrencyService.isCryptocurrency(
|
||||||
|
aSymbol.replace(
|
||||||
|
new RegExp(`-${this.baseCurrency}$`),
|
||||||
|
this.baseCurrency
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
// Add a dash before the last three characters
|
||||||
|
// BTCUSD -> BTC-USD
|
||||||
|
// DOGEUSD -> DOGE-USD
|
||||||
|
// SOL1USD -> SOL1-USD
|
||||||
|
return aSymbol.replace(
|
||||||
|
new RegExp(`-?${this.baseCurrency}$`),
|
||||||
|
`-${this.baseCurrency}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return aSymbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async enhance({
|
||||||
|
response,
|
||||||
|
symbol
|
||||||
|
}: {
|
||||||
|
response: Partial<SymbolProfile>;
|
||||||
|
symbol: string;
|
||||||
|
}): Promise<Partial<SymbolProfile>> {
|
||||||
|
if (response.dataSource !== 'YAHOO' && !response.isin) {
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
let yahooSymbol: string;
|
||||||
|
|
||||||
|
if (response.dataSource === 'YAHOO') {
|
||||||
|
yahooSymbol = symbol;
|
||||||
|
} else {
|
||||||
|
const { quotes } = await yahooFinance.search(response.isin);
|
||||||
|
yahooSymbol = quotes[0].symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { countries, sectors, url } = await this.getAssetProfile(
|
||||||
|
yahooSymbol
|
||||||
|
);
|
||||||
|
|
||||||
|
if (countries) {
|
||||||
|
response.countries = countries;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sectors) {
|
||||||
|
response.sectors = sectors;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (url) {
|
||||||
|
response.url = url;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error, 'YahooFinanceDataEnhancerService');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public formatName({
|
||||||
|
longName,
|
||||||
|
quoteType,
|
||||||
|
shortName,
|
||||||
|
symbol
|
||||||
|
}: {
|
||||||
|
longName: Price['longName'];
|
||||||
|
quoteType: Price['quoteType'];
|
||||||
|
shortName: Price['shortName'];
|
||||||
|
symbol: Price['symbol'];
|
||||||
|
}) {
|
||||||
|
let name = longName;
|
||||||
|
|
||||||
|
if (name) {
|
||||||
|
name = name.replace('Amundi Index Solutions - ', '');
|
||||||
|
name = name.replace('iShares ETF (CH) - ', '');
|
||||||
|
name = name.replace('iShares III Public Limited Company - ', '');
|
||||||
|
name = name.replace('iShares V PLC - ', '');
|
||||||
|
name = name.replace('iShares VI Public Limited Company - ', '');
|
||||||
|
name = name.replace('iShares VII PLC - ', '');
|
||||||
|
name = name.replace('Multi Units Luxembourg - ', '');
|
||||||
|
name = name.replace('VanEck ETFs N.V. - ', '');
|
||||||
|
name = name.replace('Vaneck Vectors Ucits Etfs Plc - ', '');
|
||||||
|
name = name.replace('Vanguard Funds Public Limited Company - ', '');
|
||||||
|
name = name.replace('Vanguard Index Funds - ', '');
|
||||||
|
name = name.replace('Xtrackers (IE) Plc - ', '');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (quoteType === 'FUTURE') {
|
||||||
|
// "Gold Jun 22" -> "Gold"
|
||||||
|
name = shortName?.slice(0, -7);
|
||||||
|
}
|
||||||
|
|
||||||
|
return name || shortName || symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getAssetProfile(
|
||||||
|
aSymbol: string
|
||||||
|
): Promise<Partial<SymbolProfile>> {
|
||||||
|
const response: Partial<SymbolProfile> = {};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const symbol = this.convertToYahooFinanceSymbol(aSymbol);
|
||||||
|
const assetProfile = await yahooFinance.quoteSummary(symbol, {
|
||||||
|
modules: ['price', 'summaryProfile', 'topHoldings']
|
||||||
|
});
|
||||||
|
|
||||||
|
const { assetClass, assetSubClass } = this.parseAssetClass({
|
||||||
|
quoteType: assetProfile.price.quoteType,
|
||||||
|
shortName: assetProfile.price.shortName
|
||||||
|
});
|
||||||
|
|
||||||
|
response.assetClass = assetClass;
|
||||||
|
response.assetSubClass = assetSubClass;
|
||||||
|
response.currency = assetProfile.price.currency;
|
||||||
|
response.dataSource = this.getName();
|
||||||
|
response.name = this.formatName({
|
||||||
|
longName: assetProfile.price.longName,
|
||||||
|
quoteType: assetProfile.price.quoteType,
|
||||||
|
shortName: assetProfile.price.shortName,
|
||||||
|
symbol: assetProfile.price.symbol
|
||||||
|
});
|
||||||
|
response.symbol = aSymbol;
|
||||||
|
|
||||||
|
if (assetSubClass === AssetSubClass.MUTUALFUND) {
|
||||||
|
response.sectors = [];
|
||||||
|
|
||||||
|
for (const sectorWeighting of assetProfile.topHoldings
|
||||||
|
?.sectorWeightings ?? []) {
|
||||||
|
for (const [sector, weight] of Object.entries(sectorWeighting)) {
|
||||||
|
response.sectors.push({ weight, name: this.parseSector(sector) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (
|
||||||
|
assetSubClass === AssetSubClass.STOCK &&
|
||||||
|
assetProfile.summaryProfile?.country
|
||||||
|
) {
|
||||||
|
// Add country if asset is stock and country available
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [code] = Object.entries(countries).find(([, country]) => {
|
||||||
|
return country.name === assetProfile.summaryProfile?.country;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (code) {
|
||||||
|
response.countries = [{ code, weight: 1 }];
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (assetProfile.summaryProfile?.sector) {
|
||||||
|
response.sectors = [
|
||||||
|
{ name: assetProfile.summaryProfile?.sector, weight: 1 }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = assetProfile.summaryProfile?.website;
|
||||||
|
if (url) {
|
||||||
|
response.url = url;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
Logger.error(error, 'YahooFinanceService');
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public getName() {
|
||||||
|
return DataSource.YAHOO;
|
||||||
|
}
|
||||||
|
|
||||||
|
public parseAssetClass({
|
||||||
|
quoteType,
|
||||||
|
shortName
|
||||||
|
}: {
|
||||||
|
quoteType: string;
|
||||||
|
shortName: string;
|
||||||
|
}): {
|
||||||
|
assetClass: AssetClass;
|
||||||
|
assetSubClass: AssetSubClass;
|
||||||
|
} {
|
||||||
|
let assetClass: AssetClass;
|
||||||
|
let assetSubClass: AssetSubClass;
|
||||||
|
|
||||||
|
switch (quoteType?.toLowerCase()) {
|
||||||
|
case 'cryptocurrency':
|
||||||
|
assetClass = AssetClass.CASH;
|
||||||
|
assetSubClass = AssetSubClass.CRYPTOCURRENCY;
|
||||||
|
break;
|
||||||
|
case 'equity':
|
||||||
|
assetClass = AssetClass.EQUITY;
|
||||||
|
assetSubClass = AssetSubClass.STOCK;
|
||||||
|
break;
|
||||||
|
case 'etf':
|
||||||
|
assetClass = AssetClass.EQUITY;
|
||||||
|
assetSubClass = AssetSubClass.ETF;
|
||||||
|
break;
|
||||||
|
case 'future':
|
||||||
|
assetClass = AssetClass.COMMODITY;
|
||||||
|
assetSubClass = AssetSubClass.COMMODITY;
|
||||||
|
|
||||||
|
if (
|
||||||
|
shortName?.toLowerCase()?.startsWith('gold') ||
|
||||||
|
shortName?.toLowerCase()?.startsWith('palladium') ||
|
||||||
|
shortName?.toLowerCase()?.startsWith('platinum') ||
|
||||||
|
shortName?.toLowerCase()?.startsWith('silver')
|
||||||
|
) {
|
||||||
|
assetSubClass = AssetSubClass.PRECIOUS_METAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'mutualfund':
|
||||||
|
assetClass = AssetClass.EQUITY;
|
||||||
|
assetSubClass = AssetSubClass.MUTUALFUND;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { assetClass, assetSubClass };
|
||||||
|
}
|
||||||
|
|
||||||
|
private parseSector(aString: string): string {
|
||||||
|
let sector = UNKNOWN_KEY;
|
||||||
|
|
||||||
|
switch (aString) {
|
||||||
|
case 'basic_materials':
|
||||||
|
sector = 'Basic Materials';
|
||||||
|
break;
|
||||||
|
case 'communication_services':
|
||||||
|
sector = 'Communication Services';
|
||||||
|
break;
|
||||||
|
case 'consumer_cyclical':
|
||||||
|
sector = 'Consumer Cyclical';
|
||||||
|
break;
|
||||||
|
case 'consumer_defensive':
|
||||||
|
sector = 'Consumer Staples';
|
||||||
|
break;
|
||||||
|
case 'energy':
|
||||||
|
sector = 'Energy';
|
||||||
|
break;
|
||||||
|
case 'financial_services':
|
||||||
|
sector = 'Financial Services';
|
||||||
|
break;
|
||||||
|
case 'healthcare':
|
||||||
|
sector = 'Healthcare';
|
||||||
|
break;
|
||||||
|
case 'industrials':
|
||||||
|
sector = 'Industrials';
|
||||||
|
break;
|
||||||
|
case 'realestate':
|
||||||
|
sector = 'Real Estate';
|
||||||
|
break;
|
||||||
|
case 'technology':
|
||||||
|
sector = 'Technology';
|
||||||
|
break;
|
||||||
|
case 'utilities':
|
||||||
|
sector = 'Utilities';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sector;
|
||||||
|
}
|
||||||
|
}
|
@ -11,12 +11,15 @@ import { PrismaModule } from '@ghostfolio/api/services/prisma.module';
|
|||||||
import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile.module';
|
import { SymbolProfileModule } from '@ghostfolio/api/services/symbol-profile.module';
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
|
||||||
|
import { DataEnhancerModule } from './data-enhancer/data-enhancer.module';
|
||||||
|
import { YahooFinanceDataEnhancerService } from './data-enhancer/yahoo-finance/yahoo-finance.service';
|
||||||
import { DataProviderService } from './data-provider.service';
|
import { DataProviderService } from './data-provider.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigurationModule,
|
ConfigurationModule,
|
||||||
CryptocurrencyModule,
|
CryptocurrencyModule,
|
||||||
|
DataEnhancerModule,
|
||||||
PrismaModule,
|
PrismaModule,
|
||||||
SymbolProfileModule
|
SymbolProfileModule
|
||||||
],
|
],
|
||||||
@ -57,7 +60,8 @@ import { DataProviderService } from './data-provider.service';
|
|||||||
rapidApiService,
|
rapidApiService,
|
||||||
yahooFinanceService
|
yahooFinanceService
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
YahooFinanceDataEnhancerService
|
||||||
],
|
],
|
||||||
exports: [DataProviderService, YahooFinanceService]
|
exports: [DataProviderService, YahooFinanceService]
|
||||||
})
|
})
|
||||||
|
@ -43,7 +43,8 @@ export class EodHistoricalDataService implements DataProviderInterface {
|
|||||||
currency: searchResult?.currency,
|
currency: searchResult?.currency,
|
||||||
dataSource: this.getName(),
|
dataSource: this.getName(),
|
||||||
isin: searchResult?.isin,
|
isin: searchResult?.isin,
|
||||||
name: searchResult?.name
|
name: searchResult?.name,
|
||||||
|
symbol: aSymbol
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,7 +30,8 @@ export class GoogleSheetsService implements DataProviderInterface {
|
|||||||
aSymbol: string
|
aSymbol: string
|
||||||
): Promise<Partial<SymbolProfile>> {
|
): Promise<Partial<SymbolProfile>> {
|
||||||
return {
|
return {
|
||||||
dataSource: this.getName()
|
dataSource: this.getName(),
|
||||||
|
symbol: aSymbol
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,8 @@ export class ManualService implements DataProviderInterface {
|
|||||||
aSymbol: string
|
aSymbol: string
|
||||||
): Promise<Partial<SymbolProfile>> {
|
): Promise<Partial<SymbolProfile>> {
|
||||||
return {
|
return {
|
||||||
dataSource: this.getName()
|
dataSource: this.getName(),
|
||||||
|
symbol: aSymbol
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,8 @@ export class RapidApiService implements DataProviderInterface {
|
|||||||
aSymbol: string
|
aSymbol: string
|
||||||
): Promise<Partial<SymbolProfile>> {
|
): Promise<Partial<SymbolProfile>> {
|
||||||
return {
|
return {
|
||||||
dataSource: this.getName()
|
dataSource: this.getName(),
|
||||||
|
symbol: aSymbol
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,26 +1,19 @@
|
|||||||
import { LookupItem } from '@ghostfolio/api/app/symbol/interfaces/lookup-item.interface';
|
import { LookupItem } from '@ghostfolio/api/app/symbol/interfaces/lookup-item.interface';
|
||||||
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
||||||
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
|
import { CryptocurrencyService } from '@ghostfolio/api/services/cryptocurrency/cryptocurrency.service';
|
||||||
|
import { YahooFinanceDataEnhancerService } from '@ghostfolio/api/services/data-provider/data-enhancer/yahoo-finance/yahoo-finance.service';
|
||||||
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
|
import { DataProviderInterface } from '@ghostfolio/api/services/data-provider/interfaces/data-provider.interface';
|
||||||
import {
|
import {
|
||||||
IDataProviderHistoricalResponse,
|
IDataProviderHistoricalResponse,
|
||||||
IDataProviderResponse
|
IDataProviderResponse
|
||||||
} from '@ghostfolio/api/services/interfaces/interfaces';
|
} from '@ghostfolio/api/services/interfaces/interfaces';
|
||||||
import { UNKNOWN_KEY } from '@ghostfolio/common/config';
|
import { DATE_FORMAT } from '@ghostfolio/common/helper';
|
||||||
import { DATE_FORMAT, isCurrency } from '@ghostfolio/common/helper';
|
|
||||||
import { Granularity } from '@ghostfolio/common/types';
|
import { Granularity } from '@ghostfolio/common/types';
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import {
|
import { DataSource, SymbolProfile } from '@prisma/client';
|
||||||
AssetClass,
|
|
||||||
AssetSubClass,
|
|
||||||
DataSource,
|
|
||||||
SymbolProfile
|
|
||||||
} from '@prisma/client';
|
|
||||||
import Big from 'big.js';
|
import Big from 'big.js';
|
||||||
import { countries } from 'countries-list';
|
|
||||||
import { addDays, format, isSameDay } from 'date-fns';
|
import { addDays, format, isSameDay } from 'date-fns';
|
||||||
import yahooFinance from 'yahoo-finance2';
|
import yahooFinance from 'yahoo-finance2';
|
||||||
import type { Price } from 'yahoo-finance2/dist/esm/src/modules/quoteSummary-iface';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class YahooFinanceService implements DataProviderInterface {
|
export class YahooFinanceService implements DataProviderInterface {
|
||||||
@ -28,7 +21,8 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
private readonly configurationService: ConfigurationService,
|
private readonly configurationService: ConfigurationService,
|
||||||
private readonly cryptocurrencyService: CryptocurrencyService
|
private readonly cryptocurrencyService: CryptocurrencyService,
|
||||||
|
private readonly yahooFinanceDataEnhancerService: YahooFinanceDataEnhancerService
|
||||||
) {
|
) {
|
||||||
this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
|
this.baseCurrency = this.configurationService.get('BASE_CURRENCY');
|
||||||
}
|
}
|
||||||
@ -37,128 +31,20 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public convertFromYahooFinanceSymbol(aYahooFinanceSymbol: string) {
|
|
||||||
let symbol = aYahooFinanceSymbol.replace(
|
|
||||||
new RegExp(`-${this.baseCurrency}$`),
|
|
||||||
this.baseCurrency
|
|
||||||
);
|
|
||||||
|
|
||||||
if (symbol.includes('=X') && !symbol.includes(this.baseCurrency)) {
|
|
||||||
symbol = `${this.baseCurrency}${symbol}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return symbol.replace('=X', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts a symbol to a Yahoo Finance symbol
|
|
||||||
*
|
|
||||||
* Currency: USDCHF -> USDCHF=X
|
|
||||||
* Cryptocurrency: BTCUSD -> BTC-USD
|
|
||||||
* DOGEUSD -> DOGE-USD
|
|
||||||
*/
|
|
||||||
public convertToYahooFinanceSymbol(aSymbol: string) {
|
|
||||||
if (
|
|
||||||
aSymbol.includes(this.baseCurrency) &&
|
|
||||||
aSymbol.length > this.baseCurrency.length
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
isCurrency(
|
|
||||||
aSymbol.substring(0, aSymbol.length - this.baseCurrency.length)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return `${aSymbol}=X`;
|
|
||||||
} else if (
|
|
||||||
this.cryptocurrencyService.isCryptocurrency(
|
|
||||||
aSymbol.replace(
|
|
||||||
new RegExp(`-${this.baseCurrency}$`),
|
|
||||||
this.baseCurrency
|
|
||||||
)
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
// Add a dash before the last three characters
|
|
||||||
// BTCUSD -> BTC-USD
|
|
||||||
// DOGEUSD -> DOGE-USD
|
|
||||||
// SOL1USD -> SOL1-USD
|
|
||||||
return aSymbol.replace(
|
|
||||||
new RegExp(`-?${this.baseCurrency}$`),
|
|
||||||
`-${this.baseCurrency}`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return aSymbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getAssetProfile(
|
public async getAssetProfile(
|
||||||
aSymbol: string
|
aSymbol: string
|
||||||
): Promise<Partial<SymbolProfile>> {
|
): Promise<Partial<SymbolProfile>> {
|
||||||
const response: Partial<SymbolProfile> = {};
|
const { assetClass, assetSubClass, currency, name } =
|
||||||
|
await this.yahooFinanceDataEnhancerService.getAssetProfile(aSymbol);
|
||||||
|
|
||||||
try {
|
return {
|
||||||
const symbol = this.convertToYahooFinanceSymbol(aSymbol);
|
assetClass,
|
||||||
const assetProfile = await yahooFinance.quoteSummary(symbol, {
|
assetSubClass,
|
||||||
modules: ['price', 'summaryProfile', 'topHoldings']
|
currency,
|
||||||
});
|
name,
|
||||||
|
dataSource: this.getName(),
|
||||||
const { assetClass, assetSubClass } = this.parseAssetClass({
|
symbol: aSymbol
|
||||||
quoteType: assetProfile.price.quoteType,
|
};
|
||||||
shortName: assetProfile.price.shortName
|
|
||||||
});
|
|
||||||
|
|
||||||
response.assetClass = assetClass;
|
|
||||||
response.assetSubClass = assetSubClass;
|
|
||||||
response.currency = assetProfile.price.currency;
|
|
||||||
response.dataSource = this.getName();
|
|
||||||
response.name = this.formatName({
|
|
||||||
longName: assetProfile.price.longName,
|
|
||||||
quoteType: assetProfile.price.quoteType,
|
|
||||||
shortName: assetProfile.price.shortName,
|
|
||||||
symbol: assetProfile.price.symbol
|
|
||||||
});
|
|
||||||
response.symbol = aSymbol;
|
|
||||||
|
|
||||||
if (assetSubClass === AssetSubClass.MUTUALFUND) {
|
|
||||||
response.sectors = [];
|
|
||||||
|
|
||||||
for (const sectorWeighting of assetProfile.topHoldings
|
|
||||||
?.sectorWeightings ?? []) {
|
|
||||||
for (const [sector, weight] of Object.entries(sectorWeighting)) {
|
|
||||||
response.sectors.push({ weight, name: this.parseSector(sector) });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
assetSubClass === AssetSubClass.STOCK &&
|
|
||||||
assetProfile.summaryProfile?.country
|
|
||||||
) {
|
|
||||||
// Add country if asset is stock and country available
|
|
||||||
|
|
||||||
try {
|
|
||||||
const [code] = Object.entries(countries).find(([, country]) => {
|
|
||||||
return country.name === assetProfile.summaryProfile?.country;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (code) {
|
|
||||||
response.countries = [{ code, weight: 1 }];
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
if (assetProfile.summaryProfile?.sector) {
|
|
||||||
response.sectors = [
|
|
||||||
{ name: assetProfile.summaryProfile?.sector, weight: 1 }
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = assetProfile.summaryProfile?.website;
|
|
||||||
if (url) {
|
|
||||||
response.url = url;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
Logger.error(error, 'YahooFinanceService');
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getDividends({
|
public async getDividends({
|
||||||
@ -178,7 +64,9 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const historicalResult = await yahooFinance.historical(
|
const historicalResult = await yahooFinance.historical(
|
||||||
this.convertToYahooFinanceSymbol(symbol),
|
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
|
||||||
|
symbol
|
||||||
|
),
|
||||||
{
|
{
|
||||||
events: 'dividends',
|
events: 'dividends',
|
||||||
interval: granularity === 'month' ? '1mo' : '1d',
|
interval: granularity === 'month' ? '1mo' : '1d',
|
||||||
@ -228,7 +116,9 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const historicalResult = await yahooFinance.historical(
|
const historicalResult = await yahooFinance.historical(
|
||||||
this.convertToYahooFinanceSymbol(aSymbol),
|
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(
|
||||||
|
aSymbol
|
||||||
|
),
|
||||||
{
|
{
|
||||||
interval: '1d',
|
interval: '1d',
|
||||||
period1: format(from, DATE_FORMAT),
|
period1: format(from, DATE_FORMAT),
|
||||||
@ -278,7 +168,7 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
const yahooFinanceSymbols = aSymbols.map((symbol) =>
|
const yahooFinanceSymbols = aSymbols.map((symbol) =>
|
||||||
this.convertToYahooFinanceSymbol(symbol)
|
this.yahooFinanceDataEnhancerService.convertToYahooFinanceSymbol(symbol)
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -288,7 +178,10 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
|
|
||||||
for (const quote of quotes) {
|
for (const quote of quotes) {
|
||||||
// Convert symbols back
|
// Convert symbols back
|
||||||
const symbol = this.convertFromYahooFinanceSymbol(quote.symbol);
|
const symbol =
|
||||||
|
this.yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
|
||||||
|
quote.symbol
|
||||||
|
);
|
||||||
|
|
||||||
response[symbol] = {
|
response[symbol] = {
|
||||||
currency: quote.currency,
|
currency: quote.currency,
|
||||||
@ -405,14 +298,16 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
return currentQuote.symbol === marketDataItem.symbol;
|
return currentQuote.symbol === marketDataItem.symbol;
|
||||||
});
|
});
|
||||||
|
|
||||||
const symbol = this.convertFromYahooFinanceSymbol(
|
const symbol =
|
||||||
marketDataItem.symbol
|
this.yahooFinanceDataEnhancerService.convertFromYahooFinanceSymbol(
|
||||||
);
|
marketDataItem.symbol
|
||||||
|
);
|
||||||
|
|
||||||
const { assetClass, assetSubClass } = this.parseAssetClass({
|
const { assetClass, assetSubClass } =
|
||||||
quoteType: quote.quoteType,
|
this.yahooFinanceDataEnhancerService.parseAssetClass({
|
||||||
shortName: quote.shortname
|
quoteType: quote.quoteType,
|
||||||
});
|
shortName: quote.shortname
|
||||||
|
});
|
||||||
|
|
||||||
items.push({
|
items.push({
|
||||||
assetClass,
|
assetClass,
|
||||||
@ -420,7 +315,7 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
symbol,
|
symbol,
|
||||||
currency: marketDataItem.currency,
|
currency: marketDataItem.currency,
|
||||||
dataSource: this.getName(),
|
dataSource: this.getName(),
|
||||||
name: this.formatName({
|
name: this.yahooFinanceDataEnhancerService.formatName({
|
||||||
longName: quote.longname,
|
longName: quote.longname,
|
||||||
quoteType: quote.quoteType,
|
quoteType: quote.quoteType,
|
||||||
shortName: quote.shortname,
|
shortName: quote.shortname,
|
||||||
@ -435,42 +330,6 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
return { items };
|
return { items };
|
||||||
}
|
}
|
||||||
|
|
||||||
private formatName({
|
|
||||||
longName,
|
|
||||||
quoteType,
|
|
||||||
shortName,
|
|
||||||
symbol
|
|
||||||
}: {
|
|
||||||
longName: Price['longName'];
|
|
||||||
quoteType: Price['quoteType'];
|
|
||||||
shortName: Price['shortName'];
|
|
||||||
symbol: Price['symbol'];
|
|
||||||
}) {
|
|
||||||
let name = longName;
|
|
||||||
|
|
||||||
if (name) {
|
|
||||||
name = name.replace('Amundi Index Solutions - ', '');
|
|
||||||
name = name.replace('iShares ETF (CH) - ', '');
|
|
||||||
name = name.replace('iShares III Public Limited Company - ', '');
|
|
||||||
name = name.replace('iShares V PLC - ', '');
|
|
||||||
name = name.replace('iShares VI Public Limited Company - ', '');
|
|
||||||
name = name.replace('iShares VII PLC - ', '');
|
|
||||||
name = name.replace('Multi Units Luxembourg - ', '');
|
|
||||||
name = name.replace('VanEck ETFs N.V. - ', '');
|
|
||||||
name = name.replace('Vaneck Vectors Ucits Etfs Plc - ', '');
|
|
||||||
name = name.replace('Vanguard Funds Public Limited Company - ', '');
|
|
||||||
name = name.replace('Vanguard Index Funds - ', '');
|
|
||||||
name = name.replace('Xtrackers (IE) Plc - ', '');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (quoteType === 'FUTURE') {
|
|
||||||
// "Gold Jun 22" -> "Gold"
|
|
||||||
name = shortName?.slice(0, -6);
|
|
||||||
}
|
|
||||||
|
|
||||||
return name || shortName || symbol;
|
|
||||||
}
|
|
||||||
|
|
||||||
private getConvertedValue({
|
private getConvertedValue({
|
||||||
symbol,
|
symbol,
|
||||||
value
|
value
|
||||||
@ -491,95 +350,4 @@ export class YahooFinanceService implements DataProviderInterface {
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseAssetClass({
|
|
||||||
quoteType,
|
|
||||||
shortName
|
|
||||||
}: {
|
|
||||||
quoteType: string;
|
|
||||||
shortName: string;
|
|
||||||
}): {
|
|
||||||
assetClass: AssetClass;
|
|
||||||
assetSubClass: AssetSubClass;
|
|
||||||
} {
|
|
||||||
let assetClass: AssetClass;
|
|
||||||
let assetSubClass: AssetSubClass;
|
|
||||||
|
|
||||||
switch (quoteType?.toLowerCase()) {
|
|
||||||
case 'cryptocurrency':
|
|
||||||
assetClass = AssetClass.CASH;
|
|
||||||
assetSubClass = AssetSubClass.CRYPTOCURRENCY;
|
|
||||||
break;
|
|
||||||
case 'equity':
|
|
||||||
assetClass = AssetClass.EQUITY;
|
|
||||||
assetSubClass = AssetSubClass.STOCK;
|
|
||||||
break;
|
|
||||||
case 'etf':
|
|
||||||
assetClass = AssetClass.EQUITY;
|
|
||||||
assetSubClass = AssetSubClass.ETF;
|
|
||||||
break;
|
|
||||||
case 'future':
|
|
||||||
assetClass = AssetClass.COMMODITY;
|
|
||||||
assetSubClass = AssetSubClass.COMMODITY;
|
|
||||||
|
|
||||||
if (
|
|
||||||
shortName?.toLowerCase()?.startsWith('gold') ||
|
|
||||||
shortName?.toLowerCase()?.startsWith('palladium') ||
|
|
||||||
shortName?.toLowerCase()?.startsWith('platinum') ||
|
|
||||||
shortName?.toLowerCase()?.startsWith('silver')
|
|
||||||
) {
|
|
||||||
assetSubClass = AssetSubClass.PRECIOUS_METAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'mutualfund':
|
|
||||||
assetClass = AssetClass.EQUITY;
|
|
||||||
assetSubClass = AssetSubClass.MUTUALFUND;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { assetClass, assetSubClass };
|
|
||||||
}
|
|
||||||
|
|
||||||
private parseSector(aString: string): string {
|
|
||||||
let sector = UNKNOWN_KEY;
|
|
||||||
|
|
||||||
switch (aString) {
|
|
||||||
case 'basic_materials':
|
|
||||||
sector = 'Basic Materials';
|
|
||||||
break;
|
|
||||||
case 'communication_services':
|
|
||||||
sector = 'Communication Services';
|
|
||||||
break;
|
|
||||||
case 'consumer_cyclical':
|
|
||||||
sector = 'Consumer Cyclical';
|
|
||||||
break;
|
|
||||||
case 'consumer_defensive':
|
|
||||||
sector = 'Consumer Staples';
|
|
||||||
break;
|
|
||||||
case 'energy':
|
|
||||||
sector = 'Energy';
|
|
||||||
break;
|
|
||||||
case 'financial_services':
|
|
||||||
sector = 'Financial Services';
|
|
||||||
break;
|
|
||||||
case 'healthcare':
|
|
||||||
sector = 'Healthcare';
|
|
||||||
break;
|
|
||||||
case 'industrials':
|
|
||||||
sector = 'Industrials';
|
|
||||||
break;
|
|
||||||
case 'realestate':
|
|
||||||
sector = 'Real Estate';
|
|
||||||
break;
|
|
||||||
case 'technology':
|
|
||||||
sector = 'Technology';
|
|
||||||
break;
|
|
||||||
case 'utilities':
|
|
||||||
sector = 'Utilities';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sector;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user