title: SuperTrend code using pandas python
body: <p>I am trying to code the following algorithm for SuperTrend indicator in python using pandas.</p>
  
  <pre><code>BASIC UPPERBAND = (HIGH + LOW) / 2 + Multiplier * ATR
  BASIC LOWERBAND = (HIGH + LOW) / 2 - Multiplier * ATR
  
  FINAL UPPERBAND = IF( (Current BASICUPPERBAND &lt; Previous FINAL UPPERBAND) or (Previous Close &gt; Previous FINAL UPPERBAND))
                      THEN (Current BASIC UPPERBAND) ELSE Previous FINALUPPERBAND)
  FINAL LOWERBAND = IF( (Current BASIC LOWERBAND &gt; Previous FINAL LOWERBAND) or (Previous Close &lt; Previous FINAL LOWERBAND)) 
                      THEN (Current BASIC LOWERBAND) ELSE Previous FINAL LOWERBAND)
  
  SUPERTREND = IF((Previous SUPERTREND = Previous FINAL UPPERBAND) and (Current Close &lt;= Current FINAL UPPERBAND)) THEN
                  Current FINAL UPPERBAND
              ELSE
                  IF((Previous SUPERTREND = Previous FINAL UPPERBAND) and (Current Close &gt; Current FINAL UPPERBAND)) THEN
                      Current FINAL LOWERBAND
                  ELSE
                      IF((Previous SUPERTREND = Previous FINAL LOWERBAND) and (Current Close &gt;= Current FINAL LOWERBAND)) THEN
                          Current FINAL LOWERBAND
                      ELSE
                          IF((Previous SUPERTREND = Previous FINAL LOWERBAND) and (Current Close &lt; Current FINAL LOWERBAND)) THEN
                              Current FINAL UPPERBAND
  </code></pre>
  
  <p>Here is the code that I wrote and tested:</p>
  
  <pre><code># Compute basic upper and lower bands
  df['basic_ub'] = (df['high'] + df['low']) / 2 + multiplier * df[atr]
  df['basic_lb'] = (df['high'] + df['low']) / 2 - multiplier * df[atr]
  
  # Compute final upper and lower bands
  for i in range(0, len(df)):
      if i &lt; period:
          df.set_value(i, 'basic_ub', 0.00)
          df.set_value(i, 'basic_lb', 0.00)
          df.set_value(i, 'final_ub', 0.00)
          df.set_value(i, 'final_lb', 0.00)
      else:
          df.set_value(i, 'final_ub', (df.get_value(i, 'basic_ub') 
                                       if df.get_value(i, 'basic_ub') &lt; df.get_value(i-1, 'final_ub') or df.get_value(i-1, 'close') &gt; df.get_value(i-1, 'final_ub') 
                                       else df.get_value(i-1, 'final_ub')))
          df.set_value(i, 'final_lb', (df.get_value(i, 'basic_lb') 
                                       if df.get_value(i, 'basic_lb') &gt; df.get_value(i-1, 'final_lb') or df.get_value(i-1, 'close') &lt; df.get_value(i-1, 'final_lb') 
                                       else df.get_value(i-1, 'final_lb')))
  
  # Set the Supertrend value
  for i in range(0, len(df)):
      if i &lt; period:
          df.set_value(i, st, 0.00)
      else:
          df.set_value(i, 'st', (df.get_value(i, 'final_ub')
                               if ((df.get_value(i-1, 'st') == df.get_value(i-1, 'final_ub')) and (df.get_value(i, 'close') &lt;= df.get_value(i, 'final_ub')))
                               else (df.get_value(i, 'final_lb')
                                     if ((df.get_value(i-1, 'st') == df.get_value(i-1, 'final_ub')) and (df.get_value(i, 'close') &gt; df.get_value(i, 'final_ub')))
                                     else (df.get_value(i, 'final_lb')
                                           if ((df.get_value(i-1, 'st') == df.get_value(i-1, 'final_lb')) and (df.get_value(i, 'close') &gt;= df.get_value(i, 'final_lb')))
                                           else (df.get_value(i, 'final_ub')
                                                 if((df.get_value(i-1, 'st') == df.get_value(i-1, 'final_lb')) and (df.get_value(i, 'close') &lt; df.get_value(i, 'final_lb')))
                                                 else 0.00
                                                )
                                          )
                                    ) 
                              )
                      )
  
  
  # Mark the trend direction up/down
  df['stx'] = np.where((df['st'] &gt; 0.00), np.where((df['close'] &lt; df['st']), 'down',  'up'), np.NaN)
  </code></pre>
  
  <p>I works, but I am not happy with the for loop. Can anyone help optimise it?</p>
  
  <p>You can find the released code on <a href="https://github.com/arkochhar/Technical-Indicators/blob/master/indicator/__init__.py" rel="nofollow noreferrer">Github</a>!</p>