DEV Community

Huzaifa Zahoor
Huzaifa Zahoor

Posted on

Crypto Tax Loss Harvesting for Developers: Find Your Losing Lots with a Few Lines of Python

If you hold crypto and you write code, tax season is mostly a data problem. Which coins did you buy, when, at what price, and which of those lots are worth less today? Answer that cleanly and one of the more useful year-end moves, tax loss harvesting, gets a lot less scary.

This post walks through the idea in plain terms and then shows a tiny script for spotting harvestable losses in your own records. For the full rules, limits, and a worked example, see this guide to crypto tax loss harvesting in the US.

The idea in one paragraph

In the US, the IRS treats crypto as property. When you sell, swap, or spend it, you realize a gain or loss: what you got minus your cost basis. A coin that is down on paper gives you nothing to deduct. Harvesting means deliberately realizing that loss, usually by selling, so it offsets gains elsewhere. Losses beyond your gains can reduce up to $3,000 of ordinary income a year ($1,500 if married filing separately), and the rest carries forward to later years.

Two details trip people up:

  • Crypto-to-crypto swaps count as disposals. Swapping one token for another can realize a loss (or a gain) just like selling for dollars.
  • It mostly defers tax. If you buy back in, your new basis is lower, so a later sale may show a bigger gain.

Why this is really a data problem

Which units you sell decides how big your loss is. IRS FAQs allow specific identification of units at the time of sale, or a standing order with your broker; otherwise a wallet-by-wallet first-in, first-out default applies. So if you bought the same coin at three different prices, selling the most expensive lot first gives the biggest harvestable loss.

That only works if your lot data is trustworthy. The classic failure point is wallet-to-wallet transfers, where cost basis quietly goes missing. Brokers now report on Form 1099-DA, but for 2026 and later basis is generally reported only for coins bought after 2025 and kept with that same broker. Coins moved in from elsewhere can show up with no basis at all, and self-custody or DeFi activity still has to be reported by you.

A minimal lot scanner

Here's a small sketch. Export your buys to a CSV with asset,date,qty,cost_usd columns, plug in current prices, and it lists every lot that is under water, sorted by the size of the loss, with a short-term or long-term flag.

import csv
from datetime import date

prices = {"ETH": 2400.0, "SOL": 120.0}  # fill in current prices yourself
today = date.today()

rows = []
with open("lots.csv") as f:
    for r in csv.DictReader(f):
        qty, cost = float(r["qty"]), float(r["cost_usd"])
        bought = date.fromisoformat(r["date"])
        value = qty * prices[r["asset"]]
        pnl = value - cost
        if pnl < 0:
            term = "long" if (today - bought).days > 365 else "short"
            rows.append((pnl, r["asset"], r["date"], term))

for pnl, asset, bought, term in sorted(rows):
    print(f"{asset:<5} bought {bought}  {term}-term  unrealized {pnl:,.2f} USD")
Enter fullscreen mode Exit fullscreen mode

The prices above are placeholders, not real quotes. The holding-period flag matters because short-term losses (held a year or less) offset short-term gains first, and long-term losses offset long-term gains first, before crossing over. The day count is a rough cut, so check edge cases against your own records.

The wash-sale question, carefully

For stocks, the wash-sale rule blocks a loss if you buy back substantially identical securities within 30 days. As of October 5, 2026, that rule covers stock and securities, and ordinary crypto held directly like bitcoin or ether generally isn't caught by it. Spot crypto ETF shares are different: treat them as covered.

There's a catch worth knowing. A bill introduced in September 2026, H.R. 10357, would extend the rule to traded digital assets for sales after September 14, 2026, if it is enacted as written. It has cleared a House committee but isn't law. So "sell and immediately rebuy" now carries real uncertainty. The full guide covers where that bill stands.

Takeaways

  • Losses only count once realized, and the sale has to happen in the tax year you want the loss in, which for most people means by December 31.
  • Your edge is clean data: dated lots, amounts, and dollar values, especially across wallet transfers.
  • Check your exchange's cost-basis or lot-selection settings before you sell.
  • Don't sell just for the tax break; fees, spreads, and a bad investment call can outweigh the saving.
  • Canada works differently, with a superficial loss rule and no $3,000-style income deduction.

Read the full guide on NutshellCrypto

This is educational content, not tax or financial advice. Tax rules and pending legislation change, so check current IRS guidance or a tax professional before acting.

This post was written with AI assistance.

Top comments (0)