From 711826010fad1cdf17f93396b69080a3bd5b64d5 Mon Sep 17 00:00:00 2001 From: kris Date: Wed, 3 Sep 2025 13:37:57 +0000 Subject: [PATCH] Implement dynamic currency display based on portfolio majority MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add getMajorityCurrency() to calculate which currency holds most value - Include both ETF positions and cash accounts in currency calculation - Update formatCurrency() to use majority currency symbol by default - Dashboard now displays in EUR/USD/GBP based on portfolio composition - Automatic currency switching improves user experience and accuracy - Maintain backward compatibility with optional currency parameter 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- script.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index 7c2d5e7..ecf097f 100644 --- a/script.js +++ b/script.js @@ -1418,14 +1418,18 @@ class ETFTradeTracker { breakdownList.innerHTML = breakdownHTML; } - formatCurrency(amount) { + formatCurrency(amount, currency = null) { + // Use majority currency if none specified + const currencyToUse = currency || this.getMajorityCurrency(); + const symbol = this.getCurrencySymbol(currencyToUse); + const absAmount = Math.abs(amount); if (absAmount >= 1000000) { - return (amount >= 0 ? '€' : '-€') + (absAmount / 1000000).toFixed(2) + 'M'; + return (amount >= 0 ? symbol : '-' + symbol) + (absAmount / 1000000).toFixed(2) + 'M'; } else if (absAmount >= 1000) { - return (amount >= 0 ? '€' : '-€') + (absAmount / 1000).toFixed(1) + 'K'; + return (amount >= 0 ? symbol : '-' + symbol) + (absAmount / 1000).toFixed(1) + 'K'; } else { - return (amount >= 0 ? '€' : '-€') + absAmount.toFixed(2); + return (amount >= 0 ? symbol : '-' + symbol) + absAmount.toFixed(2); } } @@ -2865,6 +2869,44 @@ class ETFTradeTracker { } } + getMajorityCurrency() { + const currencyValues = { + EUR: 0, + USD: 0, + GBP: 0 + }; + + // Calculate total value by currency from ETF positions + const etfMap = this.getActiveETFPositions(); + etfMap.forEach((etf, symbol) => { + const currentPrice = this.currentPrices.get(symbol); + const value = currentPrice ? etf.shares * currentPrice : etf.totalValue; + currencyValues[etf.currency] = (currencyValues[etf.currency] || 0) + value; + }); + + // Add cash accounts to the calculation + if (this.cashSummary && this.cashSummary.accounts) { + this.cashSummary.accounts.forEach(account => { + if (account.is_active && account.balance > 0) { + currencyValues[account.currency] = (currencyValues[account.currency] || 0) + parseFloat(account.balance); + } + }); + } + + // Find the currency with the highest value + let majorityCurrency = 'EUR'; // default + let maxValue = 0; + + for (const [currency, value] of Object.entries(currencyValues)) { + if (value > maxValue) { + maxValue = value; + majorityCurrency = currency; + } + } + + return majorityCurrency; + } + escapeHtml(text) { const div = document.createElement('div'); div.textContent = text;