Browse Source

Improve name formatting for futures

pull/845/head
Thomas 3 years ago
parent
commit
ea99561c59
  1. 44
      apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts

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

@ -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 (
@ -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): {
@ -339,6 +364,7 @@ export class YahooFinanceService implements DataProviderInterface {
break; break;
case 'future': case 'future':
assetClass = AssetClass.COMMODITY; assetClass = AssetClass.COMMODITY;
assetSubClass = AssetSubClass.COMMODITY;
break; break;
case 'mutualfund': case 'mutualfund':
assetClass = AssetClass.EQUITY; assetClass = AssetClass.EQUITY;

Loading…
Cancel
Save