Python Code Example

Discussion in 'Automated Trading' started by newdog, Jan 3, 2016.

  1. newdog

    newdog

    I have recently started coding in Python and am trying to accomplish the following using IbPy:

    (1) create an option and ETF buy order
    (2) execute the order
    (4) read the portfolio positions
    (3) track the PnL of the account every 5 min

    I have been able to achieve (1) and (2) but I am not able to do the (3)

    Currently I am using the code:

    tws_conn = conn.Connection.create(port=7497, clientId=100)
    tws_conn.register( acct_update, msg.updateAccountValue,
    msg.updateAccountTime, msg.updatePortfolio)
    tws_conn.reqAccountUpdates(True,'DU15181')​

    The line: tws_conn.reqAccountUpdates prints the position on the console but I will need to get the market information for each contract by reading their ticker (for ETF) and contract characteristics (for options).

    Does anyone have a sample python code they can share.
     
  2. jj90

    jj90

    I'll have to dig up some code, but off hand here looks like your last line calling reqAccountUpdates is not registered. Your other msg handlers are handling the callback but doesn't look like it is for account updates.
     
  3. jj1111

    jj1111

    @globalarbtrader sells a book that covers all of this. Alternatively, the guy at quantstart.com
     
  4. jj1111

    jj1111

    whoops!
     
  5. 2rosy

    2rosy

    here's a barebones example of getting orderstatus

    gist.github.com/audubon/b14eeda1c2cad704b333

    You can add calling reqAccountUpdates() and implement updateAccountValue() and updatePortfolio()
     
    globalarbtrader likes this.
  6. newdog

    newdog

    I was able to make some progress and I implemented the following:

    1. tws_conn.connect()
    2. tws_conn.reqAccountUpdates(True,'DU15023')
    It printed the following on the console though:

    1. <updateAccountTime timeStamp=18:21>
    2. <updatePortfolio contract=<Packages.IbPy.ib.ext.Contract.Contract object at 0x03627E30>, position=0, marketPrice=52.8449974, marketValue=0.0, averageCost=0.0, unrealizedPNL=0.0, realizedPNL=-17.04, accountName=DU15023>
    3. <updateAccountTime timeStamp=18:21>
    4. <updatePortfolio contract=<Packages.IbPy.ib.ext.Contract.Contract object at 0x03627E30>, position=0, marketPrice=12.89000035, marketValue=0.0, averageCost=0.0, unrealizedPNL=0.0, realizedPNL=-60.6, accountName=DU15023>
    5. <updateAccountTime timeStamp=18:21>
    6. <updateAccountTime timeStamp=18:21>

    How do I gather the above information in an array or any other variable so that I can loop through each position in the portfolio and make a buy or sell decision. I am new to the Python so I need some guidance here guys.
     
  7. jj1111

    jj1111

    If it returns a list,

    for item in list:
    do_something(item)
     
  8. newdog

    newdog

    I did the following but it didn't work at all:

    z = list( tws_conn.reqAccountUpdates(True,'DU15211') )

    Any suggestions ?
     
  9. jj1111

    jj1111

    I don't know what type of object that tws method is returning. I was using list as an example. Did 2rosy's github link have a readme?
     
    #10     Jan 7, 2016