{"id":9417,"date":"2023-07-25T15:31:16","date_gmt":"2023-07-25T15:31:16","guid":{"rendered":"https:\/\/ibkrcampus.eu\/trading-lessons\/placing-orders\/"},"modified":"2024-01-17T19:12:29","modified_gmt":"2024-01-17T19:12:29","slug":"placing-orders","status":"publish","type":"trading-lessons","link":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/","title":{"rendered":"Placing Orders"},"content":{"rendered":"\n<p>Hello, and welcome to this lesson on the Interactive Brokers Client Portal API. In this lesson, we will be discussing how to place market, limit, and stop orders. We will also be discussing order confirmation using the reply endpoint.<\/p>\n\n\n\n<p><strong>Placing a Market order<\/strong><\/p>\n\n\n\n<p>&nbsp;To begin, let\u2019s start with placing a market order. I will start with the general framework, along with my method, orderRequest(). In the orderRequest() method, I will create the base_url variable, along with an endpoint variable, which should be set to \u2018iserver\/account\/{accountId}\/orders\u2019. &nbsp;<\/p>\n\n\n\n<p>Once we have built our URL, we will need to use a JSON body, so I will go ahead with constructing that. I will call it \u2018json_body\u2019 and set it equal to a set of curly brackets for the array value. Inside these brackets, I will need to set the \u201corders\u201d field and set it to a list with an array inside. Then we need to create a few fields in this case including \u201cconid,&nbsp; orderType, side, tif, quantity\u201d. All available fields can be found on the client portal API documentation page under account\/{accountID}\/orders endpoint. With that said, I will use my AAPL conId. Then set \u201cMKT\u201d for market order, \u201cbuy\u201d for side, \u201cday\u201d for \u201ctif\u201d, and finally, I will set my \u201cquantity\u201d to 10.<\/p>\n\n\n\n<p>With the body set, I will create a reference to our coming request. I\u2019ll create the variable \u2018order_req\u2019 and set it equal to requests.post. Here we can set the URL to our base_url+endpoint, verify to False and then the \u2018json\u2019 tag to the variable json_body.<\/p>\n\n\n\n<p>You are welcome to explore the full return, but we will use json.dumps to help with the display. I can set order_json equal to \u201cjson.dumps(order_req.json()\u201d, and then \u201cindent=2\u201d. After establishing our pretty print, I will print out the order_req.status_code and order_json variables.<\/p>\n\n\n\n<p>Once we have added these initial parameters we can now run this code and view the returned values. Initially our status Response code [200] indicates that the request has succeeded.<\/p>\n\n\n\n<p>In this case the values return are our unique system generated \u201corder id\u201d, \u201corder status\u201d and \u201cencrypt message\u201d. The order status presubmitted indicates that a simulated order type has been accepted by the IBKR system and this order has yet to be elected.<\/p>\n\n\n\n<p><strong>Placing a Limit order<\/strong><\/p>\n\n\n\n<p>As with our previously placed market order in this case we will need to define and change some additional parameters in the body. Here we can change the order type from MKT to LMT as this will now be a limit order. Since we are placing a limit order, we must also define a limit price by adding the price parameter. As mentioned previously a full list of order parameters is available on our documentation.<\/p>\n\n\n\n<p>If we run this code, we will see a successful order placement, followed by our order_id and order status.<\/p>\n\n\n\n<p><strong>Placing a Stop order<\/strong><\/p>\n\n\n\n<p>A Stop order becomes a market order to buy or sell securities or commodities once the specified stop price is attained or penetrated. A Stop order is not guaranteed a specific execution price.<\/p>\n\n\n\n<p>To create a Stop order, we will change the order type from \u201cLMT\u201d to \u201cSTP\u201d. As with our limit order we need to define our STP price. We will still use our \u201cprice\u201d field to designate our stop price. It is important to note that instead of the \u201cprice\u201d field, STP_LMT and TRAIL orders will use the \u201cauxPrice\u201d field to designate a stop price.<\/p>\n\n\n\n<p>In this case, if we run the code, we will receive a notable response. We can see a reply-id message along with a message clearly explaining that we are trying to submit a stop order and to be aware of the stop order precautions. It asks us whether we are sure we want to submit the order. This is a warning that requires we submit a confirmation message, similar to how they may appear in Trader Workstation. To approve this order and acknowledge the precaution, we can use the \/iserver\/reply\/{replyid} endpoint.<strong><\/strong><\/p>\n\n\n\n<p><strong>Placing an order reply<\/strong><\/p>\n\n\n\n<p>Moving into a new file, orderReply.py, I can build out my initial framework. After that, I will use an orderReply() method structured like usual with a base_url and endpoint. I will set the endpoint variable to iserver\/reply\/ endpoint. I can create an \u2018replyId\u2019 variable and set it equal our our \u201cid\u201d field from the prior return. I can set a variable, reply_url, equal to all of these values joined together.<\/p>\n\n\n\n<p>Then, I can create a json_body variable, which simply includes \u201cconfirmed\u201d:True inside an array. Now I will just build our typical post request to the reply_url variable, passing our \u2018verify=False\u2019 and json_body variables. And to help read this, I can set up my json.dumps method once again. Printing out these values, I can see a 200 OK status, and our new status.<\/p>\n\n\n\n<p>In some cases, you might receive another \u201cid\u201d and \u201cmessage\u201d value back. This is typical if more precautions are set. Simply feed these \u201cid\u201d values back into our reply program, and you should be able to approve these orders.<\/p>\n\n\n\n<p>Thank you for watching this lesson on placing orders and order confirmations in the Client Portal API. If you found this lesson helpful, please check out our other lessons in the Client Portal API tutorial series.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-code-snippet-placeorder\">Code Snippet &#8211; placeOrder<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import requests\nimport json\nimport urllib3\n\n# Ignore insecure error messages\nurllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n\ndef orderRequest():\n  \n    base_url = \"https:\/\/localhost:5000\/v1\/api\/\"\n    endpoint = \"iserver\/account\/DU5240685\/orders\"\n\n    json_body = {\n        \"orders\": [\n            {\n            \"conid\": 265598,\n            \"orderType\": \"STP\",\n            \"price\":185,\n            \"side\": \"SELL\",\n            \"tif\": \"DAY\",\n            \"quantity\":10,\n            }\n        ]\n    }\n    order_req = requests.post(url = base_url+endpoint, verify=False, json=json_body)\n    order_json = json.dumps(order_req.json(), indent=2)\n\n    print(order_req.status_code)\n    print(order_json)\n\nif __name__ == \"__main__\":\n    orderRequest()<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-code-snippet-orderreply-py\">Code Snippet &#8211; orderReply.py<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import requests\nimport json\nimport urllib3\n\n# Ignore insecure error messages\nurllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)\n\ndef orderRequest():\n  \n    base_url = \"https:\/\/localhost:5000\/v1\/api\/\"\n    endpoint = \"iserver\/reply\/\"\n    replyId = \"7a45bf17-ae07-430f-9888-a4d79539aaa0\"\n    reply_url = \"\".join([base_url,endpoint,replyId])\n\n    json_body = {\n        \"confirmed\":True\n    }\n    order_req = requests.post(url = reply_url, verify=False, json=json_body)\n    order_json = json.dumps(order_req.json(), indent=2)\n\n    print(order_req.status_code)\n    print(order_json)\n\nif __name__ == \"__main__\":\n    orderRequest()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the event any precautions are triggered.<\/p>\n","protected":false},"author":850,"featured_media":9414,"parent":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_acf_changed":false,"footnotes":""},"contributors-categories":[149],"traders-academy":[101,103,105],"class_list":{"0":"post-9417","1":"trading-lessons","2":"type-trading-lessons","3":"status-publish","4":"has-post-thumbnail","6":"contributors-categories-interactive-brokers","7":"traders-academy-intermediate-trading","8":"traders-academy-level","9":"traders-academy-trading-lesson"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Archives | Traders&#039; Academy | IBKR Campus<\/title>\n<meta name=\"description\" content=\"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/trading-lessons\/9417\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Placing Orders\" \/>\n<meta property=\"og:description\" content=\"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the event any precautions are triggered.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus EU\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-17T19:12:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.eu\/campus\/wp-content\/uploads\/sites\/3\/2024\/01\/cp-api2.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/\",\n\t            \"name\": \"Placing Orders - IBKR Campus EU\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.eu\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/3\\\/2024\\\/01\\\/cp-api2.jpg\",\n\t            \"datePublished\": \"2023-07-25T15:31:16+00:00\",\n\t            \"dateModified\": \"2024-01-17T19:12:29+00:00\",\n\t            \"description\": \"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the event any precautions are triggered.\",\n\t            \"breadcrumb\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/#breadcrumb\"\n\t            },\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/3\\\/2024\\\/01\\\/cp-api2.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/3\\\/2024\\\/01\\\/cp-api2.jpg\",\n\t            \"width\": 1920,\n\t            \"height\": 1080,\n\t            \"caption\": \"tws api illustration\"\n\t        },\n\t        {\n\t            \"@type\": \"BreadcrumbList\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/placing-orders\\\/#breadcrumb\",\n\t            \"itemListElement\": [\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 1,\n\t                    \"name\": \"Home\",\n\t                    \"item\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/\"\n\t                },\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 2,\n\t                    \"name\": \"Academy Lessons\",\n\t                    \"item\": \"https:\\\/\\\/www.interactivebrokers.eu\\\/campus\\\/trading-lessons\\\/\"\n\t                },\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 3,\n\t                    \"name\": \"Placing Orders\"\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.eu\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.eu\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus EU\",\n\t            \"description\": \"\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.eu\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Archives | Traders' Academy | IBKR Campus","description":"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/trading-lessons\/9417\/","og_locale":"en_US","og_type":"article","og_title":"Placing Orders","og_description":"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the event any precautions are triggered.","og_url":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/","og_site_name":"IBKR Campus EU","article_modified_time":"2024-01-17T19:12:29+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/www.interactivebrokers.eu\/campus\/wp-content\/uploads\/sites\/3\/2024\/01\/cp-api2.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/","url":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/","name":"Placing Orders - IBKR Campus EU","isPartOf":{"@id":"https:\/\/ibkrcampus.eu\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.eu\/campus\/wp-content\/uploads\/sites\/3\/2024\/01\/cp-api2.jpg","datePublished":"2023-07-25T15:31:16+00:00","dateModified":"2024-01-17T19:12:29+00:00","description":"We will discuss how to place a market, limit, and stop order in the Client Portal API. We will also be discussing how to confirm status messages in the event any precautions are triggered.","breadcrumb":{"@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/#primaryimage","url":"https:\/\/www.interactivebrokers.eu\/campus\/wp-content\/uploads\/sites\/3\/2024\/01\/cp-api2.jpg","contentUrl":"https:\/\/www.interactivebrokers.eu\/campus\/wp-content\/uploads\/sites\/3\/2024\/01\/cp-api2.jpg","width":1920,"height":1080,"caption":"tws api illustration"},{"@type":"BreadcrumbList","@id":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/placing-orders\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.interactivebrokers.eu\/campus\/"},{"@type":"ListItem","position":2,"name":"Academy Lessons","item":"https:\/\/www.interactivebrokers.eu\/campus\/trading-lessons\/"},{"@type":"ListItem","position":3,"name":"Placing Orders"}]},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.eu\/campus\/#website","url":"https:\/\/ibkrcampus.eu\/campus\/","name":"IBKR Campus EU","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.eu\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/trading-lessons\/9417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/trading-lessons"}],"about":[{"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/types\/trading-lessons"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/users\/850"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/comments?post=9417"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/trading-lessons\/9417\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/media\/9414"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/media?parent=9417"}],"wp:term":[{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/contributors-categories?post=9417"},{"taxonomy":"traders-academy","embeddable":true,"href":"https:\/\/ibkrcampus.eu\/campus\/wp-json\/wp\/v2\/traders-academy?post=9417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}