liveBook · Manning

Realized gain or loss

As explained in Build a Robo-Advisor with Python (From Scratch)

A gain on an investment that becomes recognized for tax or accounting purposes when the investment is disposed of.

A realized gain or loss arises when an investment is disposed of for more or less than its cost basis. Realization matters because the result can affect taxable income and capital-gains taxes. Selling an appreciated asset creates a realized gain; selling a depreciated asset creates a realized loss that can offset realized gains or ordinary income. section 4.2.3 section 14.0

Index rebalancing and a heartbeat trade

Suppose an ETF holds $500,000 of stock A with a $300,000 cost basis. Selling A to rebalance would create a $200,000 realized capital gain. Instead, a heartbeat trade can exchange newly contributed securities for stock A without selling it, aligning the portfolio while avoiding that taxable event. section 4.2.3

Table 4.3 Holdings and cost basis
A B C D E
Portfolio value $500,000 $300,000 $200,000 $0 $0
Cost basis $300,000 $225,000 $150,000 $0 $0

Use the holdings and cost-basis table to calculate the realized gain produced by selling each position. section 4.2.3

Realization creates several practical trade-offs:

  • Tax-loss harvesting deliberately sells depreciated assets, then reinvests the proceeds in similar but not identical assets to preserve portfolio exposure. section 14.0
  • A harvested loss can defer taxes rather than eliminate them: replacing a $100,000 position with one bought for $95,000 produces a larger future gain when the replacement reaches $150,000. section 14.1.1
  • The economic value of deferral depends on the payment horizon T and discount rate r: x(1 - (1 + r)^(-T)), where x is the deferred tax liability. section 14.1.1

How Build a Robo-Advisor with Python (From Scratch) covers it

How deep it goes
Explains disposal, cost basis, tax effects, and deferral valuation with numerical examples, but does not develop complete implementation details.
Examples
Uses ETF rebalancing, a $200,000 gain, a $95,000 replacement purchase, and a future $150,000 sale to quantify consequences.
What it assumes
Assumes familiarity with investments and taxable accounts while teaching cost basis, disposal, realized results, and tax-loss harvesting mechanics through examples.
What you can do after
Evaluate tax consequences and deferral benefits by comparing sale proceeds, cost basis, realized gains or losses, timing, and discount rates.
How it connects
Connects realized gains and losses to ETFs, mutual funds, cost basis, tax-loss harvesting, capital-gains taxes, and tax deferral.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Robo-Advisor with Python (From Scratch). The section links point to the parts of the book it draws on.