Warren Buffett makes knowing when to buy a stock seem easy. You find a business you like, determine its fair (or intrinsic) value, decide how much of a discount you wish to pay, wait for the market to offer you the desired price, and pull the trigger. Of course, the devil's in the details, and the proper determination of the company's fair value, as well as your own decision of how much of a discount you'll demand, are the difference between picking a Buffett-like, long-term winner like Microsoft (NASDAQ:MSFT) versus a mere underperformer like ... well, let's not dwell on our losses.

But knowing when to buy is easy, if you compare it with deciding when to sell. Selling requires a consideration of more than just the company's fair value -- and the biggest bear of them all is taxes.

Fear not, though. I have a clear, calculable, and foolproof (lowercase "f") way for you to decide when to sell.

So I've got this three-bagger .
Consider the following situation. Shares of computer maker, iPod creator, and fan favorite Apple Computer (NASDAQ:AAPL) currently trade at around $53 per share. Now, suppose you were one of the prescient (or just lucky) souls who picked up shares of Apple when it was at $14 back in May 2004. That would feel pretty good today.

But things may not be totally hunky-dory. Suppose you have never been very comfortable with Apple's valuation throughout its rise. In fact, at various times, you may have though that the stock was downright overvalued. Suppose further that a few months ago, you decided to do something about it -- you sold half of your position at $42 per share back in February, vowing to get back in at a more favorable price, perhaps in the mid- to high $30s. Lo and behold, Apple dropped down to $36 this past May, and you scooped up the same number of shares you had sold in February -- but at six bucks per share less.

Question: Who's your daddy now?

The answer may surprise you.

Answer: The IRS.

The taxman cometh
Any capital gains you have after selling your Apple stock are taxed at either a short-term rate or, if you held the stock for longer than a year, a long-term rate. The short-term tax is equivalent to your ordinary income marginal tax rate, while the long-term rate is either 15% or 5%, again depending upon your income. Currently, you'll qualify for the 5% long-term tax rate only if you are in the 10% or 15% marginal tax brackets. (You can visit the IRS website to find out precisely which category you fall into).

Using the Apple example, since the sale took place after you'd held the stock for less than a year, it's a short-term gain. Suppose that you're in the 25% tax bracket. That means that come April, you'll owe the IRS 25% of the difference between $42 and $14. Yup, $7 per share in that realized gain is not going to you but to the IRS.

Wait ... am I really telling you that you lost money by selling a stock at $42 and buying it back at $36?!

Pretty much.

You played the market properly, you called the stock's short-term top, you picked up the shares again after a 14% drop in price ... and yet, because of the IRS's take, your $6-per-share gain turned into a $1-per-share net loss. In other words, despite the significant gyration in Apple's stock price, you did not realize a profit on your trade and would've been better off just holding onto the stock and not selling at all.

So what to do?
You have two choices: don't pay your taxes, or determine a selling price that's high enough to compensate you for the cut that the IRS will take. Since the first option will land you in jail, I strongly encourage you to read on about the second option.

First, you should consider taxes to be just as much of a trading cost as commissions are -- after all, this is money you'll have to pay from your own pocket. Not taking the tax bite into account is like having a company emphasizing earnings before taxes or a mutual fund manager touting compounded returns before all of the fees are taken out -- you don't get an accurate picture of true performance.

Second, consider the tax effect as a very large bid/ask spread -- the difference between what you can get for selling an asset versus what it would cost you to obtain it. The difference can be enormous. In the Apple example, it would cost you $42 per share to obtain the stock, but if you sell your position, you'll get $35 just per share after taxes. The stock may be fairly valued to someone thinking of opening a position, but not to a seller like you. Simply put, you can't really get $42 per share for Apple when it is trading for $42.

The formula
Thus, if you think the stock is fairly valued at $42, ask yourself what price Apple needs to trade at so that you can actually put $42 in your pocket. That analysis will ultimately lead you to the calculation below. I really like this formula because it is universal enough to apply to any stock, but is especially useful for those holding shares in companies that appreciate in value quickly at some point in time -- a list that includes Netflix (NASDAQ:NFLX), Sirius Satellite Radio (NASDAQ:SIRI), Overstock.com (NASDAQ:OSTK), and Taser International (NASDAQ:TASR). The formula is as follows:

Required Selling Price = (FV- CB) * (1 / (1-TR)) + CB

I know, it looks scary but it really isn't. Let's go through it step-by-step:

"CB" is your cost basis. That's how much you paid for the shares when you originally acquired them.

"FV" stands for fair value -- the value of the stock at which you would sell if you could pocket the full amount. For now, you'll have to determine this number for yourself. (Part 2 of this article will deal with how to determine this number and why I think you should sell a stock once it reaches its fair value).

"TR" is the tax rate and will depend on two things: (1) how long you've held the asset and (2) your marginal income tax rate. As mentioned earlier, the number will be either 5% or 15% for long-term trades and anywhere between 10% and 35% for short-term trades. (The percentages should be adjusted higher if you owe any state and local tax for stock sales in addition to the federal tax).

Play around with this formula, and try to figure out what the required selling price would be for the fictional investor above who bought a stock at $14, believes it's fairly valued at $42, faces a tax rate of 25%, and wants to realize an after-tax profit of the full difference between $14 and $42, or $28 per share. (Hint: The answer is $51.33.)

Fool contributor Marko Djuranovic owns shares of Netflix but of no other company mentioned in this article, though he wishes he had been prescient enough (or lucky enough) to have bought Apple Computer at $14, Netflix at $4, Overstock at $16 ... and so on. The Fool has a disclosure policy.

Taser and Overstock are Motley Fool Rule Breakers newsletter recommendations. Netflix is a Motley Fool Stock Advisor pick.