When you earn money in the stock market, you have to pay income tax on it, just like any other income. But paying taxes on stock gains is a little tricky. The amount you owe depends on the type of investment income you’ve earned, when you earned it, how long you’ve owned the asset, and how much you earned—as well as your total income for the year.

Why are there so many factors involved?

First, there are two different ways your stock gains may be taxed. Capital gains taxes apply when you sell a stock or other assets, and they are generally lower than your regular tax rate. You owe capital gains taxes when you sell a stock holding for more than you paid for it, and they are based on the amount you earned on that sale.

But if your stock holdings pay dividends, you may earn dividend income even without selling any assets. In that case, the tax you owe depends on the type of dividends you earn. Ordinary dividends are taxed at regular income tax rates rather than at capital gains rates.

Qualified dividends, however, are taxed at lower capital gains rates with a maximum of 15 percent. To be “qualified,” the dividends must meet certain criteria, such as they must be paid by a U.S. corporation or qualified foreign corporation and you must have held the stock for more than 60 days.

Any dividends you earn in a qualified retirement account, such as an IRA or 401k, are not taxable.

How do I figure out how much capital gains tax I owe?

If you sell a stock or other investment asset for a profit, you will owe capital gains tax. But the amount you owe depends on several factors.

First, determine how long you owned the stock before selling it. If you’ve held it for less than one year, you’ll owe short-term capital gains taxes. That rate is the same as your regular income tax rate. So, if you pay taxes of 24 percent on all your other income, you’ll also pay 24 percent on the amount you earned by selling a short-term asset.

However, if you’ve owned the stock for more than one year, before selling it you’ll pay long-term capital gains taxes. Long-term rates are lower, with a cap of 20 percent in 2019.

If your income is lower than $39,375 (or $78,750 for married couples), you’ll pay zero in capital gains taxes. If your income is between $39,376 to $434,550, you’ll pay 15 percent in capital gains taxes. And if your income is $434,551 or more, your capital gains tax rate is 20 percent.

How do I calculate capital gains tax?

When you sell a stock at a profit, you probably do owe capital gains tax, but not on the full amount of the sale. You’re only required to pay taxes on your profit, so that means you can subtract the amount you paid for the stock when you originally bought it.

To determine how much you owe in capital gains tax after selling a stock, you need to know your “basis,” which is the cost of the stock, along with any reinvested dividends and commissions paid. For example, if you purchased the stock 10 years ago for $1,000, and you reinvested dividends each year, totaling $200, your total basis is $1,200. If you sell the stock this year for $2,000, your net gain is $800, and that’s the amount on which you’ll owe capital gains taxes.

What if you didn’t purchase the stock yourself, but you inherited it from a relative or received it as a gift? No problem. The basis for an inherited stock is its fair-market value on the date of death of its previous owner. If someone gave you the stock as a gift, the basis is the lower of the fair market value on the date the gift was made, or the original price your gift-giver paid for the stock.

After subtracting your basis from your profit, you may also be able to lower your taxable capital gain by deducting management fees. If you paid fees or commissions to brokers for managing a mutual fund or providing advisory services, those are deductible investment expenses. Report them on Schedule A of your tax return to reduce your taxable capital gains income. If you’re not sure how much you paid in fees, call your broker to find out.

Can I do anything to lower my capital gains taxes?

Aside from deducting the management fees or commissions you paid to brokers, you can also potentially lower capital gains tax liability by offsetting your gains with losses.

The money you earn on the sale of stocks, bonds or other investments is a capital gain—but if you lose money when you sell one of these investments, you have a capital loss. You can use capital losses to offset capital gains to lower your tax bill.

For example, if you sold a stock for a $5,000 profit this year, but you sold another stock for a $3,000 loss, you’ll be taxed only on the capital gains of $2,000.

If you did the opposite, with a loss of $5,000 and a gain of $3,000, your losses would exceed your gains. In that case, you can deduct the total losses on your tax return, up to $3,000 per year. In this instance, you’d be able to deduct $2,000 for investment losses on your tax returns.

It may sound tempting to sell a stock at a loss to offset a gain for tax purposes and then buy the stock back again. But the IRS will not allow you to claim a capital loss if you sell a stock and then buy it back within 30 days. Therefore, if you want to claim the loss but purchase the stock again, you’ll have to wait at least 30 days before buying it back.

Capital gains tax rates are just one more reason to view the stock market as a long-term investment: You’ll pay less in taxes on the gains when you’ve held the stock for more than one year. To make sure you’re paying taxes appropriately on your stock gains, be sure to keep track of how long you’ve held the stock and the amount you’ve invested in it.

Investing involves risk including loss of principal. This article contains the current opinions of the author, but not necessarily those of Acorns. Such opinions are subject to change without notice. This article has been distributed for educational purposes only and should not be considered as investment advice or a recommendation of any particular security, strategy or investment product. Information contained herein has been obtained from sources believed to be reliable, but not guaranteed. Acorns does not provide legal or tax advice. Please consult your tax and/or legal counsel for specific tax or legal questions and concerns.