TORONTO - A session of see-saw trading ended in positive territory for North American stock markets as investors conjured up enough optimism to drive overall sentiment higher.

But the gains weren't without a struggle -- one that lasted late into the afternoon -- as traders weighed dismal home building numbers, and positive outlooks from Home Depot and Hewlett-Packard.

Toronto's S&P/TSX composite index closed ahead 40.28 points at 8,835.73.

On Wall Street, the Dow Jones industrial average moved up 151.17 points to 8,424.75.

The Nasdaq composite index was flat, ahead 1.22 points to 1,483.27 and the S&P 500 increased 8.37 to 859.12.

Earlier in the afternoon, markets were all lower after the National Association of Home Builders/Wells Fargo housing market index tumbled five points to nine in November, its weakest reading since it started in January 1985.

The Canadian dollar was at 81.31 cents US, down 0.44 of a cent, and the TSX Venture Exchange dropped 35.54 to 750.25.

Markets got a boost of optimism from Hewlett-Packard which said it expects fourth-quarter and 2009 results above Wall Street projections despite the sluggish economy.

On the U.S. retail scene, Home Depot Inc. suffered a 31 per cent drop in summer-quarter profit -- amid weak consumer spending -- but beat Wall Street expectations.

The TSX energy sector rose 0.5 per cent as light sweet crude closed below $55 a barrel after hitting a new 22-month low earlier in the day.

Crude was down 56 cents to $54.39 a barrel on the New York Mercantile Exchange.

Information technology stocks moved up four per cent.

Research In Motion was ahead 12 per cent, or $6.40, to $58. An analyst at National Bank suggested that the stock is "not one to give up on" despite lagging consumer spending.

Financials were up one per cent as Royal Bank jumped 18 cents to $43.54.

On the downside, gold stocks fell 0.7 per cent as the December bullion delivery declined $9.30 to US$732.70.

Financial markets are also focusing on congressional testimony by Federal Reserve chairman Ben Bernanke and Treasury Secretary Henry Paulson, who defended their $700-billion financial-industry bailout before the House financial services committee. Paulson insisted it would not be appropriate to let automakers have some of that money.

George Weston Ltd. -- majority owner of Loblaw Cos. -- reported flat third-quarter profits of $179 million, as sales increased 4.4 per cent from a year earlier to $10.61 billion. Shares in George Weston were down 48 cents to $62.40.

In other economic data, U.S. wholesale prices plunged a record amount in October. The Labour Department's producer price index fell 2.8 per cent, raising worries about deflation.

Shares in TransCanada Corp. dropped nearly five per cent after Monday's announcement of bought-deal issue of $1 billion in new shares to pay debt and fund capital projects including its Keystone Pipeline. Shares were down $1.68 to $32.85.

Enbridge Inc. said it will invest US$500 million in Enbridge Energy Partners LP, raising its stake in the U.S. pipeline business to 27 per cent. Its shares were down $1.65 cents to $37.50.

In other corporate news, Telus Corp., Canada's second-largest telecom operator, says it will invest $100 million over three years on a health technology division. Telus shares gained 54 cents to $38.52.

General Electric Co. says it is reorganizing its struggling GE Capital unit to save $2 billion next year, a process that will likely lead to job cuts.

Yahoo Inc. founder Jerry Yang is stepping down as chief executive. Observers believe Yang's departure will accelerate an overhaul of Yahoo and lead to a sale of the Internet company to Microsoft.