getting some figures in IB Java API - need help

Discussion in 'App Development' started by VolMakersTrading, Mar 12, 2015.

  1. 1. calc MID price of a security - do I have to request top book data and calculate my own MID using BBO or is there a better method?

    2. Daily P&L: = any profits (losses counted as negative profits) both for open position and closed, made since 09:00 AM TODAY till current point in time (so this value need to be reset to 0 every day at 16:00PM)

    3. Daily Realized Profit: = any realized profits or losses made today.

    4. Open Position Profit = unrealized P&L ( 0 in case strategy if currently flat).

    definitions:

    "today" = between 09:00AM and 16:00PM of current date.
    "profit" = P/L (losses counted as negative profits).


    many thanks in advance to anyone who can share from his experience.
     
  2. 1. You can use the real time bars, though personally I get book data (make sure your ticks are close together and you aren't matching a stale bid with a recent offer or vice versa), this also gives you the option of rejecting a price if the spread is too wide.

    More on my blog (it's python I'm afraid but hope it helps)

    http://qoppac.blogspot.co.uk/2014/04/streaming-prices-from-ib-api-swigibpy.html

    2 -4 in general:

    Use reqAccountUpdates you can get back both updateAccountValue (for your whole portfolio) and updatePortfolio calls (for specific codes).

    Again from the blog
    http://qoppac.blogspot.co.uk/2014/05/getting-accounting-data-out-of.html

    Now for specifics

    2: updateAccountValue key="NetLiquidation" is your total account value. Set up a cron job (if on unix, or equivalent in windows) to grab this value at 0900 today. Then set up another job which runs every X minutes, grabs the same value, and compares to todays starting value.

    3, 4: For each updatePortfolio line you will get unrealizedPNL and realizedPNL attributes, for each item in your portfolio. Again setup a job to snapshot these at 0900, and then subsequently.

    I hope this makes sense.


     
  3. GLOBALARBTRADER - makes perfect sense and very similar indeed to what I did when writing the same code in EasyLanguage (TradeStation). Since new to IB brokerage in general and IB API in particular I'd like to pick your mind a bit more about the first point of getting updated and reliable MID using book data, if you wouldn't mind. Loved your blog btw, although Python ins't my thing.
     
  4. Essentially what you have to do is start receiving ticks. Each tick could be an update to any part of the level 1 order book (bid / ask price or bid/ask size). So you need to keep track of what the state is of those 4 variables, and when they were last updated. I then track if the current market data 'snapshot' (state of those 4 variables) is good, where goodness could mean:

    - last bid price and last ask price are within X seconds of each other
    - last bid price and last ask price are within Y seconds of the current time
    - further constraints on size of bid/ask or spread

    Then when another part of my code says 'get me a good mid price' and I'm prepared to wait N seconds for it, it it calls to the thing keeping track of the order book. If there isn't a tick server running it starts one. It will then return a 'good' order book snapshot the moment one is available. If after N seconds there hasn't been a good order book then it returns nans. A simple function then works out the mid price inferred by that order book. This can also be used for other reasons, eg 'check the market is liquid enough for an order of Z contracts'.

    Hope this makes sense

    GAT
     
  5. xandman

    xandman

    Do you have a shortlist of Python blogs doing interesting things with the markets and IBKR in particular?

    I already bookmarked your site. Thanks.