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;