Happy days might seem to be here again for many investors. The S&P 500 (^GSPC -1.11%) is near its all-time highs with the index on track for a second consecutive year of 20%+ gains.

However, major problems could lie ahead. The stock market is doing something it has never done before -- and investors could be "playing with fire," according to Warren Buffett.

An ascending chart on fire.

Image source: Getty Images.

Buffett's warning

Buffett delivered a speech in July 1999 at Allen & Co.'s annual Sun Valley conference. He expressed his view that the stock market was in danger of a dramatic drop. Anyone familiar with history knows he turned out to be right. The dot-com bubble burst the following year, setting the stage for a multiyear decline for many stocks.

In July 2001, Buffett spoke again at the same annual event, revisiting his previous discussion of the stock market. Months later, Fortune magazine published an essay based on his second speech.

He explained a metric that might be unfamiliar to many investors: the ratio of the market value of all publicly traded securities to U.S. gross domestic product (GDP). Buffett acknowledged that this ratio (later dubbed the "Buffett indicator") "has certain limitations." However, he said that it's still "probably the best single measure of where valuations stand at any given moment."

Buffett's speech and his subsequent essay featured a chart that showed the historical trend of this metric between 1924 and 2001. The legendary investor stated, "For me, the message of that chart is this: If the percentage relationship falls to the 70% or 80% area, buying stocks is likely to work very well for you. If the ratio approaches 200% -- as it did in 1999 and a part of 2000 -- you are playing with fire."

An unprecedented level

Different variations of the Buffett indicator exist. Arguably, the most popular is the Wilshire 5000 Total Market Index (which represents nearly every publicly traded U.S. stock) as a percentage of U.S. GDP.

So where does this version of the Buffett indicator stand today? Above 200%. That's an unprecedented level. Remember, Buffett warned over 20 years ago that when the ratio gets close to 200%, investors are "playing with fire." Welcome to the flames.

Has Buffett made any recent comments about the market valuation metric that bears his name? Not to my knowledge. However, some hints indicate he might still believe it's useful.

For one thing, Buffett has been a net seller of stocks for eight consecutive quarters. More importantly, he has grown Berkshire Hathaway's cash stockpile to the largest amount ever.

BRK.B Cash and Short Term Investments (Quarterly) Chart

BRK.B Cash and Short Term Investments (Quarterly) data by YCharts.

What should investors do?

The obvious move for anyone playing with fire is to get away from the flames to avoid being burned. Should investors sell all their stocks with the Buffett indicator at a record high? I don't think so.

Buffett wrote in 2001, "I never have the faintest idea what the stock market is going to do in the next six months, or the next year, or the next two." It's quite possible that stocks could continue rising even with the Buffett indicator at a stratospheric level.

Although Buffett is almost certainly concerned about the valuation of many stocks, Berkshire remains heavily invested in equities. And he has continued to buy a few stocks in recent quarters.

Perhaps the best strategy for investors is the same one campers take with fires. First, be cautious. That could entail building your cash stockpile, trimming positions in stocks in which you don't have a high conviction, and being highly selective with the stocks you buy. Second, enjoy the heat while it lasts.