By default it is first in first out. However, you can declare a "Versus Purchase" agreement with your broker and assign each individual share purchase to specific share sales. I did it with BHEL through TD Ameritrade and it saved me thousands in taxes.