Answering for US taxes: There are a couple of different methods for calculating cost basis, and hence profit. Whichever method you choose, stick with it consistently, and you shouldn't have issues with the IRS. These methods are:
- FIFO (First In First Out)
- LIFO (Last In First Out)
- Average
Each has its advantages and disadvantages, but as long as you don't dodge taxes by switching between them mid-stream, it's arbitrarily up to you which one you use.
FIFO is the most common method for accounting, the cost basis of the share being sold is the cost paid for the 'oldest' share in your account. Hence, if you sell 120 shares in your example, your cost basis is 1400 (100*10 + 20*20).
LIFO is based on the newest share price, and is used when you want to claim short term losses on a long term account. Remember though, that you can't switch between methods partway through without targeting yourself for an audit. Here, if you sell 120 shares, your cost basis is 2200 (100*20 + 20*10).
The last method keeps a running average cost basis per share, and is calculated by the total amount paid divided by the total number of shares. For example, in month 0 your cost basis is 10 (1000 / 100). In month 1 you increase your cost basis to 15 (3000 / 200). Selling 120 shares, your cost basis is 1800 (120*15). This gets complicated if you're consistently buying and selling, however.