FIFO is one way to do it. But it's not really the best way.
For ordinary equities and ETFs, in an ordinary taxable account (tax-advantaged accounts like IRAs have different rules, as to different kinds of investments) the IRS also allows cost basis to be reported by specific identification. A lot is a group of shares you bought in one transaction. A lot is identified by the number of shares, the price you paid for them, and the date the trade executed. When you sell, you specify from which lot(s) you are selling, and the cost basis of that specific lot is used to calculate your gain or loss.
When using specific identification, you can sell whatever lots you want. The IRS doesn't care, as long as you don't do inconsistent things like share the same shares twice. But a common strategy is to sell lots in this order:
- Short term losses (held less than 1 year), with greatest losses first
- Long term losses
- Short term lots with no losses or gains
- Long term lots with no losses or gains
- Long term gains, least gain first
- Short term gains, least gain first
The objective of this strategy is to maximize tax deductions by realizing losses, and if gains must be realized, realizing them as long term capital gains as much as possible, as these are taxed at a lower rate. This is a more tax-efficient strategy than FIFO.
It's likely your broker can automatically perform this strategy for you (although you may have to ask for it), and the 1099-B and other tax documentation they send you at the end of the year will reflect this. These days any electronic tax package can automatically import all your trades from your broker so you don't have the tedium of entering all the data.
Since FIFO requires tracking all the lots anyway, and in practice computers automate all the tedium involved with a more tax-efficient cost basis method, I can't think of any reason to use FIFO.
The gritty details, if you want them, are in IRS publication 550.