In terms of "pseudocode" the changes needed should be as follow.
To obtain the global results i am clearly looping over all the instruments and accumulating positions, PNL, values, realized, unrealized, and so on.
So where we had the statements like:
Position_Signed_tmp +=...