The stock market has been on a wild ride over the past few months, with the S&P 500 (^GSPC 1.02%) soaring by more than 14% since late October and roughly 23% over the past year.

While this is an exciting turn for investors who have seen many of their stocks beaten down over the last two years, it's normal to feel apprehensive about the future. Nobody knows how long this surge will last, and some investors are worried that this rally is only temporary before another downturn hits.

If stock prices do take a turn for the worse, now may not seem like the best time to buy. But if the market is on its way up, right now could be a smart investing opportunity. So what's the best strategy? And just how safe is the market at the moment? Here's what history says about times like these.

Silver bear and bull figurines against a blue background.

Image source: Getty Images.

Is now a good time to invest in the stock market?

It can be tempting to wait until the perfect moment to buy. After all, if you manage to invest when stock prices are low and then sell after the market has surged, you could make a hefty profit. However, accurately timing the market is next to impossible, even for the experts.

The good news, though, is that history shows there's never necessarily a bad time to buy -- as long as you keep a long-term outlook.

Experts at Crestmont Research, a company that specializes in market and economic research, studied the S&P 500's historical performance over the past century. Analysts examined the index's rolling 20-year total returns, and they found that every single one of those 20-year periods ended in positive total returns.

This means that if you had invested in an S&P 500-tracking fund, such as an index fund or exchange-traded fund (ETF), at any point in history and held it for 20 years, you'd have made money. Even if the market was exceptionally volatile in that time, you'd still have seen positive total returns over the long term.

What if the market falls in the future?

One of the hardest parts of investing is simply not knowing what the future holds. There's always a chance that stock prices could fall after you buy, and those downturns are tough to stomach. But even if you invest at a "bad" moment, history shows that you can still make a lot of money over time.

For example, say you invested in an S&P 500 index fund in February 2009. This was right before the index bottomed out amid the Great Recession, and your investment would have almost immediately lost value. But within just five years, you'd have earned returns of nearly 116% -- more than doubling your money.

^SPX Chart

^SPX data by YCharts

Or let's say you had invested in an S&P 500 index fund in January 2000. The market was just about to experience the dot-com bubble burst at the time, and then as soon as prices recovered and began reaching new highs, the Great Recession began. Yet if you'd stuck it out and held on to your investment, you'd still have earned returns of nearly 120% over 20 years.

^SPX Chart

^SPX data by YCharts

Even amid all the volatility over the last few years, stocks have followed a similar trend. For instance, say you had invested in the S&P 500 in March 2020 -- immediately before the COVID-19 crash and the subsequent slump starting in 2022. While these years have been rough, you'd still have earned returns of more than 61% by today.

^SPX Chart

^SPX data by YCharts

All this is to say that the market has experienced some severe crashes, recessions, and bear markets over the last two decades. But even if you had invested at a seemingly terrible time, you'd still have earned positive total returns by simply sticking it out for the long haul.

The key, though, is to invest in the right places. Strong stocks from healthy companies have the best chances of recovering from downturns, and the more of these stocks you own, the safer your money will be. When in doubt, you can always invest in a broad-market fund like an S&P 500 index fund or ETF.

It's not easy investing when the future is uncertain, and if you're nervous about the market right now, that's normal. But despite short-term volatility, there's never necessarily a bad time to buy. By investing in strong stocks and holding them for as long as you can, you can rest easier knowing your portfolio is as protected as possible.