{
  "id": "cost-basis",
  "version": "46e6311da261",
  "status": "published",
  "name": "Cost Basis Calculator",
  "question": "What is my cost basis?",
  "summary": "Works out the cost basis of shares bought in several lots with commissions, the average cost per share, and the basis and gain of shares you sell by first in, first out or by average cost.",
  "category": "finance",
  "subcategory": "investing",
  "url": "https://www.acalculator.org/finance/cost-basis-calculator",
  "markdown": "https://www.acalculator.org/finance/cost-basis-calculator.md",
  "kind": "function",
  "method": "basis = Σ(shares × price + fees); average = basis ÷ shares; FIFO basis sold = whole oldest lots, then (shares taken ÷ lot shares) × lot cost; average basis sold = shares sold × average; gain = shares sold × sale price − sale fees − basis sold.",
  "assumptions": [
    "The purchases are listed oldest first. FIFO takes shares from the top of the list.",
    "A lot’s fees are part of its cost, so a share of a lot carries its share of the fees.",
    "Stock splits, reinvested dividends, wash sales and gifted or inherited shares need other adjustments and are not included.",
    "Your broker reports your basis on Form 1099-B; this is an estimate, not tax advice."
  ],
  "inputs": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "lots": {
        "title": "Purchases",
        "description": "Each purchase in the order you bought: the shares, the price per share, and the fees paid.",
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "s": {
              "title": "Shares",
              "description": "Shares bought in this purchase. Fractional shares are fine.",
              "type": "number",
              "minimum": 0,
              "maximum": 1000000000
            },
            "p": {
              "title": "Price per share",
              "description": "The price paid for each share.",
              "type": "number",
              "x-unit": "USD",
              "minimum": 0,
              "maximum": 10000000
            },
            "f": {
              "title": "Fees",
              "description": "Commissions and fees for the whole purchase.",
              "type": "number",
              "x-unit": "USD",
              "minimum": 0,
              "maximum": 1000000000
            }
          }
        }
      },
      "sold": {
        "title": "Shares sold",
        "description": "Optional: how many of the shares you sell.",
        "type": "number",
        "minimum": 0,
        "maximum": 20000000000
      },
      "sale": {
        "title": "Sale price per share",
        "description": "Optional: the price you sell each share at, for the gain or loss.",
        "type": "number",
        "x-unit": "USD",
        "minimum": 0,
        "maximum": 10000000
      },
      "salefee": {
        "title": "Fees on the sale",
        "description": "Commissions and fees for the whole sale.",
        "type": "number",
        "x-unit": "USD",
        "minimum": 0,
        "maximum": 1000000000
      }
    }
  },
  "outputs": {
    "basis": {
      "label": "Total cost basis",
      "description": "Σ(shares × price + fees) over every purchase.",
      "format": "money"
    },
    "shares": {
      "label": "Shares bought",
      "description": "All the shares bought, added up.",
      "format": "number"
    },
    "average": {
      "label": "Average cost per share",
      "description": "The total cost basis ÷ the shares bought.",
      "format": "money"
    },
    "fifo": {
      "label": "Basis of the shares sold (FIFO)",
      "description": "The basis of the shares sold, taken from the oldest purchases first.",
      "format": "money"
    },
    "avgSold": {
      "label": "Basis of the shares sold (average cost)",
      "description": "Shares sold × the average cost per share.",
      "format": "money"
    },
    "left": {
      "label": "Basis of the shares kept (FIFO)",
      "description": "The total cost basis minus the FIFO basis of the shares sold.",
      "format": "money"
    },
    "proceeds": {
      "label": "Sale proceeds",
      "description": "Shares sold × sale price − fees on the sale.",
      "format": "money"
    },
    "gainFifo": {
      "label": "Gain or loss (FIFO)",
      "description": "Sale proceeds − the FIFO basis of the shares sold. Negative for a loss.",
      "format": "money"
    },
    "gainAvg": {
      "label": "Gain or loss (average cost)",
      "description": "Sale proceeds − the average-cost basis of the shares sold.",
      "format": "money"
    }
  },
  "defaultAnswer": {
    "inputs": {
      "lots": [
        {
          "shares": 100,
          "price": "50",
          "fee": "5"
        },
        {
          "shares": 50,
          "price": "60",
          "fee": "5"
        },
        {
          "shares": 50,
          "price": "40",
          "fee": "0"
        }
      ],
      "sold": 120,
      "sale": 70
    },
    "outputs": {
      "basis": 10010,
      "shares": 200,
      "average": 50.05,
      "fifo": 6207,
      "avgSold": 6006,
      "left": 3803,
      "proceeds": 8400,
      "gainFifo": 2193,
      "gainAvg": 2394
    },
    "text": "Your cost basis is $10,010.00 for 200 shares, $50.0500 a share."
  },
  "examples": [
    {
      "given": {
        "lots": [
          {
            "shares": 100,
            "price": 50,
            "fee": 5
          },
          {
            "shares": 50,
            "price": 60,
            "fee": 5
          },
          {
            "shares": 50,
            "price": 40,
            "fee": 0
          }
        ],
        "sold": 120,
        "sale": 70
      },
      "expect": {
        "basis": 10010,
        "shares": 200,
        "average": 50.05,
        "fifo": 6207,
        "avgSold": 6006,
        "left": 3803,
        "proceeds": 8400,
        "gainFifo": 2193,
        "gainAvg": 2394
      },
      "source": "hand calculation in content.mdx; Internal Revenue Service, Topic no. 703, Basis of assets (the basis of stock you buy is the purchase price plus commissions and recording or transfer fees), https://www.irs.gov/taxtopics/tc703 (retrieved 2026-10-03); Internal Revenue Service, Publication 550, Investment Income and Expenses (shares not adequately identified: the basis of the shares acquired first; average basis for mutual fund and dividend reinvestment plan shares), https://www.irs.gov/publications/p550 (retrieved 2026-10-03)"
    },
    {
      "given": {
        "lots": [
          {
            "shares": 10,
            "price": 33.33
          },
          {
            "shares": 20,
            "price": 41.1
          }
        ]
      },
      "expect": {
        "basis": 1155.3,
        "shares": 30,
        "average": 38.51
      },
      "source": "hand calculation in content.mdx: 333.30 + 822 = 1,155.30; ÷ 30 = 38.51 exactly; Internal Revenue Service, Topic no. 703, Basis of assets (the basis of stock you buy is the purchase price plus commissions and recording or transfer fees), https://www.irs.gov/taxtopics/tc703 (retrieved 2026-10-03)"
    },
    {
      "given": {
        "lots": [
          {
            "shares": 40,
            "price": 25,
            "fee": 4.95
          },
          {
            "shares": 60,
            "price": 20,
            "fee": 4.95
          }
        ],
        "sold": 100,
        "sale": 18,
        "salefee": 4.95
      },
      "expect": {
        "basis": 2209.9,
        "fifo": 2209.9,
        "avgSold": 2209.9,
        "left": 0,
        "proceeds": 1795.05,
        "gainFifo": -414.85
      },
      "source": "hand calculation in content.mdx; Internal Revenue Service, Publication 550, Investment Income and Expenses (shares not adequately identified: the basis of the shares acquired first; average basis for mutual fund and dividend reinvestment plan shares), https://www.irs.gov/publications/p550 (retrieved 2026-10-03)"
    }
  ],
  "sources": [
    "Internal Revenue Service, Topic no. 703, Basis of assets. https://www.irs.gov/taxtopics/tc703 (retrieved 2026-10-03)",
    "Internal Revenue Service, Publication 550, Investment Income and Expenses: identifying stock sold, and the average basis method for mutual fund and dividend reinvestment plan shares. https://www.irs.gov/publications/p550 (retrieved 2026-10-03)"
  ],
  "related": [
    "stock-average",
    "stock-profit",
    "capital-gains-tax"
  ],
  "changelog": []
}
