Study Notes:
Hello, and welcome to this lesson on the Interactive Brokers Client Portal API. In this lesson, we will be discussing how to place orders, and how to view active orders and how to request executions as a financial advisor.
Creating an Allocation Profile
To begin, we need to address how allocation group management works in the Client Portal API at this time. In order to create or make any modifications to existing Allocation groups, this must be done through the Trader Workstation. The same names and allocations are used by the CPAPI as it is in the Trader Workstation, so there is no unique process needed for making these available for API use.
Assuming we have created our allocation groups, we can move over to building our request. We want to begin by querying the iserver/accounts endpoint. This will return a list of all connected Accounts, All Allocation Group names, and will also reference the default “All” allocation group.
Placing an Order
Now that we can find the exact groups we are working with, let’s start by placing a few orders. To begin, we can place an order for our allocation group, video_group. This group includes all subaccounts for to the Financial Advisor.
To begin, we will use a structure similar to what was set in lesson 5 and referencing our required information. In this example, I will make a limit buy for 100 AAPL stock with a limit price of 190. Now this is the point in which we can start making modifications unique for Financial Advisors.
Our standard endpoint is iserver/account/{accountId}/orders for a specific account. But in our case, we can simply modify the account ID field to match our allocation group. So I will set the value of “acctId” to “video_group” to match our intended allocation group. Now if we look at the response message, we should see an order id, our order status, and “encrypt_message”:”1”. This is a typical sign of a successful order placement.
As another example of FA order placement, we can tweak our current order, but instead of using the allocation group name, I can instead specify one of my subaccounts, like DU74649. When I place an order with this assigned, I will trigger an order for just this subaccount, but no other subaccount will be affected. We can see the response message, order id, our order status, once again.
Requesting live orders
Now that we have a few orders on the books, we can get a better idea of how to view them after the fact by changing my active account. Using our standard framework, I will set my endpoint variable to ‘iserver/account’. Then I will set my ‘acct_body’ variable to an array. We can create the “acctId” tag and set it equal to our subaccount’s account ID. Sending out this request we can see a 200 OK response with the body tags of “set” to true, and an indicator that “acctId” is set to our specified account.
After setting the account, we just need to make a GET request to iserver/account/orders, as we have done in our prior tutorial, and we will see all live traded orders this session for the account. Here I can see my latest AAPL order, how much has been filled, my limit price set, and so on. And while this is just one example of using the iserver/account endpoint to view details, you will need to use this methodology for many other similar endpoints.
You may have noticed that our /orders request only showed one order placed to this individual. But we did not see any information on the original order placed to the video_group allocation group. In order to find these values, you must query them separately. Similar to the process we just made to view the individual account’s orders, we can use our iserver/account endpoint to look at the video_group orders as well. After calling the account endpoint, then requerying the /orders endpoint, we can see our other order.
Requesting Portfolio Information
At this point, we placed an order to a subaccount and a group and then review each of their live orders. The next logical step would be to review our portfolio data. When we use a GET request to the portfolio/{acctId}/positions/0 endpoint. This will pull data for a single account only. Unlike other endpoints, this only functions on an individual level. And the reason for this is because each account could have largely different position quantities, so we can not use a generic allocation group as we had before.
Thank you for watching this lesson on Financial Advisor Management in the Client Portal API. If you found this lesson helpful, please check out our other lessons in the Client Portal API tutorial series.
Code Snippet – accounts.py
import requests import json # Disable SSL Warnings import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def reqAccounts(): base_url = "https://localhost:5000/v1/api/" endpoint = "iserver/accounts" accts_req = requests.get(url=base_url+endpoint, verify=False) accts_json = json.dumps(accts_req.json(), indent=2) print(accts_req) print(accts_json) if __name__ == "__main__": reqAccounts()
Code Snippet – liveOrder-FA.py
import requests import json import urllib3 # Ignore insecure error messages urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def orderRequest(): base_url = "https://localhost:5000/v1/api/" endpoint = "iserver/account/orders" order_req = requests.get(url=base_url+endpoint, verify=False) order_json = json.dumps(order_req.json(), indent=2) print(order_req.status_code) print(order_json) if __name__ == "__main__": orderRequest()
Code Snippet – placeOrder-FA.py
import requests import json import urllib3 # Ignore insecure error messages urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def orderRequest(): base_url = "https://localhost:5000/v1/api/" endpoint = "iserver/account/DU74649/orders" json_body = { "orders": [{ "conid": 265598, "orderType": "LMT", "price":190, "side": "BUY", "tif": "DAY", "quantity": 100 }] } order_req = requests.post(url=base_url+endpoint, verify=False, json=json_body) order_json = json.dumps(order_req.json(), indent=2) print(order_req.status_code) print(order_json) if __name__ == "__main__": orderRequest()
Code Snippet – positions-FA.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/DU74649/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 – switchAccount.py
import requests import json # Disable SSL Warnings import urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) def switchAccount(): base_url = "https://localhost:5000/v1/api/" endpoint = "iserver/account" acct_body = { "acctId":"video_group" } md_req = requests.post(url=base_url+endpoint, verify=False, json=acct_body) md_json = json.dumps(md_req.json(), indent=2) print(md_req) print(md_json) if __name__ == "__main__": switchAccount()
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 LLC, its affiliates, or its employees.
Disclosure: API Examples Discussed
Throughout the lesson, please keep in mind that the examples discussed are purely for technical demonstration purposes, and do not constitute trading advice. Also, it is important to remember that placing trades in a paper account is recommended before any live trading.