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()
Would a financial advisor also use the Portfolio Analyst API calls? If so, could you please provide a short lesson tutorial on how to call the PA? Whenever I try to use the PA endpoints the connection times out.
Separately — this series has been invaluable to get me started quickly with the Client Web API. We really appreciate the work and examples because we couldn’t have made it this far without the prior work. Thank you!
Hello David, we appreciate your positive feedback! Advisors certainly are welcome to use the Portfolio Analyst endpoints as you see fit. The values would work very similar to what has been done throughout the tutorial series as is, but our Account Performance documentation might serve as a familiar reference in our Python examples. The primary variation on the endpoint for Advisors would be the addition of comma-separated account IDs within the “acctIds” array. For example, you might use [‘U1234567’, ‘U4567890’] to receive account performance details for multiple subaccounts. If this does not answer your question, please create a web ticket for this inquiry; we have a category specifically for “API.” One of our API experts will be happy to guide you! https://ndcdyn.interactivebrokers.com/AccountManagement/AmAuthentication?loginType=1&action=CS_WEB_TICKET&loginType=1