title: Is there a way to vectorize Renko calculations in python?
body: <p>Okay, right now I use <code>df.iterrows()</code> in chunks to loop through each minute and compare with the previous Renko close. However, this takes a long time. I keep thinking is there a way to vectorize and backtest faster. Cause I will be backtesting on tick data which means millions of rows.</p>
  <p><a href="https://i.stack.imgur.com/lKiAP.png" rel="nofollow noreferrer">The raw csv</a></p>
  <p><a href="https://i.stack.imgur.com/RqOvH.png" rel="nofollow noreferrer">Renko csv</a></p>
  <p>Below is the for loop I have been using. I am a newbie to this so please excuse any inefficiencies in the code I have written and help me in any way that is possible.</p>
  <pre><code>def trend(box, df, lasttrend, lastrenko):
  for rows in df.itertuples():
      index = rows.Index
      print(index)
      df.loc[index, 'renko close'] = lastrenko
      if index == 0:
          df.loc[index, 'renko close'] = df.loc[index, 'close']
          df.loc[index, 'trend'] = 'S'
          lasttrend = 'S'
          lastrenko = df.loc[index, 'renko close']
          print('Trend start: S', lastrenko, '\n-------------------------------------')
          continue
      if df.loc[index, 'close'] - lastrenko &gt;= box:
          if lasttrend == 'R':
              if df.loc[index, 'close'] - lastrenko &gt;= 2*box:
                  df.loc[index, 'renko close'] = lastrenko + (2*box)
                  df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
                  df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
                  df.loc[index, 'trend'] = 'G'
                  if df.loc[index,'deviation boxes'] &gt;= 1:
                      df.loc[index, 'renko close'] = df.loc[index, 'renko close'] + (math.floor(df.loc[index,'deviation boxes']))*(box)
                  lasttrend = 'G'
                  lastrenko = df.loc[index, 'renko close']
                  print('Trend change: R - G', lastrenko, '\n-------------------------------------')
          elif lasttrend == 'G' or lasttrend == 'S':
              df.loc[index, 'renko close'] = lastrenko + (box)
              df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
              df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
              df.loc[index, 'trend'] = 'G'
              if df.loc[index, 'deviation boxes'] &gt;= 1:
                  df.loc[index, 'renko close'] = df.loc[index, 'renko close'] + (math.floor(df.loc[index, 'deviation boxes']))*(box)
              lasttrend = 'G'
              lastrenko = df.loc[index, 'renko close']
              print('Trend change: G - G', lastrenko, '\n-------------------------------------')
          else:
              df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
              df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
              df.loc[index, 'trend'] = 'N'
              print('Trend change: R - N', lastrenko, '\n-------------------------------------')
      elif df.loc[index, 'close'] - lastrenko &lt;= -box:
          if lasttrend == 'G':
              if df.loc[index, 'close'] - lastrenko &lt;= -2*box:
                  df.loc[index, 'renko close'] = lastrenko - (2*box)
                  df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
                  df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
                  df.loc[index, 'trend'] = 'R'
                  if df.loc[index,'deviation boxes'] &gt;= 1:
                      df.loc[index, 'renko close'] = df.loc[index, 'renko close'] - (math.floor(df.loc[index,'deviation boxes']))*(box)
                  lasttrend = 'R'
                  lastrenko = df.loc[index, 'renko close']
                  print('Trend change: G - R', lastrenko, '\n-------------------------------------')
          elif lasttrend == 'R' or lasttrend == 'S':
              df.loc[index, 'renko close'] = lastrenko - (box)
              df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
              df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
              df.loc[index, 'trend'] = 'R'
              if df.loc[index,'deviation boxes'] &gt;= 1:
                  df.loc[index, 'renko close'] = df.loc[index, 'renko close'] - (math.floor(df.loc[index,'deviation boxes']))*(box)
              lasttrend = 'R'
              lastrenko = df.loc[index, 'renko close']
              print('Trend change: R - R', lastrenko, '\n-------------------------------------')
          else:
              df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
              df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
              df.loc[index, 'trend'] = 'N'
              print('Trend change: G - N', lastrenko, '\n-------------------------------------')
      else:
          df.loc[index, 'renko close'] = lastrenko
          df.loc[index, 'deviation'] = abs(df.loc[index, 'close'] - df.loc[index, 'renko close'])
          df.loc[index, 'deviation boxes'] = df.loc[index, 'deviation']/box
          df.loc[index, 'trend'] = 'N'
          print('Trend change: N', lastrenko, '\n-------------------------------------')
  return df, lasttrend, lastrenko
  </code></pre>