Close Navigation
Learn more about IBKR accounts

Account Management

Lesson 8 of 11
Duration 7:17
Level Intermediate
Close Navigation

In this lesson, we will be discussing various account management tools in the Client Portal API. We will be primarily focusing on the use of account summary, along with viewing an accounts total position, as well as how to review an individual contract’s position information.

Study Notes:

Hello, and welcome to this lesson on Account Management in the Interactive Brokers Client Portal API. In this lesson, we will be discussing how to review account summary details as well as request position information using the Client Portal API.

Account Summary Values

To begin, we can start by taking a closer look at the portfolio/{accountId}/summary endpoint. This endpoint provides a summary of a specified account and quick reference details therein.  Now, we can jump right into our standard framework for a new python file, AcctSummary.py. I can start this file by naming my method, acctSum().

Inside my method, I can use my standard structure, but this time I will set my ‘endpoint’ variable to ‘portfolio/{accountId}/summary’. Given we have already provided our accountId, there are no further parameters or detail to request. So let’s go ahead and build our request. Given that we are just retrieving account information today, we will only be using GET requests. I can set my url to the base_url and endpoint variables. With my request variable set, I can print this along with my json value to see the full range of data.

If we run this code, we will see all of the details returned. As you can imagine, the summary endpoint does not go into too much detail, though it can provide a quick reference to details such as your account’s ‘availablefunds’. We can also find details about maintenance and initial margin requirements, and a host of other values. We would strongly recommend reviewing the full list of details available.

In many instances, we will see three returns of what seems to be the same value. Let’s use ‘availablefunds as an example. We can see “availablefunds”, “availablefunds-c”, and “availablefunds-s” all returned in a row. These values are all used to designate specific details. All fields ending in “-c” will notate the value specific to “commodities”. Meanwhile, “-s” will provide information on securities. And then the base value, like ‘availablefunds’, will be a reference for the whole account.

Requesting Positions

While it can be nice to receive account information at a glance, we often would like to retrieve more exact details. To kick things off, let’s go ahead and create a new file so we can review positions information. In addition to our normal framework, I will establish my ‘endpoint’ variable once again. I will set this to “portfolio/{accountId}/positions/0”.

You might initially find this endpoint odd given the trailing ‘/0’ value. The reason we have this extra index is because our positions endpoint is paginated. That means that you may request or receive multiple pages of data. A typical page will include 30 securities per page. If you trade a myriad of contracts, you may need to request this endpoint with a 0, 1, or even 2 to receive all your positions’ values. However, for most typical trades, you will often just use the first page, 0. 

Now, we can set up our GET requests and json.dumps values, and run our code. In doing so, we will retrieve a list of arrays. Each array will indicate a specific contract. These will include the accountId, contract ID, contract description or symbol, along with all of the values we might want to see alongside it.

Something that may help to point out is that we can see a “mktPrice”, “mktValue”, “avgCost” and “avgPrice” fields returned. These are all understandably similar, so let’s discuss what each value means. “mktPrice” indicates the current value of an individual contract on a per-share basis. Meanwhile, “mktValue” will return “mktPrice” multiplied by the total “position” value. This can be helpful to understand what closing your position might look like.

“avgPrice” will represent the average cost of each share when you bought it. While it may not mean much if you bought a single share, if you bought a share on Monday at $50, a share on Tuesday at $100, and then a share on Wednesday at $150, our “avgPrice” will return “100” given that is the average of every time you entered the position. The “avgCost” field  will often show the same value, but in the case of derivatives, this will show the average cost multiplied by the position then the multiplier. My ES contract can represent this by showing average price, multiplied by my position, multiplied by the multiplier of 50.

In addition, if this endpoint is queried in a polling mode, every few moments, then we will eventually see this endpoint return the full info & rules for all of your contracts.  This can be helpful in saving the separate contract requests elsewhere and can be a powerful tool to show all contract and position information.

Individual Positions

While it is great to see a whole list, as we mentioned it can take some time to generate all of our contract details. So, to better manage those requests, I may instead want to request an individual position’s information. As long as I know my contract, like AAPL’s 265598, I can make a request or two to my endpoint and I will have the full contract information right away.

All we need to do is make a new file and set a GET request to the endpoint, ‘portfolio/{accountId}/position/{conid}’. After populating my accountId and contract Id, I can run my code and see all of my information come back for the entire AAPL contract.

Thank you for watching this lesson on account management in the Client Portal API. If you find this lesson helpful, please check out our other lessons in the Client Portal API tutorial series.

Code Snippet – acctPositions.py

import requests
import json
import urllib3

# Ignore insecure error messages
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def acctPos():
  
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "portfolio/DU5240685/positions/0"
    
    pos_req = requests.get(url=base_url+endpoint, verify=False)
    pos_json = json.dumps(pos_req.json(), indent=2)

    print(pos_req.status_code)
    print(pos_json)

if __name__ == "__main__":
    acctPos()

Code Snippet – acctPositionsSingle.py

import requests
import json
import urllib3

# Ignore insecure error messages
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def acctPosSingle():
  
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "portfolio/DU5240685/position/265598"
    
    pos_req = requests.get(url=base_url+endpoint, verify=False)
    pos_json = json.dumps(pos_req.json(), indent=2)

    print(pos_req.status_code)
    print(pos_json)

if __name__ == "__main__":
    acctPosSingle()

Code Snippet – acctSummary.py

import requests
import json
import urllib3

# Ignore insecure error messages
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

def acctSum():
  
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "portfolio/DU5240685/summary"
    
    sum_req = requests.get(url=base_url+endpoint, verify=False)
    sum_json = json.dumps(sum_req.json(), indent=2)

    print(sum_req.status_code)
    print(sum_json)

if __name__ == "__main__":
    acctSum()

Join The Conversation

If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.

4 thoughts on “Account Management”

  • How do we find out the date that our position last traded? This is not one of the columns returned in the payload response.

    • Hello David, we appreciate your question. You can view the date your position last traded in TWS by clicking Edit> Global Configuration> Display > Default Layout. Then, select Trade Time in the Available Columns. Please reach back out with any more questions. We are here to help!

Leave a Reply

Your email address will not be published. Required fields are marked *

Disclosure: Interactive Brokers

The analysis in this material is provided for information only and is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad-based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation by IBKR to buy, sell or hold such investments. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

The views and opinions expressed herein are those of the author and do not necessarily reflect the views of Interactive Brokers, its affiliates, or its employees.

Disclosure: Order Types / TWS

The order types available through Interactive Brokers LLC's Trader Workstation are designed to help you limit your loss and/or lock in a profit. Market conditions and other factors may affect execution. In general, orders guarantee a fill or guarantee a price, but not both. In extreme market conditions, an order may either be executed at a different price than anticipated or may not be filled in the marketplace.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.