I have a pandas dataframe below which I am using for backtesting. Where +1 in buyOrSell represents enter a long(buy) position, -1 represents enter a short(sell) position. For example at 2019-02-25 00:20:00, the signal indicates to sell. A sell position will be triggered to sell at 00:21:00 VWAP ($3755.117955). The position will be closed when the price moves in the opposite direction meaning for this case, it will be closed at 00:24:00 because the price at 00:24:00 is $3748.86177 while the price at 00:23:00 is $3747.329916, which indicates the price changes direction. Price is moving downwards from 00:20:00 to 00:23:00, hence the position is being held.

I will want to create a column called df['pnl] at 00:20:00 which obtain the pnl at that trade as well as a cumulative pnl graph. may i ask how can i do it? Thank you!

time                vwap         buyOrSell
2019-02-25 00:20:00 3756.128313 -1
2019-02-25 00:21:00 3755.117955 0
2019-02-25 00:22:00 3747.515625 0
2019-02-25 00:23:00 3747.429916 0
2019-02-25 00:24:00 3748.861770 0
2019-02-25 00:25:00 3751.981658 1
2019-02-25 00:26:00 3753.543028 0
2019-02-25 00:27:00 3750.159481 0
2019-02-25 00:28:00 3748.259104 0
2019-02-25 00:29:00 3747.840970 0
2019-02-25 00:30:00 3747.177121 0
‚Äč```

0 Answers