If you have kept money in the stock market consistently over the last few years, congratulations.

The odds are that, after enduring terrible market declines early in the pandemic, your stock stash has grown magnificently.

This calendar year alone, the S&P 500 has risen more than 20%.

What's more, the market has been rising for years: more than 16% in 2020 and 28% in 2019. In the last 20 years, the S&P 500 has returned 9.5% annualized, including dividends — a cumulative gain of more than 500%, according to Bloomberg. Clearly, most stock investors have ample reasons for celebration.

Not so for bond investors. Bond prices have declined this year, punished by low yields and high inflation. Unlike stocks, which are inherently risky and can theoretically fall in value all the way to zero, bonds, especially U.S. government bonds, will repay their principal in full, and their prices are far more stable than stocks'. That stability may be bonds' greatest appeal.

But when you adjust current bond yields for inflation, they turn negative. (Treasury Inflation-Protected Securities, known as TIPS, do match inflation, but provide no more than that.)

Stocks, in short, have been great lately, and bonds have been a drag on portfolios. Given those facts, you may be tempted to hold only stocks and abandon bonds. But I think that would be a mistake. Just as it makes sense to hold a broadly diversified group of stocks for the long run, preferably through low-cost index funds, there is value in bonds, too.

Rejoice, yes, but then worry

First, this is no time to be complacent about stocks, not with inflation surging, millions of people still out of work and the coronavirus still at large in the United States and ravaging millions around the world. Even if you wear blinders and focus solely on the markets and the companies that underlie it, there are problems aplenty.

For one thing, some Wall Street strategists, normally a bullish lot, are projecting that the stock market will fall about 2% in the remainder of this year, according to a Bloomberg survey. Some expect declines in 2022.

The surge in inflation — with the Consumer Price Index rising 6.2% in October from a year earlier, its fastest rate since 1990 — could disrupt the stock market. So could an abrupt response to inflation by the Federal Reserve.

Stock valuations are stretched

The stock market's long rise has consequences: Most stocks are no longer bargains. As Robert Shiller, the Yale economist, has pointed out, an important measure of stock valuations, the cyclically adjusted price earnings (CAPE) ratio, has been hovering in a rarefied range, exceeded only in December 1999, during the dot-com bubble.

The Shiller index can't predict short-term stock market movements, but, like other valuation measures, it suggests that stock market returns over the next decade are likely to be lower than those of the last one. Vanguard, for instance, projects that the U.S. stock market will produce annualized returns of only 2.4% to 4.4% for the next decade, in no small part because prices are so high.

When stocks are terrifying

The past is no guarantee of the future, but it provides clues. Countless academic studies suggest that the key to prosperity for nonprofessional investors is to hold stocks for the long term and avoid market timing.

That implies that investors need to be able to withstand big losses periodically because the stock market fluctuates, sometimes painfully, as it did last year. Recall that from Feb. 19 to March 23, 2020, the S&P 500 fell 34%. Further declines of that magnitude or greater could happen at any time.

Does that make you uneasy? It bothers me.

An excellent strategy for buffering losses and hanging on to stocks, come what may, has been to own bonds. That's because bonds and stocks have been inversely correlated much of the time: When one rises, the other falls.

That beneficent relationship isn't foolproof: The inverse correlation has broken down periodically, and the current low yields may limit bonds' ability to counteract stock market losses. Still, several recent studies show that in periods of spiking inflation, bonds have performed their essential function: limiting portfolio losses and rising when stocks decline.

Bonds can be a balm

I hold bonds because I'm afraid to hold only stocks. Yet I want to remain in the stock market because I don't know exactly when it will rise.

My mutual fund portfolio contains more than 40% bonds, which I view as insurance. When the stock market crashes, the bonds will be invaluable. I'll sell some of them then at rich prices, and buy more stock cheaply, rebalancing the portfolio. You can do this yourself, or with professional help.

You can't gain from stocks if you don't hold them when they rise. But to stay calm and stick with the stock market, you may want to own bonds. Plenty of them.