With the major indices knocking on the door of new all-time highs, investors may be wondering if it's a bad time to buy stocks. After all, the Nasdaq Composite has nearly doubled over the last five years, and the S&P 500 is up 73%.

It's been a particularly strong hot streak over the last 16 months or so, with the Nasdaq up over 50% since Dec. 31, 2022, and the S&P 500 up 32%. And while it's true that stock prices are outpacing earnings growth, that doesn't necessarily mean you should smash the sell button and run for the exits.

Here's an important lesson on how to view a stock's price, and why Microsoft (MSFT -0.18%) is a good example of a company that is a great buy despite being worth more now than at any point in its history.

A person sitting down while looking at a laptop computer in a skeptical manner.

Image source: Getty Images.

Changing your perspective on price

Often, a price increase doesn't correspond with an increase in benefits. For example, if the price of a dozen eggs goes up, it's not because eggs have suddenly become more nutritious or tastier. The same applies to a service like a haircut or an oil change. Even housing prices usually go up due to demand outpacing supply, not because all homes are magically better than they used to be.

Since we are so used to higher prices being a bad thing in our daily lives, it takes a lot of effort to rewire our brains and realize that the same dynamic isn't true in the stock market. In fact, a stock can be a better deal at a higher price.

This is possible because buying shares in a company is totally different from buying a commodity, a good, or a service. A company can become much more valuable for myriad reasons, such as increased earnings, a higher growth rate, improved efficiency, a better balance sheet with less debt, consistent dividend raises and stock repurchases, etc.

The easiest way for a stock to be a better value despite having a higher price is for its price-to-earnings (P/E) ratio to go down. For example, if the stock price grows at a compound annual growth rate of 5% per year but earnings grow at 7%, then the P/E ratio will be lower, and the stock could be a better value even though the price is higher. However, that's not what is happening in the broader market today.

The market is more expensive

The P/E ratio of the S&P 500 is a whopping 26.9. That's higher than normal, but it's not a sky-high valuation.

The average monthly P/E ratio of the S&P 500 over the last 20 years is 24.5. Granted, that figure is skewed higher because of a 123.7 P/E on May 1, 2009, due to a post-financial crisis collapse in earnings and a 36 P/E ratio on Jan. 1, 2021, due to the COVID-19-induced earnings decline. If we take out the outliers that are above 35, the average P/E ratio over the last 20 years is 21.0.

Since that is meaningfully lower than the current level, it's easy for skeptics to argue that the stock market is overvalued. But again, just because the market is more expensive from an earnings multiple standpoint doesn't necessarily mean it's a bad time to buy stocks. An excellent example of this is Microsoft.

A better business deserves a higher valuation

Microsoft is up a blistering 954% over the last decade. It's a mind-numbing gain, especially for a legacy tech company that has been center stage for so long. But Microsoft has mostly backed up that gain by transforming itself into a much higher-quality business.

MSFT Chart
MSFT data by YCharts.

As you can see in the chart, Microsoft's stock price is near an all-time high. That's because its sales growth has been commensurate. In addition, its operating margin is now much higher than it used to be. 

So not only is Microsoft earning more revenue, but it's booking nearly $0.45 on the dollar in operating income. That's high for a company generating hundreds of billions of dollars in sales every year.

Most importantly, Microsoft has made sizable raises to its dividend every year and has a larger dividend expense than any other U.S.-based company. What's more, Microsoft is now reducing its outstanding share count. When the company was in its earlier stages of growth, stock-based compensation would outpace stock buybacks, leading to dilution. Microsoft's stock-based compensation is at a record high, but so are its buybacks.

Despite paying $10.5 billion over the last year in stock-based compensation, Microsoft is still able to reduce its share count. In fact, its share count has dropped by 9.8% over the last decade thanks to buybacks, which boosts earnings per share and makes each outstanding share a better value.

MSFT Shares Outstanding Chart
MSFT Shares Outstanding data by YCharts.

Stock-based compensation is an important way to recruit and retain top talent who want a skin in the game. In the past, it came at the expense of existing shareholders. But today, Microsoft is such a high-margin cash cow that it can reward its employees and its shareholders, thanks to its higher quality business today.

Microsoft's trailing P/E ratio is 34.5 -- higher than its historic average. But notice how the company's median P/E ratio has increased in recent years, with the three-year higher than the five-year median, and so on. That's because Microsoft has steadily become a better business, and the market has responded by giving the stock a higher multiple.

MSFT PE Ratio Chart
MSFT PE Ratio data by YCharts.

Microsoft is a perfect example of a stock that looks expensive at first glance because the stock price is hovering around an all-time high and the valuation is higher. However, the quality of the business, from its profitability to the multiple ways it rewards shareholders, has also improved with time. So overall, Microsoft is arguably a better buy now than five to 10 years ago because it is a far better business, which more than justifies its higher valuation.

The right way to invest

When major, benchmark indices are hovering around all-time highs, it's natural to be a bit skeptical. With most bull markets, there is usually a blend of higher prices based on improving fundamentals and froth based on greed and momentum traders looking to make a quick buck.

If you're buying a stock high, then it's important to make sure it's going up for the right reasons -- as in earnings, growth, and the investment thesis in general can back up the appreciation in the stock. Being too timid would have led investors to miss out on the megacap growth names that have been leading the market for over a decade -- like Microsoft.

In sum, now is not the worst time to buy stocks in history -- far from it. But given higher prices, knowing what a company does and why you want to own a piece of it is especially important.