From 93a1fae51c7aad38da8261831218bf0bb7b11eb1 Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Tue, 27 Sep 2022 17:38:53 +0200 Subject: [PATCH] Feature/support sectors of mutual funds (#1298) * Support sectors * Update changelog Co-authored-by: Mitchell <5503199+m11tch@users.noreply.github.com> --- CHANGELOG.md | 1 + .../yahoo-finance/yahoo-finance.service.ts | 56 ++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f89a0d83..998990288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Set up the language localization for EspaƱol (`es`) +- Added support for sectors in mutual funds ## 1.198.0 - 25.09.2022 diff --git a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts index 2a7e93ed6..bc408ca4d 100644 --- a/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts +++ b/apps/api/src/services/data-provider/yahoo-finance/yahoo-finance.service.ts @@ -6,6 +6,7 @@ import { IDataProviderHistoricalResponse, IDataProviderResponse } from '@ghostfolio/api/services/interfaces/interfaces'; +import { UNKNOWN_KEY } from '@ghostfolio/common/config'; import { DATE_FORMAT, isCurrency } from '@ghostfolio/common/helper'; import { Granularity } from '@ghostfolio/common/types'; import { Injectable, Logger } from '@nestjs/common'; @@ -90,7 +91,7 @@ export class YahooFinanceService implements DataProviderInterface { try { const symbol = this.convertToYahooFinanceSymbol(aSymbol); const assetProfile = await yahooFinance.quoteSummary(symbol, { - modules: ['price', 'summaryProfile'] + modules: ['price', 'summaryProfile', 'topHoldings'] }); const { assetClass, assetSubClass } = this.parseAssetClass( @@ -109,7 +110,16 @@ export class YahooFinanceService implements DataProviderInterface { }); response.symbol = aSymbol; - if ( + 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 ) { @@ -437,4 +447,46 @@ export class YahooFinanceService implements DataProviderInterface { 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; + } }