US stocks went on another dizzying ride as they worked their way back from an early-morning plunge to send the Standard & Poor’s 500 index to its first gain in four days.

It was the latest swing in a frenetic week for markets around the world as investors recalibrated how worried to be about a possible trade war and a more aggressive Federal Reserve.

When US markets opened for trading, the S&P 500 lost as much as 1.1% to join a worldwide sell-off after President Donald Trump doubled down on “trade war” talk.

He took to Twitter to defend his promise from Thursday to impose stiff tariffs on imports of steel and aluminium, saying that the United States is losing on trade with virtually every country and that “trade wars are good” and “easy to win”.

Investors had a different impression. Markets tumbled from Asia to Europe on fears that escalating retaliation between countries could choke off trade and the global economy.

The president of the European Union’s governing body suggested possible tariffs on blue jeans and motorcycles.

The S&P 500 trimmed its loss as the day went on and was bouncing between gains and losses by the early afternoon.

It accelerated in the last half hour of trading and ended at 2,691.25, up 13.58 points, or 0.5%.

The Dow Jones industrial average fell 70.92, or 0.3%, to 24,538.06, and the Nasdaq composite rose 77.31, or 1.1%, to 7,257.87.

Stocks pared their losses as investors questioned how far Mr Trump will end up going, said Brent Schutte, chief investment strategist at Northwestern Mutual Wealth Management.

“I view nearly every one of Trump’s actions through a negotiation lens,” he said. “This was an anchor, an opening bid. … I think the market senses some of that, and I would imagine that we will see some horse trading going on with what ultimately happens with these tariffs.”

The S&P 500 still ended the week with a loss of 2%, its third decline that severe in the last five weeks. Last year, the worst weekly loss was just 1.4%.

If a trade war does indeed break out, it could threaten a key reason investors were optimistic about stocks coming into 2018 – the global economy is finally strengthening in sync, which should lead to higher corporate profits.

Big US companies are heavily reliant on global trade, and companies in the S&P 500 got 43% of their sales from outside the United States in 2016, according to S&P Dow Jones Indices.

That means Apple and other big US companies are dependent on customers not only in Peoria but also Paris and Peru.

Stocks of smaller US companies, which tend to do more of their business at home, did much better than the rest of the market. The Russell 2000 index of small-cap stocks rose 25.78, or 1.7%, to 1,533.17.

The trade worries are piling on to a market that was already nervous. Concerns about the possibility of higher inflation and interest rates have rocked markets since the S&P 500 set its latest record high in late January.

Inflation has been low in the years following the Great Recession, but if it jumps higher, it could force the Federal Reserve to raise short-term rates more sharply than investors are expecting. That could easily upset markets, which had been enjoying a remarkably smooth ride last year.