Implement dynamic currency display based on portfolio majority

- 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 <noreply@anthropic.com>
This commit is contained in:
kris 2025-09-03 13:37:57 +00:00
parent ae6b0ac80e
commit 711826010f

View File

@ -1418,14 +1418,18 @@ class ETFTradeTracker {
breakdownList.innerHTML = breakdownHTML; 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); const absAmount = Math.abs(amount);
if (absAmount >= 1000000) { 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) { } else if (absAmount >= 1000) {
return (amount >= 0 ? '€' : '-€') + (absAmount / 1000).toFixed(1) + 'K'; return (amount >= 0 ? symbol : '-' + symbol) + (absAmount / 1000).toFixed(1) + 'K';
} else { } 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) { escapeHtml(text) {
const div = document.createElement('div'); const div = document.createElement('div');
div.textContent = text; div.textContent = text;