title: How to return positions from IBKR API (interactive brokers) consistently?
body: <p>IBKR gives me horribly inconsistent results so far, I hope it's just because I don't understand something.</p>
  <p>Here is my code to try to get my account positions, but it only worked the first time I ran it, and doesn't work anymore. I find that half of the things I try to do on IBKR are this way... Is there like a simpler API that doesn't require me instantiating classes to simply get a list of positions in my account? Thanks guys.</p>
  <blockquote>
  <p>def read_positions(): #read all accounts positions and return
  DataFrame with information</p>
  <pre><code>from ibapi.client import EClient 
  from ibapi.wrapper import EWrapper
  from ibapi.common import TickerId
  import pandas as pd
  
  class ib_class(EWrapper, EClient): 
      def __init__(self): 
          EClient.__init__(self, self)
          self.all_positions = pd.DataFrame([], columns = ['Account','Symbol', 'Quantity', 'Average Cost'])
  
      def position(self, account, contract, pos, avgCost):
          index = str(account)+str(contract.symbol)
          self.all_positions.loc[index]=account,contract.symbol,pos,avgCost
  
      def error(self, reqId:TickerId, errorCode:int, errorString:str):
          if reqId &gt; -1:
              print(&quot;Error. Id: &quot; , reqId, &quot; Code: &quot; , errorCode , &quot; Msg: &quot; , errorString)
  
      def positionEnd(self):
          super().positionEnd()
          self.disconnect()
  
  ib_api = ib_class() 
  ib_api.connect(&quot;127.0.0.1&quot;, 7496, 0) 
  ib_api.reqPositions()
  current_positions = ib_api.all_positions
  ib_api.run()
  
  return(current_positions)
  </code></pre>
  </blockquote>