It’s never fun to lose money in the stock market, except when you file your taxes. Those losses that you reaped in the previous calendar year in your taxable retail accounts can now be used to save you some money. Experienced traders and investors know that that there’s a silver lining to be found in their depressed holdings, as they can be utilized to lower their tax bills below what they would owe otherwise. The rules for computing capital gains and losses are relatively straightforward. Once you understand the basics, you’ll know when and how to use these strategies to minimize your own tax bill.

Capital Gains 101

The first rule to remember is that you only need to worry about capital gains and losses that you have realized in your retail investment accounts. Gains and losses inside traditional or Roth IRAs or any other type of tax-deferred plan or account are not reportable. You also don’t have to report gains or losses on any security until they sold. Gains on appreciated holdings that you still own are not reportable until you sell them, at which time you realize a gain or loss. Capital gains and losses are divided into two holding periods. Short-term gains and losses happen when you buy and then sell an investment within a one year time period, and this includes the day on which you bought it. For example, if you bought a stock on October 23 of 2014, then you will realize a short-term capital gain or loss if you sell that stock on October 23 of 2015. If you sell the stock more than one year to the day later than when you bought it, then you will realize a long-term gain or loss. (For more, see: Capital Gains Tax 101.)

There is a specific order in which gains and/or losses are computed. If you realize both long and short-term gains and losses in the same year, then this example shows the process that you will use to compute your net gain or loss:

Example

Your total gains and losses for the year are as follows:

$10,000 short-term capital gain from sale of stock

$12,000 short-term loss from sale of stock

$15,000 long-term capital gain from sale of a publicly-traded exchange-traded fund (ETF)

$5,000 long-term capital loss from sale of publicly-traded real estate investment trust (REIT)

Your first step is to net each of the gains and losses against their own kinds. So the $10,000 short-term gain is netted against the $12,000 short-term loss. This leaves you with a net short-term loss of $2,000. Your long-term loss is then netted against your long-term gain to give you a net long-term gain of $10,000. Your net short-term loss is now netted against your net long-term gain to give you a final net $8,000 long-term capital gain. This number is the amount that you will put on the bottom line of your Schedule D when you fill out your tax forms. (For more, see: What You Need to Know About Capital Gains and Taxes.)

Tax Loss Harvesting

Knowing how to net your gains and losses is only the first step towards being a tax-efficient investor. If November comes and you’re holding some securities in your retail account that have dropped in value since their purchase, then you can use this as an opportunity to realize some capital losses that you can use to net against your gains or other ordinary income. This is easily accomplished by simply selling the losing holdings and then buying them back. The only stipulation here is the wash sale rule that is imposed by the Internal Revenue Service (IRS) on this type of buyback strategy. This rule says that investors have to allow at least 30 calendar days to elapse before they can buy back what they sold, or else the loss will be disallowed. This is because the IRS doesn’t want to make it too easy for people to realize capital losses. If investors could sell and then buy right back, then everyone could do it absolutely every single time their holdings dip under the purchase value and create millions of additional transactions-and an untold fortune in realized losses that could be netted against gains and other income.

The 30 day wait introduces an element of market risk that makes investors think twice before trying this strategy. If the stock or other security rises substantially in price after it is sold, then the investor will have shot him or herself in the foot by missing out on the gain. Therefore, this strategy is generally only appropriate if the current value of the holding is considerably lower than the purchase price and not likely to rise in value during the waiting period. The 30 day waiting period also means that you cannot buy them back any later than the last business day in December when the markets are open if you want to realize your loss for this year. Count backward 31 days from that day and that’s the last day that you can sell your holdings and report the realized loss when you file next spring. (For more, see: Pros and Cons of Annual Tax-Loss Harvesting.)

The wash sale rule can be legally circumvented by buying back a different stock or security than the one that was sold. This eliminates the waiting period, because that rule mandates that it only applies to the sale and repurchase of “substantially identical” holdings. And buying back something else may be a good idea anyway. If you bought one company’s stock and also happen to be bullish on that company’s sector of the economy, then you may be wise to ditch that holding and buy an ETF that invests in a broad-based index of that sector. For example, if you buy stock in a pharmaceutical company and it drops in price for a company-specific reason, then you could dump the stock on the last trading day of the year and use the proceeds to buy an ETF that holds all of the stocks in one of the pharmaceutical or healthcare indices. This way you have not only realized a valuable loss, but also diversified your portfolio.

Loss Carryovers

If your net losses in your taxable investment accounts exceed your net gains for the year, then you will have no reportable income from your security sales. You may then write off up to $3,000 worth of net losses against other forms of income such as wages or taxable dividends and interest for the year. Any net realized loss in excess of this amount must be carried over to the following year. If you have a large net loss, such as $20,000, then it would take you seven years to deduct it all against other forms of income (a $3,000 loss every year for 6 years and a $2,000 loss in the seventh year). However, if you were to realize an $8,000 gain three years after you realized your loss, then you would be able to write off that amount of loss against this gain, leaving you with no taxable income for that gain for that year. (For more, see: Tax-Loss Harvesting: Reduce Investment Losses.)

Example

Capital loss of $20,000 in 2012 – no gains against which to net it in year realized-must deduct against ordinary income.

2013 - $3,000 loss

2014 - $3,000 loss

2015 - $8,000 gain

The $8,000 of the remaining undeclared loss can be netted against this gain for the year, bringing the total amount of declared losses to $17,000. The remaining $3,000 can be deducted against gains or ordinary income on the 2016 return. (For more, see: Cost Basis 101: How to Understand It)

The Bottom Line

Sophisticated investors who know the rules can turn their losing picks into tax savings. By using the rules and strategies outlined here, you can lower your tax bill and perhaps diversify your portfolio in some cases. For more information on how you can deduct losses from stocks, read the instructions for Schedule D at the IRS website or consult your financial advisor. (For more, see: When Would I Have to Fill Out a Schedule D IRS Form?)