The stock market's been creeping up over the past few weeks, and although you may not have noticed it, the S&P 500 now sits just below its four-year high. The market's extremely complacent in the face of rising interest rates and oil prices. A common measure of volatility that's often cited as an indicator of the degree of fear in the market is near multi-decade lows. So, is now the time for the contrarian to cut and run?

Well, sad to say, I have no idea. People tend to be extremely bad at predicting markets, and I'm no exception. Oil could skyrocket and the economy might slow, leading to a meltdown in October. Or the market might decide to go up, based on the same news. I think speculation about the direction of the markets has about the same utility and entertainment value as speculation about Tom Cruise's next romance.

But this doesn't mean all is lost. At the Motley Fool Inside Value newsletter, our stocks have returned 13% vs. the market's 5% since inception. We've done this by finding great undervalued stocks, not by guessing where the market will go. As long as you stay focused on undervalued companies, it doesn't make sense to sell based on hunches about the health of the economy or how the markets will move. The investments you own may or may not suffer during a downturn, but if they're solid investments, they'll bounce back and outperform over the long term -- as they have for master value investor Warren Buffett, who boasts a 40-year track record of average annual gains north of 20%.

My own personal Omaha
This strategy has also worked well for me. The worst bear market in recent memory started in 2000. Many people lost piles of money, but value investors had a great time. While technology was way overpriced, old-economy companies like real estate investment trusts (REITs) were cheap. I didn't care where the stock market was headed. I just bought great REITs such as HeathcareRealty Trust (NYSE:HR), Hospitality Properties Trust (NYSE:HPT), and Sunrise Senior Living (NYSE:SRZ). They doubled or tripled at a time when other people were losing their shirts.

So, when should you sell? Here are three rules of thumb:

1. The stock's overvalued
As a value investor, I buy when companies are cheap. That is, I buy them when their stocks trade below what I figure the company's is truly worth, it's "intrinsic value." I know that the odds are in my favor when I do this because these stocks are likely to one day rise to fair value -- and I'll profit off that move.

So, if a stock is no longer undervalued -- if it's fairly valued or overvalued -- I'll start thinking about selling. This doesn't mean that I'll jump ship the instant that a stock hits its fair value. If a company has a strong competitive position, reasonable growth prospects, and isn't way overvalued, I'll hold the position so as to minimize transaction costs and taxes.

But if a company's trading at a significant premium to its fair value, then the odds are no longer in my favor. The stock has little upside, and significantly more risk. So, I'm generally happy to sell at that point.

For example, consider Nutraceutical (NASDAQ:NUTR), a nutritional supplement manufacturer. In September 2003, the company traded at a mid-single-digit price-to-free cash flow ratio, had decent management, and was a solid business in a no-growth industry. I estimated that the shares were worth about $18, so I was happy to pick some up at $10.

I got lucky, and within months, the stock broke above $20. I didn't sell immediately, since I had nowhere better to put the cash. But when it passed $25, the market seemed to be paying too much for such a low-growth company. I sold half my position at $26 and planned to sell the rest in the mid-$30s. However, the stock fell almost immediately and now trades around $13.50. Selling some of my Nutraceutical shares when it was overvalued gave me a profit and reduced my exposure to the subsequent downturn.

Want help calculating a company's intrinsic value? Use Inside Value's discounted cash flow (DCF) calculator, which spits out intrinsic values after you input some simple figures. If you're already an Inside Value subscriber, click here to access it now. If not, click here to take a free 30-day trial to the newsletter and play with the calculator to your heart's content. If you need help using the calculator, post a question on our message board dedicated to valuing stocks.

2. There is a better opportunity
Another time that I sell is when I need cash to take advantage of some other investment. After all, why would I stay invested in something returning 8% a year if there's another investment that I expect to return 12% annually?

For instance, back in October 2003, I sold shares of Vornado (NYSE:VNO) to buy shares of Kingsway Financial (NYSE:KFS). Vornado, a REIT with office properties in New York and Washington, was exceptionally well-managed and poised for growth, but it was uncommunicative with shareholders and compensated its managers richly. Kingsway, with a solid position in specialty automobile insurance, had taken a hit as a result of a poor quarter and looked quite cheap. I still liked Vornado, but didn't have enough cash to buy Kingsway, so Vornado had to go.

Unfortunately, excluding taxes and transaction costs, the trade has been a wash, since both stocks have returned about 80% since then. (That in and of itself may be a lesson that selling isn't always a good idea.)

3. The position is too big
The third time I sell is if a position has become such a large proportion of my portfolio that it makes me uncomfortable. Some value investors are perfectly happy with a single position making up 30% of their portfolio, but I'm more cowardly. My maximum position size is 10%, and I prefer even smaller positions for volatile small-cap stocks. Of course, if I sell because a position has become too large, I generally sell just enough to reach my comfort level.

I did this with Polaris (NYSE:PII), a manufacturer of all-terrain vehicles that has been growing relentlessly for more than a decade. Several years ago, I bought a large position at prices below $20. When the stock hit $40, it was more than 10% of my portfolio, so I sold a third of my shares. I sold a third of the remaining shares in the mid-$50s, and call options on another third when the stock was above $70. I still liked the company, so at each point, I was just reducing my position according to my comfort level. The psychological payoff came when Polaris recently fell from $70 to $50.

The Foolish bottom line
These rules of thumb work well for me, but may not make sense for investors who use other strategies. For instance, selling a stock because it's grossly overvalued may be a bad idea for someone using a momentum or Gorilla Game strategy. But for me, and others at Inside Value, such rules work because we constantly purchase cheap stocks that have high expected returns. We want to have capital continually available to exploit great opportunities, and these rules for selling help to ensure that is the case.

Subscribers and people on a free trial alike can enter our "A Stock I'd Love to Own" contest. We're asking people to name their favorite company. First prize is a one-year subscription to Inside Value. The first four runners-up will get six- or three-month subscriptions. Subscribers can click here for contest rules. Click here to take a free trial so you can enter the contest.

Richard Gibbons, a member of the Inside Value team, still owns shares of Nutraceutical, Kingsway, and Polaris, and is still short the Polaris calls, but doesn't have a financial position in any other company mentioned in this article. (Thank goodness. This disclosure statement's too long anyway.) The Motley Fool has a disclosure policy.