We all love going to experts for advice. And who better to advise us on buying stocks than Warren Buffett? The Oracle of Omaha is known for investing in quality companies that are undervalued and holding them for the long term. He benefits once the stocks rise as the market recognizes their value.

His strategy clearly works. At the helm of Berkshire Hathaway, the billionaire investor has driven a compounded annual gain of nearly 20% over the past 57 years. That's compared to a little less than 10% for the S&P 500. So it makes sense to generally follow Buffett's advice.

But there was one particular time when Buffett compared investing in stocks to playing with fire, and we could be approaching such a moment right now. Should you go against Buffett's advice in this particular situation and keep buying stocks? Let's find out.

Warren Buffett.

Warren Buffett. Image source: The Motley Fool.

Buffett's favorite indicator

Stocks have rallied since the beginning of the year. And the S&P 500 has climbed more than 20% from its bear market low back in October. But today, one of Buffett's favorite indicators regarding stock prices is sounding an alarm.

The "Buffett Indicator" divides the total market value of the world's publicly traded companies by the global gross domestic product (GDP) and has risen to about 110%, according to Business Insider. A level above 100 suggests stocks have become too expensive in relation to the economy. 

Buffett tends to look at the U.S. version of this indicator, which involves the market value of U.S. stocks and GDP. And this indicator recently climbed to 159%, Business Insider reported. Buffett considers this indicator "the best single measure" of valuations, according to a Fortune article a few years ago -- and said that buying stocks when the number approaches 200% could be considered "playing with fire."

If this indicator moves close to 200%, Buffett's warning applies: Buying stocks could be dangerous.

But does this truly mean everyone should stop buying stocks if we do approach those levels? Not necessarily. It's important to note that Buffett issues these sorts of warnings because he realizes some people invest without giving each individual stock a close look.

They might get excited about a popular stock's performance -- and pile in due to a fear of missing out. Or they may invest in a basket of top-performing stocks without studying whether the players have what it takes to grow earnings over time. Those stocks may be at their peak right now. In those cases, an investment at a time when the market is overvalued truly would be playing with fire, as Buffett says.

Going against advice -- along with Buffett

But if you look at each company individually, you can continue to buy stocks in a generally overvalued market -- and go against Buffett's advice. As a matter of fact, Buffett himself has gone against his advice in such situations.

For example, in August 2021, the Buffett indicator for U.S. stocks climbed past 200%. But during the third quarter of that year, Berkshire Hathaway continued to selectively buy stocks. For example, Buffett increased his position in Chevron Corp. by 24%.

The key word here is "selectively." Buffett didn't rush out and aggressively buy stocks. Instead, as always, he carefully considered each individual company -- and invested only when valuation, earnings, and prospects looked promising. You can find these opportunities at any time, even when the market, as a whole, is overvalued.

So what does this mean for you today and in the coming weeks? As Buffett suggests, it's important to be cautious, especially when indicators point to stocks becoming more and more expensive.

But this doesn't mean you should stop investing. Instead, pay close attention to valuation and the cash a business is generating or expected to generate down the road. And consider a company's market position and the size of the market it serves.

If the stock you have your eye on still looks reasonably priced, then you might go against Buffett's warning -- as he has done. That's because, as a stock picker, you're able to invest in any market environment and potentially win over the long haul.