Wall Street could get a big Santa Claus rally in December, especially in the second half of the month, history shows.

The Dow Jones industrial average, , Nasdaq 100 and Russell 2000 all averaged returns of at least 1.8 percent in the final month of the year since 1987, according to CNBC analysis using Kensho.

The small-cap Russell 2000 has been the best-performing index in December, averaging a 2.87 percent gain and trading positive 86 percent of the time, the data show.

What happens is "you see a lot of optimism about the new year, and that gives stocks a boost in December," said Michael Arone, chief investment strategist at State Street Global Advisors. Consumer spending "which is such an important part of the economy" picks up and it helps stocks rally.

The holiday season has gotten off to a strong start this year. The National Retail Federation said Tuesday that more than 174 million Americans shopped during Thanksgiving weekend, surpassing a forecast of 164 million shoppers. Adobe Insights also said that online transactions totaled $6.59 billion on Cyber Monday, making it the largest online shopping day in U.S. history.

Stocks are coming into December with strong gains for the year. The major indexes have gained at least 14 percent in 2017, reaching all-time highs. The Dow also broke above 24,000 for the first time Thursday.

But the bulk of the stock market's December gains usually comes in the second half of the month. Kensho's data show that, between Dec. 15 and Dec. 31, the Dow rose on average 1.46 percent, the S&P advanced 1.63 percent, the Nasdaq 100 surged 2.08 percent and the Russell 2000 soared 3.09 percent.

JJ Kinahan, chief market strategist at TD Ameritrade, said part of the late-month gains are driven by portfolio positioning.

"Portfolio managers want to show they were holding stocks that worked at the end of the year," Kinahan said. "That leads to the winners being pushed higher" at the end of the year.

Disclosure: NBCUniversal, parent of CNBC, is a minority investor in Kensho.