Bank of America Corp said it expects to take more than $20 billion of charges after settling with mortgage bond investors, resulting in a second-quarter loss.

The sum, which includes an $8.5 billion settlement, removes a question mark that had been hovering over the bank since October, and Bank of America's shares rallied.

Investors can now start attaching a number to these unknowns and what they will cost the bank. With the swipe of a pen, they've dealt with a large chunk of these issues, said Paul Miller, a banking analyst with FBR Capital Markets.

Chief Executive Brian Moynihan is working hard to move past the mortgage crisis, and this settlement is the latest step in that process.

But the large dollar amounts linked to the settlement and the bank's other efforts to clean up mortgage exposure in recent months could weigh on the bank's capital levels as most banks are looking to boost capital and return more money to shareholders.

The bank was hit hard by toxic home loans after Ken Lewis, Bank of America's prior CEO, bought mortgage lender Countrywide Financial in 2008, just as the housing market bubble was bursting.

Other banks, including JPMorgan Chase & Co and Wells Fargo & Co, could now face pressure to resolve similar allegations, and new lawsuits may arise, analysts said.

A group of 22 investors, including BlackRock Financial Management, alleged that bonds it bought from Countrywide Financial were packed with mortgages that should never have been sold. Bank of America bought Countrywide, once the largest U.S. mortgage lender, in 2008.

Bank of America said that excluding items such as the settlement, second-quarter earnings could top the average Wall Street estimate.

CHEVY VEGA

The settlement is the third in six months for BofA, following similar deals with government-backed mortgage investors Fannie Mae and Freddie Mac, and insurer Assured Guaranty Ltd.

In January, the bank announced plans to settle with Fannie and Freddie for $2.8 billion. In April, BofA disclosed a $1.6 billion settlement with Assured Guaranty.

Last fall, CEO Moynihan said the bank would fight any such repurchase requests. He described talks with investors over claims as hand-to-hand combat and said some investors were looking for a better deal through repurchases.

Their attitude, Moynihan said, was I bought a Chevy Vega but I want it to be a Mercedes.

We're going to protect shareholders against that, he said during the company's third-quarter earnings conference call.

But Moynihan struck a different tone on Wednesday, saying the company was looking to put repurchase woes behind it in terms that would be favorable to BofA shareholders.

Our job is to eliminate risks to allow this company to go forward, he said, dismissing suggestions from analysts that the bank did not put up a fight in the settlement process.

The repurchase dispute with investors began last fall, when a group of prominent mortgage securities holders threatened to sue over the toxic mortgages.

In December, the two sides avoided a court case by agreeing to settlement talks that have continued since then.

But the deal comes at a price for BofA. FBR analyst Miller said the settlement leaves little margin for error as the bank works to meet new capital requirements.

Other analysts are less concerned. Marty Mosby of Guggenheim Securities said the bank has $67 billion in excess capital under current rules -- and $26 billion under new proposed industry rules.

During a conference call announcing the settlement, BofA Chief Financial Officer Bruce Thompson said the bank projects it can replace the capital with earnings through the next two quarters.

Investors largely welcomed the settlement, as shares rose 3 percent to $11.14 in late afternoon trading.

The bank has to get out of the litigation business and back into the banking business, said Greg Donaldson, founder of Evansville, Indiana-based Donaldson Capital Management, which owns BofA shares. Donaldson said the settlement was the best move he had seen from the bank in the last two years.

Bank of America said it expected to post a loss of 88 cents to 93 cents per share for the second quarter.

Excluding special items, it expects earnings of 28 cents to 33 cents a share. Analysts' average forecast was 28 cents, according to Thomson Reuters I/B/E/S.

SIX OR SEVEN YEARS

The bank said charges would include the $8.5 billion settlement with bond investors, $5.5 billion to cover expected payments to other mortgage bond investors, and $6.4 billion in other charges linked to mortgages.

Separately, BofA said it would record a $2.5 billion gain in the quarter from the sale of Balboa Insurance and a chunk of its remaining BlackRock stake.

CFO Thompson also said during the conference call with analysts that sale and trading results were higher in the second quarter compared with a year ago, but lower than in the first quarter of 2011.

The settlement must still be approved in court, and small investors not part of the initial agreement could contest it.

An attorney for the investor group said the deal was good for all investors. The settlement will be shared among all investors in the securities, and the 22 institutional investors will not receive special benefits, Kathy Patrick, an attorney at Gibbs & Bruns LLP, said in an interview with Reuters' On the Case legal blog.

I have a hard time seeing how anyone could recover more than this in six or seven years of litigation, Patrick said.

Patrick said Bank of New York Mellon -- the trustee for the mortgage-backed securities -- played a crucial role in the settlement.

The investors argued that the mortgages packaged into their bonds did not meet their specifications, and that Bank of America, which is collecting payments on the mortgages, was not doing enough to maximize the collections. Part of the settlement includes improvements in gathering payments, known as servicing.

This settlement is likely to embolden the other plaintiff's lawyers to go after other banks and look for similarities in their securitizations, said Nancy Bush, a veteran bank analyst.

BofA is still negotiating with a group of state and federal regulators -- including a coalition of all 50 state attorneys general -- over allegations the industry improperly foreclosed on delinquent borrowers.

(Reporting by Joe Rauch and David Henry, additional reporting by Brenton Cordeiro in Bangalore and Lauren Tara LaCapra and Dan Wilchins in New York; Editing by Lisa Von Ahn, John Wallace, Gary Hill)