liveBook · Manning

Closed tax lot

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

A closed tax lot is a tax lot whose remaining share quantity has reached zero after disposal.

A closed tax lot is a tax lot whose remaining share quantity has reached zero after disposal. Unlike an ordinary position update, a tax-loss-harvesting backtester retains closed lots so later wash-sale checks can examine their sell date, sell price, and realized loss. section 14.4.1 section 14.2.2

Representing a closed lot requires preserving its historical state:

  • Store the original ticker, purchase price, quantity, and purchase date. section 14.2.2
  • Record the sell price and sell date instead of discarding the row when its quantity reaches zero. section 14.4.1 section 14.2.2

A closed VTI lot

A VTI lot purchased at $90 is sold at $75 on 2022-04-01. Its sell price and sell date remain in the tax-lot table, and its still_held status becomes false. The record can therefore document the realized loss and help determine whether a later VTI purchase creates a wash sale. 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],
                  })

Inspect a pandas DataFrame containing two open VTI lots and one closed lot with recorded sale details. section 14.2.2

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

How deep it goes
Implements closed-lot storage, eligibility flags, remaining-quantity calculations, and trade-update paths rather than only defining the term.
Examples
Uses VTI DataFrames containing open and closed lots, recorded sale fields, and subsequent purchase restrictions.
What it assumes
Assumes tax-lot records and pandas DataFrames, while deriving sold status and remaining quantities from recorded trade fields.
What you can do after
Operate a tax-lot DataFrame that retains disposed lots and supports wash-sale checks after sales and replacement purchases.
How it connects
Connects tax lots with wash sales, tax-loss harvesting, rebalancing, and cost-basis adjustments.

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.