U.S. stock-market indexes closed at records on Friday and booked weekly gains, as investors remained optimistic over the economy and the state of American corporations as the fourth-quarter earnings season began.

The U.S. stock market will be closed on Monday for Martin Luther King Jr. Day.

The Dow Jones Industrial Average DJIA, -0.87% rose 228.46 points, or 0.9%, to 25,803.19 and gained 2% over the week.

The S&P 500 SPX, -1.11% added 18.68 points, or 0.7%, to 2,786.24, booking a 1.6% gain over the week. Friday’s gains were broad, with eight of the 11 primary S&P 500 sectors finishing in positive territory. Consumer-discretionary and energy shares led the gains, up 1.3% and 1% respectively.

This is the second straight weekly gain for both the Dow and the S&P, as well as their seventh positive week of the past eight.

The Nasdaq Composite Index COMP, -1.07% rose 49.28 points to 7,261, a gain of 0.7%. The tech-heavy index rose 1.7% over the past week, its second consecutive weekly advance.

The Russell 2000 index of small stocks RUT, -0.37% rose 4.9 points, or 0.3%, to 6,758.54 and gained 2% over the week, its biggest weekly advance since September.

What is driving the markets?

Fourth-quarter earnings season unofficially kicked off on Friday, with results from a number of major financial institutions, a sector analysts expect to put in a mixed performance. Earnings growth is expected to be strong this quarter, although the recently passed tax law out of Washington is expected to “muddy” some results, according to Goldman Sachs.

JPMorgan Chase & Co. JPM, -0.21% reported a 37% drop in earnings, although this was largely related to one-time charges associated from the new tax law, which is expected to provide further gains over the longer term. Wells Fargo & Co. WFC, +0.08% reported earnings and revenue that came in below consensus analyst forecasts in the fourth quarter. Shares of J.P. Morgan rose 1.6% while Wells lost 0.7%.

BlackRock Inc. BLK, +1.55% reported earnings that came in above expectations, helped by the massive inflows into the company’s iShares line of exchange-traded funds. Shares jumped 3.3%.

Read:What to expect when Goldman Sachs and Morgan Stanley report earnings

Plus: Big bank stocks have plenty of juice left—here’s why

Separately, German Chancellor Angela Merkel’s conservative CDU party and Martin Schulz’s center-left SPDreached an agreement on a blueprint for formal coalition negotiations. This was seen as a sign of political stability in the eurozone’s biggest economy, and it supported the euro, which in turn led to a decline in the U.S. dollar. A weaker greenback is seen as providing a tailwind for the profits of multinational companies. The iShares MSCI Germany ETF EWG, -0.62% rose 1.2%.

U.S. retail sales rose 0.4% in December, the fourth straight monthly gain, although it was slightly below the 0.5% increase that analysts had forecast. The consumer-price index rose 0.1% in December.

What are strategists saying?

“The economy is looking good, numbers look good, and companies are making money. This quarter may be a little messy because of the tax bill, but there’s so much optimism, so much enthusiasm about earnings,” said Michael Matousek, head trader at U.S. Global Investors. “Things are really starting to take off, and the only thing that makes it scary is the idea that we may be culminating to a top in the market.”

Stocks have been in an essentially uninterrupted uptrend for years, and the S&P 500 is poised to go a historic length of time without a dip of even 5%. Volatility has also been historically low.

“The move isn’t about fundamentals anymore, but the overall euphoria,” said Jason Browne, chief investment officer of FundX Investment Group. “When we do get negative moves we don’t hold them; the environment pushes people to buy dips. Will that last forever? Of course not. You have to hope people are being realistic.”

Which stocks are in focus?

Shares of Facebook Inc. FB, -0.89% fell 4.5%, in what is on track to be its biggest one-day percentage loss since September. The decline came after Chief Executive Mark Zuckerberg, in a blog post published late Thursday, said the social-media website will prioritize personal posts from family and friends over business and news media posts. He acknowledged the move could come at the expense of the company’s bottom line.

GameStop Corp. GME, +2.93% sank 11% after it reported a big jump in holiday sales, but said it would take impairment charges of $350 million to $400 million related to its technology brands business.

Shares of Snap Inc. SNAP, -1.97% fell 3.4% after it was downgraded to underperform at Raymond James.

Read: ADT is planning a $2 billion IPO: Here are 5 things you need to know

What are other markets doing?

In Europe, equity gains were checked by a strong euro, with the Stoxx 600 index SXXP, -0.66% up 0.3%.

Stocks in Asia closed mostly higher, but Japan’s Nikkei 225 index NIK, +0.17% fell 0.2%.

Gold US:GCG8 rose 1.2% as the dollar fell. The ICE dollar index DXY, +0.03% tumbled 1% to 90.94, with losses concentrated against the euro EURUSD, -0.06% , which hit a three-year high against the dollar at $1.2205.

Oil prices resumed their climb, trading near 3-year highs, as Trump extended temporary waivers on U.S. sanctions against Iran, as expected. The West Texas Intermediate February contract US:CLG8 was last up 0.9% at $64.40 a barrel.

Read:Trump will extend waiver on sanctions relief for Iran, say sources

In cryptocurrencies, the bitcoin spot price BTCUSD, +2.42% rose 2.7% to $13,640. Bitcoin futures on the CME Group Inc. US:BTCF8 rose 1.5% to $13,590.

—Anora Gaudiano contributed to this article.