When the stock market goes crazy -- and it always does -- you may find yourself holding stocks that you like from a fundamental standpoint but are now worth less than what you paid for them. They may be worth nothing at all. What to do?

All of a sudden, your brain crosses a red and black wire, and a flash appears. You say to yourself, "Self, I'll just sell these shares, take the loss on my tax return, and then immediately buy those shares back. I'll be able to keep the stock I really like and take a tax loss all at the same time. Sweet."

Our advice: Don't do it unless you know the rules. Uncle Sammy doesn't see the humor in your attempt to retain the economic position in your stock while you nab a tax deduction. To close this glaring loophole, the feds long ago named this transaction a "wash sale."

Wash sales explained
Under the wash-sale rules, if you sell stock for a loss and buy it back within 30 days before or after the loss-sale date, the loss cannot be immediately claimed for tax purposes.

This rule is designed to prevent you from selling stock to claim the loss and then buying it back within a short period of time to retain ownership. The rule applies to a 30-day period before or after the sale date to prevent "buying the stock back" before it's even sold.

This might sound outrageously unfair to you. After all, if your money was plunked into the stock and your dollars were lost, how can it be that you're not allowed to claim the loss?

Well, you do get to claim the loss -- just not now. Although the loss can't be claimed on a wash sale, the disallowed amount is added to the cost of the repurchased stock. So the loss can be claimed when the stock is finally disposed of, other than in a wash sale.

Example: Larry Laundry buys 500 shares of XYZ Corp. for $10,000 and sells them on June 5 for $3,000. On June 30, he buys 500 shares of XYZ for $3,200. Since the stock was repurchased within 30 days of loss-sale date, the wash-sale rules apply. Larry can't claim his $7,000 loss. Instead, he must adjust his basis in the repurchased shares. His basis in his new 500 shares is $10,200 -- the actual cost plus the $7,000 disallowed loss.

Larry would also be in violation of the wash-sale rules if he purchased his new shares on June 1 and then made the loss sale on June 5. Remember, the rule is 30 days before or after the date of the loss sale. But also remember that if Larry had waited for the required 30 days before he purchased another 500 shares, there would be no wash sale.

Buying fewer shares
What if you repurchase fewer shares than you originally sold for a loss? Is all of the loss disallowed? Nope. Only the portion of the loss attributable to the "washed" shares will be disallowed.

Thus, in the above example, if Larry had bought back only 300 of the 500 shares (60%), he would be able to claim 40% of the loss on the sale ($2,800). The remaining $4,200 of the loss disallowed under the wash-sale rules would be added to Larry's cost of the 300 shares, and Larry's basis in the new shares would be $6,120 -- the cost of the original 300 shares of $1,920 plus the disallowed loss of $4,200.

Clearly, if you're doing a bunch of trading in a specific stock (that's not very Foolish, by the way), the wash-sale rules can really complicate things.

Burning bridges
But -- and this is a very big "but" -- the wash-sale rules don't apply if you close out your entire position in the stock before the end of the year and then stay out of the stock for the required 30-day period before or after the date of the loss sale.

Let's look at Larry again. He certainly has a wash sale in the example above. But let's say that Larry tires of his position in XYZ and sells his 500 shares on Dec. 20 of the same year for $4,000. Larry's adjusted basis in the shares is $10,200 based on his wash-sale computations, and his overall loss would amount to $6,200.

But if you break down the two separate buy and sell transactions, you see that Larry generated a loss of $7,000 on the first transaction and a gain of $800 on the second transaction -- for a net loss of $6,200. This, amazingly, is the same amount of loss Larry computes when taking the wash-sale and basis-adjustment rules into account. So, since Larry closed out his entire position in the shares before the end of the year and stayed out of the stock for the required 30-day period, the wash-sale transactions actually become meaningless, and Larry can compute his gains and losses as he regularly would.

One final note: The wash-sale provisions work on shares that you sell for a loss, but there are no corresponding provisions for stock that you sell at a gain and then immediately repurchase. So although wash-sale losses can't be claimed, gains can't be avoided. That is, if you sell stock for a gain and buy it right back, you must still report the entire gain -- no special gain-deferral rule applies.

What to do with worthless stock
Occasionally, your losses will be really bad. Say you bought a stock at $20 per share, and it dropped to a few cents before going off the board altogether. How can you get rid of it now and claim it on your tax return as a loss?

If the company was liquidated, you should receive a 1099-DIV form at year's end showing a liquidating distribution. Treat this as if you sold the stock for the amount of the distribution. The date of "sale" is the date that the distribution took place. Using your original cost basis in the shares, you can now compute your loss.

If the company hasn't actually been liquidated, you'll need to make sure it's totally worthless before you claim a loss. If you have worthless stock that's not worth the hassle of selling through your broker, you can sell it to a friend (or cousin, aunt, or uncle) for pennies. (However, you can't sell the stock to a spouse, siblings, parents, grandparents, or lineal descendants.) Here's one way to do it:

  1. Get the actual stock certificates from your broker.
  2. Formally sell the shares to the purchaser, with a check for payment and a bill of sale.
  3. Sign over the stock certificate (on its back) to the purchaser. Have the signatures verified by your banker and/or a local stockbroker.
  4. Send the certificate to your stock-transfer agent. Explain that the shares have been sold, and ask to cancel the old shares and issue a new certificate to the new owner.

Some brokerages will offer you a quicker alternative, by buying all of your shares of the stock for a penny. They do it to help out their customers; in addition, over time, some of the shares may actually become worth more than the penny the brokers paid for them.

By selling the shares, you have a closed transaction with the stock and can declare a tax loss. Meanwhile, your friend, relative, or broker, for a pittance, has just bought a placemat or birdcage liner.

For more on the taxes that affect investors, read about: