Some insurance company stocks fell in choppy trading as investors fretted about the costs of rules included in the law.

“For the hospitals, it means that they get payment and they get more potential customers,” said JJ Kinahan, chief derivatives strategist for TD Ameritrade. Insurers “have to be less selective of their clients, which is bad for them,” he said.

The stock of Hospital Corp. of America, the largest private hospital chain in the United States, closed up 11 percent. Community Health Systems rose 8 percent, Health Management Associates 9 percent.

Quest Diagnostics, which runs laboratories, and Laboratory Corp. of America both gained about 3 percent.

The ruling will add 32 million Americans to the rolls of the insured, vastly expanding the pool of health care consumers, said Jeffrey Loo, a stock analyst with S&P Capital IQ, a research group.

Under the current system, about one-fourth of the care provided by hospitals is never paid for, either because debts go bad or because the patient is uninsured, Loo said. He said the law will cut the portion of care that is not paid for in half.

However, Gary Taylor, a financial analyst for Citi Investment Research, warned clients that hospital stocks could “erase all their gains” from the court decision if Mitt Romney is elected president this fall. He has pledged to repeal the law.

Many insurance companies fell sharply after the ruling was announced. They bounced around while reporters and analysts dissected the court’s ruling, and they later recovered some of the losses.

UnitedHealth Group, the biggest insurer, closed up half of a percentage point. It had been down as much as 7 percent after the ruling. WellPoint plunged 8 percent, Aetna and Cigna nearly 3 percent.

UnitedHealth escaped the fate of other insurers because it depends less on individual and small-group policies for its revenue. UnitedHealth also has an information technology business and a pharmacy benefits business and is the biggest provider of Medicare Advantage plans for the elderly.

By comparison, WellPoint draws more of its revenue from insurance on individuals and small groups. The health overhaul is seen as more damaging to those businesses because of the costs they would incur to cover more people.

Because of the volatility around the time the decision was reported, trading was halted for five health care stocks, a spokeswoman for the New York Stock Exchange said: HCA, Community Health Systems, Tenet Healthcare, WellPoint and Health Management Associates. She said all five stocks returned to normal trading shortly after the halts. She would not say how many stocks are halted on a typical trading day.

Under market rules, trading of a stock is halted for five minutes if its price swings more than 10 percent in a five-minute period.

Some analysts expressed surprise at the quick sell-off in insurance stocks. After all, those insurance companies will also gain millions of premium-paying customers.

Analysts with Jefferies, an investment bank, said in a note to clients that “after the dust settles, we are generally bullish” on big insurance companies because they will benefit from about 18 million new customers in 2014 and millions more after that.

‘Overall net positive’ for insurers

Loo said some investors sold because they believe the insurance industry agreed to too many costly rules included in the law, such as covering people with existing medical conditions and ending pricing discrimination based on gender and age.

“But our view is that it should be an overall net positive” for health insurers because of the millions who will have to buy health insurance, Loo said.

Major insurance stocks are up strongly since March 2010, when Congress passed the law. UnitedHealth is up 72 percent in that time, Aetna 18 percent and Cigna 22 percent. The Standard & Poor’s 500 index is up 15 percent in that time.

Chief Justice John Roberts, in a break with other conservative justices on the high court, sided with the majority in allowing the law to go forward with its aim of covering more than 30 million uninsured Americans.

The court majority said that Congress had not overstepped its authority by penalizing people who refuse to buy health insurance.

One category of insurers was lifted strongly by the news: companies that focus on patients who receive Medicaid, an existing federal health program that covers some people who can’t afford care.

The health overhaul will increase the number of people on Medicaid by 16 million in 2014, analysts said. Centene rose 2 percent, Amerigroup 5 percent and Molina Healthcare 9 percent.

Most observers had expected the court to uphold the expansion of Medicaid, Loo said. But he said concern about the court ruling had hurt stock prices.

“There’s always that lingering uncertainty, and investors clearly don’t like uncertainty, so a lot of them waited until the court upheld it,” he said.

The broader stock market was down about 1 percent for most of the day, but rallied late and closed only slightly lower. The Dow Jones industrial average, which was down about 100 points before the ruling came out at 10 a.m. EDT, was down 160 points after the ruling and closed down 25 points.

The effect of health care stocks on the market is limited, said David Lefkowitz, senior equity strategist with UBS Wealth Management Research. He said that health care stocks make up about 11.5 percent of the S&P 500, and of that, half is pharmaceutical companies. Health insurance companies make up only about 1 percent.

Traders were glum because of persistent fear about the European debt crisis and weak economic data released earlier Thursday.

Stocks of the largest drug companies in the country closed mixed. Stocks of medical device makers fell about the same amount as the broader market.