Tau,
What you are referring to is a "Wash Sale".
The wash sale rule applies to stocks and securities that replace sold shares. It occurs only with substantially identical securities.
Buying and selling 30 days before or after taking a loss on a sale prohibits the tax deduction for that year. Every person with stocks and securities need to know a few basic rules to keep their deduction intact or understand how to carry it over.
Keep records of all stock transactions in a file or on the hard drive. You need to know your buying and selling activity for each year's income tax.
Consider using a spreadsheet to log and track all purchases and sales.
Type column headings with specific information. Enter dates, symbols, number of shares, price bought, price sold and gains or losses.
Look at the previous 30 days buys or sales before you purchase or sell a security. Find stock activity relating to the same company. Consider not buying the same stock within 30 days of selling for a loss.
Sell your security for a loss or gain. The wash rule kicks in when you sold for a loss 30 days prior to buying or selling the same or similar type security.
Enter all securities in your log for 30 days after selling for a loss
Know that within this 30 day period, buying the same stock comes under the wash sale rule only if previously sold. This new purchase voids deducting the loss on your current year's taxes.
from ehow.com