|
|
@ -20,10 +20,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 yahooFinance from 'yahoo-finance2';
|
|
|
|
import yahooFinance from 'yahoo-finance2';
|
|
|
|
import type {
|
|
|
|
import type { Price } from 'yahoo-finance2/dist/esm/src/modules/quoteSummary-iface';
|
|
|
|
Price,
|
|
|
|
|
|
|
|
QuoteSummaryResult
|
|
|
|
|
|
|
|
} from 'yahoo-finance2/dist/esm/src/modules/quoteSummary-iface';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Injectable()
|
|
|
|
export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
export class YahooFinanceService implements DataProviderInterface {
|
|
|
@ -92,7 +89,12 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
response.assetSubClass = assetSubClass;
|
|
|
|
response.assetSubClass = assetSubClass;
|
|
|
|
response.currency = assetProfile.price.currency;
|
|
|
|
response.currency = assetProfile.price.currency;
|
|
|
|
response.dataSource = this.getName();
|
|
|
|
response.dataSource = this.getName();
|
|
|
|
response.name = this.formatName(assetProfile);
|
|
|
|
response.name = this.formatName({
|
|
|
|
|
|
|
|
longName: assetProfile.price.longName,
|
|
|
|
|
|
|
|
quoteType: assetProfile.price.quoteType,
|
|
|
|
|
|
|
|
shortName: assetProfile.price.shortName,
|
|
|
|
|
|
|
|
symbol: assetProfile.price.symbol
|
|
|
|
|
|
|
|
});
|
|
|
|
response.symbol = aSymbol;
|
|
|
|
response.symbol = aSymbol;
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
if (
|
|
|
@ -247,7 +249,7 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
|
|
|
|
|
|
|
|
const quotes = searchResult.quotes
|
|
|
|
const quotes = searchResult.quotes
|
|
|
|
.filter((quote) => {
|
|
|
|
.filter((quote) => {
|
|
|
|
// filter out undefined symbols
|
|
|
|
// Filter out undefined symbols
|
|
|
|
return quote.symbol;
|
|
|
|
return quote.symbol;
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter(({ quoteType, symbol }) => {
|
|
|
|
.filter(({ quoteType, symbol }) => {
|
|
|
@ -256,7 +258,7 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
this.cryptocurrencyService.isCryptocurrency(
|
|
|
|
this.cryptocurrencyService.isCryptocurrency(
|
|
|
|
symbol.replace(new RegExp(`-${baseCurrency}$`), baseCurrency)
|
|
|
|
symbol.replace(new RegExp(`-${baseCurrency}$`), baseCurrency)
|
|
|
|
)) ||
|
|
|
|
)) ||
|
|
|
|
['EQUITY', 'ETF', 'MUTUALFUND'].includes(quoteType)
|
|
|
|
['EQUITY', 'ETF', 'FUTURE', 'MUTUALFUND'].includes(quoteType)
|
|
|
|
);
|
|
|
|
);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter(({ quoteType, symbol }) => {
|
|
|
|
.filter(({ quoteType, symbol }) => {
|
|
|
@ -264,6 +266,9 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
// Only allow cryptocurrencies in base currency to avoid having redundancy in the database.
|
|
|
|
// Only allow cryptocurrencies in base currency to avoid having redundancy in the database.
|
|
|
|
// Transactions need to be converted manually to the base currency before
|
|
|
|
// Transactions need to be converted manually to the base currency before
|
|
|
|
return symbol.includes(baseCurrency);
|
|
|
|
return symbol.includes(baseCurrency);
|
|
|
|
|
|
|
|
} else if (quoteType === 'FUTURE') {
|
|
|
|
|
|
|
|
// Allow GC=F, but not MGC=F
|
|
|
|
|
|
|
|
return symbol.length === 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
@ -288,7 +293,12 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
symbol,
|
|
|
|
symbol,
|
|
|
|
currency: marketDataItem.currency,
|
|
|
|
currency: marketDataItem.currency,
|
|
|
|
dataSource: this.getName(),
|
|
|
|
dataSource: this.getName(),
|
|
|
|
name: quote?.longname || quote?.shortname || symbol
|
|
|
|
name: this.formatName({
|
|
|
|
|
|
|
|
longName: quote.longname,
|
|
|
|
|
|
|
|
quoteType: quote.quoteType,
|
|
|
|
|
|
|
|
shortName: quote.shortname,
|
|
|
|
|
|
|
|
symbol: quote.symbol
|
|
|
|
|
|
|
|
})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
} catch (error) {
|
|
|
@ -298,8 +308,18 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
return { items };
|
|
|
|
return { items };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private formatName(aAssetProfile: QuoteSummaryResult) {
|
|
|
|
private formatName({
|
|
|
|
let name = aAssetProfile.price.longName;
|
|
|
|
longName,
|
|
|
|
|
|
|
|
quoteType,
|
|
|
|
|
|
|
|
shortName,
|
|
|
|
|
|
|
|
symbol
|
|
|
|
|
|
|
|
}: {
|
|
|
|
|
|
|
|
longName: Price['longName'];
|
|
|
|
|
|
|
|
quoteType: Price['quoteType'];
|
|
|
|
|
|
|
|
shortName: Price['shortName'];
|
|
|
|
|
|
|
|
symbol: Price['symbol'];
|
|
|
|
|
|
|
|
}) {
|
|
|
|
|
|
|
|
let name = longName;
|
|
|
|
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
if (name) {
|
|
|
|
name = name.replace('iShares ETF (CH) - ', '');
|
|
|
|
name = name.replace('iShares ETF (CH) - ', '');
|
|
|
@ -314,7 +334,12 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
name = name.replace('Xtrackers (IE) Plc - ', '');
|
|
|
|
name = name.replace('Xtrackers (IE) Plc - ', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return name || aAssetProfile.price.shortName || aAssetProfile.price.symbol;
|
|
|
|
if (quoteType === 'FUTURE') {
|
|
|
|
|
|
|
|
// "Gold Jun 22" -> "Gold"
|
|
|
|
|
|
|
|
name = shortName?.slice(0, -6);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return name || shortName || symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private parseAssetClass(aPrice: Price): {
|
|
|
|
private parseAssetClass(aPrice: Price): {
|
|
|
@ -337,6 +362,10 @@ export class YahooFinanceService implements DataProviderInterface {
|
|
|
|
assetClass = AssetClass.EQUITY;
|
|
|
|
assetClass = AssetClass.EQUITY;
|
|
|
|
assetSubClass = AssetSubClass.ETF;
|
|
|
|
assetSubClass = AssetSubClass.ETF;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'future':
|
|
|
|
|
|
|
|
assetClass = AssetClass.COMMODITY;
|
|
|
|
|
|
|
|
assetSubClass = AssetSubClass.COMMODITY;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 'mutualfund':
|
|
|
|
case 'mutualfund':
|
|
|
|
assetClass = AssetClass.EQUITY;
|
|
|
|
assetClass = AssetClass.EQUITY;
|
|
|
|
assetSubClass = AssetSubClass.MUTUALFUND;
|
|
|
|
assetSubClass = AssetSubClass.MUTUALFUND;
|
|
|
|