If you trade more than one lot of a security (you buy at different times) the book value is adjusted after a sell order. This actually doesn't affect the price you receive or your profit/loss even though there might be some distortions in your book value.
For example, say you buy:
100 of ABC at $10 for $1,000
100 of ABC at $20 for $2,000
Then you sell:
150 of ABC at $25 for $3,750
Clearly, you should be allowed to sell the 150 shares since you own 200 in aggregate (two purchases of 100 shares each). However, which 150 shares did you sell? Was it 100 you bought at $20 and 50 that you bought at $10 (or the other way around)? In our simulation, we really have no way of knowing this.
What we do know is you have 50 shares of ABC remaining and the book value could be either $10 or $20. Book value is thus calculated as a weighted average of the original book values. In our case: (100*10 + 100*20)/200 = $15. So, even though you never actually purchased anything at $15, the weighted average is a "best guess".
If the price of the stock went down (say you sold at $12.50 instead of $25) then you'd show a loss on these shares since your book value is $15. Even though some shares were bought below $12.50 (at $10), this display approximately correct because you bought some shares above $12.50 (at $20).
In real life which shares you sell can have tax consequences, but in our case, your profit is not affected by the book value. This is just displayed as a convenience for you. No matter what the book value is, you've paid a total of $2,000 for your shares, and any amount you receive when you sell is your profit (or your loss).