HONG KONG/SHANGHAI (Reuters) - China's yuan soared against the U.S. dollar on Thursday following a sharp rise in the offshore spot rate as China worked to stem capital flows and stabilize the currency ahead of Donald Trump's inauguration as U.S. president and the Lunar New Year.

The offshore yuan <CNH=D3> gained 0.8 percent against the dollar and traded at 6.8151 as of 0830 GMT (3.30 a.m. ET), driven up by a rising cost of funds in Hong Kong and trading firmer than the onshore spot rate for a second straight day.

It was not clear if Chinese authorities had engineered the spike in yuan borrowing rates in Hong Kong. But they had used such a tactic several times last year to support offshore yuan exchange levels and by extension relieving some of the pressure on the yuan onshore, which is at more than eight-year lows.

The cost of borrowing yuan surged in Hong Kong, with the rate of overnight contracts <HICNHONDF=> jumping to its highest level in nearly a year.

The CNH Hong Kong Interbank Offered Rate benchmark (CNH Hibor), set by the city's Treasury Markets Association (TMA), rose to 38.33500 percent for overnight contracts, the highest since Jan. 12, 2016. It was 16.94767 percent on Wednesday.

With signs of short-term liquidity stress, the implied overnight deposit rate for the offshore yuan <CNHONID=R> stood at 90.393 percent as of 0830 GMT, after soaring as high as 101.694 percent earlier on Thursday.

The rate closed at 10.587 percent a day earlier.

BIG ONSHORE GAIN

Onshore, the yuan was on track for its biggest one-day gain since the first quarter of 2016.

"London traders have come into the market in the afternoon and that's why you saw another round of yuan short sellers squaring their positions," said a trader with an American bank in Hong Kong.

The yuan is limited onshore to trading within a 2 percent band on either side of a reference rate set each day by the central bank. But offshore, it can move more freely, which accounts for the different exchange rates.

Traders in the onshore market said they were primarily trying to stop the bleeding on Thursday.

"The onshore market is reacting to the offshore as traders were liquidating their positions to prevent further losses," said a trader at a Chinese bank in Shanghai.

Another trader at a foreign bank said the sell-offs kicked off at the market open, right after the central bank fixed a stronger-than-expected midpoint at 6.9307 per dollar, the strongest in nearly three weeks. The previous fix was 6.9526.

Other Asian currencies rose on Thursday in response to the surging yuan.

The spot market <CNY=CFXS> opened at 6.9280 per dollar and was changing hands at 6.8817 at 4:30 p.m, 489 pips stronger than the previous late session close and 0.71 percent firmer than the midpoint.

The yuan had settled at 6.9485 at 4:30 pm on Wednesday, and finished that day's late session at 6.9306.

Some analysts said the liquidity conditions in Hong Kong were unlikely to improve in the short term as the peak season for cash demand - the Lunar New Year holiday - was nearing. This year, the holiday starts just before the end of January.

Beijing has recently rolled out a slew of measures to stabilize the Chinese currency including tightening scrutiny on individuals' foreign exchange purchases.

Some analysts said the Chinese government was keen to keep the yuan stable around Trump's inauguration on Jan. 20. Trump had threatened during the election campaign to slap high import tariffs on Chinese goods and label Beijing a currency manipulator.

The Thomson Reuters/HKEX Global CNH index <.RXYH>, which tracks the offshore yuan against a basket of currencies on a daily basis, stood at 97.3, firmer than the previous day's 96.47.

The global dollar index <.DXY> fell to 102.17 from the previous close of 102.7.

Offshore one-year non-deliverable forwards contracts (NDFs)<CNY1YNDFOR=>, considered the best available proxy for forward-looking market expectations of the yuan's value, traded at 7.136, 2.88 percent weaker than the midpoint.

One-year NDFs are settled against the midpoint, not the spot rate.

(Reporting by Michelle Chen in HONG KONG; Winni Zhou and John Ruwitch in SHANGHAI; Editing by Richard Borsuk)