TWS Scrapper

Discussion in 'Interactive Brokers' started by tonyf, May 6, 2022.

  1. xandman

    xandman

    What if you send an API query to a demo account with no positions? Probably not up to date, unreliable.
     
    #11     May 6, 2022
    d08 likes this.
  2. d08

    d08

    This should be a parameter for whatif orders, something like margin change relating to a theoretical $100k portfolio without other positions.
     
    #12     May 6, 2022
  3. tonyf

    tonyf

    Am sorry for coming back to the original question, but in the absence of pulling the data via an API or a report, is anyone experienced with an RPA like UiPath to scrap TWS?
     
    #13     May 7, 2022
  4. I'm not sure what the problem is.
    I use IB's API and wrote my own program to determine the maintenance margin for one contract (and the entire position) for each of the futures positions I'm holding. I'm using futures in multiple currencies, so the program also gives me a maintenance margin per currency.
    I'm not going to give the program but can tell you what steps it takes to do these calculations.
     
    #14     May 8, 2022
  5. tonyf

    tonyf

    Do share.
     
    #15     May 8, 2022
  6. traider

    traider

    Can you also share on how to calculate yearly pnl (realized+unrealized) for an underlying asset (options + shares) using API
     
    #16     May 8, 2022
  7. Okay. As I mentioned before, I use this for an account in which I only trade futures, not stocks or options. So I haven't verified it for anything else but futures. I usually have about 25~30 open positions at any time, in about 3~4 different currencies.

    Step 1. Get the account overview, including all open positions and all cash positions in any currency. Use reqAccountUpdates(), updatePortfolio(), updateAccountValue() and accountDownloadEnd().
    Step 2. For each position in my portfolio: determine the maintenance margin for one contract, and for the current position size. Place a what-if order to do this. Use the current account total maintenance margin as starting point (this was obtained in step 1). Use placeOrder() to buy 1 contract, and receive the new account maintenance margin via openOrder(). The absolute value of the difference between the two values is the maintenance margin for one contract expressed in the BASE currency of your account. You can convert this to the instrument's currency by getting the latest exchange rate. I already have that exchange rate stored on my computer, so don't need to request that separately. Multiply by the position size to get the maintenance margin of the position.
    Step 3. Find the maintenance margin per currency. Loop through your positions to make a list of all currencies involved. For each currency: collect the instruments which quote in that currency, and collect the maintenance margin of these positions. At step 1 you received your cash positions in any currency you may have. Compare for each currency the cash position versus the maintenance margin for these positions. If your cash amount is less than the maintenance margin for those positions you will get a debit interest line in your monthly IB statement.

    I wrote this software in Java. It is not fast, it takes about 10~20 seconds, but speed is not a requirement for me. I built it as I wanted to be able to monitor whether I was incurring debit interest in any currency. It helps me to determine whether I should do some money conversions, from a currency into another currency.
     
    #17     May 8, 2022
  8. You can get the unrealized pnl for your open positions via the API. But you can't get historical trade information via the API. You have to maintain a log or database of all your trades and calculate the realized pnl yourself.
     
    #18     May 8, 2022