Dice on stock listings © Kate Kunz, Corbis

The idea of trying to "time" the market -- of trying to get in before it goes up, and get out before it goes down -- has a terrible reputation.

Timing "is a wicked idea -- don't try it, ever," wrote Charles Ellis, one of the leading lights of index investing, many years ago.

According to conventional wisdom, any attempt to time the market is fundamentally flawed. Stock markets follow a "random walk," they say. No one can predict the market's next move, so trying to do so will end up costing you money. A lot of your long-term gains will come from a few big "up" days, and these are completely unpredictable -- if you are out of the market when they happen you will miss out on a lot of profits.

Money managers often push this idea to the clients. It has, from their point of view, a side benefit: It helps keeps the clients fully invested at all times, which means their assets are generating more fees.

But is the idea correct? The simple answer: No.

Yes, most people who try to time the market end up screwing it up -- they buy and sell at the wrong times -- but that does not mean the idea is flawed. On the contrary, historically, "smart" timing, based on market fundamentals, has been one of the soundest ways to beat the market and produce above-average investment returns over the long term.

What is smart timing? Simple: It is long-term timing, and it is based on following a few solid valuation metrics.

It is not about trying to trade short-term. It is not about selling stocks on Wednesday and planning to buy them back on the following Monday. It is not about obscure market technicals like "head and shoulders" formations or Bollinger bands.

It is about cutting your exposure to stocks when the market is expensive in relation to fundamentals, and keeping your exposure down -- if need be, for years -- until the market becomes much cheaper. It then involves increasing your exposure, and keeping it high, again for years if necessary.

The myth of 'random' returns

Techniques available to anyone have worked, and worked well, for more than a century. That does not mean they will work in the future, but it is a strong argument in their favor.

First, let's demolish the myth that the stock market produces entirely "random" returns -- that some years it's up, other years it's down, that over time it just goes up, and no one can predict anything in advance.

According to long-term data tracked by New York University's Stern School of Business, an index of the top 500 U.S. stocks has produced since the late 1920s an average return of about 9.3 percent a year, when you include reinvested dividends.

That sounds like a great return, and it's the sort of thing money managers often tell their clients. But it contains two hidden nasties.

The first is that it isn't adjusted for inflation, which means that in real spending-power terms you made several percentage points less each year. And the second thing is that those returns did not come randomly. They came in long waves -- bull markets followed by bear markets followed by new bull markets. They were not random at all.

Using Stern's data and inflation numbers from the U.S. Labor Department, I stripped out the hidden inflation in the stock market returns and looked at the "real" returns -- in other words, the returns in actual purchasing power. This is what actually matters. Then I looked at these returns over ten-year periods. The reason for that is that if you are an ordinary investor -- rather than a trader on Wall Street — what you are usually looking for is somewhere to grow your money soundly over the medium to long term.

You can see the results in this chart -- and these results are not random. They are nothing like random. The waves are as clear as -- well, as clear as a big wave at Sunset Beach.

If you invested in the stock market in the 1940s or early 1950s, you earned spectacular returns as you cashed in from the gigantic postwar boom. And if you invested from the late 1970s to the early 1990s, once again you earned spectacular returns in the subsequent returns due to the huge boom from 1982 through 1999. Lucky you.

But what about at the other times?

Hmmm. If you were unlucky, or foolish, enough to invest in the late 1920s, the later 1930s, or between 1963 and 1973, you were right out of luck. Your returns were terrible. In many cases you actually lost money on the stock market, after accounting for inflation.

Not just for one or two years. More than 10 years.

So even though since the late 1920s the average ten-year "real" return to U.S. stocks (after inflation) has been about 6.4 percent a year, a quarter of the time it was actually less than 1.3 percent -- a number I chose because it happens to be the guaranteed "real" return on long-term inflation-protected U.S. bonds (I own a few) available right now. When you deduct taxes and investment costs -- even in low-cost funds -- the actual returns earned by most investors were lower still.

Remember how people tell you the indexes will never let you down if you stick with them for five to 10 years? It's nonsense.

Note also, please, that these 10-year figures do not include any allowance whatsoever for volatility. Someone who invested in Wall Street in 1928 and held on for 10 years earned a real return of just 0.25 percent a year, after accounting for inflation. But just to earn that miserable payoff he had to stick with his stocks during the biggest crash in modern history, the 90 percent collapse of 1929 to 1932.

If he lost his job, or even just lost some of his nerve (understandable), and trimmed his position in the meltdown, he didn't even get his 0.25 percent a year. He probably lost money.

The go-with-the-flow crowd pretends that these long periods of poor performance are basically costless. "Just sit there and wait," they say, "and the next bull market will come along in due course. Don't try to time these things." But that's deeply disingenuous. While you are earning nothing in stocks, you are missing out on gains in bonds or other assets.

The full cost of waiting out these bear markets is horrendous. When you factor in the fees, the taxes, the volatility and the opportunity cost of what you could have been earning elsewhere, the investor gets hosed.

More from MarketWatch: