NEW YORK (Reuters) - Bank of America Co will pay a $42 million fine and admitted wrongdoing to settle claims by New York's attorney general that it fraudulently routed clients' stock trades to outside firms, including one run by swindler Bernard Madoff.

New York Attorney General Eric Schneiderman announced the settlement on Friday, and called the fine the largest collected by the state to resolve an electronic trading probe.

The attorney general said Bank of America Merrill Lynch had undisclosed agreements with several electronic trading firms from March 2008 to May 2013 to handle client trades.

He said the bank told clients it was processing the trades in-house, even going so far as to alter trade confirmations, as part of an effort to make its electronic trading services appear safer and more sophisticated than they were.

Schneiderman said the "masking" scheme affected more than 16 million trade orders and 4 billion shares, benefiting such firms as Madoff Securities, Citadel Securities, D.E. Shaw, Knight Capital and Two Sigma Securities.

The bank also admitted to having told traders in its "dark pool," a private venue where they expected protection from high-speed traders, that up to 30 percent of orders came from retail traders, when the percentage was closer to 5 percent.

"Bank of America Merrill Lynch went to astonishing lengths to defraud its own institutional clients about who was seeing and filling their orders, who was trading in its dark pool, and the capabilities of its electronic trading services," Schneiderman said in a statement.

Bill Halldin, a bank spokesman, said in an email: "At all times we met our obligation to deliver the best prices to clients. About five years ago, we addressed the issues concerning communicating to clients about where their trades were executed."

The Charlotte, North Carolina-based bank also admitted that its masking activity violated New York's powerful securities fraud law, the Martin Act.

In 2016, Barclays Plc, Credit Suisse Group and Deutsche Bank settled separate electronic and high-speed trading probes by Schneiderman's office for a respective $35 million, $30 million and $18.5 million. They also reached related settlements with the U.S. Securities and Exchange Commission.

Madoff is serving a 150-year prison term for running a huge Ponzi scheme involving his investment advisory business.

(Reporting by Jonathan Stempel in New York; Editing by Jeffrey Benkoe and Phil Berlowitz)

By Jonathan Stempel

Stocks treated in this article : Bank of America, Deutsche Bank, Credit Suisse Group, Barclays