title: Calculating stocks's On Balance Volume (OBV) in python
body: <p>I am doing my first project in python. I have a pandas dataframe called df with two columns "close" and "volume". I want to calculate/obtain OBV column based on first two columns. The formula is given below;</p>
  
  <p>If the close is above the prior close then: 
  Current OBC = Previous OBC + Current Volume</p>
  
  <p>If the closing price is below the prior close price then: 
  Current OBV = Previous OBV  -  Current Volume</p>
  
  <p>If the closing prices equals the prior close price then:
  Current OBV = Previous OBV (no change)</p>
  
  <pre><code>close volume  OBC
  30    2500    nan
  32    3000    5500
  25    2700    2800
  35    4000    6800
  20    1000    5800
  </code></pre>
  
  <p>I am using this code:</p>
  
  <pre><code>for i in df.close[1:]:
      if i &gt; df.close.shift(-1):
          df["OBC"] = df.volume + df.OBC.shift(-1) 
      elif i &lt; df.close.shift(-1):
          df["OBC"] = df.OBC.shift(-1) - df.volume
      else:
          df["OBC"] = df.OBC
  </code></pre>
  
  <p>and i get this error:</p>
  
  <blockquote>
    <p>ValueError: The truth value of a Series is ambiguous. Use a.empty,
    a.bool(), a.item(), a.any() or a.all().</p>
  </blockquote>
  
  <p>I have looked at this question but didn't get any help.
  <a href="https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o">Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()</a></p>
  
  <p>Beyond this error, I feel that code may run into trouble in calculating correct OBV. Any help will be of great value.</p>