Sell a losing investment in a taxable account, and the loss cancels gains you’ve taken, while up to $3,000 a year beyond that ($1,500 if filing separately) comes off ordinary income. Reinvest in something similar but not substantially identical, and don’t buy the same investment for 30 days on either side of the sale.

Source