S&P 500 and Dow surge in best rally after midterm elections since 1982

U.S. stocks closed broadly higher on Wednesday after the midterm election results came in about as expected, lifting a cloud of uncertainty that was weighing on the market.

The major averages hit their session highs after President Donald Trump indicated he is willing to work with Democrats on policy initiatives that would help the economy keep growing.

The Dow Jones Industrial Average closed up 545 points, led by gains in UnitedHealth and Apple. The S&P 500 gained 2.1 percent as the health care, tech and consumer discretionary sectors each rallied more than 2.8 percent. The Nasdaq Composite rose 2.6 percent.

Wednesday's post-midterms rally was larger than the average gain that follows the contests. Goldman Sachs noted the S&P 500 has averaged a gain of 0.7 percent from the day before the elections to the day after midterms. Wednesday marked the biggest post-midterms gain for both the Dow and S&P 500 since the day after the 1982 contests, when the indexes surged 4.3 percent and 3.9 percent, respectively.

"Hopefully we can all work together next year to continue delivering for the American people, including on economic growth, infrastructure, trade, lowering the cost of prescription drugs," Trump said in a news conference. "The Democrats will come to us with a plan for infrastructure, a plan for healthcare, a plan for whatever they're looking at and we'll negotiate."

Democrats won control of the House of Representatives while Republicans retained their hold on the Senate, as the midterm's outcome split Congress.

"We believe (out of consensus) that a split Congress is the best outcome for US and global equity markets," said Marko Kolanovic, a widely followed quantitative analyst at J.P. Morgan, in a note. "As the President cannot count on Congress or the Fed for more easing, he will need to do what is in his power to keep the economy rolling – drop the damaging trade war and turn it into a winning deal."

Investors expect Trump's business-friendly policies to continue, while some expressed optimism about Congress providing a larger check on Trump's more disruptive market actions. Historically, equity markets see strong returns when Congress is divided.

Stocks rallied across multiple sectors, as shares of Caterpillar, Goldman Sachs, Amazon and Alphabet all rose. Caterpillar is seen getting a boost from continued economic growth.

There's also some optimism the president will work with Democrats on an infrastructure plan. Vulcan Materials and United Rentals, jumped 4.5 percent and 0.9 percent, respectively.

Tech shares rose, as a divided Congress could also keep Trump from seriously going after giants like Amazon for being too big and influential on the economy.

"The pollsters were correct. The markets went into this, given [Tuesday's] close, expecting this outcome," said Quincy Krosby, chief market strategist at Prudential Financial. "Now the market is trying to figure out which sectors will do better."

But trade remains one area where Trump still has most control as tariffs are on foreign goods are implemented through the executive branch.

"A further ratcheting up of measures against China in January is still a risk," strategists at MRB Partners said in a note. "Such an outcome would also add to upward pressure on U.S. inflation, while adding deflationary pressure to global goods prices."

Meanwhile, the Federal Reserve is kicking off a two-day meeting on Wednesday. Worries around the pace of interest rate hikes last month saw global markets hit with sharp bouts of volatility. Markets have been pricing in a higher probability of the Fed raising rates again in December, with further tightening seen through 2019.

"I think they'll just say the data is good and they're still on the path toward normalization," said Anwiti Bahuguna, senior portfolio manager at Columbia Threadneedle Investments. "They have the luxury of being data dependent because inflation and wage growth show no signs of the economy overheating."

'This is your entry point'

Equities in the U.S. closed higher on Tuesday as the elections began earlier in the day.

Defense stocks are also among the expected winners from a divided Congress as it is one of the areas Democrats and Trump may find common ground. Democrats agreed to a Defense Department budget increase for fiscal year 2019.

Art Hogan, chief market strategist at B. Riley FBR, said financials could lose out as the Democrats take control of the House. "In terms of the financials, they might need a bit more deregulation and that is harder to promote under a split Congress," he said.

Overall, stocks typically do well when Congress is split and the White House is under Republican control. In those instances, the S&P 500 averages an annual return of 12 percent, according to Bank of America Merrill Lynch. "The best case scenario from here, in our view, might be gridlock: do nothing, and undo nothing," wrote Bank of America Merrill Lynch analysts and economists in a report leading up to the election.

These results could also lead to more investigations into Trump and therefore more stock market volatility.

But regardless of how the elections shake out, this is the best buying opportunity for investors before year-end, said Phil Blancato, CEO of Ladenburg Thalmann Asset Management.

"This is your entry point," he said, adding that positive seasonal factors, including strong holiday sales, should boost markets higher before the end of 2018. "You might get some volatility in the next couple of days as the market absorbs the result, but you buy those dips. ... Everything is telling you this is the time to buy" following the correction in October.

—CNBC's Ryan Browne , Tom Franck and Patti Domm contributed to this report.