I believe however that if you want to realize a loss to offset a gain you cannot buy the security you sold back until 30 days after the sale.