๐ฌ FIFO Lot Analysis
FIFO lot analysis is the per-lot complement to Weighted Average Cost (WAC).
WAC answers: "What is my blended average cost for this position?" FIFO lot analysis answers a different question: "How is each individual purchase batch performing through time?"
Instead of merging all acquisitions into one pool, LibreFolio tracks each lot through its own lifecycle โ open, partially closed, fully closed โ and matches sells in FIFO order (first in, first out).
Complement, not replacement
WAC is aggregate and position-level. FIFO lot analysis is granular and lot-level. Both views are useful: one for blended cost basis, one for economic attribution lot by lot.
๐ก What is FIFO Lot Analysis?
A lot is one acquisition batch: for example, one BUY of 100 shares, or one transfer-in that preserves historical cost basis.
When a SELL occurs, the oldest still-open lots are closed first. This creates a lot-by-lot history:
- how much of each lot is still open
- how much has already been sold
- how much sale proceeds that lot has generated
- how much income was earned while that lot was held
- how much return came from price change versus cash income
This makes FIFO lot analysis especially useful when two positions in the same asset were bought at very different prices or dates.
The Lot Life & Custody timeline above makes the lifecycle visual: each bar is one lot, colored by the broker currently holding it, with thickness proportional to the quantity still held in that segment. A bar that ends mid-chart is a fully closed lot; a bar reaching "today" is still open.
๐งฎ Open Return per Lot
Open Return isolates the price-only move of a lot relative to its opening reference price.
\(\text{MarketPrice}\) is the asset's mark from the unified resolver: on the end date for the lot summary, on each day for the return history. The reference depends on what opened the lot:
- A trade โ a BUY, or a SELL that opens a short lot: the lot's own unit price, even when the asset has a market quote that day. A trade is a price.
- An adjustment โ an opening position, a succession, a transfer from an untracked broker: its cost basis is a cost, not a price, so the reference is the asset's mark on the opening day. That is the price of the last day on or before it that has one โ its market quote or, without a quote, the average price of its BUY and SELL trades, never an adjustment โ carried forward. For an asset with nothing else yet, the cost of an incoming transfer is such a price too, as an estimate: a transfer from a broker outside the scope, on an asset with no quote and no other operation on or before its day, gets its own cost as its reference, flagged as a fallback (
REFERENCE_PRICE_FALLBACK). - An adjustment before any price โ no quote, BUY, SELL or transfer cost estimate on or before its opening day: the lot's own unit price, its per-unit cost basis. Without a cost basis that price is zero, and the lot has no Open Return.
reference_price_source is exact for a market quote of the opening day itself and for the lot's own price, and fallback for a quote carried from an earlier day or a price derived from trades. The data-quality banner reports such a fallback (REFERENCE_PRICE_FALLBACK), and an adjustment with no price on or before its opening day (REFERENCE_PRICE_UNAVAILABLE).
Each price converts into the target currency at the rate of its own day: \(\text{MarketPrice}\) at the rate of the day it values, the reference at the rate of the opening date, even when it was carried from an earlier quote or trade. For a price in another currency than the target, Open Return therefore includes the exchange-rate move since the opening.
This metric excludes dividends, interest, and realized sale proceeds. It answers: "How much has market price moved since this lot was opened?"
A lot received by succession
An asset is quoted at 50 on 7 March and bought at 52 on 9 March, with no quote since. A lot received by succession on 10 March has a reference of 52: the BUY is the asset's most recent price on or before that day, carried forward. Its reference_price_source is fallback, since 52 is not a quote of 10 March; a quote on 10 March itself would be the reference, with exact.
The lot's own price, on the quote axis
When the reference is the lot's own price, LibreFolio scales it to the asset's quote convention. This avoids misleading percentage returns on instruments quoted per 100 nominal units. A mark from the resolver is already on that axis.
The reference is then \(\text{OpeningUnitPrice}\times qbq\). For qbq = 100, a bond bought at 0.992 compares against the market quote axis as 99.20, not 0.992.
The WAC / Market Price chart plots each lot as a bubble against the market price line: bubble color marks the broker where the lot was opened, bubble size scales with the lot's opening value. A lot whose current value is estimated at cost โ its asset has no price at all on the end date, see Estimated-at-Cost โ is drawn with a dashed outline.
๐ฐ Total Return per Lot
Total Return is broader than Open Return. It includes the lot's remaining market value, any sale proceeds already realized from that lot โ an outgoing adjustment adds none, having no value โ and any allocated income received while the lot was held.
LibreFolio's lot math uses these exact building blocks:
Every amount is in the analysis's target currency. For a lot bought or sold in another currency, each closure converts its two legs at their own dates โ see Lots in Another Currency.
For the scalar lot summary, the return percentage is:
For return history through time, LibreFolio uses:
This answers: "What is full economic return of this lot, including both price movement and cash yield?"
The Value / Return comparison chart, switched to Return mode, plots exactly this percentage โ one line per lot, each measured from its own opening date, over the currently selected lot set.
๐ฑ Lots in Another Currency
A closure's two legs convert into the target currency each in its own currency, at its own date: the cost of the units in the lot's currency at the lot's opening date โ the purchase rate โ and the closing price in the closing transaction's currency at the closing date โ the sale rate. With \(\text{Convert}(x, ccy, D)\) the amount \(x\) in currency \(ccy\) converted into the target currency at the rate of date \(D\), a closure of \(q\) units of a LONG lot opened on \(D_{\text{open}}\) at \(\text{LotUnitCost}\) in \(ccy_{\text{lot}}\), and closed on \(D_{\text{close}}\) at \(\text{ClosePrice}\) in \(ccy_{\text{close}}\), gives:
\(\text{ClosePrice}\) is a sale's unit price, in the sale's currency, or zero, in the lot's currency, for an outgoing adjustment, which has no value (see Outgoing Adjustments). \(ccy_{\text{close}}\) need not be \(ccy_{\text{lot}}\): a lot moved to a broker of another currency and sold there converts the sale from that currency. A SHORT lot mirrors the rule: it realizes the proceeds of its opening sale, \(\text{Convert}(q \cdot \text{LotUnitCost}, ccy_{\text{lot}}, D_{\text{open}})\), minus the cost of the purchase that closes it, \(\text{Convert}(q \cdot \text{ClosePrice}, ccy_{\text{close}}, D_{\text{close}})\).
When a lot is bought and sold in one currency, with rates \(r_{\text{open}}\) and \(r_{\text{close}}\) on the two dates, its realized result splits into the price gain at the sale rate and the exchange-rate move on the cost of the units sold:
The exchange-rate move between purchase and sale is therefore realized with the units, as on the Dashboard, where a sale's proceeds convert at its date and the units leave at their historical cost (see WAC). A missing rate leaves the closure's realized P&L unknown, and with it the lot's \(\text{RealizedPnL}\): it is never computed from the native amounts.
Every closure's cost converts at the opening date, like \(\text{OriginalCost}\), so a LONG lot's market P&L is the value of its open units minus their cost at the purchase rate:
A fully closed LONG lot has no market P&L: its closures' costs add up to its \(\text{OriginalCost}\), and its whole P&L is realized.
A dollar lot, target currency EUR
Buy 1 unit at 100 USD when 1 USD = 1.00 EUR; sell it at 110 USD when 1 USD = 0.90 EUR.
The price gain, \(10 \times 0.90 = +9\) EUR, is outweighed by the dollar's fall on the cost, \(100 \times (0.90 - 1.00) = -10\) EUR. Converting the native realized P&L, 10 USD, at the sale rate would instead report \(+9\) EUR realized and \(-10\) EUR of market P&L on a lot no longer held; the total, \(-1\) EUR, is the same either way.
โ๏ธ qbq Scaling
Some instruments are quoted per base quantity, not per single unit. LibreFolio calls this base quantity qbq (quote_base_quantity).
- For most stocks,
qbq = 1 - For many bonds,
qbq = 100
The exact valuation rule is:
qbq scaling matters
Suppose a bond has face quantity 1,000 and is quoted at 101.50 per 100 nominal.
qbq = 100- lot quantity =
1,000 - market value =
(1,000 / 100) ร 101.50 = 1,015.00
If you compare 101.50 directly with a per-single-unit cost basis such as 0.992, you get nonsense because the two numbers live on different scales.
The correct comparison rescales the lot cost onto the market quote axis:
So the meaningful price comparison is 101.50 vs 99.20, not 101.50 vs 0.992.
Without this scaling, bond returns and valuations can be off by orders of magnitude.
๐ Estimated-at-Cost
Each day, a lot is valued at the asset's mark from the unified resolver: a market quote, or the average price of a day's BUY and SELL trades, carried forward. An adjustment is never a price, with or without a cost basis; the cost of an incoming transfer is one only as an estimate, while the asset has no quote and no other operation on or before that day. A day with no price at all โ no market quote, no BUY or SELL and no such estimate on or before it โ does not fail the analysis: LibreFolio values the still-open portion of a LONG lot at cost that day, the day-by-day rule the Dashboard applies to a holding with no price:
with \(\text{OpeningValue}\) the lot's original cost converted at its opening date. The choice is made day by day, with no look-ahead. A price, once observed, is carried forward, so these days all come before the asset's first quote, BUY, SELL or transfer cost estimate: they run from the first transaction of the analysis to the day before that first price, or to the end date when the asset has none โ and there are none when the asset already has a price on the day of the first transaction.
- Before the first price. A lot opened before the asset's first price โ by an adjustment, for example โ is valued at cost from its opening to the day before that price, even when the asset is priced later: its value line stays flat at cost, with no market gain or loss, then follows the price.
- No price at all. An asset with no quote, BUY, SELL or transfer cost estimate up to the end date โ one held only through adjustments, or through transfers without a cost basis โ is valued at cost on every day, and the lot summary reports
value_source = ESTIMATED_AT_COST. With a price on or before the end date, the summary values the lot at that price instead:value_source = MARKET_PRICE, the label of any mark, quote, trade or transfer cost estimate. An asset that arrived only by transfers with a cost basis therefore has no day at cost: from its first transfer it is valued at that cost, converted at each day's exchange rate, and the price series have a point for it, flagged as estimated. - A price without a rate. A day whose mark has no exchange rate into the target currency is not valued at cost: a still-open lot has no value that day, and on the end date the lot summary has no value and no
value_source. - An adjustment without a cost basis. An adjustment with no cost, such as a gift or a spin-off, opens a zero-cost lot, and that lot is worth 0 on the days valued at cost until a price arrives. It counts at zero on those days, while the Dashboard's average cost treats that cost as unknown and leaves the holding unvalued. Such a row is rare: a positive adjustment can be saved without a cost basis only while it is linked to a split, and then opens no lot, so it comes from an older version, or from one whose split event was later changed to a price adjustment.
Practical implication, on the days valued at cost:
- the lot still shows residual value
- already realized proceeds still remain visible
- allocated dividends or interest still remain visible
- unrealized volatility is temporarily understated
market_pnl = 0, and no Open Return: there is no market price to compare with the reference- no ROI or TWRR in the lots performance history, which reads only days with a price
Interpretation
Estimated-at-cost is conservative operational fallback. It means: "We know what you paid, but we do not currently know what market would pay."
The data-quality warning CURRENT_PRICE_ASSUMED_AT_COST โ "No market price available between {date_from} and {date_to} โ open value estimated at cost, market P&L excluded" โ names exactly those days: from the first transaction to the day before the asset's first price, or to the end date when the asset has none. It is raised whenever such days exist and the lot summary or the value, return, price or performance history is requested.
๐ธ Income Allocation Across Lots
Dividends and interest linked to an asset are allocated pro-rata across the LONG lots that are eligible as of the day before the income date (D-1), and only across lots held at the paying broker.
Exact allocation rule:
Where:
- \(I\) = income amount received on date \(D\)
- \(\text{Convert}(I, ccy, D)\) = income converted into target currency on date \(D\)
- \(\text{EligibleQty}_i(D)\) = quantity of lot \(i\) open at the paying broker on \(D-1\) (quantity that left that broker in transit still counts as originating there)
- only LONG lots participate in the denominator
The D-1 rule keeps the day of record clean: a purchase made on the income date does not earn that distribution, and a lot sold the day before does not either. Larger eligible lots receive a larger share; lots held at other brokers, or not yet (or no longer) eligible, receive nothing.
Income keeps its sign: a negative correction of a dividend or interest payment lowers the lot's income, as it does on the Dashboard.
Changed in FIFO v5
Earlier versions used the income date itself with all brokers (\(\text{OpenQty}_i(t)\) over every lot). The current engine uses D-1 eligibility scoped to the paying broker. If no lot is eligible, the income is kept as asset-level orphan income (never dropped, never assigned to the wrong lot).
Conservation rule
The allocated lot amounts add back exactly to the converted income event total. Income is distributed, not created.
The lot detail modal's Asset Income row is exactly \(\text{Income}_i\) from the formula above โ the pro-rata slice this specific lot received. When the lot's current value is estimated at cost โ its asset has no price at all on the end date โ, the same modal also shows the Estimated-at-Cost badge from the previous section.
๐ธ Costs & Net Metrics
Asset-linked FEE and TAX are allocated to lots with a deterministic operation-matching ladder, then
subtracted to produce net figures alongside the gross ones.
๐งญ Deterministic cost allocation
A cost pool (same broker, same day, same type) is matched to the first non-empty target in this order:
| Cost | Matching order |
|---|---|
FEE |
same-day trades โ previous-day trades โ open holdings โ asset orphan |
TAX |
same-day income โ same-day trades โ previous-day income โ previous-day trades โ open holdings โ asset orphan |
Within a matched trade the cost crosses to the very lots that trade touched โ a BUY's cost lands on the lot it opened, a SELL's cost lands on the FIFO-consumed lots โ so cost attribution never contradicts the FIFO matching itself. Amounts are converted to the target currency and stored as positive magnitudes.
Conservation
Per pool, \(\sum_i \text{Cost}_i + \text{Orphan} = \text{Convert}(\text{pool}, ccy, D)\). A cost that finds no eligible lot (e.g. a fee booked after the position is fully closed) becomes asset-level orphan cost rather than being dropped or forced onto an unrelated lot.
โ๏ธ Gross vs net
With costs attributed per lot, LibreFolio reports both gross and net performance:
where \(\text{TotalPnL}_i\) already includes income (market P&L + realized P&L + asset income). The per-lot value-history series instead reports a capital-only net P&L, \(\text{pnl}_i - \text{Fees}_i - \text{Taxes}_i\), which excludes income โ each net line mirrors its own gross counterpart minus costs.
Annualized lot return uses the net return, not the gross one:
with \(d_i\) from opening date to closing date for closed lots, or to the analysis end date for open lots. Windows below 30 days return no annualized value; see Net Annualized Return.
Canonical numbers
BUY 10ร100, SELL 4ร120, current price 110, dividend 50, fees 8, taxes 5: Gross Total P&L \(= 60 + 80 + 50 = 190\); Net Total P&L \(= 190 - 13 = 177\); on opening value 1000 that is a 19% gross vs 17.7% net total return.
Costs with asset_id = null are not part of this lot-level view โ they are portfolio-level and handled
by the Portfolio Engine. See
Fee & Tax for the instrument-level theory.
๐ Worked Example
Example: two lots, one dividend, one market price
Assume same stock, same currency, qbq = 1.
| Date | Event | Lot A Open Qty | Lot B Open Qty | Notes |
|---|---|---|---|---|
| Jan 2 | BUY 100 @ $10 | 100 | 0 | Lot A opens with original cost $1,000 |
| Feb 10 | BUY 50 @ $14 | 100 | 50 | Lot B opens with original cost $700 |
| Mar 15 | DIVIDEND $30 | 100 | 50 | Both lots are still open |
| Apr 1 | Market price = $16 | 100 | 50 | Evaluate both lots |
Step 1 โ Allocate dividend pro-rata
Step 2 โ Open Return for each lot
Step 3 โ Market value and Total Return
Since no shares were sold yet, proceeds and realized P&L are both zero.
Step 4 โ Aggregate return across displayed lots
Even though both lots belong to same asset, their returns differ because they were opened at different prices.
๐ From Lots to Aggregate Metrics
Lot-level returns can be rolled up into an aggregate return series, but percentages must not be added directly.
LibreFolio uses this exact aggregate rule across displayed lots:
This lot-level view helps explain where return came from. Higher-level metrics such as ROI and TWRR answer broader portfolio questions:
- ROI focuses on gain relative to invested capital
- TWRR neutralizes external cash-flow timing
- FIFO lot analysis explains contribution and path inside a position
Price lookup is deliberately outside the FIFO engine itself. The engine produces lots and closures, and asks for a price only to give a lot opened by an adjustment its reference; LotsAnalysisService answers from the unified resolver (Price Resolution), then applies the same resolver and the estimated-at-cost fallback when deriving valuation metrics.
The Unified Lots Table lists exactly the per-lot rows \(i\) that the aggregate formulas above sum over โ opening date, total return, current value, custody, and status, all filterable to the same visible lot set used by the charts.
๐ Related
- ๐ Weighted Average Cost (WAC) โ blended cost basis view
- ๐ Buy & Sell โ brief FIFO matching overview
- ๐ธ Dividend & Interest โ source of asset-linked income events
- ๐ฐ Taxation โ capital gains and lot matching context
- โ๏ธ Lots Analysis Service โ developer implementation deep-dive