I have the following code (inside a broader code base) which in essence calculates the average buy price for a series of stock trades. The code works as expected except when a f