9.7. Selling Shares

Entering an investment you are selling is done in the same way as buying one (see Section 9.5.2, “Buying New Shares”) except the total cost of the transaction is entered in the Sell column and the Shares column is entered as a negative amount. The net proceeds from the sale should be transferred from the stock account to your bank or brokerage account.

The proper recording of the stock sale *must* be done using a split transaction. In the split transaction, you must account for the profit (or loss) as coming from an Income:Capital Gains account (or Expenses:Capital Loss). To balance this income, you will need to enter the stock asset twice in the split. Once to record the actual sale (using the correct number of shares and correct price per share) and once to balance the income profit (setting the number of shares to 0 and price per share to 0).

Note

In order to get GnuCash to commit this zero-share, zero-price split to the transaction, you *must* Tab out of the split. If you use the Enter key, GnuCash will convert the split into shares of the commodity.

In the split transaction scheme presented below, the symbol NUM_SHARES is the number of shares you are selling, SELL_PRICE is the price for which you sold the shares, GROSS_SALE is the total price for which you sold shares, equal to NUM_SHARES*SELL_PRICE. PROFIT is the amount of money you made on the sale. COMMISSIONS are the brokerage commissions. NET_SALE is the net amount of money received from the sale, equal to GROSS_SALE - COMMISSIONS.

Table 9.1. Selling Stock using Split Transaction Scheme

AccountNumber of SharesShare PriceTotal BuyTotal Sell
Assets:Bank ABC  NET_SALE 
Assets:Stock:SYMBOL00PROFIT(Loss)
Expenses:Commissions  COMMISSION 
Assets:Stock:SYMBOL-NUM_SHARESSELL_PRICE GROSS_SALE
Income:Capital Gains  (Loss)PROFIT

If you will be recording the sale of the stock as a capital gain (or loss), please see Chapter 11, Capital Gains and Chapter 20, Depreciation for more information on this topic.

9.7.1. Example - Sale of stock with profit

As an example we will use the AMZN account created in the previous section. So you bought 100 shares of AMZN for $20 per share, then later sell them all for $36 per share with a commission of $75. In the split transaction scheme above, PRICEBUY is $20 (the original buying price), NUM_SHARES is 100, TOTALBUY is $2000 (the original buying cost), GROSS_SALE is $3600, and finally PROFIT is $1525 (GROSS_SALE-TOTALBUY-COMMISSION).

Table 9.2. Selling Stock Split Transaction Scheme

AccountSharesPriceBuySell
Assets:Bank ABC  3525.00 
Assets:Brokerage Account:Stock:AMZN001600.00 
Expenses:Commissions  75.00 
Assets:Brokerage Account:Stock:AMZN-10036.00 3600.00
Income:Cap Gain (Long):AMZN   1600.00

Selling Stock Example

An example of selling stock for gain. You bought 100 shares of AMZN for $20 per share, and sold for $36.

Selling Stock Example

An image of the account tree after the example of selling stock for gain.

9.7.2. Example - Sale of stock with loss

As an example we will use the IBM account created in the previous section. You then have bought 51.7598 shares of IBM for $96.6001 per share, then later sell them all for $90 per share. In the split transaction scheme below, PRICEBUY is $96.6001 (the original buying price), NUM_SHARES is 51.7598, TOTAL_BUY is $5,000 (the original buying price), (Loss) is $341.62, and finally GROSS_SALE is $4658.38. Assume the commission was $100.00.

Table 9.3. Selling Shares at loss Split Transaction Scheme

AccountSharesPriceBuySell
Assets:Bank ABC  4558.38 
Assets:Brokerage Account:Stock:IBM00 341.62
Expenses:Commissions  100 
Assets:Brokerage Account:Stock:IBM-51.759890.00 4658.38
Income:Cap Gain (Long):IBM  341.620

Note

You may either enter the loss as a positive number in the buy column or as a negative number in the sell column, GnuCash will move the negative profit to the other column.

Selling Stock at loss Example

Above is a screenshot of the example of selling stock. You bought $5000 worth of IBM shares with a stock value of $96.6001, and sold for $90.

Selling Stock at loss Example

An image of the account tree after the example of selling stock for loss.

9.7.3. Automatic Calculation of Capital Gain or Loss Using Lots

9.7.3.1. Introduction

Wikipedia includes the following definition of a lot

a set of goods for sale together in an auction; or a quantity of a financial instrument.

GnuCash has a built-in lot management facility that can be used to keep track of capital gains or losses resulting from security sales. Buy and sell transactions are put into lots for the purpose of calculating the cost of the sale. More specifically, a lot is used to link particular buy and sell transaction splits. Lots can be automatically or manually created and linked. Capital gain or loss can be automatically calculated and transaction(s) created for the difference between the sale value and the cost of the securities sold. GnuCash refers to this process as scrubbing.

