Stock markets have risen dramatically in 2017, extending the bull market into a ninth straight year. Yet those who have diversified investment portfolios often own at least one or two stocks that have lost money, and that can open up an opportunity to use a key tax-saving strategy known as tax-loss harvesting. Before you take steps to use this strategy, it's important to understand the details so that you avoid costly mistakes.

The basics of tax-loss harvesting

When you sell an investment, you'll typically recognize a capital gain or loss, depending on whether it has gone up or down in value. If you sell more than one investment, then you're allowed to offset gains on one with losses on the other to come up with a net gain or loss. If you have more losses than gains, you're also allowed to claim up to $3,000 in losses against other types of income. Any unused loss in a given year gets carried forward to future years.

In order to claim a tax loss on your 2017 tax return, you have to sell your losing investments by Dec. 31. After that, you won't be able to take a loss until 2018.

Tax forms with $100 bills, glasses, a pen, and a calculator.

Image source: Getty Images.

The biggest potential pitfall with tax-loss harvesting

Tax-loss harvesting is easy to do when you've lost confidence in a stock. If you sell with no intention of ever wanting to buy the stock back ever again, then there aren't any complications.

Often, though, investors still like the stock or at least expect that it could rebound from its prior losses. In that case, you might want to sell shares to claim the tax loss but then buy them back in order to benefit from a future bounce.

In order to prevent tax abuse, there's a rule that specifically addresses this situation. Known as the wash-sale rules, the tax laws require there to be at least 30 days between the time you sell a stock at a loss and when you buy it back. If you don't wait, then you're not allowed to take the loss. Instead, the loss is reincorporated into the tax basis of the newly bought shares. So for instance, if you sold the stock at a $1,000 loss and then buy it back within less than 30 days, then your new tax basis will be $1,000 more than the purchase price. This will ensure that you eventually get credit for having lost money on the original position, but it forces you to wait until you sell the newly acquired shares.

Don't make this costly mistake

The wash-sale rules are intended to make you wait longer to claim a loss rather than to take it away forever. However, one tempting workaround can actually lead to a disastrous permanent loss of your tax-loss-harvesting efforts.

Some taxpayers think that they can sell an investment in a regular taxable brokerage account at a loss and then use another account, such as an IRA at a different broker, to buy the stock back. It's true that because the two different financial institutions don't know what you've done in each account, they will report the sale as if you had complied with the wash-sale rules. However, those rules are quite clear that you can't just use a different account to buy back the stock. Moreover, if the new stock is in a tax-favored account, then the fact that its tax basis will have been increased by the wash-sale rules won't do you any good when you sell them in the future, because you can't deduct capital losses on sales of investments within an IRA.

Be smart about tax loss harvesting

It's good to get at least a small tax break to make up for the pain of suffering a loss on your investments. By understanding the wash-sale rules and ensuring that they don't snare you, you'll be in a much better position to make the most of your tax losses.