liveBook · Manning

Cost-basis adjustment

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

A cost-basis adjustment is a change to an investment's tax basis to account for a tax-relevant transaction such as a wash sale.

A cost-basis adjustment updates a tax lot’s recorded purchase price when a wash sale occurs. Rather than leaving the loss solely on the closed lot, the workflow marks washed lots and adds their basis to the replacement shares, preserving the tax-relevant history in the lot data. section 14.2.2

Replacing washed VTI shares

Two VTI lots are sold at losses, then 12 VTI shares are bought at $90. The new lot is recorded with a purchase price of $114.166667 because basis from washed shares is added. A three-share remainder from one prior lot remains separate and is not marked as a wash sale. section 14.2.2

An automatable lot-update workflow:

  • Create new tax lots from buy trades with ticker, price, quantity, date, and wash-sale fields. section 14.2.2
  • Identify blocking lots, limit washed shares to the replacement quantity, and process blocking lots in purchase-date order. section 14.2.2
  • Split lots when only part is affected, flag closed washed lots, and retain leftover shares as distinct lots. section 14.2.2
  • For sales, first close unblocked lots, then calculate remaining quantities and update lots involved in wash sales. section 14.2.2
import datetime as dt
import numpy as np
import pandas as pd

lots = \
    pd.DataFrame({'ticker': ['VTI', 'VTI', 'VTI'],
                  'purchase_price': [90, 85, 105],
                  'quantity': [25, 30, 20],
                  'purchase_date': ['2021-02-10', '2021-07-21',
                                    '2022-03-15'],
                  'sell_price': [75, np.NaN, np.NaN],
                  'sell_date': ['2022-04-01', np.NaN, np.NaN],
                  })

A minimal pandas tax-lot table supplies the ticker, purchase and sale data needed to calculate and store adjustments. section 14.2.2

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

How deep it goes
Implements purchase- and sale-driven adjustments, including partial lots, remaining quantities, and wash-sale flags.
Examples
Uses VTI loss sales, a $90 replacement purchase, 12 adjusted shares, and a three-share leftover lot.
What it assumes
Assumes familiarity with Python, pandas DataFrames, tax lots, and buy or sell trade records.
What you can do after
Build it from scratch in Python by creating, splitting, flagging, and updating tax lots around wash sales.
How it connects
Connects cost basis with wash sales, tax lots, tax-loss harvesting, buys, and sells.

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.