By Anora Mahmudova and William L. Watts, MarketWatch

NEW YORK (MarketWatch) -- U.S. stocks ended June and the second quarter higher, even as the broader market closed marginally lower on Monday.

The S&P 500 recorded the fifth consecutive month of gains and the biggest second-quarter gain since 2009.

The benchmark index (SPX) closed less than a point lower at 1,960.19, gaining 3.9% over the month and 5% over the quarter.

The Dow Jones Industrial Average (DJI) shed 25.24 points, or 0.2%, to 16,825.96, but gained 1.9% over the past month and 2.2% over the past quarter.

The Nasdaq Composite (RIXF) defied the trend and ended the day 10.25 points, or 0.2%, higher at 4,408.18. The tech-heavy index gained 3.9% in June and 5% over the past quarter. Read the recap of stock market live blog.

For the first half of 2014, the S&P 500 recorded a 6.1% rise, the Dow industrials rose 1.5%, and the Nasdaq gained 5.5%. Airline, pharmaceutical, and utilities stocks led advancers during the period, which was marked by the impact of bad weather, a drop in first-quarter gross domestic product and a decline in Treasury yields.

Among individual stocks, shares of Yahoo Inc. (YHOO) gained 2.5% after Piper Jaffray upgraded shares to overweight. Bank of New York Mellon(BK) climbed 3.1% as activist investor Trian Fund Management builds up a stake, according to The Wall Street Journal. A newcomer GoPro Inc (GPRO) climbed 14% on its third day of trading.

Also read: MannKind leaps, American Apparel sinks

The S&P 500 and Dow briefly turned higher after a gauge of pending home sales jumped 6.1% in May to hit its highest level in eight months. On Tuesday, investors will be watching numbers from the Institute for Supply Management's manufacturing report for June.

This week will be shortened by the Fourth of July holiday on Friday, which means closely watched U.S. jobs data for June will be released on Thursday. Economists are looking for the addition of 215,000 nonfarm-payrolls jobs and no change in the unemployment rate of 6.3%.

Also read: Market exuberance out of touch with economy.

In other markets, Asian shares ended mixed, with Japan's Nikkei Average posting a 0.4% gain. European stocks were flat after a reading of steady inflation in the euro zone.

The ICE dollar index (DXY), which pits the greenback against six other currencies, fell to 79.779 from 80.331 late Friday.

Oil futures(CLQ4) continued to give back the fear premium on growing confidence Iraq's oil exports will remain insulated from the turmoil, but prices still logged sizable gains for the month and quarter. Gold futures(GCQ4) settled with their biggest monthly gain since February.

More must-reads from MarketWatch:

One chart explains the unexpected first-half Treasury rally

What could make stocks tumble? Watch the energy sector

If history repeats itself, get ready for six more bull months

Subscribe to WSJ: http://online.wsj.com?mod=djnwires

Altaba (NASDAQ:AABA)
Historical Stock Chart
From Aug 2024 to Sep 2024 Click Here for more Altaba Charts.
Altaba (NASDAQ:AABA)
Historical Stock Chart
From Sep 2023 to Sep 2024 Click Here for more Altaba Charts.