From c71a4c078eaad554c601b3a27b8c81301d6176ae Mon Sep 17 00:00:00 2001 From: Thomas Kaul <4159106+dtslvr@users.noreply.github.com> Date: Wed, 18 Aug 2021 18:22:01 +0200 Subject: [PATCH] Bugfix/convert g bp to gbp in yahoo finance service (#301) * Fix currency inconsistency in the yahoo finance service (GBp to GBP) * Update changelog --- CHANGELOG.md | 1 + .../yahoo-finance/yahoo-finance.service.ts | 11 +++++++++++ libs/common/src/lib/helper.ts | 14 ++------------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fb619d47..47ed33d50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed the node engine version mismatch in `package.json` - Fixed an issue on the buy date in the position detail dialog +- Fixed an issue with the currency inconsistency in the _Yahoo Finance_ service (convert from `GBp` to `GBP`) ## 1.39.0 - 16.08.2021 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 3d0b8e7e5..9b744468b 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 @@ -10,6 +10,7 @@ import { Granularity } from '@ghostfolio/common/types'; import { Injectable } from '@nestjs/common'; import { AssetClass, Currency, DataSource } from '@prisma/client'; import * as bent from 'bent'; +import Big from 'big.js'; import { format } from 'date-fns'; import * as yahooFinance from 'yahoo-finance'; @@ -72,6 +73,16 @@ export class YahooFinanceService implements DataProviderInterface { name: value.price?.longName || value.price?.shortName || symbol }; + if (value.price?.currency === 'GBp') { + // Convert GBp (pence) to GBP + response[symbol].currency = Currency.GBP; + response[symbol].marketPrice = new Big( + value.price?.regularMarketPrice ?? 0 + ) + .div(100) + .toNumber(); + } + const url = value.summaryProfile?.website; if (url) { response[symbol].url = url; diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index d99b039c6..cfbbcd603 100644 --- a/libs/common/src/lib/helper.ts +++ b/libs/common/src/lib/helper.ts @@ -102,18 +102,8 @@ export function isRakutenRapidApiSymbol(aSymbol = '') { return aSymbol === 'GF.FEAR_AND_GREED_INDEX'; } -export function parseCurrency(aString: string): Currency { - if (aString?.toLowerCase() === 'chf') { - return Currency.CHF; - } else if (aString?.toLowerCase() === 'eur') { - return Currency.EUR; - } else if (aString?.toLowerCase() === 'gbp') { - return Currency.GBP; - } else if (aString?.toLowerCase() === 'usd') { - return Currency.USD; - } - - return undefined; +export function parseCurrency(aCurrency: string): Currency { + return Currency[aCurrency]; } export function resetHours(aDate: Date) {