Stocks shook off an early wobble on Wall Street Tuesday, finishing modestly higher and extending the market’s gains into a fourth week.
Solid earnings from Walmart encouraged investors to bid up other retailers and consumer goods companies. Communication services stocks and banks also contributed to the broad gains.
Home builders also notched gains after a survey showed improved confidence among builders heading into the key spring buying season.
The National Association of Home Builders/Wells Fargo Housing Market Index released Tuesday has a reading of 62 — up four points from last month’s index and the highest since October. Readings above 50 indicate more builders see sales conditions as good rather than poor.
William Lyon Homes was among the biggest gainers, adding 3.1 percent.
Roughly 81 percent of S&P 500 companies have reported results for the last three months of 2018, delivering earnings growth of 13.1 percent versus a year earlier, according to FactSet. First-quarter snapshots are expected to result in a 2.5 percent decline in earnings, however.
Even so, the strong quarterly performance by the world’s largest retailer was an encouraging signal on US consumer spending after a government report last week showed retail sales slumped in December.
‘‘Now that we’re winding down on earnings, investors are looking forward to what’s going to move the market higher,’’ said Karyn Cavanaugh, senior markets strategist at Voya Investment Management. ‘‘The fact that the consumer is still strong is a comfort to investors.’’
The benchmark S&P 500 index, which has risen for the past three weeks, gained 4.16 points, or 0.1 percent, to 2,779.76.
The Dow Jones industrial average rose 8.07 points, or 0.03 percent, to 25,891.32. The Nasdaq Composite added 14.36 points, or 0.2 percent, to 7,486.77. The Russell 2000 index of smaller companies picked up 5.22 points, or 0.3 percent, to 1,574.47.
Major European indexes finished mostly lower.
US stock indexes got off to a downbeat start Tuesday following the Presidents’ Day holiday. They wavered between small gains and losses for most of the morning, then veered higher in late morning trading and held on to most of their gains the rest of the day.
London-based bank HSBC and oil and gas rig operator Transocean declined after both companies reported quarterly results that fell short of Wall Street analysts’ forecasts. HSBC fell 3.1 percent, and Transocean lost 2.2 percent.
But Walmart’s results helped lift the market. The retailer rose 2.2 percent after its quarterly earnings beat forecasts. Walmart benefited from growth in online sales and the expansion of its grocery pickup and delivery business.
Amazon gained 1.2 percent, while Target added 1.5 percent.
The latest round of company earnings showed solid profit growth for the final three months of 2018, but caution about conditions going forward amid signs of a weaker global economy this year. Europe and China have both reported slower growth.
Meanwhile, uncertainty over the costly US-China trade conflict has clouded the outlook for company profits.
‘‘We still have that overhang of global growth and trade issues,’’ Cavanaugh said, noting that traders are looking ahead now to company earnings for the first quarter with ‘‘a little trepidation.’’
Beyond the quarterly corporate report cards, investors were keeping a close eye on talks between US and Chinese negotiators in Washington that are aimed at ending the trade war.
A truce on increased American tariffs on Chinese goods expires at the end of next week, leaving the United States free to more than double its import duties on $200 billion in Chinese goods.
President Trump has said there is a possibility he would extend that March 2 deadline if the two countries are close to a deal. Much is riding on the outcome of the talks after an inconclusive end to an earlier round in Beijing last week.
Vice Premier Liu He, China’s economy czar, was due in Washington Thursday, China’s state media reported.
The United States is wrangling over trade with many nations. On Monday, the European Union warned that the bloc will hold back on a commitment to buy more American soybeans and liquefied gas if European cars are hit with punitive tariffs.
Bond prices rose. The yield on the 10-year Treasury note fell to 2.64 percent from 2.66 percent late Friday. That yield is used to set rates on mortgages and other kinds of loans.