The stock market has been rallying lately, with the S&P 500 (^GSPC 1.02%) up by nearly 42% from its low point in October 2022.

While new bull markets are exciting and many investors are feeling optimistic about the future, others are concerned that perhaps the best buying opportunity is already behind us. With stock prices soaring by the day, it may feel like it's too late to invest now.

Nobody can say for certain where the market is headed over the coming weeks and months, and past performance doesn't predict future returns. Sometimes, though, it's helpful to look back and see what history says about times like these. Turns out, it has good news about the future.

Person with a serious expression looking at a laptop.

Image source: Getty Images.

A safer investing strategy

It can be tempting to put off investing until the perfect moment, especially when you have the power of hindsight. If you'd invested in October 2022 when the S&P 500 reached its low point, you'd have earned substantial returns by today. It may seem wise, then, to wait until prices drop a bit before you buy.

But in the moment, it's impossible to say where the market is going. Back in 2022, nobody knew that the next bull market was about to begin, and many experts were warning that a recession was imminent. While stocks could fall in the near future, they could also continue surging. If that happens and you choose not to buy now, you'll have missed out on those potential gains.

Rather than trying to find the perfect moment to invest, then, it's often safer to hedge your bets through a strategy called dollar-cost averaging. With this approach, you're investing a set amount of money on a regular schedule throughout the year. Sometimes you'll end up buying at peak prices, while others you'll invest when stocks are priced at steep discounts.

With dollar-cost averaging, you don't need to worry as much about when to buy. If the market dips in the future, simply stay invested and keep buying on your schedule. The market has managed to recover from every single downturn it's ever faced, and chances are good it will survive future volatility, too.

The biggest risk you're facing right now

When it comes to building wealth in the stock market, one of the biggest risks you'll face is not buying at the wrong time -- it's waiting too long to buy at all.

History has proven that time is your most valuable asset when investing, and waiting too long to invest can actually cost you more than buying at an inopportune time. While nobody knows exactly what the future holds for the market, historically, it's managed to earn positive total returns over time -- no matter how much volatility it experiences in the short term.

For example, say you had invested in an S&P 500 index fund in January 2009. The market was just about to face a big drop before bottoming out in March of that year amid the Great Recession, and at the time, it may have seemed like a terrible moment to invest. Yet over the next 10 years, you'd still have seen returns of more than 177%.

^SPX Chart

^SPX data by YCharts

On the other hand, say you'd waited a year and invested in January 2010. At that point, the S&P 500 had already been in a bull market for nearly a year, and the index had soared by more than 65% from its low point in March 2009. Still, though, by January 2019, you'd have a return of close to 125%.

^SPX Chart

^SPX data by YCharts

Let's imagine one more scenario. Perhaps you were concerned that the best time to invest had already passed, and you held off on buying in case another downturn was looming. If you had waited just one more year until January 2011 to buy, you'd have only notched a total return of 99% by January 2019.

^SPX Chart

^SPX data by YCharts

All of this is to say that there's never a perfect time to invest, and giving your money as much time as possible to grow can help you earn more over time than buying at just the right moment. Even if the market takes a turn for the worse in the coming months, you can still see substantial gains over the long run by simply staying invested.

You may not be able to go back in time and invest when stock prices were lower. But you can take advantage of this opportunity to buy while we're still in the early stages of this bull market. There's still plenty of room for growth in the market, and investing now can help you maximize your long-term wealth-building potential.