Whenever you sell a stock at a higher market price than you bought it for, you may have to pay income tax on the profit -- which is considered a capital gain. But if the stock price increases after your initial purchase and you scoop up additional shares before selling off part of the portfolio, it can be a little confusing figuring out which shares' tax basis to use when calculating your gain. In fact, the Internal Revenue Service lets you choose between two ways of determining which tax basis to use: specific identification and FIFO, or "first in, first out." This choice presents a tax planning opportunity -- but you have to take advantage of it before you sell the stock.

The tax basis of your stock
Figuring out your capital gain is a bit simpler when all of the shares you sell were purchased at the same price, as you don't have to worry about whether you use specific identification or FIFO to choose shares. Your capital gain is essentially the difference between your basis in the stock and the price you sell it for. A stock's tax basis is the market price your broker executes your buy trade at plus any commission he charges. When your broker executes the sell trade, however, you don't increase the stock's tax basis again. Instead, the commission reduces your total proceeds from the stock sale.

To illustrate how this all works, suppose you bought 1,000 shares of Sony Corporation at $15 per share and then sold the entire portfolio two weeks later when the market price was $21 per share. If your broker charges a $25 commission for each trade, your basis in the 1,000 shares is $15,025 -- the $15,000 market price you paid plus the broker's commission. Your net proceeds from the sale are $20,975 -- the $21,000 market price minus the $25 commission. In this scenario, your profit is $5,950 ($20,975 minus $15,025) and is taxed as a short-term capital gain because you held the Sony shares for less than one year before selling them. With a short-term gain, you'll owe ordinary rates of tax on it -- not the lower rates that apply to net long-term gains -- if you don't have losses to offset it with.

Multiple purchases of identical stock
Things aren't so straightforward when selling shares purchased in separate lots and at different market prices. Because all of the shares are identical, you may be unsure which lot's tax basis to use for the sale transaction. In cases like this, the IRS generally requires a "first in, first out," or FIFO, approach to figuring out the correct tax basis. This means you have to use the entire acquisition cost for the first lot of shares before you can use the cost basis of the more recent stock purchases. But if you're proactive and tell your broker the specific shares you want to trade and receive some form of written confirmation after the sale, you can cherry-pick the shares with the higher tax basis by using what's known as specific identification. Ultimately, the higher your tax basis is, the less taxable gain you'll have.

To appreciate the difference in tax liabilities that can result from the same stock sale, suppose your portfolio includes 3,000 shares of Sony stock that you acquired through three separate trades of 1,000 shares. If the market prices you paid for each lot are $15,000, $17,000 and $21,000, respectively, your total basis (before commissions) is $53,000. Under the FIFO rule, you have to use the basis of the oldest stocks first, so if you sell 1,000 shares for $20,000, your capital gain is $5,000 -- the sales proceeds minus the $15,000 cost of the first lot of 1,000 shares purchased.

But if you use specific identification and tell your broker in advance to sell the third lot that you paid $21,000 for, you'll end up with a $1,000 capital loss, rather than a taxable $5,000 capital gain. At the end of the day, not only do you avoid paying income tax on the stock by identifying the specific lot sold, but you may save even more money in tax with the loss.