The week began with a warning that America was about to enter the worst days of a once-in-a-generation pandemic that would kill thousands of Americans and overrun hospitals and medical supplies.

Brace for a “hell of a bad two weeks,” President Trump had warned.

But investors saw something more.

U.S. health officials made mentions over the previous weekend toward “inklings” of progress against the coronavirus. New cases were slowing domestically and across the globe. More people were wearing masks. On Monday, new modeling data suggested infection levels might not be as bad as initially projected. Social distancing was working.

Seizing on those and other micro-cues, Wall Street sent the three major U.S. stock indexes on a week-long tear, seemingly ignoring the tangible. The nation’s death toll is five figures and counting. Entire industries are offline. And nearly 17 million Americans filed for unemployment benefits in three weeks.

“The market is a forward-looking discounting mechanism and showed its belief that a worst-case recession is less likely and a quicker-than-expected recovery is more likely,” said David Bahnsen, the chief investment officer of the Bahnsen Group, a private wealth firm in Newport Beach, Calif., that manages $2.3 billion in assets. “There were more positives than negatives.”

Stocks have swung wildly the past several weeks, often suggesting a disconnect with the terrible economic and public health news of the moment. Investors such as Bahnsen try to sort through the uncertainty and anticipate what the economy will look like when the virus is vanquished — and where there is money to be made.

The stock market has been one of the best real-time measures for gauging what investors are thinking. The Standard & Poor’s 500-stock index — a reliable indicator of future performance for America’s 500 largest companies — closed Thursday up 13 percent for the week, its best since 1974.

“Stocks move on anticipation of where people think the economy is headed, not where it is at the moment,” said Howard Silverblatt of S&P Dow Jones Indices. “The major question is what is going to happen tomorrow, next week, next month. Trades are based on that perception. Investors perceive that there may be light at the end of the coronavirus tunnel, but there will still be volatility because of so many questions remaining on the economy.”

“I don’t buy a stock because of what it is doing today. I buy a stock because of what it might do in the future,” said Randy Frederick, the vice president of trading and derivatives at the Schwab Center for Financial Research.

The S&P 500’s role as a business indicator is based on the collective wisdom of millions of investors who are wagering tens of billions of dollars a day based on untold bits of available information. The efficient market theory, as it is known to its adherents, holds to the belief that the market prices most stocks correctly, based on the information available at that time.

But stocks are not infallible. In a 1966 Newsweek article, the eminent economist Paul Samuelson famously quipped that the stock market had predicted nine of the past five recessions.

Despite its imperfections, the S&P index remains one of the 10 leading indicators that Wall Street and Main Street use to predict where the business cycle is going. Some analysts view it as Wall Street’s canary in the coal mine: It’s the first to warn that trouble is around the corner.

“The S&P is a good leading indicator because it reflects the expectations for future profits of companies, future demand for goods and services in the economy,” said Gad Levanon, the head of the labor market institute at the Conference Board, a think tank focused on global business. “Companies are very much impacted by future economic conditions. And when expectations for future business conditions are improving, that typically raises stock prices and the S&P 500.”

But investors and computer algorithms can twist markets into a pretzel trying to decipher every utterance from government leaders, central bankers or chief executives as they look to sell or buy stocks.

Take the “good news is bad news” phenomenon. Seemingly good economic news can boomerang on investors, which happened last July when an unexpectedly strong jobs report from the previous month caused a stock sell-off. That is because investors thought it would discourage the Federal Reserve from cutting interest rates. Low interest rates are generally perceived as good for the stock markets and companies.

As recently as a month ago, while the coronavirus was spreading in the United States, stocks jumped on news that former vice president Joe Biden had dominated on Super Tuesday, all but erasing Sen. Bernie Sanders’s insurgent campaign for the Democratic presidential nomination. The surge was led by the health-care stocks, which would have suffered under Sanders’s pledge to provide free health care to Americans.

“The market moves erratically in the short term, what I would term a knee-jerk reaction on a day-to-day basis,” said Kristina Hooper, the chief global market strategist at Invesco. “There is some method to the madness. The market is trying to look ahead … to look out a few months.”

“We’ve seen market reactions to Democratic primary results,” Hooper said. “We saw markets move when there was polling showing Sen. Elizabeth Warren gaining momentum. We saw it when Sen. Bernie Sanders was gaining momentum.”

The coronavirus and the subsequent lockdown of the economy happened so swiftly that investors did not detect recession warnings from the normal economic signals. As a result, stock prices peaked around the same time the economy did and not earlier, as is usually the case.

The S&P 500 hit its all-time high as recently as Feb. 19, after the coronavirus had flattened China and was working its way through South Korea and Europe and on its way to the United States. By March 23 — 33 days after its peak — the index had lost more than 30 percent of its value and a 11-year stock market expansion had collapsed.

“We’ve never seen anything like this before,” Frederick said. “A month ago, I didn’t know we would have a bear market. It’s the most abrupt bear market in history. It’s almost certain we will have a recession.

Fritz Gilbert, 57, who writes a retirement blog in the Atlanta area, is incrementally buying stocks and not paying too much attention to which way stocks are pointing on a daily basis.

“I am not blowing all my dry powder at once, but I don’t want to pass up an opportunity when stocks are on sale,” Gilbert said. “I am selling bonds and using cash to buy stocks. I knew a bear market would come at some point. And the effects of the virus are horrific. This is going to take a while to ripple through the market.”

The usual leading indicators that feed into stock picking were showing little sign of slowing down when the National Basketball Association shelved its season, which was soon followed by a succession of statewide shutdowns by New York, California, New Jersey and others.

The trickle of leading indicators, including initial jobless claims, building permits, consumer confidence and manufacturing, came later. The only tell that trouble was ahead in the past year came from the inversion of the yield curve, which occurs when yields — or returns — on short-term U.S. bonds eclipse those of long-term bonds. An inversion occurred last August, which many economists interpreted as a sign that a recession would be coming in a year or two.

The question going into the three-day Easter weekend was whether the week’s huge rally was an indicator of better times, or a false start that will be overwhelmed by the poor economic data that is certain to register in coming days.

When in doubt, economists look back to history.

Frederick referred to March 9, 2009, which marked the bottom of the Great Recession and the launch of the bull market, as a time when markets were able to sniff out what was ahead.

The S&P 500 index cratered that day, smack in the middle of where other indicators — initial jobless claims, durable good orders, building permits, consumer credit — had hit bottom.

“At that point, the unemployment rate was very high. Much of the bad stuff had peaked,” Frederick said. “No one thought things were going to get better. But the market anticipated better times and it turned out to be right.”

“It gets it right most of the time,” he said.