The stock market extended gains Wednesday as the S&P 500 and the Nasdaq closed at a record for a fourth session, while the Dow finished at its highest since February. Optimism over the possibility of an imminent trade deal with Canada bolstered sentiment.

The U.S. economy’s strength—despite some concern about slack in the housing market—and solid corporate results also laid the groundwork for continued market gains.

The Dow Jones Industrial Average DJIA, +1.33% rose 60.55 points, or 0.2%, to 26,124.57, coming within 1.9% of its record close set on Jan. 26. The S&P 500 index SPX, +1.59% climbed 16.52 points, or 0.6%, to 2,914.04, while the Nasdaq Composite Index COMP, +2.26% added 79.65 points, or 1%, to 8,109.69, both records.

For the month of August, the Dow is up nearly 3%, the S&P 500 is up 3.5% and the Nasdaq is up 5.7%.

What drove the market?

Gains have been fueled by an apparent easing of tensions between the U.S. and other major countries on trade policy.

Late Tuesday, Canadian Foreign Minister Chrystia Freeland met with Trump administration officials in an attempt to resolve testy discussions between the two countries on trade, coming on the heels of the U.S. and Mexico’s announcement of progress toward a bilateral trade agreement that may ultimately result in a retooling of the trilateral North American Free Trade Agreement.

Read:Here’s why the Canadian dollar is holding its ground

The threat of tensions escalating into a full-blown trade war have been a primary driver of market activity in both directions for the past several months. While investors continue to watch the situation, particularly ahead of the latest round of negotiations with China and other major trading partners, stocks have largely looked past the issue of late. Instead, the uptrend in equities has been supported by strong corporate earnings and improvements in economic data.

On Tuesday, a reading of consumer confidence came in at its highest level since October 2000. While this pointed to growing optimism about the economy, a factor that could continue to fuel market gains, high levels of confidence can also be a contrary indicator if it suggests rising complacency about the risks facing stocks.

Read more:Why a trend divergence in the S&P 500 and the VIX may spell trouble for stocks

What data were in focus?

An update to second-quarter gross domestic product came in showing growth at a 4.2% annualized pace, slightly better than estimates that matched the initial reading at 4.1%, which represented the fastest pace of expansion in almost four years.

Pending-home sales declined 0.7% in July, the National Association of Realtors said. NAR’s index, which tracks real-estate transactions where a contract has been signed but the deal hasn’t yet closed, fell to a reading of 106.2, missing the consensus forecast of a flat reading.

Read:The 60 events that could rattle investors in the months ahead

What were market experts saying?

“It looks like a revised Nafta agreement between the U.S., Mexico and Canada may be reached shortly. That is giving investors optimism that a U.S.-China trade deal can be worked out in the fall which would remove an obstacle for markets to move higher,” said Jeff Kravetz, a regional investment strategist at U.S. Bank Private Wealth Management.

“Today’s [GDP] estimate should serve as another lesson to shelf geopolitical noise and focus on the fundamentals, which are nothing if not strong,” wrote Mike Loewengart, vice president of investment strategy at E-Trade Financial Corp.

“The breakout to new all-time highs by the [S&P 500] reflects positive momentum across time frames,” said Katie Stockton, managing partner and founder at Fairlead Strategies, in a note. “We expect short-term overbought conditions to be sustained another week or two before a pullback arises,” Stockton said.

What stocks were in focus?

Hewlett Packard Enterprise Co. HPE, -0.21% shares fell 0.2% after rising earlier in the wake of the tech company’s third-quarter results that topped expectations.

Box Inc. BOX, +0.98% slumped 11% after it gave an earnings and sales outlook that was below expectations late Tuesday.

Cannabis producer Tilray Inc. TLRY, +0.84% late Tuesday reported second-quarter sales that doubled from the prior year, thanks to the sale of more high-potency weed and extracts. The company’s stock jumped 21%.

Weight Watchers International Inc. US:WTW rose 2% after an analyst at Oppenheimer initiated coverage of the stock at overweight with a price target of $98.

What were other markets doing?

European stocks SXXP, -0.09% ticked slightly higher and Asian markets finished mostly stronger.

Gold futures US:GCZ8 settled slightly lower, while the ICE U.S. Dollar Index DXY, +0.23% inched down, and U.S. crude-oil futures US:CLV8 rallied more than 1%.

—Ryan Vlastelica contributed to this report