{"id":116840,"date":"2021-12-23T10:00:00","date_gmt":"2021-12-23T15:00:00","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=116840"},"modified":"2022-11-21T09:50:20","modified_gmt":"2022-11-21T14:50:20","slug":"historical-options-futures-data-using-tws-api","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/","title":{"rendered":"Historical Options &#038; Futures Data using TWS API"},"content":{"rendered":"\n<p>The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures. While there are many good resources available online explaining how to extract historical data for stocks, I was unable to find any good resource for derivatives. Since many of my students regularly reach out to me seeking help with the same, I have decided to write this two-part article delving into this topic.&nbsp;<\/p>\n\n\n\n<p>While the approach&nbsp;to extract historical data for derivatives and stocks&nbsp;is similar, there are some derivative specific peculiarities which could make things tricky. In addition, getting historical data for expired derivatives contracts is a challenge because IB does not provide historical data for expired options contracts&nbsp;and provides historical data for expired futures contracts&nbsp;only going back&nbsp;a few years. However, there is a way to extract historical data for expired contracts&nbsp;as well. Don\u2019t believe&nbsp;me? Refer to the below screenshot wherein I have extracted the data for an option contract which expired&nbsp;a few months back.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"300\" height=\"231\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-1.png\" alt=\"\" class=\"wp-image-116913 lazyload\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 300px; aspect-ratio: 300\/231;\"><\/figure>\n\n\n\n<p>But before you get too excited about raiding the historical data for expired options, let me warn you that the approach has a learning curve and therefore&nbsp;it is important that we&nbsp;go step by step. By the end of this two-part article you should become reasonably proficient in extracting historical data for both live and expired derivatives contracts. In this first part, let us take care of the basics required to extract derivatives data using TWS API.&nbsp;<\/p>\n\n\n\n<p>This article assumes that you have basic understanding about TWS API. If not, please refer&nbsp;to <a href=\"\/campus\/ibkr-quant-news\/implementing-market-scanners-using-tws-api-part-i\/\" target=\"_blank\" rel=\"noreferrer noopener\">my earlier article<\/a>&nbsp;about implementing market scanner using TWS API where I have explained the basics of TWS API and the building blocks of a TWS API based trading application.&nbsp;<\/p>\n\n\n\n<p><strong>Building the Application<\/strong>&nbsp;<\/p>\n\n\n\n<p>Before we create a trading application that requests historical data for the given derivative contract, let us build our boilerplate trading application.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-2.png\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\"><\/figure>\n\n\n\n<p>As explained in my previous article, when executed, this trading application will simply create a websocket connection with your&nbsp;locally running&nbsp;TWS software. However, since we have not requested any information or sent any commands to the TWS in the above code, this application will not do anything much.&nbsp;<\/p>\n\n\n\n<p>To request historical data from the TWS, we need to execute the&nbsp;reqHistoricalData()&nbsp;Eclient&nbsp;function. We need to pass some arguments to the&nbsp;reqHistoricalData() function so that TWS understands the scope of our request.&nbsp;We will get into the details of this function in a bit but as&nbsp;of now it is important to note that one of&nbsp;the most&nbsp;critical&nbsp;arguments&nbsp;to be passed to this function is the contract for which you want to extract data.&nbsp;<\/p>\n\n\n\n<p><strong>Quick Introduction to&nbsp;IBAPI Contract Class<\/strong>&nbsp;<\/p>\n\n\n\n<p>IBAPI\u2019s contract class lets us define the contract to be used in our trading application. Once&nbsp;we define the contract, we can perform operations such as requesting historical data, submitting orders etc.&nbsp;for that&nbsp;contract.&nbsp;To&nbsp;create an object of the contract class&nbsp;we need to define&nbsp;the relevant parameters&nbsp;that helps TWS identify the exact contract. For example, the&nbsp;critical&nbsp;parameters required to identify a stock are the stock symbol,&nbsp;the security type, the exchange where it is listed&nbsp;and&nbsp;the currency in which it trades. However, the above parameters are not sufficient for derivatives since each derivative contract has an expiry date&nbsp;and multiple&nbsp;derivative&nbsp;contracts&nbsp;for a given&nbsp;underlying&nbsp;may be&nbsp;trading simultaneously. Therefore, it is important that we create our contract by defining the required parameters. TWS API documentation has a very useful section wherein sample contract definition for a number of products is provided&nbsp;<a href=\"https:\/\/interactivebrokers.github.io\/tws-api\/basic_contracts.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/interactivebrokers.github.io\/tws-api\/basic_contracts.html<\/a>&nbsp;and it may be a good idea to refer to this resource.&nbsp;<\/p>\n\n\n\n<p>Now how do we get information on all the parameters required to define a contract?&nbsp;There are a couple of ways to do that. We can add the contract in the TWS monitor panel and then select \u201cdescription\u201d by right clicking on the contract. This will provide all the details pertaining to the contract. Refer to the below screenshots.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1100\" height=\"551\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3-1100x551.png\" alt=\"\" class=\"wp-image-116926 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3-1100x551.png 1100w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3-700x351.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3-300x150.png 300w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3-768x385.png 768w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-3.png 1355w\" data-sizes=\"(max-width: 1100px) 100vw, 1100px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1100px; aspect-ratio: 1100\/551;\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"666\" height=\"493\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-4.png\" alt=\"\" class=\"wp-image-116929 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-4.png 666w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-4-300x222.png 300w\" data-sizes=\"(max-width: 666px) 100vw, 666px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 666px; aspect-ratio: 666\/493;\" \/><\/figure>\n\n\n\n<p>We can also access contract details by searching for the contract in the IB Contract &amp; Symbol Database (<a href=\"https:\/\/www.interactivebrokers.com\/en\/index.php?f=463\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.interactivebrokers.com\/en\/index.php?f=463<\/a>)&nbsp;<\/p>\n\n\n\n<p>It is important to note that we need to purchase the relevant market data pack from IB in order to access historical data.&nbsp;Thankfully, the subscription fee tends to be quite reasonable and you can subscribe to whichever market data you&nbsp;are interested in&nbsp;from your IB account settings&gt;&gt;User Settings&gt;&gt;Market Data Subscription.&nbsp;While I am not subscribed to the futures market data from the&nbsp;Globex&nbsp;exchange, I do have subscription to the Futures data from India\u2019s National Stock Exchange&nbsp;and that is what I will be using for this demo. Let us&nbsp;now&nbsp;improve our trading application by creating the contract for which we want to extract historical data.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"468\" height=\"397\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-5.png\" alt=\"\" class=\"wp-image-116935 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-5.png 468w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-5-300x254.png 300w\" data-sizes=\"(max-width: 468px) 100vw, 468px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 468px; aspect-ratio: 468\/397;\" \/><\/figure>\n\n\n\n<p>As you can see in the above code snippet, I first imported the Contract class from IBAPI which&nbsp;allows&nbsp;me create objects&nbsp;of the Contract class.&nbsp;I then created an object of the Contract class and proceeded to define the parameters that would help TWS identify the exact&nbsp;futures contract that I am interested in. Now that we have taken care of the most important argument of the&nbsp;reqHistoricalData() let us get into the details of this function.&nbsp;<\/p>\n\n\n\n<p><strong>Quick Introduction to&nbsp;the Historical Data API<\/strong>&nbsp;<\/p>\n\n\n\n<p>TWS&nbsp;API\u2019s&nbsp;reqHistoricalData&nbsp;function&nbsp;provides historical data in the form of successive \u201cbars\u201d. Each bar&nbsp;(or candle)&nbsp;contains the \u201cOpen\u201d, \u201cHigh\u201d, \u201cLow\u201d, \u201cClose\u201d and \u201cVolume\u201d information for a specific time duration in the past. Refer to the the documentation on historical data&nbsp;<a href=\"https:\/\/interactivebrokers.github.io\/tws-api\/historical_bars.html\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/interactivebrokers.github.io\/tws-api\/historical_bars.html<\/a>&nbsp;which should provide a good overview of the historical data API.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Let us now expand our trading application code further and add the&nbsp;reqHistoricalData() function.&nbsp;<\/p>\n\n\n\n<p>The parameters&nbsp;for this&nbsp;function are:&nbsp;<\/p>\n\n\n\n<p><em>reqID<\/em>&nbsp;\u2013 This is the request number which helps TWS identify one request from another&nbsp;<\/p>\n\n\n\n<p><em>contract<\/em>&nbsp;\u2013 This is the object of Contract class for which historical data is requested&nbsp;<\/p>\n\n\n\n<p><em>endDateTime<\/em>&nbsp;\u2013 This is the time (in YYYYMMDD format) until which historical data is required. Empty&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string passed as an argument&nbsp;is interpreted as request to&nbsp;provide data till the current moment.&nbsp;<\/p>\n\n\n\n<p><em>durationStr<\/em>&nbsp;\u2013 The duration or length or time for which data is required (Refer to historical data documentation for valid&nbsp;values)&nbsp;&nbsp;<\/p>\n\n\n\n<p>barSizeSetting&nbsp;\u2013 The granularity of data requested&nbsp;(Refer to historical data documentation for valid&nbsp;values)&nbsp;<\/p>\n\n\n\n<p>whatToShow&nbsp;\u2013 The method of creating bar data from bid and trade data e.g. bars created using bid and ask, mid-point, adjusted last price etc.&nbsp;(Refer to historical data documentation for valid&nbsp;values)&nbsp;<\/p>\n\n\n\n<p>UseRTH&nbsp;\u2013 Whether you want data within regular trading hours (1) or want pre and post market data as well (0)&nbsp;<\/p>\n\n\n\n<p>fotmatDate&nbsp;\u2013 The date format to be returned (use 1)&nbsp;<\/p>\n\n\n\n<p>keepUpToDate&nbsp;\u2013 Whether you want unfinished real-time bars&nbsp;(1)&nbsp;or not&nbsp;(0).&nbsp;<\/p>\n\n\n\n<p>chartOptions&nbsp;\u2013 Documentation does not provide any detail on this parameter so safe to pass blank list.&nbsp;<\/p>\n\n\n\n<p>Below is our updated code:&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"601\" height=\"523\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-6.png\" alt=\"\" class=\"wp-image-116940 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-6.png 601w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-6-300x261.png 300w\" data-sizes=\"(max-width: 601px) 100vw, 601px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 601px; aspect-ratio: 601\/523;\" \/><\/figure>\n\n\n\n<p>As you can see In the above code, we&nbsp;have&nbsp;applied the&nbsp;reqHistoricalData() function to our trading application and passed&nbsp;the relevant arguments&nbsp;to request 30 minute bars for the last 300 days of the future contract of Infosys expiring on 27<sup>th<\/sup>&nbsp;Jan 2022.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Now you may ask, where have we defined this&nbsp;reqHistoricalData&nbsp;function? Well, this function is already defined in the&nbsp;EClient&nbsp;class and when we created&nbsp;our trading application&nbsp;(TradeApp), we inherited&nbsp;from the&nbsp;EClient&nbsp;and the&nbsp;Ewrapper&nbsp;classes and therefore have access to&nbsp;all&nbsp;the functions already defined in these&nbsp;classes.&nbsp;The relevant&nbsp;EClient&nbsp;and&nbsp;EWrapper&nbsp;functions for various applications (such as requesting historical data, streaming market data, placing orders etc.) can be obtained from the TWS API documentation (https:\/\/interactivebrokers.github.io\/tws-api\/index.html)&nbsp;<\/p>\n\n\n\n<p>In the above code, we&nbsp;have used&nbsp;the&nbsp;historicalData()&nbsp;Ewrapper&nbsp;function&nbsp;in our trading application. This function handles the response from TWS once we execute the&nbsp;app.reqHistoricalData() function. The TWS returns the&nbsp;reqID&nbsp;and bar information which can be manipulated in the&nbsp;historicalData() function. In the above code, I am simply printing out the received data but you can store the data in your desired data structure&nbsp;(topic for later). Below is the partial output that we receive once we run the updated code.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"712\" height=\"378\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-7.png\" alt=\"\" class=\"wp-image-116947 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-7.png 712w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-7-700x372.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-7-300x159.png 300w\" data-sizes=\"(max-width: 712px) 100vw, 712px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 712px; aspect-ratio: 712\/378;\" \/><\/figure>\n\n\n\n<p><strong>Quick Introduction to&nbsp;the Historical Data API<\/strong>&nbsp;<\/p>\n\n\n\n<p>Let us&nbsp;now&nbsp;change our contract object and try to extract historical data for an expired future contract. Let me change the&nbsp;<em>lastTradeDateOrContractMonth<\/em>&nbsp;attribute of the contract object to&nbsp;a date in the past.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"366\" height=\"94\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-8.png\" alt=\"\" class=\"wp-image-116951 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-8.png 366w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-8-300x77.png 300w\" data-sizes=\"(max-width: 366px) 100vw, 366px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 366px; aspect-ratio: 366\/94;\" \/><\/figure>\n\n\n\n<p>Below is the output when we run our updated code.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"457\" height=\"15\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-9.png\" alt=\"\" class=\"wp-image-116956 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-9.png 457w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-9-300x10.png 300w\" data-sizes=\"(max-width: 457px) 100vw, 457px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 457px; aspect-ratio: 457\/15;\" \/><\/figure>\n\n\n\n<p>As expected, TWS does not identify the expired Future contract. Thankfully, all we need to do here is add another attribute,&nbsp;<em>includeExpired,<\/em>&nbsp;to our contract object which will instruct TWS not to be&nbsp;facetious and provide the data for the expired contract.&nbsp;Please note that IB keeps data of expired contracts going back only a few years (I think it is two years but you may want to double check).&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image img-twothird\"><img decoding=\"async\" width=\"357\" height=\"106\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-10.png\" alt=\"\" class=\"wp-image-116961 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-10.png 357w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-10-300x89.png 300w\" data-sizes=\"(max-width: 357px) 100vw, 357px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 357px; aspect-ratio: 357\/106;\" \/><\/figure>\n\n\n\n<p>Below is the partial output with the updated code.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"725\" height=\"269\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-11.png\" alt=\"\" class=\"wp-image-116965 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-11.png 725w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-11-700x260.png 700w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/Historical-Options-Futures-Data-using-TWS-API-11-300x111.png 300w\" data-sizes=\"(max-width: 725px) 100vw, 725px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 725px; aspect-ratio: 725\/269;\" \/><\/figure>\n\n\n\n<p><strong>Summary &amp; Next Steps<\/strong>&nbsp;<\/p>\n\n\n\n<p>In this article we gained an understanding of the TWS API tools required to extract historical data. We also gained proficiency in creating and tweaking the contract object and learnt how to extract historical data for both live and expired futures contracts. In the follow up article, we will discuss historical data for options which has some additional challenges.&nbsp;<\/p>\n\n\n\n<p>The author can be reached at&nbsp;<strong>contact@rasuquant.com<\/strong>&nbsp;<\/p>\n\n\n\n<p>If you found this article interesting, you may&nbsp;consider enrolling in&nbsp;my&nbsp;Interactive Brokers API centric&nbsp;courses on&nbsp;<a href=\"https:\/\/learning.rasuquant.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/learning.rasuquant.com<\/a>.&nbsp;The courses range from&nbsp;basics of TWS API&nbsp;to advanced concepts that will&nbsp;help you&nbsp;backtest and&nbsp;build fully automated trading applications on Interactive Broker\u2019s platform.&nbsp;&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures. <\/p>\n","protected":false},"author":565,"featured_media":116969,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[340,338,350,341,9563,344],"tags":[1014,1015,575],"contributors-categories":[13715],"class_list":{"0":"post-116840","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-api-development","8":"category-ibkr-quant-news","9":"category-quant-asia-pacific","10":"category-quant-development","11":"category-options-quant","12":"category-quant-regions","13":"tag-eclient","14":"tag-ewrapper","15":"tag-ibkr-api","16":"contributors-categories-rasuquant"},"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.7) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Historical Options &#038; Futures Data using TWS API<\/title>\n<meta name=\"description\" content=\"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its...\" \/>\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\/posts\/116840\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Historical Options &amp; Futures Data using TWS API | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-23T15:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:50:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mayank Rasu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mayank Rasu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 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\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Mayank Rasu\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/41a7b1d2d6971906a0d461646888e9fc\"\n\t            },\n\t            \"headline\": \"Historical Options &#038; Futures Data using TWS API\",\n\t            \"datePublished\": \"2021-12-23T15:00:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:20+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/\"\n\t            },\n\t            \"wordCount\": 1880,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/12\\\/world-charts.jpg\",\n\t            \"keywords\": [\n\t                \"EClient\",\n\t                \"EWrapper\",\n\t                \"IBKR API\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"IBKR API Development\",\n\t                \"Quant\",\n\t                \"Quant Asia Pacific\",\n\t                \"Quant Development\",\n\t                \"Quant Options\",\n\t                \"Quant Regions\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/\",\n\t            \"name\": \"Historical Options & Futures Data using TWS API | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/12\\\/world-charts.jpg\",\n\t            \"datePublished\": \"2021-12-23T15:00:00+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:50:20+00:00\",\n\t            \"description\": \"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/\"\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.com\\\/campus\\\/ibkr-quant-news\\\/historical-options-futures-data-using-tws-api\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/12\\\/world-charts.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/12\\\/world-charts.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"Quant\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/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            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/41a7b1d2d6971906a0d461646888e9fc\",\n\t            \"name\": \"Mayank Rasu\",\n\t            \"sameAs\": [\n\t                \"https:\\\/\\\/rasuquant.com\"\n\t            ],\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/mayankrasu\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Historical Options &#038; Futures Data using TWS API","description":"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its...","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\/posts\/116840\/","og_locale":"en_US","og_type":"article","og_title":"Historical Options & Futures Data using TWS API | IBKR Quant Blog","og_description":"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/","og_site_name":"IBKR Campus US","article_published_time":"2021-12-23T15:00:00+00:00","article_modified_time":"2022-11-21T14:50:20+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","type":"image\/jpeg"}],"author":"Mayank Rasu","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mayank Rasu","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/"},"author":{"name":"Mayank Rasu","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/41a7b1d2d6971906a0d461646888e9fc"},"headline":"Historical Options &#038; Futures Data using TWS API","datePublished":"2021-12-23T15:00:00+00:00","dateModified":"2022-11-21T14:50:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/"},"wordCount":1880,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","keywords":["EClient","EWrapper","IBKR API"],"articleSection":["IBKR API Development","Quant","Quant Asia Pacific","Quant Development","Quant Options","Quant Regions"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/","name":"Historical Options & Futures Data using TWS API | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","datePublished":"2021-12-23T15:00:00+00:00","dateModified":"2022-11-21T14:50:20+00:00","description":"The ability to extract and analyze historical data efficiently is critical for the success of any quantitative strategy. Thankfully, TWS API affords its users the ability to extract historical data at scale for a number of products including derivatives such as options and futures.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/historical-options-futures-data-using-tws-api\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","width":1000,"height":563,"caption":"Quant"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/41a7b1d2d6971906a0d461646888e9fc","name":"Mayank Rasu","sameAs":["https:\/\/rasuquant.com"],"url":"https:\/\/www.interactivebrokers.com\/campus\/author\/mayankrasu\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/12\/world-charts.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/116840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/565"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=116840"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/116840\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/116969"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=116840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=116840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=116840"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=116840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}