Requesting historical data through TWS API with Java

Author: Joe
Tutorial Objective: Request historical quote data from TWS and print the data to the screen..
Souce Code:  Download the full source for this tutorial Here

Disclaimer

This tutorial is to be used at your own risk. By using this tutorial you agree not to hold the writers of this tutorial, StockBotProgramming.com, or anyone affiliated with StockBotProgrammign.com liable for any damages that may occur as a result of using this tutorial and/or provided source code.

This tutorial will assume you are using Java NetBeans, however if you are familiar with Eclipse the same ideas apply. You will also need the TWS API, which is available for download Here.

It is highly recommended that you read Connecting to Interactive Brokers TWS Using Java before attempting this tutorial. If you have any suggestions regarding this tutorial or if you notice any mistakes please suggest it here.

Today we are going to modify our code to read historical data from MSFT and print it to the screen. Go ahead and open up your code from Connecting to Interactive Brokers TWS Using Java. If you did not complete the tutorial, you can download the full source code at the end of its page.

The first thing we will want to do is add the following line into the function error:

System.out.println("Error: "+errorMsg);

We do this so that we will know if any errors happen while attempting to request our data. Now go to the main function. We will need to specify a security that we wish to get data for. Add the following lines:

Contract contract;
contract = new Contract();

contract.m_symbol = "MSFT";
contract.m_secType = "STK";
contract.m_expiry = "";
contract.m_right = "";
contract.m_multiplier = "";
contract.m_exchange = "SMART";
contract.m_primaryExch = "";
contract.m_currency = "USD";
contract.m_localSymbol = "";

The above code specifies which security we want to retrieve historical data for. The most commonly changed attributes are:

  • m_symbol: This is the symbol for the security.
  • m_sectype: This is the type of security, valid values are: "STK","FUT","OPT","IND","FOP","CASH"
  • m_exchange: Here you specify what exchange the security trades on.
  • m_currency: What currency the security trades in.


Now we have the security defined. The next step is to simply make the request. To do so add the line:

m_client.reqHistoricalData(0,contract,"20070429 16:26:44", "3600 S","1 min","TRADES", 1, 1);

  • Parameter 1: This is your request ID. This ID needs to be unique. Used later so you know which request the incomming data belongs to and used if you need to cancel the request. In our example we just used 1. However, if you went to make a second request in the same program you need to change the ID to something new each time.
  • Parameter 2: The security contract that we defined a few steps before.
  • Parameter 3: End Date, this specifies the date at which you want the historical data query to end. The format for the date is: yyyymmdd HH:mm:ss zzz The "zzz" section is optional and is for timezone.
  • Parameter 4: Duration, in my example I am specifying 3600 seconds which is 60 minutes. The value is entered by an integer value followed by either "S" for seconds, "D" for days, or "W" for week
  • Parameter 5: Bar Size, this is what size each data bar sent will be in. Valid values are 1 secs, 5 secs, 10 secs, 15 secs, 30 secs, 1 min, 2 mins, 5 mins, 10 mins, 15 mins, 30 mins,1 hour, 1 day, 1 week, 1 month, 3 months, 1 year
  • Parameter 6: What to show. This field specifies what data to show, such as "TRADES", "MIDPOINT", "BID","ASK","BID/ASK"
  • Parameter 7: Use real time hours. Set this value to "1" if you want data only from the real time hours. If you don't care if the data comes from pre or after market use "0"
  • Parameter 8: Date format. You have 2 choices in how the data is returned for each data bar. "1" means the date will be in the format yyyymmdd{space}{space}hh:mm:dd and "2" means the date will be a timestamp.
Now our historical request has been made. Our next step is to make sure our program does not terminate before our data comes in. Add the following lines after the request:

while(1==1)
{
}

We are almost done. The last thing we need to do is add the following line to our historicalData function.

System.out.println("Data Date: "+date+" Open: "+open+" High: "+high+" Low: "+low+" Close: "+close+" Volume: "+volume);

Now make sure TWS is running and run this program. You should see quotes printed to the screen.

Download the full source for this project Here

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com