The term scrub is used because security accounts need to be cleaned after sales to ensure the difference between the cost paid for securities, and value received from selling them, is accounted for as capital gain or loss. If the capital gain/loss is not correct, the Trial Balance (ReportsIncome & ExpenseTrial Balance) bottom line total debits will not balance to total credits.

Note

If you make an error, you can delete the capital gain/loss transaction(s) and lot(s) and retry.

If you are not familiar with FIFO, LIFO or Average costing, please see Wikipedia FIFO and LIFO accounting and Average cost method.

If you are not familiar with the difference between GnuCash transactions and splits, please see Section 4.3, “Simple vs. Split Transactions”.

The GnuCash lot management facility can be a useful feature, reducing manual calculation, especially if dividends have been reinvested over years and there are many different costs involved. It can automatically link buy transactions to sell transactions using FIFO cost method and one can manually link specific buy transactions to sell transactions in order to use LIFO. Advanced Portfolio Report basis costs and gains/losses will agree with the costs and gain/loss transactions created by scrubbing if either the FIFO or LIFO cost methods are used.

9.7.3.2. Lots in Account Window

The Lots in Account SSSS window, where SSSS is a security account, is used to manually or automatically link security transaction splits to lots and create capital gain/loss transactions to account for the difference between the costs of buying a security and the value received by selling it.

To open the Lots in Account window, open the security account register, then select ActionsView Lots.

Figure 9.1. Selling Shares - Capital Gains - Lots in Account window

Example of Lots in Account window

An image of the Lots in Account window.


Refer to the Help Manual, Chapter 8 Tools & Assistants, Lots in Account for details of the Lots in Account screen elements.

9.7.3.3. Procedure Summary

Using the lot management facility for the automatic calculation of capital gain or loss typically follows these steps:

9.7.3.4. Manual Lot Creation and Linking

Before using this feature, ensure you have read Section 9.7.3.8, “Considerations”.

This functionality allows the manual linking of specfic buy and sell transactions. It may be used in the case where a user wishes to use a different cost method than the automatic linking method (FIFO). Effectively, if one wishes the cost basis and capital gains in the Advanced Portfolio Report to be consistent with the capital gains transactions created by scrubbing, manual lot creation only needs to be used when using LIFO or sale of designated lots (the same thing for securities as far as US personal tax law is concerned). This is because the scrub function can automatically do FIFO linking so there is no need to do it manually and scrubbing cannot be used for average costing.

See Section 9.7.3.9, “Example of Manual Lot Creation and Linking”.

9.7.3.5. Automatic Creation of Capital Gain Or Loss Transactions

Note

Do NOT do this unless you are using FIFO or LIFO to cost sales. See Section 9.7.3.8, “Considerations”

GnuCash can automatically calculate and create security sale capital gain/loss transactions. Lots are used to link buy transaction splits with sell transaction splits so the correct cost of the securities sold can be determined. GnuCash will use any existing lots, and create new lots for any buy transaction splits not already linked to a lot. Buy and sell transaction splits are linked to lots using FIFO method.

See:

Section 9.7.3.10, “Example 1 of Automatic Creation of Capital Gain Or Loss Transactions”

Section 9.7.3.11, “Example 2 of Automatic Creation of Capital Gain Or Loss Transactions - 2 Sales at Once”

Section 9.7.3.12, “Example 3 of Automatic Creation of Capital Gain Or Loss Transactions - After a Simple Stock Split”

9.7.3.6. Change Orphaned Gains-CCC to Gain/Loss Account

The capital gain/loss transaction(s) created by scrubbing uses an automatically created generic Orphaned Gains-CCC account (where CCC is the security currency) because GnuCash doesn't know which capital gain or loss account should be used. After scrubbing, the user should edit the Orphaned Gains-CCC transaction split to re-assign the income account to a more meaningful income (or expense) gain or loss account (e.g. Income:Long Term Capital Gain:IBM).

See Section 9.7.3.13, “Example of Changing the Orphaned Gains-CCC to Gain/Loss Account”.

9.7.3.7. Run a Trial Balance

Running a Trial Balance report (ReportsIncome & ExpenseTrial Balance) after creating capital gain/loss transactions, is a basic check that capital gains/losses are correctly accounting for the difference between the cost paid for securities, and value received from selling them. At the end of the report, total debits should equal total credits.

Tip

A Trial Balance may not balance due to some other problem. To determine if the cause of an imbalance is from incorrectly accounting for capital gain/loss:

If necessary, temporarily change the date of the sell transaction and the capital gain/loss transaction, so they are the only transactions for a particular date, then run the Trial Balance as at the day before. If the Trial Balance is still out by the same amount, it is not the capital gain/loss that is causing the problem.

