Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Bugfix/fix symbol conversion for yahoo finance (#405)

Thomas Kaulde94494aa0e0
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 513da672cb..5b948bd574 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
+## Unreleased
+
+### Fixed
+
+- Fixed an issue in the symbol conversion for _Yahoo Finance_ (for a cryptocurrency with the same code as a currency)
+
## 1.58.0 - 02.10.2021
### Changed
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 25624c6a39..c32d543e13 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
@@ -262,16 +262,21 @@ export const convertFromYahooFinanceSymbol = (aYahooFinanceSymbol: string) => {
* SOL1USD -> SOL1-USD
*/
export const convertToYahooFinanceSymbol = (aSymbol: string) => {
- if (isCurrency(aSymbol)) {
- if (isCrypto(aSymbol) || isCrypto(aSymbol.replace('1', ''))) {
+ if (
+ (aSymbol.includes('CHF') ||
+ aSymbol.includes('EUR') ||
+ aSymbol.includes('USD')) &&
+ aSymbol.length >= 6
+ ) {
+ if (isCurrency(aSymbol.substring(0, aSymbol.length - 3))) {
+ return `${aSymbol}=X`;
+ } else if (isCrypto(aSymbol) || isCrypto(aSymbol.replace('1', ''))) {
// Add a dash before the last three characters
// BTCUSD -> BTC-USD
// DOGEUSD -> DOGE-USD
// SOL1USD -> SOL1-USD
return aSymbol.replace('USD', '-USD');
}
-
- return `${aSymbol}=X`;
}
return aSymbol;
diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts
index 738399bf79..c4f593dd9f 100644
--- a/libs/common/src/lib/helper.ts
+++ b/libs/common/src/lib/helper.ts
@@ -1,3 +1,4 @@
+import * as currencies from '@dinero.js/currencies';
import { getDate, getMonth, getYear, parse, subDays } from 'date-fns';
import { ghostfolioScraperApiSymbolPrefix } from './config';
@@ -85,12 +86,7 @@ export function isCrypto(aSymbol = '') {
}
export function isCurrency(aSymbol = '') {
- return (
- (aSymbol.includes('CHF') ||
- aSymbol.includes('EUR') ||
- aSymbol.includes('USD')) &&
- aSymbol.length >= 6
- );
+ return currencies[aSymbol];
}
export function isGhostfolioScraperApiSymbol(aSymbol = '') {
diff --git a/package.json b/package.json
index bc33df7e5f..2609d5be35 100644
--- a/package.json
+++ b/package.json
@@ -58,6 +58,7 @@
"@angular/platform-browser-dynamic": "12.2.4",
"@angular/router": "12.2.4",
"@codewithdan/observable-store": "2.2.11",
+ "@dinero.js/currencies": "2.0.0-alpha.8",
"@nestjs/common": "7.6.18",
"@nestjs/config": "0.6.3",
"@nestjs/core": "7.6.18",
diff --git a/yarn.lock b/yarn.lock
index d29767a5dc..eae139db33 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -1681,6 +1681,11 @@
debug "^3.1.0"
lodash.once "^4.1.1"
+"@dinero.js/[email protected]":
+ version "2.0.0-alpha.8"
+ resolved "https://registry.yarnpkg.com/@dinero.js/currencies/-/currencies-2.0.0-alpha.8.tgz#aa02a04ce3685a9b06a7ce12f8c924726386c3fd"
+ integrity sha512-zApiqtuuPwjiM9LJA5/kNcT48VSHRiz2/mktkXjIpfxrJKzthXybUAgEenExIH6dYhLDgVmsLQZtZFOsdYl0Ag==
+
"@discoveryjs/[email protected]":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.3.tgz#90420f9f9c6d3987f176a19a7d8e764271a2f55d"