40 line Python code for P&L calculation
Here is my implementation of P&L calculation for real-time marketdata and trade log streaming, as you can see from the two member functions:
def update_by_tradefeed() and
- The traded quantity is a signed number: positive for buy and negative for sell. That means the net position is negative if the transactioin short sells some products.
- If the trade feed takes close action, need to be careful of whether the close quantity is larger than the net position. In this case, the average open price should also be updated to the close price.
PnlSnapshot class for a product: