Defying the coronavirus pandemic’s mounting human and economic toll, stocks closed Tuesday at a record high, bringing an end to the shortest bear market in U.S. history.

After notching three consecutive weeks of gains, the Standard & Poor’s 500-stock index closed at 3,389, gaining 0.23 percent on the day. The finish capped a remarkable comeback from the March plunge that slashed 34 percent off the previous record, set Feb. 19, as the pandemic tightened its grip on the country.

Investors Tuesday brushed aside worries about the nation’s continuing struggle to contain the pandemic, focusing instead on signs of strength in the housing and retail sectors. Housing starts in July rose 22.6 percent to a seasonally adjusted annual rate of nearly 1.5 million, the Commerce Department said. Permits also rose sharply for both single- and multifamily dwellings.

“One of the economy’s most important leading indicators is telling us that not only is the pandemic recession over, the economic outlook is actually growing brighter by the day,” Chris Rupkey, chief financial economist for MUFG Union Bank in New York, wrote in a note to clients.

Walmart, one of the nation’s largest retailers, reported same-store sales rose 9.3 percent in the quarter that ended July 31. The company said its online business rose by 97 percent.

The pandemic, meanwhile, continues to stalk the $19 trillion U.S. economy. More than 5.4 million Americans have been infected by the coronavirus, and more than 171,000 have lost their lives, according to Johns Hopkins University.

As in previous stock market rallies, the financial rewards of the latest surge have been highly uneven. Savvy investors have been showered with profits, while millions of people without money to invest or even pay rent in the months ahead have been left out. Only about half of Americans own stocks, mainly through retirement accounts, according to the Federal Reserve.

“At the end of the day, the record is just a number,” said Wayne Wicker, chief investment officer at Vantagepoint Investment Advisers. But the rise in the stock market suggests that investors see a recovery on the horizon. “Although today’s economy remains in a miserable state in an absolute sense,” he said, “markets care about better or worse, not good or bad,” and the leading economic indicators continue to improve.

The S&P 500’s milestone came one day after the tech-heavy Nasdaq composite index posted its own record, drawing kudos on Twitter from President Trump. Before trading began on Tuesday, Trump touted signs of economic recovery and took credit for the market upswing.

“Jobs are flowing, NASDAQ is already at a record high, the rest to follow. Sit back & watch!” Trump tweeted.

For Main Street, moves in the market often presage moves in the economy, said Kristina Hooper, Invesco’s chief global market strategist. “So a strong rebound for stocks could indicate that the economy will also experience a strong rebound,” she said.

The Dow Jones industrial average, which remains about 5 percent below its February peak, gave up 66.8 points, or 0.2 percent, ending the day at 27,778. And the Nasdaq increased by 81.1 points, or 0.73 percent, posting 11,210.

Stock prices cratered in March after the initial coronavirus cases and subsequent stay-at-home mandates designed to slow infections. Since then, Wall Street has mounted a dramatic comeback. Several companies, such as Tesla, Wayfair and Masco, have set their own share-price records. Perhaps most prominently, investors have flocked to the tech giants, which wield huge cash reserves and whose products are seen as increasingly valuable in the shift to working from home and remote learning.

In 2018, Apple generated a stream of headlines for becoming the first company with a market value of $1 trillion. Two years later, the maker of the iPhone is on the cusp of doubling that figure. Microsoft and Amazon are not far behind, with valuations near $1.6 trillion.

“Notably, the rally has largely been driven by the continued surge in megacap tech stocks, which have carried the S&P 500 to new record highs through the pandemic as investors wager on their ability to deliver strong growth unhindered by the broader economic shutdowns,” said Nicole Tanenbaum of Chequers Financial Management.

But the stock market rally also coincides with dire economic data, double-digit unemployment rates and the relentless virus.

“Certainly the last time we hit this number the world looked a lot different than it does today,” said Wicker. “Despite the fact that we are faced with new challenges it seems to indicate that investors are pretty positive about where we are going in the future,” he said.

The turnaround concludes the shortest bear market in history. A bear market is generally understood to refer to a period when stocks fall at least 20 percent from the previous peak. And a bull market is defined as a 20 percent rise from the previous low.

Now that the S&P 500 set a new peak, it means the bear market lasted from Feb. 19 to March 23, when stocks bottomed and inaugurated a fresh bull run.

“Even though financial markets reflect a lot of volatility this year, the long-term story is they are resilient and have an upward bias over time,” said Wicker.

The record arrived as the Democratic Party kicked off its national convention, as presumptive nominee Joe Biden holds a double-digit lead over Trump, according to a Washington Post-ABC News poll. Congressional Democrats have pushed for a more aggressive economic response to the coronavirus crisis and have characterized Republican plans as inadequate for a public health emergency. Earlier this month, Trump attempted to bypass Congress by signing executive actions to make dramatic changes to tax and spending policy in an effort to expand unemployment aid for jobless Americans.

Investors have been buoyed by the dramatic actions of the Federal Reserve and Congress to keep the economy from sliding further. “As it did during the global financial crisis, the Fed provided the rocket fuel for stocks’ gains, despite high unemployment and high covid-19 infections and deaths,” said Hooper, explaining how the stock market has “decoupled” from the broader economy.

Shareholders are also anticipating another round of emergency government relief. But as both parties prepare to deliver their political sales pitches to the American public, Congress and the White House have yet to agree on the next stage of coronavirus aid.