Calculate your estimated tax liability on investment sales.
Single
Married Filing Jointly
Head of Household
Married Filing Separately
Includes salary, wages, etc.
Short Term (Less than 1 year)
Long Term (1 year or more)
Total Capital Gain:$0.00
Applied Tax Rate:0%
Estimated Tax Owed:$0.00
Net Profit (After Tax):$0.00
function calculateCapitalGains() {
// 1. Get Input Values
var purchasePrice = parseFloat(document.getElementById('purchasePrice').value);
var salePrice = parseFloat(document.getElementById('salePrice').value);
var annualIncome = parseFloat(document.getElementById('annualIncome').value);
var filingStatus = document.getElementById('filingStatus').value;
var holdingPeriod = document.getElementById('holdingPeriod').value;
// Validation
if (isNaN(purchasePrice) || isNaN(salePrice) || isNaN(annualIncome)) {
alert("Please enter valid numbers for prices and income.");
return;
}
// 2. Calculate Gross Gain
var gain = salePrice – purchasePrice;
// 3. Variables for calculation
var taxRate = 0;
var taxOwed = 0;
// Logic for Capital Losses
if (gain <= 0) {
taxRate = 0;
taxOwed = 0;
} else {
// Logic for Positive Gains
if (holdingPeriod === 'short') {
// Short Term Capital Gains are taxed as Ordinary Income
// Simplified 2024 Tax Bracket Logic for estimation
// Note: In a real scenario, this stacks on top of income.
// We will determine the marginal rate based on total income + gain.
var totalTaxable = annualIncome + gain;
// Simplified marginal rate lookup based on status and total income
taxRate = getOrdinaryIncomeRate(totalTaxable, filingStatus);
taxOwed = gain * taxRate;
} else {
// Long Term Capital Gains (0%, 15%, 20%)
// 2024 Thresholds (Approximate)
var rate0Limit, rate15Limit;
if (filingStatus === 'single') {
rate0Limit = 47025;
rate15Limit = 518900;
} else if (filingStatus === 'married_joint') {
rate0Limit = 94050;
rate15Limit = 583750;
} else if (filingStatus === 'head') {
rate0Limit = 63000;
rate15Limit = 551350;
} else { // married_separate
rate0Limit = 47025;
rate15Limit = 291850;
}
// Determine Long Term Rate based on Income
// The rate applies to the gain, determined by where the income falls
if (annualIncome < rate0Limit) {
// Check if gain pushes into next bracket
var roomIn0 = rate0Limit – annualIncome;
if (gain <= roomIn0) {
taxOwed = 0;
taxRate = 0;
} else {
var taxableAt15 = gain – roomIn0;
// Check if it pushes into 20%
var roomIn15 = rate15Limit – rate0Limit;
if (taxableAt15 = rate0Limit && annualIncome < rate15Limit) {
var roomIn15 = rate15Limit – annualIncome;
if (gain <= roomIn15) {
taxOwed = gain * 0.15;
taxRate = 0.15;
} else {
var taxableAt20 = gain – roomIn15;
taxOwed = (roomIn15 * 0.15) + (taxableAt20 * 0.20);
taxRate = taxOwed / gain;
}
} else {
// Already in 20% bracket
taxOwed = gain * 0.20;
taxRate = 0.20;
}
}
}
var netProfit = gain – taxOwed;
// 4. Update UI
document.getElementById('resultsArea').style.display = 'block';
// Formatting currency
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
document.getElementById('totalGainDisplay').innerText = formatter.format(gain);
document.getElementById('taxRateDisplay').innerText = (taxRate * 100).toFixed(1) + '%';
document.getElementById('taxOwedDisplay').innerText = formatter.format(taxOwed);
document.getElementById('netProfitDisplay').innerText = formatter.format(netProfit);
// Visual cue for loss
if (gain < 0) {
document.getElementById('totalGainDisplay').style.color = '#e74c3c';
document.getElementById('taxOwedDisplay').innerText = '$0.00';
document.getElementById('netProfitDisplay').innerText = formatter.format(gain);
} else {
document.getElementById('totalGainDisplay').style.color = '#2c3e50';
}
}
function getOrdinaryIncomeRate(income, status) {
// Simplified Marginal Tax Brackets 2024
// Returns the top marginal rate for simplicity in this estimation context
if (status === 'single') {
if (income < 11600) return 0.10;
if (income < 47150) return 0.12;
if (income < 100525) return 0.22;
if (income < 191950) return 0.24;
if (income < 243725) return 0.32;
if (income < 609350) return 0.35;
return 0.37;
}
if (status === 'married_joint') {
if (income < 23200) return 0.10;
if (income < 94300) return 0.12;
if (income < 201050) return 0.22;
if (income < 383900) return 0.24;
if (income < 487450) return 0.32;
if (income < 731200) return 0.35;
return 0.37;
}
// Fallback for others to single/joint approximation for this demo
if (income < 16000) return 0.10;
if (income < 60000) return 0.12;
return 0.24;
}
Understanding Capital Gains Tax
When you sell an asset—such as stocks, real estate, or cryptocurrency—for more than you paid for it, the profit is considered a "capital gain." The IRS taxes these gains differently depending on how long you held the asset before selling it and your overall taxable income.
Key Definition: Capital Gains Tax is a tax on the growth in value of investments incurred when individuals and corporations sell those investments.
Short-Term vs. Long-Term Capital Gains
The duration you hold the asset is the most critical factor in determining your tax rate:
Short-Term Capital Gains: This applies to assets held for one year or less. These gains are taxed as ordinary income, meaning they are added to your salary and wages and taxed at your regular income tax bracket (ranging from 10% to 37%).
Long-Term Capital Gains: This applies to assets held for more than one year. The government incentivizes long-term investing by offering lower tax rates on these gains, typically 0%, 15%, or 20%, depending on your income.
How Capital Gains Are Calculated
The calculation formula used in the calculator above follows these steps:
Determine the Basis: This is usually your purchase price plus any commissions or fees paid.
Determine the Realized Amount: This is the sale price minus any fees associated with the sale.
Calculate Gain/Loss: Subtract the basis from the realized amount.
Apply Tax Rate: Based on the holding period and your taxable income, the appropriate percentage is applied to the gain.
Current Long-Term Tax Brackets (2024 Estimates)
For long-term assets, your rate depends on your taxable income and filing status:
0% Rate: Applied if your taxable income is below approximately $47,025 (Single) or $94,050 (Married Filing Jointly).
15% Rate: Applied if your income falls between the 0% limit and $518,900 (Single) or $583,750 (Married Filing Jointly).
20% Rate: Applied if your income exceeds the 15% bracket limits.
Net Investment Income Tax (NIIT)
High earners should also be aware of the Net Investment Income Tax (NIIT). This is an additional 3.8% tax that applies to the lesser of your net investment income or the amount by which your modified adjusted gross income exceeds statutory thresholds ($200,000 for single filers, $250,000 for joint filers). This calculator provides a base estimate and does not automatically include NIIT.
Strategies to Minimize Capital Gains Tax
Investors often use specific strategies to manage their tax liability:
Hold for over a year: Waiting until the 12-month mark passes can significantly reduce your tax rate from ordinary income rates to the preferential long-term rates.
Tax-Loss Harvesting: You can sell losing investments to offset the gains from winning investments, thereby reducing your total taxable capital gains for the year.
Use Tax-Advantaged Accounts: Trading within a 401(k) or IRA defers taxes until withdrawal (or avoids them entirely in a Roth IRA).