Section 54F requires that the new house is purchased within one year before or after the date of selling the original asset, or the construction of the new house should be completed within three years of the sale. Given that is is allowed to buy the house first and then sell shares within a year, it means that the proceeds from share sale need not be the exact same pile of cash, so to say, that was used to buy the house a year back. Therefore, the Section allows fungibility of the funds. As long as the conditions of the capital gains and house purchase are fulfilled you can interchangeably get the exemption even with a home loan. Source: here