title: Trying to get historical data for multiple securities using python and IB API - df not clearing between loops
body: <p>I'm trying to get historical data for several products through the IB API, and store each product in a dataframe (which I need to save in separate csv files).</p>
  <p>This is my code, the main issue is that the dataframe isn't clearing between loops, when moving onto the second loop the df contains data for 2 products, the third for 3.  I'm not sure where / how to clear the df.</p>
  <pre><code>from ibapi.client import EClient
  from ibapi.wrapper import EWrapper
  from ibapi.contract import Contract
  import pandas as pd
  import threading
  import time
  
  class IBapi(EWrapper, EClient):
      def __init__(self):
          EClient.__init__(self, self)
          self.data = []
  
      def historicalData(self, reqId, bar):
          self.data.append([bar.date, bar.open, bar.high, bar.low, bar.close, bar.volume])
             
      def error(self, reqId, errorCode, errorString):
          print(&quot;Error. Id: &quot; , reqId, &quot; Code: &quot; , errorCode , &quot; Msg: &quot; , errorString)
  
      def historicalDataEnd(self, reqId: int, start: str, end: str):
          print(&quot;HistoricalDataEnd. ReqId:&quot;, reqId, &quot;from&quot;, start, &quot;to&quot;, end)
          self.df = pd.DataFrame(self.data)
  
  def run_loop():
      app.run()
      
  app = IBapi()
  
  #Create contract object
  ES_contract = Contract()
  ES_contract.symbol = 'ES'
  ES_contract.secType = 'FUT'
  ES_contract.exchange = 'GLOBEX'
  ES_contract.lastTradeDateOrContractMonth  = '202209'
  
  #Create contract object
  VIX_contract = Contract()
  VIX_contract.symbol = 'VIX'
  VIX_contract.secType = 'IND'
  VIX_contract.exchange = 'CBOE'
  VIX_contract.currency = 'USD'
  
  #Create contract object
  DAX_contract = Contract()
  DAX_contract.symbol = 'DAX'
  DAX_contract.secType = 'FUT'
  DAX_contract.exchange = 'EUREX'
  DAX_contract.currency = 'EUR'
  DAX_contract.lastTradeDateOrContractMonth  = '202209'
  DAX_contract.multiplier = '25'
  
  products={'ES': ES_contract, 'VIX': VIX_contract,  'DAX': DAX_contract}
  
  nid=1
  
  app.connect('127.0.0.1', 4001, 123)
  #Start the socket in a thread
  api_thread = threading.Thread(target=run_loop, daemon=True)
  api_thread.start()
  time.sleep(1) #Sleep interval to allow time for connection to server
  
  def fetchdata_function(name,nid):
      df=pd.DataFrame()
      #Request historical candles
      app.reqHistoricalData(nid, products[name], '', '1 W', '5 mins', 'TRADES', 0, 2, False, [])
      time.sleep(10) #sleep to allow enough time for data to be returned
      df = pd.DataFrame(app.data, columns=['Date', 'Open', 'High', 'Low', 'Close', 'Volume'])
      df['Date'] = pd.to_datetime(df['Date'],unit='s')
      df=df.set_index('Date')
      df.to_csv('1week'+str(name)+'5min.csv')  
      print(df)
  
  names=['ES', 'DAX', 'VIX']
  
  for name in names:     
      fetchdata_function(name,nid)
      nid=nid+1   
      
  app.disconnect()
  </code></pre>