Thanks for the detailed post. But I find this too much of work and difficult to spot any errors. Why not use Cointracking Portfolio Manager.
It has
- Automatic import of trades through APIs.
- Average purchase and sale price reports.
- Booked and unbooked profits.
- Ability to calculate your taxes.
- Set up price notifications.
I have been using it for three months. It does what you are trying to achieve and much more.
Looks like a nice service.