Lawmakers hammered out a historic overhaul of financial regulations on Friday, handing President Barack Obama a major domestic policy victory on the eve of a global summit of world leaders.

In a marathon session of more than 21 hours, congressional negotiators agreed to a rewrite of Wall Street rules that may crimp the industry's profits and subject it to tougher oversight and tighter restrictions.

The bill, the most sweeping financial rules revamp since the 1930s, is headed toward final congressional approval next week although implementation will be bogged down for months in regulatory rule-making.

The legislation would set up a new financial consumer watchdog, create a protocol for dismantling troubled financial firms and mandate higher bank capital standards, all in an effort to avoid a repeat of the 2007-2009 credit crisis that hammered the economy and triggered taxpayer bailouts of floundering firms.

To secure agreement, lawmakers reached deals in the final hours on the most controversial sections, which restrict derivatives dealing by banks and curb their proprietary trading to shield taxpayer-backed deposits from more risky activities.

Banks will be allowed to keep most swaps dealing activity in-house, although the riskiest trading would be pushed out into an affiliate. They will also be permitted small investments in hedge funds and private equity funds.

The concessions could lessen the impact on bank profits.

The KBW bank stock index, which registered its worst performance since October last month, closed up 2.9 percent, with Goldman Sachs Group Inc and Morgan Stanley, two of the banks that will be most affected, helping lead the way.

The reforms must still win final approval from both chambers of Congress before Obama can sign them into law. Quick approval is expected.

Democrats had raced to complete their work before Obama left for a weekend meeting of the Group of 20 economic powers, where he can tout the changes as a blueprint for other countries.

Just as economic turmoil in one place can quickly spread to another, safeguards in each of our nations can help protect all nations, Obama said at the White House shortly before departing.

Despite last-minute deals, the bill has actually gotten tougher in its yearlong journey through the halls of Congress. Democrats rode a wave of public disgust at an industry that awarded itself rich paydays while much of the country struggled through a deep recession.

They tried to water it down, but still there's enough regulation in there that it's going to affect banks, it's going to affect their profitability, said Chris Hobart, founder of Hobart Financial Group in Charlotte, North Carolina.

Passage of the bill will give Democrats an important legislative victory, alongside healthcare reform, ahead of congressional elections in November. As part of the package, financial institutions would have to pay $19 billion to cover the estimated cost of the bill.

The House of Representatives could vote as soon as Tuesday, and Senate action is expected to swiftly follow.

Under congressional rules, it would be difficult but not impossible to change the bill, and Democratic leaders are confident that they won't have to do so. However, they will need to hold the support of some moderate Republicans in the Senate to assure they can clear any procedural roadblocks.

CURBS ON RISKY TRADING

Lawmakers munched chocolates to stay awake as regulators and administration officials hovered in the wood-paneled room and as the night wore on, they yielded the microphones to staff to debate the bill's finer points.

The panel completed its work just after 5:30 a.m., more than 21 hours after it sat down to its final negotiating session.

Along the way, the negotiators resolved several sticking points that had threatened to scuttle the bill.

They agreed to water down a proposal by Democratic Senator Blanche Lincoln that would have required banks to spin off their lucrative swaps-dealing desks to a separately capitalized affiliate.

Dozens of House Democrats said Lincoln's proposal would force trading to move overseas, and they threatened to vote against the bill if it included the provision.

The compromise allows banks to stay involved in foreign-exchange and interest-rate swaps dealing, which account for the bulk of the $615 trillion over-the-counter derivatives market. They also could participate in gold and silver swaps and derivatives designed to hedge banks' own risk.

But they would need to spin off dealing operations that handle agricultural, energy and metal swaps, equity swaps and uncleared credit default swaps.

Quite frankly, common sense prevailed, Lincoln said shortly after agreement was reached on the bill. Our objectives were to get the risky stuff out of banks. We figured out how to do that.

Lawmakers resolved another controversial element around midnight when they agreed that banks should face restrictions on the proprietary trading they do for their own accounts and not for their customers.

As with Lincoln's swaps provision, the financial industry won significant last-minute concessions in that rule, named for White House economic adviser Paul Volcker.

The final version of the Volcker rule would give regulators little wiggle room to waive the trading ban but it would allow banks to invest up to 3 percent of their Tier 1 capital in hedge funds and private equity funds.

The bill would dramatically reshape the U.S. financial landscape. The industry is already turning its sights on how it might influence implementation by regulators.

We need to hold the course, Federal Deposit Insurance Corp Chairman Sheila Bair, one of the regulators who would be charged with putting the reforms in place, told Reuters. We cannot let ourselves forget what happened in October of 2008 when the financial system risked breaking down.

The legislation sets up a new agency within the Federal Reserve charged with protecting consumers of financial products. It also gives regulators new power to seize troubled financial firms before they harm the broader economy.

Though it leaves largely intact the patchwork of federal regulators that failed to stop the last crisis, it sets up an interagency council to monitor system-wide risks to stability.

It forces much of the over-the-counter derivatives market, which worsened the financial crisis and led to a $182 billion bailout of insurer AIG, onto more accountable channels like clearinghouses and exchanges.

Larger banks will have to raise more capital to help them ride out future crises.

Credit-rating agencies such as Moody's Corp could see their business models upended by regulators seeking to resolve conflicts of interest, while debit-card issuers like Bank of America will probably have to reduce the transaction fees they charge merchants who use their cards.

(Additional reporting by Roberta Rampton, Rachelle Younglai, Kevin Drawbaugh, Karey Wutkowski, Deborah Charles in Tabassum Zakaria in Washington, and Leah Schnurr in New York; writing by Andy Sullivan and Tim Ahmann; Editing by Alistair Bell, Jackie Frank and Dan Grebler)