If you find a prior out of balance Trial Balance, keep running the Trial Balance report with different dates until you find the date it starts being out of balance. Temporarily change the transaction dates for each transaction on the problem date to the following day, then change the dates back to the correct date 1 at a time, running the Trial Balance each time, until you identify the problem transaction. When you change the date of a security sell transaction, you also need to change the date of the corresponding capital gain transaction as it is only the sum of these that will balance in the Trial Balance.

9.7.3.8. Considerations

There are some points that should be considered before using the lot management facility.

  1. GnuCash implements only the First In/ First Out (FIFO) cost method when automatically linking buy transactions to sell transactions. I.e. The oldest securities are always sold first. The Last In First Out (LIFO) cost method may be used by manually linking the most recent buy security splits to the sell split before scrubbing.

  2. The Advanced Portfolio Report does not use lot information when calculating costs, just the security transaction splits. It calculates the cost basis and gains or losses using the selected Basis calculation method report option (Average, FIFO or LIFO). If one wishes the Advanced Portfolio Report costs and gains/losses to be consistent with the capital gain/loss transactions created by scrubbing, the same cost model must be used in both places.

  3. Scrubbing does not work correctly if there has been a previous sale where the capital gain splits have been manually entered as part of the sale transaction. Therefore ensure previous sales are recorded as 2 transactions:

    Table 9.4. Transaction 1 dealing with value received, reduction of the number of shares and (optionally) commission

    AccountTot Shares(Unit) PriceBuy (Debit)Sell (Credit)
    Brokerage or Bank  Debit 
    Commission (optional)  Debit 
    Security-NumSoldSaleUnitPrice SaleValue

    Table 9.5. Transaction 2 capital gain/loss (loss in this example)

    AccountTot Shares(Unit) PriceBuy (Debit)Sell (Credit)
    Capital Loss  Debit 
    Security00 Credit

  4. The automatic capital gains calculations can handle straightforward buy, sell, and return of capital transactions but any transaction that affect the number of shares, even simple splits, will cause it to produce wrong answers so those cases must be handled manually.

9.7.3.9. Example of Manual Lot Creation and Linking

Here is an example of selling part of a security holding using the LIFO method. In this example, the most recent buy transaction (dated 01/07/2016, a reinvested dividend), is linked to a lot, along with the sell transaction, and the GnuCash scrub function is used to calculate capital gain or loss and create the capital gain/loss transaction.

  1. Open the security account's register.

    Figure 9.2. Selling Shares - Capital Gains - Security register before scrubbing a single lot

    Example of security register before lot scrubbing

    An image of the Security Account register in transaction journal view, before lot scrubbing.


  2. Ensure all previous capital gain/loss transactions are separate transactions to the sell transactions which record the reduction in the number of shares and the value received.

  3. Select ActionsView Lots to open the Lots in Account SSSS window where SSSS is the security account.

    Figure 9.3. Selling Shares - Capital Gains - Lots before scrubbing a single lot

    Example of Lots in Account window before lot scrubbing

    An image of the Lots in Account window before lot scrubbing.


  4. Create a new lot using the New Lot button. Initially this lot is not linked to any buy or sell split.

  5. Highlight the new lot in the Lots in This Account panel.

  6. Highlight the buy split (dated 01/07/2016) of the security to be sold in the Splits free panel.

  7. Click the >> button to link the buy split with the highlighted lot. The split moves from the Splits free panel to the Splits in Lot panel.

  8. Repeat the previous 2 steps for any other buy splits that should be included in the lot (in this example, there is only 1 buy split in the sale).

  9. Highlight the sell split in the Splits free panel.

  10. Click the >> button to link the sell split with the highlighted lot.

  11. Check the lot Balance is as expected. I.e. in this example the lot balance should be zero as the number of securities sold in the lot, is matched with the same number of security buys.

    Figure 9.4. Selling Shares - Capital Gains - Lots before scrubbing a single lot, after manual linking

    Example of Lots in Account window before scrubbing a single lot

    An image of the Lots in Account window before scrubbing a single lot.


  12. Click the Scrub button (NOT the Scrub Account button).

    The Lots in Account window has not changed after using the Scrub button so no example screen image is supplied.

  13. Continue to Section 9.7.3.6, “Change Orphaned Gains-CCC to Gain/Loss Account”

9.7.3.10. Example 1 of Automatic Creation of Capital Gain Or Loss Transactions

