It's a great feeling when an investment goes way up in value. There's just one tiny hitch: When you decide to sell said investment to take advantage of your earnings, you'll have to pay the piper (and the federal government) in the form of a capital gains tax. That is, unless you take advantage of some of the following strategies to help you avoid capital gains taxes entirely.

Stock chart

IMAGE SOURCE: GETTY IMAGES.

1. Use a tax-sheltered account

If you like to do a lot of buying and selling, the best place to do it is within your 401(k), IRA, or HSA. Any trading that takes place inside these tax-sheltered accounts won't trigger a capital gains tax. For your HSA, as long as you use the funds only for qualified medical expenses, you'll never have to pay taxes on those gains at all. With traditional 401(k)s and IRAs, you'll need to pay income tax at the time you withdraw the funds, but not capital gains tax. And with a ROTH account, you won't have to pay taxes on any distributions, including earnings (assuming you wait until retirement age to withdraw the funds; if you take money out earlier than that, then your contributions will be tax-free, but any earnings will be taxed).

2. Drop into the 15% tax bracket

Let's say you have an investment you really want to sell, but it's so heavily appreciated that you'd end up with an enormous capital gains tax on it. If you can arrange to drop into the 15% tax bracket or lower, your capital gains rate for that year will be 0%. You can now safely sell that investment and dodge the capital gains bullet entirely. For 2017, the 15% tax bracket tops out at $37,950 for singles, $50,800 for head of household, and $75,900 for married filing jointly. You may think there's no way you could lower your income that far, but with a little planning, it's surprisingly easy. The trick is to group a whole lot of big deductions in one year in order to bring your taxable income way down for that year. If you've still got too much income at the end of the year, consider a last-minute contribution to a tax-deferred retirement account.

3. Use tax exemptions

There are several tax exemptions that can completely cancel out capital gains taxes under the right circumstances. Take the home sale exclusion, for one. If you meet the requirements, you'll be exempt from taxes on up to $500,000 of the gain from selling your home. Another option is to give highly appreciated investments to a charity. Charities are exempt from capital gains tax, so they won't have to pay for it when they sell the investment. And you get to write off the donation as a charitable contribution, giving you a nice extra deduction on your tax return.

4. Use capital losses

If you've got investments or other assets that have lost value, you can sell them and use the capital losses to counteract your capital gains for the year, removing the need to pay taxes on those gains. That saves you money on taxes and also helps reduce the sting of having backed a loser investment. Hey, you might as well get some benefit out of your investing boo-boos. 

5. Leave the investments to your heirs

Dying is a bummer, but it does have a few benefits. When you leave an appreciated asset to someone else, the federal government hits the reset button on capital gains. In other words, even if you'd accrued thousands of dollars of capital gains on an investment, if you die and someone else inherits it, they are considered to have zero capital gains on it at that moment. Your beneficiary can sell the asset and not get hit with the monster tax bill you would have encountered. Now there's an example of a win-win inheritance.