The stock market has been surging lately, with the S&P 500 up by roughly 11% since late October alone. The index has soared by just under 20% since the beginning of the year, and the tech-heavy Nasdaq is up by a whopping 35% in that time.

While many people are optimistic that the worst is behind us, other investors worry that there's nowhere to go but down right now. That can make it confusing to decide whether or not to buy, especially when nobody knows what prices will do in the future.

So is it really the right time to invest? Or should you hold off to see what the market does? The answer may be surprising.

Is it safe to invest right now?

If stock prices continue rising, now could be a fantastic time to invest to take advantage of those gains. But if prices fall, it may seem like it's best to avoid investing now. However, as long as you're a long-term investor, there isn't necessarily a bad time to buy at all.

Person with an uncertain expression looking at a computer.

Image source: Getty Images.

Nobody can predict exactly how the market will perform over the coming weeks or months. Over decades, though, it's incredibly consistent at earning positive total returns.

In fact, analysts at Crestmont Research studied the S&P 500's rolling 20-year total returns and found that every single period ended in positive total gains. In other words, if you had invested in an S&P 500 index fund or exchange-traded fund (ETF) at any point in history and simply held it for 20 years, you'd have made money regardless of how the market performed during that time.

Even if you invest at a "bad" time, it's still possible to earn more over the long haul than if you wait for the perfect time to buy.

For example, say you had invested in an S&P 500 index fund in February 2009 just before the market bottomed out amid the Great Recession. That may have seemed like the worst possible moment to invest, as prices almost immediately plummeted. But by the end of the year, you'd still have earned returns of more than 35%.

^SPX Chart

^SPX data by YCharts.

On the other hand, say you had waited until September to invest. At that point, prices were already significantly higher and the market was well into its recovery. However, by the end of the year, you'd have only earned returns of around 12%.

In the short term, nobody knows exactly what the market will do. But over time, it's incredibly likely to see positive returns. Rather than getting caught up in the day-to-day fluctuations and trying to find the perfect time to buy, it's often better to simply invest whenever you can and hold those investments for the long term.

The key to long-term investing success

One crucial thing to note is that not all investments will experience positive long-term returns. If you invest in the wrong places, your portfolio may have trouble bouncing back from periods of volatility.

While there's no single correct way to invest, the best stocks are the ones from companies with solid underlying business fundamentals -- such as a competitive advantage in the industry, healthy financials, and a competent leadership team.

Healthy stocks will still experience short-term fluctuations, especially when the market is volatile. But they're far more likely to rebound and go on to see long-term growth. The more of these stocks you have in your portfolio, the safer your money will be (and the more you can potentially earn over time).

It's been a tough couple of years for the stock market, and it's normal to feel apprehensive about where prices are headed. But even if stocks fall again, that doesn't necessarily mean it's a bad time to buy. By investing in quality stocks and holding them for the long term, you can better protect your portfolio regardless of what happens with the market.