The defense sector has been among the big winners of the last two years, with the SPADE Defense Index rising more than 55% since October 2016 compared to the S&P 500's 30% gain. Investors have cheered a return to business as usual in Washington after years of budget battles that slowed the pace of government spending and depressed valuations.

It's best never to get too complacent. Some of the factors that caused defense stocks to suffer prior to this latest rally are once again gathering on the horizon. Here's what you need to know to avoid getting caught in the crossfire if the budget wars flare up again in the months to come.

Aerial view of the Pentagon

The Pentagon could face post-election budget challenges. Image source: Getty Images.

What is sequestration?

In 2011 defense stocks hit lows not seen since the end of the Cold War when a partisan impasse over raising the debt ceiling and funding government resulted in the Budget Control Act of 2011, which required Congress to either reduce the deficit by more than $1 trillion or cut spending over a 10-year period by a similar amount.

The net effect, known as sequestration, was caps on spending for defense and civil departments, with defense outlays reduced by 6.4% in 2012 and a further 6.4% in 2013. While the Pentagon still had more than $600 billion to spend per year, the budget caps forced the military to prioritize programs, limited spending on research and development, and led to delays in some contracts deemed nonessential, such as IT modernization.

The rally in defense stocks began shortly after the U.S. election in November 2016. The gains were likely attributable more to a single party controlling both the White House and Congress, and not investor preference for a particular candidate, with investors betting that the result made a compromise more likely and that the caps would eventually be lifted.

^DXS Chart

Defense index vs. the S&P 500 data by YCharts.

It took a while, but investors finally got their wish earlier this year when Congress agreed to a two-year budget deal that increased Pentagon spending by more than $165 billion annually and has led to a wave of new program announcements.

Alas that deal was only a two-year reprieve, and another compromise is necessary if lawmakers want to avoid reinstating the caps for fiscal 2020. The Congressional Budget Office has estimated the Pentagon allocation could fall by $90 billion in 2020 and a further $89 billion in 2021 if caps are not adjusted.

Given the level of rancor in Washington, that new compromise is not going to come easy. Should one or both houses of Congress flip in November, the challenge becomes significantly more difficult.

What it means for defense stocks

The Pentagon has already warned that after two years of rapid increases, spending is likely to be flat in 2020, so investors should be grounded in their views on the sector's growth prospects for the next few years. Still, if history is any indication, stocks will face severe turbulence if a return to budget battles appears likely.

Shares of defense titans Lockheed Martin (NYSE:LMT), Northrop Grumman (NYSE:NOC), General Dynamics (NYSE:GD), and Raytheon (NYSE:RTN) all underperformed the S&P 500 in the 12 months leading up to when the Budget Control Act was signed in August 2011, a period when the tension between different congressional factions intensified, with shares of individual companies dropping between 7% and 13% during the first two weeks of August alone.

It's worth noting that those companies now trade at a premium to earnings more than 20% higher than where they stood in 2008. If Wall Street was to lose faith in future Pentagon spending power and that premium was to disappear, the share price drop could be significant.

What you should do

If the United States returns to a divided government in November, meaning in this case Democrats take control of at least one house of Congress, a negative reaction by defense stocks is likely. How severe the correction, and how long it lasts, will depend on the tone of the post-election rhetoric.

I'm a strong believer in the long-term mentality preached by The Motley Fool and would not recommend trying to time the market or bet on an election outcome, but investors with less tolerance for volatility should be aware of potential rough sailing ahead.

Over the longer term, there is a lot to like about the defense sector. The world is not getting any safer, and the high-tech solutions the Pentagon needs to keep pace with China and Russia will not come cheap. I continue to believe companies like Lockheed Martin and Raytheon with exposure to areas of Pentagon emphasis, and General Dynamics with a near-term potential catalyst in a key business segment, are good long-term buys today.

Even if the stocks are long-term winners, investors need to be aware of the toll that politics can play on this sector. Depending on what happens in November, there could be much-cheaper entry points into these stocks in the weeks to come.

Lou Whiteman has no position in any of the stocks mentioned. The Motley Fool has no position in any of the stocks mentioned. The Motley Fool has a disclosure policy.