Single
Married Filing Jointly
Married Filing Separately
Head of Household
More than 1 year (Long Term)
1 year or less (Short Term)
Total Capital Gain:$0.00
Federal Tax Rate Applied:0%
Estimated Federal Tax:$0.00
Estimated State Tax:$0.00
Net Profit (After Tax):$0.00
function calculateCapitalGains() {
// 1. Get input values
var buyPrice = parseFloat(document.getElementById('cgt_buy_price').value);
var sellPrice = parseFloat(document.getElementById('cgt_sell_price').value);
var income = parseFloat(document.getElementById('cgt_income').value);
var status = document.getElementById('cgt_status').value;
var duration = document.getElementById('cgt_duration').value;
var stateRate = parseFloat(document.getElementById('cgt_state_rate').value);
// 2. Validate inputs
if (isNaN(buyPrice) || isNaN(sellPrice) || isNaN(income)) {
alert("Please enter valid numbers for Purchase Price, Sale Price, and Income.");
return;
}
if (isNaN(stateRate)) {
stateRate = 0;
}
// 3. Calculate Gross Gain
var gain = sellPrice – buyPrice;
// Handle Loss
if (gain <= 0) {
document.getElementById('cgt_results_area').style.display = 'block';
document.getElementById('cgt_res_gain').innerText = formatMoney(gain);
document.getElementById('cgt_res_gain').classList.add('negative');
document.getElementById('cgt_res_fed_rate').innerText = "0%";
document.getElementById('cgt_res_fed_tax').innerText = "$0.00";
document.getElementById('cgt_res_state_tax').innerText = "$0.00";
document.getElementById('cgt_res_net_profit').innerText = formatMoney(gain); // Loss is net result
return;
}
document.getElementById('cgt_res_gain').classList.remove('negative');
// 4. Calculate Tax Rate based on Duration and Income (2024 Brackets approximation)
var fedTaxRate = 0;
var totalIncome = income + gain; // Simplified: marginal rate often based on total stack
if (duration === 'short') {
// Short Term: Taxed as Ordinary Income
// Using 2024 standard tax brackets (simplified for top marginal rate of the stack)
// This logic finds the bracket the income falls into.
// Single filers brackets (2024)
if (status === 'single') {
if (totalIncome <= 11600) fedTaxRate = 0.10;
else if (totalIncome <= 47150) fedTaxRate = 0.12;
else if (totalIncome <= 100525) fedTaxRate = 0.22;
else if (totalIncome <= 191950) fedTaxRate = 0.24;
else if (totalIncome <= 243725) fedTaxRate = 0.32;
else if (totalIncome <= 609350) fedTaxRate = 0.35;
else fedTaxRate = 0.37;
}
// Joint filers
else if (status === 'joint') {
if (totalIncome <= 23200) fedTaxRate = 0.10;
else if (totalIncome <= 94300) fedTaxRate = 0.12;
else if (totalIncome <= 201050) fedTaxRate = 0.22;
else if (totalIncome <= 383900) fedTaxRate = 0.24;
else if (totalIncome <= 487450) fedTaxRate = 0.32;
else if (totalIncome <= 731200) fedTaxRate = 0.35;
else fedTaxRate = 0.37;
}
// Married Separate
else if (status === 'separate') {
if (totalIncome <= 11600) fedTaxRate = 0.10;
else if (totalIncome <= 47150) fedTaxRate = 0.12;
else if (totalIncome <= 100525) fedTaxRate = 0.22;
else if (totalIncome <= 191950) fedTaxRate = 0.24;
else if (totalIncome <= 243725) fedTaxRate = 0.32;
else if (totalIncome <= 365600) fedTaxRate = 0.35;
else fedTaxRate = 0.37;
}
// Head of Household
else if (status === 'head') {
if (totalIncome <= 16550) fedTaxRate = 0.10;
else if (totalIncome <= 63100) fedTaxRate = 0.12;
else if (totalIncome <= 100500) fedTaxRate = 0.22;
else if (totalIncome <= 191950) fedTaxRate = 0.24;
else if (totalIncome <= 243700) fedTaxRate = 0.32;
else if (totalIncome <= 609350) fedTaxRate = 0.35;
else fedTaxRate = 0.37;
}
} else {
// Long Term: Preferential Rates (0%, 15%, 20%)
// 2024 Long Term Capital Gains Brackets
if (status === 'single') {
if (income <= 47025) fedTaxRate = 0.00;
else if (income <= 518900) fedTaxRate = 0.15;
else fedTaxRate = 0.20;
}
else if (status === 'joint') {
if (income <= 94050) fedTaxRate = 0.00;
else if (income <= 583750) fedTaxRate = 0.15;
else fedTaxRate = 0.20;
}
else if (status === 'separate') {
if (income <= 47025) fedTaxRate = 0.00;
else if (income <= 291850) fedTaxRate = 0.15;
else fedTaxRate = 0.20;
}
else if (status === 'head') {
if (income <= 63000) fedTaxRate = 0.00;
else if (income niitThreshold) {
// NIIT applies to the lesser of the gain OR the amount by which MAGI exceeds threshold
// Simplified for calculator: If income > threshold, apply 3.8% to the gain
niitTax = gain * 0.038;
// Technically adds to rate, but we will add to dollar amount for clarity
}
// 5. Calculate Amounts
var fedTaxAmount = (gain * fedTaxRate) + niitTax;
var stateTaxAmount = gain * (stateRate / 100);
var totalTax = fedTaxAmount + stateTaxAmount;
var netProfit = gain – totalTax;
// Effective Federal Rate for display (including NIIT implication)
var effectiveFedRate = (fedTaxAmount / gain) * 100;
// 6. Output to DOM
document.getElementById('cgt_results_area').style.display = 'block';
document.getElementById('cgt_res_gain').innerText = formatMoney(gain);
document.getElementById('cgt_res_fed_rate').innerText = effectiveFedRate.toFixed(1) + "%";
document.getElementById('cgt_res_fed_tax').innerText = formatMoney(fedTaxAmount);
document.getElementById('cgt_res_state_tax').innerText = formatMoney(stateTaxAmount);
document.getElementById('cgt_res_net_profit').innerText = formatMoney(netProfit);
}
function formatMoney(number) {
return number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}
Understanding Capital Gains Tax
Capital gains tax is a levy assessed on the positive difference between the sale price of the asset and its original purchase price. When you sell an asset—such as stocks, bonds, real estate, or cryptocurrency—for more than you paid for it, the profit is considered a "capital gain," and the government taxes it.
Short-Term vs. Long-Term Capital Gains
The duration you hold an asset significantly impacts your tax bill. The IRS divides capital gains into two categories:
Short-Term Capital Gains: Assets held for one year or less before being sold. These are taxed as ordinary income, meaning they are subject to your standard federal income tax bracket (ranging from 10% to 37% in 2024).
Long-Term Capital Gains: Assets held for more than one year. These benefit from preferential tax rates, typically 0%, 15%, or 20%, depending on your filing status and taxable income. This lower rate is designed to encourage long-term investment.
Net Investment Income Tax (NIIT)
High-income earners may be subject to an additional 3.8% Net Investment Income Tax. This applies if your Modified Adjusted Gross Income (MAGI) exceeds specific thresholds ($200,000 for single filers and heads of household, $250,000 for married filing jointly). Our calculator automatically estimates if this surtax applies to your gain based on the income you provided.
How to Minimize Capital Gains Tax
There are several strategies investors use to reduce their tax liability:
Hold for over a year: Waiting until the 1-year mark passes converts the gain from short-term to long-term, potentially cutting your tax rate nearly in half.
Tax-Loss Harvesting: You can sell underperforming assets at a loss to offset gains realized from other sales. Up to $3,000 in excess losses can also offset ordinary income.
Utilize Tax-Advantaged Accounts: Trading within a 401(k) or IRA does not trigger immediate capital gains taxes. Taxes are deferred until withdrawal (or tax-free in a Roth IRA).
Disclaimer: This calculator provides estimates based on 2024 federal tax brackets and standard deductions. Tax laws are complex and subject to change. State taxes vary significantly by location. This tool does not constitute professional financial or legal advice. Please consult with a certified public accountant (CPA) or tax professional for your specific situation.