In Partnership With 
170706-capital-gains-zero-percent
Getty Images—This content is subject to copyright.

The Stock Market Is Soaring. Here's How to Cash In on Your Gains Without Paying Taxes

If you're closing in on retirement, you have a unique opportunity ahead for a potentially huge tax break.

That’s because when you stop taking home a salary, you're more likely to be eligible to pay zero taxes on your long-term capital gains — as opposed to the typical 15% rate — letting you cash in on some of your investments without giving the U.S. government a cut.

This tax break isn't exactly a secret, but it requires some careful planning. “Many retirees don’t even think about utilizing this" break and end up pulling money from their IRAs rather than from taxable investment accounts, says Palm Harbor, Fla., financial planner Kirk Kinder.

The 0% rate is available only to those whose earnings place them in the bottom two tiers of the IRS’s tax bracket. That means your taxable income must be below $75,900 for married couples or $37,950 for individuals— for 2017.

You have to be careful, though: If you realize too much in capital gains by selling off too many of your investments all at once that could push you into a higher tax bracket, which may force you out of the 0% capital gains rate.

So here’s how to take advantage of this tax-saving strategy — and avoid any costly mistakes:

Know When to Sell

To qualify for the 0% capital gains rate, your income must be low enough to land you in the 10% or 15% federal income bracket.

A good time to try to capitalize on this is during the first few years of your retirement, says Bedford, N.H., financial planner Jean Fullerton. In those first few years after leaving work, you're no longer collecting a paycheck, you've likely delayed taking a Social Security benefit, and you're not yet 70½, when you'd be required to take distributions from your IRA, 401(k), or other retirement accounts.

You don't have to be completely income free, though. Fullerton sketches out an example: a married couple with $40,000 in part-time earnings, $4,000 in interest and dividends, and $16,000 in an employer pension, for total income of $60,000 in 2017, with a standard deduction of $12,700 and $8,100 in exemptions, could take an additional $36,700 in long-term capital gains this year at a 0% rate.

Be Mindful of the Risks

Keep an eye on your total income for the year — because the more ordinary income you earn, the fewer investment gains you can cash in without taking a tax hit.

“Many people assume they can take any amount of capital gains if they would otherwise be in the 10% or 15% tax bracket,” says Kinder. "They don’t realize that taking too much in capital gains can negate that 0% rate."

That’s because the 0% rate only applies so long as your total taxable income — including the capital gains — will keep you within those bottom two brackets. If your realized capital gains carry you out of the those brackets, the overage will be taxed at 15%, not the whole amount.

For example, if a couple’s sole income is from long-term gains, they can effectively realize $96,700 of gains and pay nothing in federal income taxes because their standard deduction and personal exemptions would land them at the top of the 15% bracket for married couples, says Chapel Hill, N.C., financial planner Ben Birken.

A couple could take even more gains by using other tax deductions to stay below the threshold, suggests Pewaukee, Wis., financial planner Kevin Reardon. A few big ones might include mortgage interest, medical expenses, and charitable contributions.

If you're still closing in on the income threshold, you may want to defer some gains until next year, and use this strategy again, if possible.

There are a few other caveats to be aware of. For instance, even though you'd owe no federal taxes on your capital gains using this strategy, you may face a small state income tax liability.

The 0% rate also only applies to long-term capital gains, or investments held for longer than 12 months. Short-term gains, assets held for less than a year before selling, are taxed at ordinary income tax rates.

Retirees looking to take advantage of this tax break should note that the additional income received from selling investments could impact your tax deductions, your ability to qualify for certain tax credits, and even trigger further taxation of Social Security benefits, if you’re currently collecting.

And you’ll want to be cautious about trying to do this while also doing a partial Roth conversion at the end of the year.

Put the Proceeds to Work

So what should you do with the money when you sell? Realizing these gains can create tax-free income for you to spend and enjoy in retirement. But if you don't need the money right away, you also have the opportunity to do a little pre-planning for future tax bills.

Many financial planners recommend you sell the investment and then buy it back again immediately. By doing so, you retain your current asset allocation.

However, you will "step up" the cost basis to the stock’s current value, reducing any future gains and increasing the value of any future losses you may want to harvest.

In other words, not only will this strategy reduce your taxes now, it could set you up to lower your future capital gains taxes as well — if you play it right.

All products and services featured are based solely on editorial selection. MONEY may receive compensation for some links to products and services on this website.

Quotes delayed at least 15 minutes. Market data provided by Interactive Data. ETF and Mutual Fund data provided by Morningstar, Inc. Dow Jones Terms & Conditions: http://www.djindexes.com/mdsidx/html/tandc/indexestandcs.html. S&P Index data is the property of Chicago Mercantile Exchange Inc. and its licensors. All rights reserved. Terms & Conditions. Powered and implemented by Interactive Data Managed Solutions