(Reuters) -Canada's main stock index edged lower on Friday but still posted a weekly gain, as investors pinned their hopes on easing trade tensions and weighed bets to take advantage of potential moves after next week's Canadian general election.

The Toronto Stock Exchange's S&P/TSX Composite Index ended down 17.02 points, or 0.1%, at 24,710.51, after posting a three-week high on Thursday.

For the week, the index was up 2.1%, its third straight weekly advance.

Wall Street also ended higher for the week as investors parsed a spate of earnings and Beijing exempted some U.S. imports from its 125% tariffs.

"The TSX has been walking on a tight rope - balancing domestic political uncertainty against potential spillover from global economic shifts," said Victor Kuntzevitsky, a portfolio manager at Stonehaven, Wellington-Altus Private Counsel.

Canada stands to elect a government that will push policies to boost business more than in recent years but its trade-dependent economy could be in line for an increased level of deficit spending, analysts and investors said before Monday's vote.

Prime Minister Mark Carney's ruling Liberal Party remains ahead in the latest polls but the gap with the main opposition Conservative Party, led by Pierre Poilievre, is tightening.

The materials sector, which includes metal mining shares, has been the strongest performing sector on the TSX this year, advancing more than 20%, as safe-haven demand propelled the price of gold to a record high.

"We have been lightening up on the materials space and using that to buy technology-heavy indices elsewhere," Kuntzevitsky said, adding that he could raise his energy share holdings if a potential election win for the Liberals, who are seen more receptive to climate policies than the Conservatives, leads to those stocks becoming cheaper.

The industrials sector fell 1.1% on Friday, with shares of professional services firm WSP Global Inc down 4.3%.

Declines for gold and copper prices weighed on the materials sector, which ended 0.7% lower.

(Reporting by Fergal Smith in Toronto and Ragini Mathur in Bengaluru; Editing by Shreya Biswas and Aurora Ellis)

By Fergal Smith