Average basis

by havoc

I can’t seem to write a Python function that will spit out the same
numbers for average cost basis that Vanguard puts on their statement.

My many Google attempts are not turning up any description of the average
tax basis algorithm (single or double category) that is precise enough
to implement. All of them parrot what publication 564 says,
but that publication only describes the simplest cases. If you sell
multiple times, sometimes with mixed short and long term basis, it is
very unclear to me what is supposed to happen. I’ve tried various
things that seem logical but none of them match Vanguard’s

Surely there’s at least an open source program that implements this
algorithm, but I’m not finding that either.

How can there be nobody on the whole Internet giving more info than
the few sentences in the IRS publication?

Update: the secret is wash sales, which Vanguard mixes in to
the average basis calculation. The actual average basis calculation is
the simple thing described by the IRS. A side effect of this is
that the total allowable gain or loss shown on the Vanguard statement
is the right thing to put on your return, but the average basis shown
on the Vanguard statement is NOT correct to put on your return. To
fill in the basis column on schedule D you have to adjust the basis
Vanguard shows by the disallowed loss they show.

(This post was originally found at http://log.ometer.com/2008-03.html#30)

My Twitter account is @havocp.
Interested in becoming a better software developer? Sign up for my email list and I'll let you know when I write something new.