You can calculate gains and losses using FIFO (First in, First Out). This means the first shares that you purchased are the first that are sold. For example, let’s say you bought 1 share of company XYZ at $10 a share then 2 shares of company XYZ at $15 six months later. One year has passed and you decide to sell 1 of the 3 shares you own. The price of company XYZ is now $20 per share. What is your cost basis in this example? The answer is $10 because we sell the first share that you bought which was purchased at $10 per share. Since you sold the stock at $20 per share, you have a gain of $10 ($20 sale price - $10 cost basis).
This same formula can be used in calculating your losses. For example, you buy 2 shares of company XYZ at $20 a share two years ago and 2 more shares of company XYZ at $25 a share 6 months later. Today, the stock is trading at $10 a share and you decide you want to sell 2 shares. Your loss would be calculated by doing (sale price – cost) with the same FIFO method. This means that you purchased $40 total first and you are selling two stocks at a price of $10 a share, or $20 total since that is what it is worth today. Your loss would come out to $20 ($40 total cost to purchase 2 shares two years ago - $10 share price for each share today) and you would still own 2 shares of the stock purchased at the $25 stock price.