0

So I get this list back from interactive brokers. (API 9.73 using this repo)

    ib = IB()
    ib.connect('127.0.0.1', 7497, clientId=2)

    data = ib.positions()
    print((data))
    print(type(data))

The data comes back as , but here is the response.

`[Position(account='DUC00074', contract=Contract(conId=43645865,    symbol='IBKR', secType='STK', exchange='NASDAQ', currency='USD', localSymbol='IBKR', tradingClass='NMS'), position=2800.0, avgCost=39.4058383), Position(account='DUC00074', contract=Contract(conId=72063691, symbol='BRK B', secType='STK',exchange='NYSE', currency='USD', localSymbol='BRK B', tradingClass='BRK B'), position=250.0, avgCost=163.4365424)]`

I have got this far:

for d in data:
       for i in d:
           print(i) 

But I have no idea as to how I would parse and then dump into a DB, anything after Position(... So to be really clear, I don't know how I would parse, like I would say in php / json.

Chad
  • 643
  • 2
  • 11
  • 22
  • But as far as I know, you do not get a string that contains a list, you get the list itself, so no need to parse. – Willem Van Onsem Nov 23 '17 at 21:02
  • So if I get a list, then how can break it apart and put it into a db – Chad Nov 24 '17 at 17:03
  • 1
    It is already broken apart for you. Just use attributes of the objects that are returned. How you get it into a DB is a separate question, is that what you need help with? – spookylukey Nov 25 '17 at 10:31

1 Answers1

0

Okay, Im new to python, not new to programing. So the response from Interactive Brokers threw me off. I'm so used to JSON response. Regardless, what it comes down to is this is a list of objects, (the example above). That might be simple, but missed it. Once I figured it out it became a little easier.

Here is the final code, hopefully this will help someone else down the line.

for obj in data:

#the line below just bascially dumps the object
print("obj={}  ".format(obj))

#looking for account number
#"contract" is an object inside an object, so I had to extract that. 
if(getattr(obj, 'account') == '123456'):
    print("x={}  ".format(getattr(obj, 'account')))
    account = (getattr(obj, 'account'))
    contract = getattr(obj, 'contract')
    contractId = getattr(contract, 'conId')
    symbol = getattr(contract, 'symbol')
    secType = getattr(contract, 'secType')
    exdate = getattr(contract, 'lastTradeDateOrContractMonth')
    strike = getattr(contract, 'strike')
    opt_type = getattr(contract, 'right')
    localSymbol = getattr(contract, 'localSymbol')
    position = getattr(obj, 'position')
    avgCost = getattr(obj, 'avgCost')

    sql = "insert into IB_opt (account, contractId, symbol, secType, exdate, opt_type, localSymbol, position, avgCost,strike) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)";
    args = (account, contractId, symbol, secType, exdate, opt_type, localSymbol, position, avgCost,strike)
    cursor.execute(sql, args)

Of all the sites I looked at this one was pretty helpful.

Chad
  • 643
  • 2
  • 11
  • 22