Get your taxes done using TurboTax

Can I sale those share in the same year and benefit from the higher/adjusted cost base?
For example,
(1) I sell stock A for a $100 loss.
(2) A week after, I buy stock A and (3) immediately sell it at the same price I bought it. Because of the adjusted cost base, the second sale has a $100 loss.
(4) Then I sell stock B at gained 100$ on.
Overall the net gain/loss is $0.
Can all this happen in the same year?