Consider a pandas dataframe like:
>> df
date_time op_type price volume
01-01-1970 9:30:01 ASK 100 1800
01-01-1970 9:30:25 ASK 90 1000
01-01-1970 9:30:28 BID 90 900
01-01-1970 9:30:28 TRADE 90 900
01-01-1970 9:31:01 BID 80 500
01-01-1970 9:31:09 ASK 80 100
01-01-1970 9:31:09 TRADE 80 100
I would like to do three calculations: i) the cumulative sum of the volume for op_type == "ASK"
rows; ii) the cumulative sum of the volume for op_type == "BID"
rows; and iii) the sum of the previous two volumes.
That is simple enough, but there is a condition for op_type == "TRADE"
operations:
Whenever there is a
TRADE
operation whoseprice
matches theprice
on aBID
operation, I would like to subtract thatTRADE
operation's volume from the cumulativeBID
volume.Whenever there is a
TRADE
operation whoseprice
matches theprice
on anASK
operation, I would like to subtract thatTRADE
operation's volume from the cumulativeASK
volume.
The output I'm looking for is:
>> df
date_time op_type price volume ASK_vol BID_vol BIDASK_vol
01-01-1970 9:30:01 ASK 100 1800 1800 0 1800
01-01-1970 9:30:25 ASK 90 1000 2800 0 2800
01-01-1970 9:30:28 BID 90 900 2800 900 3700
01-01-1970 9:30:28 TRADE 90 900 2800 0 2800
01-01-1970 9:31:01 BID 80 500 2800 500 3300
01-01-1970 9:31:09 ASK 80 100 2900 500 3400
01-01-1970 9:31:09 TRADE 80 100 2800 500 3300
I read this question but I'm not sure how to incorporate the conditional subtraction to that answer. I would greatly appreciate any help, thank you.