Python Code Example

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

  1. 2rosy

    2rosy

    Code:
    def updatePortfolio(self, contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName):
               self.myvariable = position
    
    Code:
    def cxlAccountUpdates(self):
           self.connection.reqAccountUpdates(False,"DU666")
    
    PM me. I will write it for you
     
    #21     Jan 8, 2016
  2. newdog

    newdog

    Good news: I was able to successfully install swigibipy on home machine and I am working through your example
     
    #22     Jan 11, 2016
    globalarbtrader likes this.
  3. newdog

    newdog

    hi globalarbtrader your website has been a great reading example. I am learning Python also and it's fun.

    I was running your script using a code I created for myself:

    1. ibcontract = IBcontract()
    2. ibcontract.secType = "OPT"
    3. ibcontract.expiry = "20160115"
    4. ibcontract.symbol = "MSFT"

    5. ans = client.get_IB_market_data(ibcontract)

    However, inside wrapper_v3.py I get error:
    line 212, in get_IB_market_data
    self.cb.init_tickdata(tickerid)
    AttributeError: 'IBWrapper' object has no attribute 'init_tickdata'

    Can you provide some hints regarding this ?
     
    #23     Jan 11, 2016
  4. First of all I assume that test3_IB works okay ( you should always check the official test code works before trying to modify it yourself). It would probably be be easier if you started with the test code and modified it rather than writing your own from scratch.

    You must be missing some code. Can you give me *all* the code in your script up to this point (i.e. how do you define 'client') plus the full traceback when it fails. Can you also give me the results of running dir(client) and dir(client.cb)

    GAT
     
    #24     Jan 12, 2016
  5. newdog

    newdog

    Hi globalarbtrader

    I played around more with your code and seems like test3_IB works great in pulling historical data for futures.

    It however gives error when I try the following
    1. ibcontract2 = IBcontract()
    2. ibcontract2.secType = "STK"
    3. ibcontract2.symbol="MSFT"
    4. ans2=client.get_IB_historical_data(ibcontract2)
    5. print ans2

    6. ibcontract3 = IBcontract()
    7. ibcontract2.secType = "OPT"
    8. ibcontract2.expiry="20160219"
    9. ibcontract2.symbol="MSFT"
    10. ans3=client.get_IB_historical_data(ibcontract3)
    11. print ans3
    I am wondering what the required fields when I want to pull the option and stock historical data.
     
    #25     Jan 15, 2016
  6. Probably the exchange and strike respectively. But I hope someone else can tell you for sure as I don't trade these
     
    #26     Jan 15, 2016
  7. Hello GAT,

    I was wondering how you determine in IB if a market is closed? What specific things do you look for. For example when I query the BBO for a closed market it gives me negative prices. Is this consistent for a closed market? Is it a reliable?
     
    #27     Jan 19, 2016
  8. I don't know if this is reliable. My code checks for zero or negative prices, and treats them the same - as an 'untradeable' market. I don't care if a market is closed, or just happens to have no liquidity at that moment.

    GAT
     
    #28     Jan 20, 2016