Many people put money in the stock market to let it compound and grow for decades. But even a five-year time horizon can result in good returns. A shorter time in the market adds to the risks, however.

Since 2000, it has taken the S&P 500 index an average of about 2.5 years after a U.S. recession ends for it to reach pre-recession levels, according to a Motley Fool research report. An investor can help manage the timing risk through diversification and purchasing stocks at good historical valuations.

Here are two stocks that can be bought today to help give a $5,000 investment a better chance of a successful outcome in the next five years regardless of how the market swings. 

1. Home Depot

Home Depot (HD 1.07%) has been a wildly successful investment over the last decade. Its total return, including dividends, has more than doubled that of the S&P 500. But more recently, it has outpaced the index to the downside. Year to date, Home Depot stock is down more than 30%. That has presented investors with an opportunity. Home Depot's price-to-earnings (P/E) ratio is at a level only seen once in the last 10 years. 

HD PE Ratio Chart

HD PE Ratio data by YCharts.

The concern from investors that has hit the stock price is that the economic environment could cause the earnings portion of that ratio to fall. But Home Depot has been making investments to ensure that it can succeed in various types of markets. It announced a multi-year investment strategy called One Home Depot in 2017.

The $11 billion investment has helped build the company's online channels, and that paid off as more consumers than ever shop online. The company also acquired HD Supply, a supplier of maintenance, repair, and operations products, in an $8 billion deal announced in late 2020 to refocus on the professional contractor base. Various housing markets will attract more business from either consumers or professionals. Home Depot has both sides covered. 

2. Berkshire Hathaway

There may not be a single stock investment that creates as much diversity in a portfolio as Warren Buffett's Berkshire Hathaway (BRK.A 0.72%) (BRK.B 0.51%). It owns operating businesses in manufacturing and retail, an energy and utilities business, BNSF railroad, and insurance. It also holds large investments in a broad mix of companies that include several banks, Apple, Coca-Cola, as well as automakers. 

But it's not just diversity that makes it a good place in which to invest right now. Like Home Depot, Berkshire is trading at a historically low valuation. Berkshire has spent more than $51 billion to repurchase about 9% of its own shares in 2020 and 2021. Buffett has said that price-to-book value is the metric he uses to gauge when share buybacks make good sense. 

BRK.B Price to Book Value Chart

BRK.B Price to Book Value data by YCharts.

Its recent price-to-book value is nearly as low as its been over the past decade other than during the pandemic-induced plunge. At 1.2 times book value, the stock price is at the level at which Buffett has previously said he would tend to increase prioritizing share buybacks. That makes it a good time for investors to follow his advice and add diversity to a portfolio at the same time.