A surprise move by China to devaluate its currency sent North American markets into a tailspin, with both Toronto and New York ending the session lower.

In Toronto, the S&P/TSX composite index closed down 51.72 points at 14,414.67, recovering from a steeper plunge of more than 150 points earlier in the session.

The TSX got a final-hour push from a recovery in the gold sector, as the December gold advanced $3.60 to US$1,107.70 an ounce.

The September crude contract lost $1.88 to US$43.08 a barrel.

On Wall Street, the decline was even steeper as the Dow Jones industrial average plunged 212.33 points to 17,402.84, the Nasdaq dropped 65.01 points to 5,036.79 and the S&P 500 fell 20.11 points to 2,084.07.

The Canadian dollar, like other commodity-based currencies, was also a casualty of the overnight announcement by China's central bank, with the loonie tumbling 0.61 of a U.S. cent to 76.31 cents.