Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts

Request & Modify Orders

Lesson 6 of 11
Duration 5:21
Level Beginner
Close Navigation

Hello, and welcome to this lesson on the Interactive Brokers Client Portal API. In this lesson, we will be discussing how to request all live orders as well as how to modify and cancel existing orders.

Retrieve list of live orders

In many instances, you would want to review the orders on your account. To do this, we simply need to make a GET request to the “iserver/account/orders”. I can set that to my endpoint variable and send this as a GET request.

This endpoint functions similar to the /iserver/marketdata/snapshot endpoint in the sense that I need to instantiate the request, and then send the request a second time to retrieve my information. After the second request, my response body will show a list of all orders I’ve placed today.

Modifying Orders

Receiving our active orders is important, as it allows us to modify orders. If we look through our open orders, we may find orders previously submitted and their orderIds. These orderIds allow us to modify a specific order. This will require a unique endpoint, so let’s go ahead and create a new python file with our standard framework.

In a modifyOrder() method, let’s go ahead and create a base_url variable. Next, we can create an endpoint variable set to “iserver/account/{accountId}/order/”. Similar to the reply endpoint, I will need to append our orderId onto the endpoint. To do so, I will create a variable order_id, and set this equal to one of our submitted order ID’s. Now I can create the variable modify_url and set it equal to ‘“”.join([base_url, endpoint, order_id])’.

 Next, I can designate the json_body variable. This will largely mimic the order we had created before. While most of these values can be copied from our /orders response, it makes sense that we may want to modify some of these values. I will update the price to make this three dollars higher. If submit this request, we can call our live orders endpoint to see this value has updated.

Cancelling Orders

While some individuals may need to modify their orders, an equal number may need to fully cancel an order using the Client Portal API. This process will be quite similar to the last. Opening a new file and filling in our typical framework, let’s start by creating our endpoint variable. This will be set to “iserver/account/{accountId}/order/”. Now, like I did before, I can retrieve an orderId from my live orders request, and set the value to my order_id variable. Finally, I can join these three values together and set it to my cancel_url variable.

With my variable in place, I can start building my request. I will set the variable, cancel_req, equal to requests.delete(url=cancel_url, verify=False). If you have been following along in the series, we can see the pattern that GET will receive information, POST will add or modify information, and our new DELETE will understandably delete something.

Getting this all sorted, I can create a quick json.dumps reference, and then print the status code and body response. Here, I will see my usual 200 message, but now I can see the field “msg” stating that the request was submitted to cancel the order. I can also retrieve the orderId of the order I canceled. Given our order is now in a “cancel” state, the conid and accountId are now null values. This means our order is now closed.

Thank you for watching this lesson on retrieving order information along with modifying and cancelling orders in the Client Portal API. If you find this lesson helpful, please check out our other lessons in the Client Portal API tutorial series.

openOrders.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 – modifyOrder.py

import requests
import json
import urllib3

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

def orderModify():
  
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "iserver/account/DU5240685/order/"
    order_id = "1010551026"
    modify_url = "".join([base_url, endpoint, order_id])
    
    json_body = {
        "conid":265598,
        "orderType":"STP",
        "price":187,
        "side": "SELL",
        "tif": "DAY",
        "quantity":10
    }
    order_req = requests.post(url = modify_url, 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__":
    orderModify()

Code Snippet – cancelOrder.py

import requests
import json
import urllib3

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

def orderCancel():
  
    base_url = "https://localhost:5000/v1/api/"
    endpoint = "iserver/account/DU5240685/order/"
    order_id = "1010551026"
    cancel_url = "".join([base_url, endpoint, order_id])
    
    cancel_req = requests.delete(url = cancel_url, verify=False)
    cancel_json = json.dumps(cancel_req.json(), indent=2)

    print(cancel_req.status_code)
    print(cancel_json)

if __name__ == "__main__":
    orderCancel()

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.

Leave a Reply

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: 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.

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.