title: How to create a rolling difference between two different conditions?
body: <p>I got to create a new column in Pandas DataFrame with rolling profit between buy and sell (holding period).. buy=1 is buying sell=1 is selling .. between buying and selling is the holding period.</p>
  <p>So, the rolling profit should be the different between the current days' price (close column) and the last buy price (close value of the buy =1 condition), until the position is closed.</p>
  <pre><code>trading_df = pd.DataFrame({'Date': ['10/1/2020', '10/2/2020', '10/3/2020', '10/4/2020', '10/5/2020', '10/6/2020', '10/7/2020', '10/8/2020', '10/9/2020'],
   'Close': [90, 91, 89, 92, 93, 92, 94, 91, 89],
   'Buy': [0, 1, 0, 0, 0, 0, 1, 0, 0],
   'Sell': [0, 0, 0, 1, 0, 0, 0, 0, 1],
   'Rolling Profit': [None, 0.0, -2.0, 1.0, None, None, 0.0, -3.0, -5.0]})
  </code></pre>
  <p><a href="https://i.stack.imgur.com/R8Vxs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R8Vxs.png" alt="dataframe" /></a></p>