Computing the optimal portfolio policy of an investor facing capital gains tax is a challenging problem: because the tax to be paid depends on the price at which the security was purchased (the tax basis), the optimal policy is path dependent and the size of the problem grows exponentially with the number of time periods. Dammon et al., Garlappi et al., and Gallmeyer et al. address this problem by approximating the exact tax basis by the weighted average purchase price. Our contribution is threefold. First, we show that the structure of the problem has several attractive features that can be exploited to determine the optimal portfolio policy using the exact tax basis via nonlinear programming. Second, we characterize the optimal portfolio policy in the presence of capital gains tax when using the exact tax basis. Third, we show that the certainty equivalent loss from using the average tax basis instead of the exact basis is very small: it is typically less than 1% for problems with up to 10 periods, and this result is robust to the choice of parameter values and to the presence of transaction costs, dividends, intermediate consumption, labor income, tax reset provision at death, and wash-sale constraints.