Many investors were fixated on what Royce Funds portfolio manager Bill Hench called “the most over-analyzed midterm election in our history.” (Richard Drew/AP)

Investor anxiety that one or both houses of Congress will flip to Democratic control in next week’s midterm elections is thought to have added to one of the stock market’s worst Octobers in years.

The brutal month reversed some of its losses in its final two days when a cluster of strong earnings, led by Facebook and General Motors, appeared to rescue the market from a steeper decline, particularly in technology shares.

Still, it was ugly. The tech-heavy Nasdaq composite index on Wednesday was down about 9 percent for the month. The Dow Jones industrial average was about 5 percent in the red, and the Standard & Poor’s 500-stock index was about 7 percent lower.

Many investors were fixated on what Royce Funds portfolio manager Bill Hench called “the most over-analyzed midterm election in our history.”

And that may really be saying something.

Sam Stovall, chief of U.S. equity strategy at CFRA, published an analysis that showed midterm election years make for dicey days in the financial markets.

“The second and third quarters of the midterm election year traditionally were the most challenging of the entire 16-quarter presidential cycle,” Stovall said in a report examining post-World War II midterms. “The reason boils down to one word: uncertainty.”

Historically, the party controlling the White House has lost an average of 22 seats in the House after the midterm election and four seats in the Senate. “With this kind of unsettling track record, it should come as no surprise that the S&P 500 posted erratic returns in September of midterm election years,” Stovall wrote.

If that track record holds, President Trump would have to face Democratic control of Capitol Hill.

Trump made clear that he is watching, too, taking to Twitter on Tuesday to say: “The Stock Market is up massively since the Election, but is now taking a little pause — people want to see what happens with the Midterms. If you want your Stocks to go down, I strongly suggest voting Democrat. They like the Venezuela financial model, High Taxes & Open Borders!”

The election is the talk of Wall Street, where a historic bull market has pushed in several all-time highs over the past year. But some say concerns that a Democratic legislature could put the kibosh on the good times are exaggerated. The more likely culprits would be the Federal Reserve raising rates, less-than-perfect earnings and a looming Cold War with China.

“This correction is Fed-driven,” said Peter Fitzgerald, a former Republican senator from Illinois and a lifelong banker. “They are the big bear draining money out of the system by raising interest rates. They are seeing the economy potentially overheating and asset prices going unsustainably high because of the low interest rates we’ve had for the past 10 years. I don’t think the market correction will stop unless the Fed reverses course.”

Democrats are widely thought to have a good chance of picking up the 23 seats necessary for the party to gain the majority in the House. Many polls show the Republicans holding — and perhaps picking up a seat or two — in the Senate.

Conventional wisdom says Democrats are less friendly to business than Republicans, and more open to regulation and higher taxes.

Kristina Hooper, global market strategist at Invesco, said the trade stalemate between the United States and China is fueling the angst.

“It seems that we are headed for more tariffs, given that China does not appear interested in capitulating,” she said. The “stock market sell-off is a direct result of the exacerbation of trade tensions.”

Concern about the House going Democratic and increased regulation that might come with it, especially for technology, is “a mistaken assumption,” she said.

“If the Dems take the House, their agenda will be focused on protecting the Affordable Care Act,” Hooper said. “They may also attempt repealing or at least chipping away at the tax reform package.”

Since World War II, according to Stovall, the market’s best average annual returns were in years when the same party held the White House and majorities in both chambers of Congress.

In the 30 years of one-party rule, the S&P 500 gained an average 11 percent and rose 80 percent of the time.

The next best performances in the postwar era were in years when the president and a unified Congress were of different parties.

So that means if the Democrats take both the Senate and House, markets may be better off than if they take one or the other.