you want to calculate the PnL using the original trade prices
the presented algorithm for calculation of PnL is overcomplex, for sure, and it should be considered a mere approximation of the real PnL
you cannot remove the sell_sum_qty from the formula since then it would be considered twice in the calculations - see the line 104 and 106
See the comment "# long position and some sell trades have been made against it, close that amount based on how much was sold against this long position"
mark the remaining position to market i.e. pnl would be what it would be if we closed at current price
Above block of code is wrong. it should be open_pnl += abs(position)*(.........). Position is the amount the should be marked to market