The S&P 500 index and Nasdaq closed at all-time highs Thursday after stocks climbed on fresh optimism that the U.S. and China are close to reaching a deal in their costly trade war.

Financial, technology and health care stocks powered much of the rally, which gave the S&P 500 its second straight gain and erased its losses from earlier in the week. Bond yields surged and shares in real estate companies, utilities stocks and household goods makers fell as investors shifted money away from safe-play investments.

On Thursday:

The S&P 500 index climbed 26.94 points, or 0.9%, to 3,168.57.

The Dow Jones Industrial Average rose 220.75 points, or 0.8%, to 28,132.05.

The Nasdaq gained 63.27 points, or 0.7%, to 8,717.32.

The Russell 2000 index of smaller company stocks climbed 12.89 points, or 0.8%, to 1,644.81.

For the week:

The S&P 500 is up 22.66 points, or 0.7%.

The Dow is up 116.99 points, or 0.4%.

The Nasdaq is up 60.79 points, or 0.7%.

The Russell 2000 is up 10.98 points, or 0.7%.

For the year:

The S&P 500 is up 661.72 points, or 26.4%.

The Dow is up 4,804.59 points, or 20.6%.

The Nasdaq is up 2,082.04 points, or 31.4%.

The Russell 2000 is up 296.25 points, or 22%.