# Realized and Unrealized Profit and Loss [closed]

What I want to know is, after Scenario 4 in this article how does the Realized PnL, Opening Price, Unrealized PnL behave now that direction has changed!

Edit: I have figured out how to deal with the scenario when the opening position is >=0, however, I wish to know what happens after Scenario 4 of this article where-in during the next fill the opening position will <0, how does the different factors behave then.

• Same question again? – noob2 Feb 3 at 15:01
• Hey, actually this is kind of a different one built on the earlier one. I have made some progress using this article. And would like to build upon it further if I can close out scenario 4. Would be great if you could give the article a read. It has been presented in a lucid and concise way too. Thank you so much for your help earlier! – Anirban Saha Feb 3 at 15:03
• Can you please organise all of this into one question? I can do it too but I think you can do a better job than I can. – Bob Jansen Feb 3 at 15:10
• Hi Bob, I didn't quite get what you meant by organising into one question. Can you please clarify it. – Anirban Saha Feb 3 at 15:13
• Okay Bob, sure I will do that right away! – Anirban Saha Feb 3 at 15:30

I think I finally got the required answer to my problem. I'm sharing the code in Python 3 in case it helps anyone struggling with a similar problem.

Running Profit and Loss in Python 3

import pandas as pd
import numpy as np

#Initialising Variables to be used throughout the program
position=0;
averageopenprice=0;
pnlunrealized=0;
pnlrealized=0;
pnltotal=0;

#Inserting Columns into the dataset to visualise the working in a better way
dataset.insert(2,'Position',np.zeros(len(dataset.index)));
dataset.insert(3,'Average Open Price',np.zeros(len(dataset.index)));
dataset.insert(4,'PnL Unrealized',np.zeros(len(dataset.index)));
dataset.insert(5,'PnL Realized',np.zeros(len(dataset.index)));
dataset.insert(6,'PnL Total',np.zeros(len(dataset.index)));

#Algorithm Begins
for i in range(0,len(dataset.index)):
qty=dataset.iat[i,0]; #This will help in writing more legible codes
price=dataset.iat[i,1];
if position==0:
position=qty;
averageopenprice=price;
pnlrealized=pnlrealized;
pnlunrealized=0;
pnltotal=pnlrealized+pnlunrealized;
elif position>0:
if qty>0:
averageopenprice=(position*averageopenprice+qty*price)/(position+qty);
position+=qty;
pnlrealized=pnlrealized;
pnlunrealized=(price-averageopenprice)*position;
pnltotal=pnlrealized+pnlunrealized;
elif qty<0:
if abs(qty)<position:
averageopenprice=averageopenprice;
pnlrealized+=(price-averageopenprice)*abs(qty);
position+=qty;
pnlunrealized=(price-averageopenprice)*position;
pnltotal=pnlrealized+pnlunrealized;
elif abs(qty)==position:
pnlrealized+=(price-averageopenprice)*abs(qty);
position=0;
averageopenprice=0;
pnlunrealized=0;
pnltotal=pnlrealized+pnlunrealized;
elif abs(qty)>position:
pnlrealized+=(price-averageopenprice)*position;
position+=qty;
averageopenprice=price;
pnlunrealized=(price-averageopenprice)*position;
pnltotal=pnlrealized+pnlunrealized;
elif position<0:
if qty<0:
averageopenprice=(abs(position)*averageopenprice+abs(qty)*price)/(abs(position+qty));
position+=qty;
pnlrealized=pnlrealized;
pnlunrealized=(averageopenprice-price)*abs(position);
pnltotal=pnlrealized+pnlunrealized;
elif qty>0:
if qty<abs(position):
averageopenprice=averageopenprice;
pnlrealized=(averageopenprice-price)*qty;
position+=qty;
pnlunrealized=(averageopenprice-price)*abs(position);
pnltotal=pnlrealized+pnlunrealized;
elif qty==abs(position):
pnlrealized+=(averageopenprice-price)*qty;
position=0;
averageopenprice=0;
pnlunrealized=0;
pnltotal=pnlrealized+pnlunrealized;
elif qty>abs(position):
pnlrealized+=(averageopenprice-price)*abs(position);
position+=qty;
averageopenprice=price;
pnlunrealized=0;
pnltotal=pnlrealized+pnlunrealized;
dataset.iat[i,2]=position;
dataset.iat[i,3]=averageopenprice;
dataset.iat[i,4]=pnlunrealized;
dataset.iat[i,5]=pnlrealized;
dataset.iat[i,6]=pnltotal;

output=dataset[['qty','price','PnL Realized','PnL Unrealized','PnL Total']];