By Caitlin Ostroff

U.S. stocks swung between small gains and losses Wednesday as investors weighed a rise in coronavirus infections against signs that the economy is recovering.

All three indexes opened modestly higher before waffling in morning trading. The S&P 500 was recently off less than 0.1%. The Dow Jones Industrial Average fell 70 points, or 0.4%. Meanwhile, the Nasdaq Composite rallied, adding 0.3%.

Stocks are attempting to continue their stretch of gains after a punishing selloff last week. Despite recent indications that the economic recovery from the coronavirus pandemic will be bumpier than investors originally hoped, traders' risk appetite has continued this week. If stocks close higher Wednesday, major indexes will have rallied for four consecutive days.

The Federal Reserve's moves to backstop credit markets, combined with better-than-expected economic indicators such as a rebound in monthly retail sales, have added to traders' confidence this week. Fresh data Wednesday showed new home construction rose 4.3% in May after having declined a seasonally adjusted 26% in April from a month earlier.

"There continues to be evidence of a durable bottom in economic activity. The worst is over in my mind," said Patrick Spencer, managing director of U.S. investment firm Baird. "I think a V-shaped recovery is under way."

Large-cap technology companies were among the leaders shortly after Wednesday's session began, with Netflix gaining 1.6% and Amazon adding 1.3%. Home builders also rallied, with Lennar adding 1.8% and PulteGroup gaining 1.5%.

Meanwhile, shares of cruise companies tumbled after Norwegian Cruise Line Holdings said Tuesday night it would suspend the majority of its voyages through at least Sept. 30. Its shares fell 8.5%, while shares of Carnival declined 5.6% and Royal Caribbean Cruises slipped 7.1%.

Still, the choppiness of Wednesday's session underscores how some investors are still taking a more cautious approach as they track rising coronavirus infections. Global coronavirus infections have surpassed 8 million, with Texas, Arizona and North Carolina reporting record daily coronavirus-related hospitalizations on Tuesday.

"Certainly the next phase is not going to be as straightforward or as easy as the leg we've just had" with the stock market's rally, said Derek Halpenny, head of research for global markets in the European region for MUFG Bank. "Now the equity market is going to be a bit more unpredictable."

Fed Chairman Jerome Powell will continue to testify before Congress on monetary policy on Wednesday, and is scheduled to speak before the House Financial Services Committee at 12 p.m. On Tuesday, he told the Senate Banking Committee that a full economic recovery is unlikely "until the public is confident that the disease is contained."

The yield on the 10-year Treasury on Wednesday fell to 0.743%, from 0.754% Tuesday. Yields fall when prices rise.

Globally, the pan-continental Stoxx Europe 600 added 0.7%, and in Asia, most major equity benchmarks drifted higher by the close of trading, though some markets were turbulent as investors assessed growing geopolitical risks in the region. North Korea blew up an inter-Korean liaison office after South Korea's market close Tuesday, marking Pyongyang's resumption of a provocative stance while denuclearization talks with Washington remain gridlocked. Chinese and Indian troops also clashed along their disputed border, causing 20 Indian soldier deaths and Chinese troop casualties.

Hong Kong's Hang Seng Index ended the day up 0.6%, while the benchmark indexes in South Korea and China ticked up 0.1%. India's Sensex edged down 0.3%.

Japan's Nikkei 225 fell 0.6% after trade data showed exports fell 28% year-over-year in May, the biggest decline since 2009.

-- Caitlin McCabe contributed to this report.

Write to Caitlin Ostroff at caitlin.ostroff@wsj.com