code: def handlePosition(self, msg):
          
  
          # log handler msg
          self.log_msg("position", msg)
  
          # contract identifier
          contract_tuple = self.contract_to_tuple(msg.contract)
          contractString = self.contractString(contract_tuple)
  
          # try creating the contract
          self.registerContract(msg.contract)
  
          # new account?
          if msg.account not in self._positions.keys():
              self._positions[msg.account] = {}
  
          # if msg.pos != 0 or contractString in self.contracts.keys():
          self._positions[msg.account][contractString] = {
              "symbol":        contractString,
              "position":      int(msg.pos),
              "avgCost":       float(msg.avgCost),
              "account":       msg.account
          }
  
          # fire callback
          self.ibCallback(caller="handlePosition", msg=msg)
docs: handle positions changes