Whether you have been investing for a while or are just getting started, you've probably heard the advice to "buy low and sell high." After all, everyone likes a good deal. But the decision is rarely that easy.

When stock prices are falling, and your screen is flashing red, it can be difficult to make the contrarian move to step in and buy stocks.

Similarly, it seems counterintuitive to buy a stock when the price is higher than ever. However, buying great companies at high prices can be a winning strategy if the company has a path toward future earnings growth.

Microsoft (MSFT -1.30%), Walmart (WMT 0.05%), and Procter & Gamble (PG -0.03%) are all within 5% of their 52-week highs. Each stock isn't as cheap as it used to be, but there's still a lot to like about these companies as long-term investments. Here's what stands out about each dividend stock.

A person shopping for cleaning supplies.

Image source: Getty Images.

This growth stock is hiding in plain sight

For a while, some investors doubted that there would ever be a U.S. company worth $1 trillion. But in August 2018, Apple crossed that coveted threshold. And almost exactly five years ago in late April 2019, Microsoft also passed the $1 trillion mark.Today, Microsoft is worth $3.16 trillion, compared to Apple's $2.62 trillion. The difference in market cap exceeds the entire value of Tesla.

The sheer size of Microsoft relative to other companies -- as well as the meteoric run-up in the stock price in recent years -- can make it look overvalued at first glance. After all, this is the same familiar stodgy tech company that has been around for decades. But Microsoft is arguably a completely different business than it was at any other point in its history.

Microsoft is leveraging artificial intelligence (AI) across new and legacy solutions. Its cloud infrastructure business continues to be a juggernaut. But at the end of the day, what really matters are Microsoft's margins in relation to its sales growth. And right now, Microsoft is growing sales and margins -- which is the golden one-two punch for driving efficiency and catching Wall Street's attention.

MSFT Operating Margin (TTM) Chart

MSFT Operating Margin (TTM) data by YCharts

In the chart, you can see that Microsoft's operating margins fell off a cliff during the dot-com bust, took several years to recover, and surpassed 40% in the early 2010s (Microsoft Office 365 was launched in June 2011). But then, Microsoft entered a period of declining margins that rebounded largely thanks to the growth from Microsoft Azure.

Microsoft's Intelligent Cloud segment -- led by Azure -- continues to grow. But the key is that Microsoft is also unlocking growth using AI. Microsoft is achieving sales growth from multiple high-margin segments at scale. The growth runway looks sustainable -- making the investment thesis stronger than ever.

Microsoft also buys back stock and pays more dividends than any other U.S. company. So even at a lofty 38.5 price-to-earnings (P/E) ratio, Microsoft is a buy.

Walmart stock isn't as expensive as it looks

Walmart may seem like a safe, low-growth, dividend-paying company. At a 31.3 P/E, it looks pretty expensive too. But it's not so much about where Walmart is, but where it is going.

Analyst consensus estimates call for $2.35 in fiscal 2025 earnings per share (EPS) and $2.59 in fiscal 2026 EPS compared to $1.91 in fiscal 2024. Granted, the company reported $2.21 in adjusted EPS for fiscal 2024. The adjustments factor in currency exchange rates, restructuring charges, opioid legal charges, and other unusual costs. So the jump from fiscal 2024 to the analyst forecasts isn't that profound.

Walmart's P/E based on the fiscal 2025 estimates would be 25.5 -- which is much more reasonable. Coincidentally, this forward projection is the same as Walmart's 10-year median P/E.

Walmart is a buy at around its all-time high because the valuation makes sense. The company is also making improvements with store changes, new design concepts, and accelerating growth with its Walmart+ delivery.

Walmart made its largest dividend increase in over 10 years -- a whopping 9% increase to $0.83 per share per year. That's still a yield of only 1.4%, but Walmart is a Dividend King with over 50 consecutive years of dividend increases. If the raises are more sizable going forward, Walmart stock could become a more meaningful passive income producer.

You can count on this Dividend King no matter what the market is doing

Procter & Gamble (P&G) is down the most from its 52-week high and is the only stock on this list whose current price is below the 50-day moving average.

MSFT Chart

MSFT data by YCharts

As you can see in the charts, all three stocks have a 50-day moving average relatively close to their 52-week highs, which indicates that they have been within striking distance of the 52-week high for a while.

Metrics like the 50-day moving average and the 200-day moving average can provide good readings on market sentiment toward a company within a given window. For P&G, the break below the 50-day moving average may simply be a natural pullback from a sizable run-up.

P&G finished March up nearly 11% on the year, outperforming the consumer staples sector, the S&P 500, and even the Nasdaq Composite. The company has shown strong pricing power and growth, but not enough to justify that kind of move. Even after pulling back, P&G has a 26.2 P/E ratio, but its forward P/E is 24.3.

P&G has a habit of growing into a premium valuation because it grows earnings steadily regardless of the market cycle, thanks to its recession-resistant business model. It repurchases stock, which lowers the share count and boosts EPS, and it has high margins -- much higher than many of its peers'. With 67 consecutive years of dividend raises, P&G is one of the longest-tenured Dividend Kings. But dividends are just one aspect of its capital return program, as it also spends a ton on buybacks and has reduced its share count by over 13% over the last decade.

P&G isn't dirt cheap, but it also doesn't deserve to be.

Quality that's worth the premium price

Microsoft, Walmart, and P&G all deserve to be bought at around all-time highs, but for different reasons.

Microsoft is a high-quality business accelerating its high-margin growth while continuing to reward shareholders with buybacks and dividends.

Walmart is a better value than it appears to be at first glance, and it just increased its dividend by a sizable amount.

P&G has high margins, is a stable business, and rewards its shareholders in various ways.

All three companies are excellent examples of why it is worth paying up for quality in the stock market, rather than trying to buy a weaker business just because the valuation is cheaper.