Create the capital gains transaction by following these steps:

  1. Open the security account's register.

  2. Ensure the sell transaction has been entered with no splits for capital gains or losses.

  3. Select ActionsView Lots to open the Lots in Account SSSS window where SSSS is the security account.

  4. If using LIFO, use the above procedure Section 9.7.3.4, “Manual Lot Creation and Linking” to create a lot for each sell transaction, link the lot with the sell transaction and each of the buy transactions that make up the sale.

  5. Click the Scrub Account button which:

    • Creates lots for any buy transactions that are not already linked to a lot and links them to sell transactions splits using the FIFO method. As a transaction split can only be linked to 1 lot, if a sell transaction needs to be linked to multiple lots, the sell transaction split is itself split into multiple subsplits. In the case of multiple subsplits, it is possible to have different splits from the same transaction in both the Splits free and Splits in lot panels.

    • Creates a separate transaction per lot for capital gain/loss.

      Note

      If there are already gain/loss splits as part of a sell transaction, GnuCash scrubbing does not recognize them. So if manually adding gain/loss, separate gain/loss transaction splits from the transaction that reduces the No of Shares) by putting them in a separate transaction if you may use scrubbing in future.

  6. Continue with the instructions in the next section.

9.7.3.11. Example 2 of Automatic Creation of Capital Gain Or Loss Transactions - 2 Sales at Once

Here is an example of FIFO scrubbing without manual lot creation. In this example, the transactions for 2 sales are scrubbed at once but usually scrubbing would be performed after each sale. One reason for scrubbing 2 sales at once, could be because there were multiple sales on the same day.

Figure 9.5. Selling Shares - Capital Gains - Register before Scrub Account

Example of security register before Scrub Account is used.

An image of the Security Account register in transaction journal view, before Scrub Account is used.


  1. Select ActionsView Lots to open the Lots in Account SSSS window where SSSS is the security account.

    Figure 9.6. Selling Shares - Capital Gains - Lots before Scrub Account

    Example of Lots in Account window before Scrub Account is used.

    An image of the Lots in Account window before Scrub Account is used.


  2. Click the Scrub Account button.

    Figure 9.7. Selling Shares - Capital Gains - Lots after Scrub Account

    Example of Lots in Account window after using Scrub Account

    An image of the Lots in Account window after using Scrub Account.


    Note

    After using the Scrub Account button only the last lot is shown, so the above image is after the Lots in Account window has been closed and reopened so all the lots show.

  3. Close the Lots in Account SSSS window and return to the security account register.

    Figure 9.8. Selling Shares - Capital Gains - Register after Scrub Account

    Example of security register after using Scrub Account

    An image of the security register after using Scrub Account.


    Note

    The security splits in the sell transactions have been split into subsplits, one subsplit per lot, and a capital gain transaction has been created for each security subsplit of each sell transaction.

  4. Continue to Section 9.7.3.6, “Change Orphaned Gains-CCC to Gain/Loss Account”

9.7.3.12. Example 3 of Automatic Creation of Capital Gain Or Loss Transactions - After a Simple Stock Split

Here is an example of FIFO scrubbing without manual lot creation/linking, where the Stock Split Assistant has been used for a simple stock split. In this example, 100 shares of security XYZ were bought for $10.00 each, there was a simple 2 for 1 stock split for zero cost (so the holding was then 200 shares @ $5.00 each), then all 200 shares were sold for $6.00 each.

Figure 9.9. Selling Shares - Capital Gains - Register after Scrub Account

Example of security register after Scrub Account is used.

An image of the Security Account register in transaction journal view, after Scrub Account is used.


Figure 9.10. Selling Shares - Capital Gains - Lot 0 after Scrub Account

Example of security register after Scrub Account is used.

An image of the Security Account register in transaction journal view, after Scrub Account is used.


Figure 9.11. Selling Shares - Capital Gains - Lot 1 after Scrub Account

Example of security register after Scrub Account is used.

An image of the Security Account register in transaction journal view, after Scrub Account is used.


The above screen shots show that scrubbing created:

2 lots. A separate lot for each buy (it essentially treats the stock split as a buy of 100 for no cost)

2 capital gain transactions (one for each lot) on the date of the sale:

  • Lot 0: 1/7/2009 loss $400 (sale $600 - cost $1000)

  • Lot 1: 1/7/2009 gain $600 (sale $600 - cost $0)

Total gain $200 is correct. Whether the gain is a single long-term one or one each of long-term and short-term or whether there's even a distinction depends on the user's tax jurisdiction and the way the split is structured. If the user needs help figuring it out they should consult a professional.

9.7.3.13. Example of Changing the Orphaned Gains-CCC to Gain/Loss Account

  1. Close the Lots in Account SSSS window if open and return to the security account register.

    Figure 9.12. Selling Shares - Capital Gains - Register after scrubbing a single lot

    Example of security register after scrubbing a single lot

    An image of the security register after scrubbing a single lot.


  2. Find each new Realized Gain/Loss transaction in the security account register (they will have the same date as the sell transactions). Edit the Orphaned Gains-CCC transaction split to re-assign the income account to a more meaningful income (or expense) gain or loss account (e.g. Income:Long Term Capital Gain:IBM).

    Tip

    You may like to split the capital gain/loss into taxable and non taxable parts if that is in accord with your tax laws.