You don't have a wash sale, even though you bought identical shares within the previous 30 days, if the shares you bought aren't replacement shares . There are mechanical rules to handle the situation where you don't buy exactly the same number of shares you sold, or where you bought and sold multiple lots of shares. See Wash Sale Mechanics . You don't actually have to purchase stock within the wash sale period to have a wash sale. It's enough if you merely enter into a contract or option to acquire replacement stock .
Short sales present a special problem in connection with the wash sale rule.
There are special considerations in applying the wash sale rule to traders . Losses Only The wash sale rule only applies to losses. You can't wipe out a gain from a sale by buying the same stock back within 30 days. No technique is completely safe. Here are some ideas to consider.