I would like to calculate the average weighted price of an asset based on purchase orders.
I thought it would be total cost / total amount.
But looking at the following sequence:
| operation | units | price | total units | total cost | average price |
|---|---|---|---|---|---|
| BUY | 1 | 1\$ | 1 | 1\$ | 1\$ |
| BUY | 100 | 2\$ | 101 | 201\$ | ~2\$ |
| SELL | 100 | 1\$ | 1 | 101 \$ | 101\$ |
And the average price is 101$ which makes absolutely no sense... Looks like I am missing something very basic here.
X.Y: Eventually I am trying to calculate Unrealized P&L for a trade position.