Very few people will sell their house and move into rented accommodation every time the housing market slows down. But many investors do exactly that with their share portfolios, when stock market conditions become uncertain.

This can be a costly mistake. For one thing, it’s impossible to forecast when the market will turn back up again. The FTSE 100 rose by 9% in six days in February this year. If you’d sat this out because you thought further falls were likely, you’d have missed out on big gains.

Similarly, I don’t think many people predicted that companies such as BP, Diageo and Unilever would deliver gains of 15% or more in the two weeks following last month’s referendum.

The biggest reason?

If you’re still not convinced, I believe the biggest reason to stay invested is that cash doesn’t pay dividends. Reinvesting dividends during stock market dips is a great way to lower your average purchase price and boost your long-term returns.

Figures produced by investment manager GMO a few years ago estimated that 80% of all European equity returns since 1970 have come from dividends. Staying invested and reinvesting your dividends is almost certain to increase your returns.

As long as the companies you own shares of are sound and can recover from any short-term headwinds, then selling is likely to be more costly than doing nothing.

There’s an old investment adage — it’s time in the market that counts, not timing the market. I believe we should follow this advice.

How to avoid big losses

Volatile markets can be quite disturbing. Your portfolio may fall by 10% to 15% in a single day. Surely you should do something?

In situations like this, it’s important to recognise that short-term price movements don’t necessarily mean that the long-term earning power of the businesses that make up the market has changed.

The FTSE fell by nearly 7% when it opened on Friday 24 June, but has since climbed by 10%. Clearly, the fair value of huge companies such as BP, Lloyds Banking Group and J Sainsbury can’t have changed that much in such a short time.

What we do need to watch for is a situation where a company’s earnings power is gradually falling. If this happens, it may be necessary to sell, even at a loss.

Which companies to buy?

In the wake of the referendum, housing, banking and retail stocks have been battered. The market is pricing-in a decline in the housing sector and a slowdown in consumer spending.

It’s too soon to say what will really happen, in my opinion. The sectors above may offer buying opportunities, depending on your investing strategy. There may also be opportunities in the commodity sector, or among traditional defensive stocks such as pharmaceuticals.

I’d only sell and move into cash if I expected to need the money in the next year or two. My view is that the most effective way of protecting your wealth is to pick stocks very carefully.

I believe it’s important to focus on companies with low debt levels, well-covered dividends and strong cash flow. Doing this should mean that your portfolio can ride out short-term setbacks and deliver long-term gains.

Today's top buy tips?

The Motley Fool's experts have been working hard since the referendum. They've been hunting for the best buying opportunities in today's uncertain markets.

They've identified five stocks with the potential to deliver long-term returns that beat the market.

We believe these shares might even help you to retire earlier than expected.

You can find full details of all five companies in our exclusive new report, 5 Shares To Retire On.

It's free and without obligation. To download your copy, just click here now.

Roland Head owns shares of BP, Diageo and Unilever. The Motley Fool UK owns shares of and has recommended Unilever. The Motley Fool UK has recommended BP and Diageo. We Fools don't all hold the same opinions, but we all believe that considering a diverse range of insights makes us better investors.