Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const addTradeBtn = document.getElementById('add-trade');
+ const tradesContainer = document.getElementById('trades-container');
+
+ // Add event listeners
+ addTradeBtn.addEventListener('click', addTrade);
+ document.addEventListener('input', calculateResults);
+
+ function addTrade() {
+ const tradeEntry = document.createElement('div');
+ tradeEntry.className = 'trade-entry';
+ tradeEntry.innerHTML = `
+
+
+ ×
+ `;
+
+ tradeEntry.querySelector('.remove-trade').addEventListener('click', () => {
+ tradeEntry.remove();
+ calculateResults();
+ });
+
+ tradesContainer.appendChild(tradeEntry);
+ }
+
+ function calculateResults() {
+ const leverage = parseFloat(document.getElementById('leverage').value) || 0;
+ const margin = parseFloat(document.getElementById('margin').value) || 0;
+ const trades = document.querySelectorAll('.trade-entry');
+
+ let totalSize = 0;
+ let weightedPrice = 0;
+
+ trades.forEach(trade => {
+ const price = parseFloat(trade.querySelector('.entry-price').value) || 0;
+ const size = parseFloat(trade.querySelector('.position-size').value) || 0;
+
+ totalSize += size;
+ weightedPrice += price * size;
+ });
+
+ const avgEntry = totalSize > 0 ? weightedPrice / totalSize : 0;
+ const liquidationPrice = calculateLiquidationPrice(avgEntry, leverage, true);
+
+ // Update results
+ document.getElementById('avg-entry').textContent = avgEntry.toFixed(2);
+ document.getElementById('total-size').textContent = totalSize.toFixed(2);
+ document.getElementById('liquidation').textContent = liquidationPrice.toFixed(2);
+
+ calculatePNL(avgEntry, totalSize);
+ }
+
+ function calculateLiquidationPrice(entryPrice, leverage, isLong) {
+ const maintenanceMargin = 0.005; // 0.5%
+ const fee = 0.0005; // 0.05%
+
+ if (isLong) {
+ return entryPrice * (1 - (1 / leverage) + maintenanceMargin + fee);
+ } else {
+ return entryPrice * (1 + (1 / leverage) - maintenanceMargin - fee);
+ }
+ }
+
+ function calculatePNL(avgEntry, totalSize) {
+ const exitPrice = parseFloat(document.getElementById('exit-price').value) || 0;
+
+ if (exitPrice && avgEntry && totalSize) {
+ const pnl = (exitPrice - avgEntry) * totalSize;
+ const pnlPercentage = ((exitPrice - avgEntry) / avgEntry) * 100;
+
+ document.getElementById('pnl-amount').textContent = `$${pnl.toFixed(2)}`;
+ document.getElementById('pnl-percentage').textContent = `${pnlPercentage.toFixed(2)}%`;
+ }
+ }
+
+ // Initialize with one trade entry
+ addTrade();
+ });