After a tumultuous 2022, the S&P 500 has been rallying in recent months. The index is currently up more than 15% since its low point in October, and many investors may be wishing they'd invested at the bottom to take advantage of these gains.

Further complicating matters is the fact that a recession is looking more likely. Fed officials now expect a "mild recession" by the end of 2023, and analysts at JPMorgan Chase also believe there's a greater than 50% chance we will see a recession sometime this year.

Nobody knows how long this rally will continue or whether stock prices will take another fall in the coming months. If you missed investing at the bottom, is it too late to buy now? Or is it better to hold off in case another downturn is looming? The answer may surprise you.

Does it really matter when you invest?

When the market is rocky, it's easy to get caught up in the short-term fluctuations. But over the long term, it doesn't make that much of a difference when you buy. Even if you invest at seemingly the worst moment, you can still make a lot of money over time.

For example, say you had invested in an S&P 500 index fund in January 2009 -- just before the index bottomed out during the Great Recession. At the time, that may have seemed like a terrible time to buy, as prices almost immediately plummeted.

But over the following five years, you'd still have earned returns of just over 100%.

^SPX Chart

^SPX data by YCharts.

History also shows that there's never necessarily been a bad time to invest in the S&P 500 -- as long as you're a long-term investor.

Analysts from Crestmont Research studied the rolling 20-year total returns of the stock market starting in 1900 to determine which of those 20-year periods resulted in positive returns. 

They found that all 104 periods (from 1919 to 2022) resulted in positive total returns. That means that if you had invested in an S&P 500-tracking fund and held it for 20 years, you'd have made money regardless of what was going on with the market during that time.

Where is the market headed right now?

Hindsight is always 20/20, so it's easy to look back on the market's performance and think about how much you'd have earned if you'd invested at just the right time.

In the moment, though, it's impossible to know precisely where the market is going. Even the experts can't say with certainty whether the S&P 500 will be up or down a month from now, so trying to invest at its lowest point is more of a game of luck than skill.

But waiting too long to invest is also costly. If you're putting off investing because you're waiting to see if the market experiences another dip, you'll miss out on valuable time to allow your money to grow.

In the previous example, you'd have earned returns of around 105% between January 2009 and 2014 despite the fact that the market took a major tumble immediately after investing.

However, if you'd waited until, say, October of that year to invest (at which point the market was well into recovery mode), you'd only have earned returns of around 80% by January 2014.

^SPX Chart

^SPX data by YCharts.

There's no such thing as the perfect time to invest, as it's impossible to know in the moment when the market has reached its highest or lowest point. The best you can do, then, is invest consistently and keep a long-term outlook.

Nobody knows for certain whether the S&P 500 will keep up this rally or if another downturn is on the way. But by investing in the right places and holding those investments for the long haul, you can earn more than you might think.