The S&P 500 (^GSPC 0.16%) has returned 14% year to date, rocketing through more than two dozen record highs in the process. The index peaked most recently on Wednesday, June 12 following better-than-expected inflation data from May.

To elaborate, the Consumer Price Index (CPI) climbed 3.3% during the 12-month period that ended in May, slightly below the 3.4% analysts anticipated. Investors hope the Federal Reserve will cut interest rates in the near future, and as inflation cools, the odds of that happening increase.

Additionally, the enthusiasm surrounding artificial intelligence (AI) has contributed to the upward momentum in the stock market. Nvidia alone is responsible for approximately one-third of the gains in the S&P 500 this year.

That puts investors in a tricky situation. On one hand, cooling inflation and investments in artificial intelligence could turbocharge the economy and send the stock market higher. On the other hand, the S&P 500 may be due for a correction after ripping higher so quickly. Is it safe to buy stocks with the S&P 500 near its record high?

History offers a clear (and somewhat surprising) warning.

The S&P 500 has historically produced superior returns from record highs

The U.S. stock market is having a very good year. The S&P 500 recorded its best first-quarter performance since 2019, and the benchmark index returned more than 10% during the first 100 trading days for only the fourth time in the last quarter-century. Meanwhile, the S&P 500 has been rolling through record highs at an impressive pace.

Against that backdrop, investor sentiment (as measured by weekly surveys from the American Association of Individual Investors) has steadily declined in recent months. Specifically, 52% of investors surveyed in early March expected the stock market to produce a positive return during the subsequent six months, but just 39% of investors surveyed in early June expected the stock market to rise over the next six months.

That trend is presumably due to the notion that record highs are frequently followed by stock market corrections. But history refutes that idea. If you invested money in the S&P 500 on any random day between January 1988 and December 2022, your average one-year return would have been 11.9%. But if you only invested money in the S&P 500 at record highs during that period, your average one-year return would have been 13.7%, according to JPMorgan Chase.

The same pattern holds over other time periods, as shown in the chart below.

Time Period

Invest on Any Day (Average S&P 500 Return)

Invest at Record High (Average S&P 500 Return)

6 months

5.8%

6.2%

1 year

11.9%

13.7%

2 years

25%

28.9%

3 years

40.2%

48.1%

Data source: JPMorgan Asset Management..

The chart above carries a clear warning: Investors could shortchange themselves by avoiding the S&P 500 at record highs. Of course, that depends on which stocks those hypothetical investors plan to purchase. But I can say this with certainty: Since 1988, an S&P 500 index fund would have produced superior returns (on average) if money was invested only at record highs, compared to any random day.

Some Wall Street analysts expect the S&P 500 to decline sharply

A bear market occurs when the S&P 500 falls at least 20% from its peak. In other words, every bear market starts when the index reaches a record high, then subsequently slips at least 20%. I mention that to remind investors that any record high could be the last one before a stock market drawdown begins.

Several Wall Street analysts expect the S&P 500 to decline sharply in the coming months. JPMorgan Chase has set the index with a year-end target of 4,200, which implies downside of 23% from its current level of 5,421. Similarly, analysts at Morgan Stanley and Evercore have set the S&P 500 with year-end targets that imply downside of 17% and 12%, respectively.

One reason for that pessimism is pricey valuations. The S&P 500 trades at 25.7 times earnings, a premium to the five-year average of 23.3 times earnings and the 10-year average of 21.4 time earnings, according to FactSet Research. That means many stocks are historically expensive. Investors should be cognizant of that fact when making decisions.

In summary, investors shouldn't avoid the stock market completely, as they could shortchange themselves by sitting on the sidelines. However, investors should only buy stocks in which they have a great deal of confidence, and only if the current valuation is sensible.