I got some questions about average cost basis then just to see if i understand correctly.

Say for example..

Year 1 I buy 100 shares at $10

Year 2 I buy 100 shares at $20

Year 3 I buy 100 shares at $30

Year 4 I buy 100 shares at $40

Average cost basis is $25 a share correct?

Lets say one day i need $5000. On the day i sell, the share price is $35.

So i need to sell roughly 143 shares.

I'm assuming it sells shares from Year 1 and 2 first?

No, that's what it would do if you are using First In First Out.

So after i sell those 143 shares. I'm left with 257 shares. Avg cost basis still $25?

Yes, if using average cost.

Next 2 years i buy..

100 shares at $35

100 shares at $15

Is cost basis now $28.75??

No, it's still $25. After buying the 100 shares at $35, it was $27.80.

See

Cost basis methods available at Vanguard | Vanguard for more.

Back to that first sale of 143 shares. Using average cost, your gain is 143 * ($35 - $25) = $1,430. 15% tax costs $214.50, so your net is 143 * $35 - $214.50 = $4790.50.

If using SpecID, you could have chosen to sell the lot of 100 with $40 cost basis, plus 43 of the $30 basis. Now you would have a 143 * 35 - (100 * $40 + 43 * $30) = $285 loss. If your marginal rate for ordinary income is 25% (and you have no other capital gains or losses), this would save $71.25, giving you a net of 143 * $35 + $71.25 = $5076.25.