The S&P 500 (^GSPC 0.06%) has been booming over the past year and a half, currently up by nearly 50% from its low in late 2022. The index has also reached two dozen all-time highs throughout 2024, its most recent in late May.

Stock market surges can't last forever, though, and a correction is bound to happen sooner or later. If you invest when prices are at all-time highs and the market dips, it could take a major toll on your portfolio.

So is it really wise to invest when the S&P 500 is reaching new heights? Or should you hold off and see what happens? Here's what history says about investing during the market's high points.

Bull figurine on stacks of hundred dollar bills.

Image source: Getty Images.

To buy or not to buy

Investing at all-time-high prices may seem like a dangerous move, but there's one trick to ensuring it doesn't wreck your portfolio: Stay invested for the long term.

The stock market will always be volatile to a degree in the short term, but over time, it's incredibly consistent. Over the last two decades, the market has faced some of the worst crashes in history -- including the dot-com bubble burst, the Great Recession, the COVID-19 crash, and, of course, the slump throughout 2022.

However, the S&P 500 has also earned total returns of 263% since 2000. Even if you'd invested at seemingly the worst possible moment (at any of the market's highest points before a crash or recession), you'd still have earned positive returns by today.

^SPX Chart

^SPX data by YCharts

Research also shows that, historically, there's never been a bad time to invest in the S&P 500 if you're a long-term investor.

Analysts at Crestmont Research examined the S&P 500's rolling 20-year total returns since the index's inception. From there, they determined how many of those 20-year periods ended in positive total returns.

Their findings? All 105 periods -- from 1919 through the end of 2023 -- ended in positive total returns. In simpler terms, this means that if you'd invested in an S&P 500 index fund or exchange-traded fund at any point in history and held it for 20 years, you'd have made money.

The most dangerous mistake you could make right now

The moves you make right now can make or break your earning potential, and one of the riskiest strategies is attempting to time the market.

In theory, timing the market makes sense. If you can buy when prices are low and then sell when the market peaks, you could make a substantial profit. Likewise, by holding off on buying until the next correction, you could snag stocks at discount prices.

In practice, though, timing the market is nearly impossible to pull off. While a correction could be right around the corner, it's also likely that the market could continue soaring.

The average bull market since 1929 has lasted over 1,000 days, according to investment group Bespoke. Considering we're just over 600 days into the current bull market, there's a chance we could have many more months -- or even years -- of soaring stock prices still ahead. If that's the case, you could miss out on serious gains by not investing right now.

It's unclear where the S&P 500 is headed in the coming months, but the best thing you can do right now is to continue investing consistently. By keeping your money in the market for the long haul, you can minimize risk while maximizing your earnings potential over time.