The month of June is looking to be a mostly positive one for the U.S. stock market — bucking a historical trend of seasonal weakness — but after all the volatility and selling, many investors are likely looking forward to July. If history is any guide, they have reason to be optimistic about the start of the third quarter.

According to the Stock Trader’s Almanac, July ranks among the best months of the year for both the Dow and the S&P 500, although the results are notably worse for the Nasdaq and the Russell 2000 — the two gauges that have generally been holding up the best of late.

The Dow Jones Industrial Average

DJIA, +0.12%

has historically risen 1.2% over the month of July, according to data that goes back to 1950, making it the fourth-best month of the year. Over the past 67 years, July has been positive for the blue-chip average 42 times, and negative 25 times.

For the S&P 500

SPX, +0.22%

, July is the fifth-best month of the year. The benchmark index typically gains 1% over the month, according to the Almanac, and the odds of a positive month are lower compared with the Dow: of the past 67, just 37 have been positive.

Historical statistics are somewhat more ominous for the Nasdaq Composite Index

COMP, +0.39%

, which has historically gained 0.4% over the month. This makes for the tech-heavy index’s 10th best month of the year. Over the past 46 years, the Nasdaq has seen 22 negative Julys, compared with 24 positive ones.

The Nasdaq has been the top-performing index of the three main gauges in 2018. It is up 9.8% thus far this year, compared with the 2.1% rise of the S&P. The Dow is off 1.5% in 2018. The Nasdaq’s outperformance has come as large-capitalization technology and internet names have continued their march higher in 2018. The weakness in the Dow has been attributed to the prospect of escalating trade tensions between the U.S. and major trading partners, a headline risk that the Dow — which is more heavily weighted toward multinational stocks — is more exposed to.

The other outperforming index of 2018 is the Russell 2000

RUT, +0.66%

, which has gained 8.8% year to date, in large part due to the domestic revenue exposure of its components, which has insulated it from both trade concerns and the headwind that a rising U.S. dollar can have on multinational profits. However, historical performance suggests reason for caution: July is the second-worst month of the year for the Russell; it typically declines 0.3% over the month.

Such historical moves, were they to be repeated over the coming month, would extend the market’s lengthy trend of trading in a narrow range, as well as the Dow’s and S&P’s stretch in correction territory. The Dow is about 8.7% below its record levels, while the S&P is 5.2% under and the Nasdaq — which never corrected, as defined by a decline of 10% from a peak — is off 3.1%.

Thus far in June, the Dow is down 0.5% while the S&P is up 0.7% and the Nasdaq is up 1.7%. Historically, the Dow falls 0.3% in June, in what is its second-worst month of the year. The S&P is typically flat over the month, while the Nasdaq rises 0.7% over the month.

Summer months are sometimes seen as risky for Wall Street, due to a historical trend embodied by the phrase “sell in May and go away.” What this means is that the stretch between the start of May and the end of October has been a seasonally weak period for markets. While this has been true over the long term, as seen in the following table that looks at S&P 500 performance, the trend hasn’t held over the past five years, according to the WSJ Market Data Group.

Courtesy WSJ Market Data Group

Despite the positive historical trends, an element of uncertainty could come from the runup to the November midterm elections. Per the Almanac, the Dow, S&P, Nasdaq and the Russell all tend to see worse performance during midterm years, sometimes substantially so. While the Dow’s average gain only narrows to 1.1% from 1.2%, the S&P’s goes to 0.7% from 1%, and both the Nasdaq and the Russell see their averages turn negative. In midterm years, the Nasdaq has historically dropped 2.2% in July, compared with its 0.4% gain in non-midterm years; July is the worst month of the year for the Nasdaq in midterm years. The Russell’s 0.3% decline widens to a 4.5% slump, historically speaking.