Requesting historical data through TWS API with Java Part 2

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 Requesting historical data through TWS API with 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 build off of the source code from Requesting historical data through TWS API with Java and add code save the historical data in a comma separated format for use with backtesting. This will be a very small and easy change.

The first thing to do is create a global variable named globalName by adding the following line to the top of the program:

String globalName="MSFT";

The next step is to change the following piece of code to use the globalName variable:

contract.m_symbol = globalName;

Next we need to prepare the program for saving to a file, so add this line to the very top of the program:

import java.io.*;

Next we need to replace the printing statement in the historicalData() function with the following lines:

try
{
BufferedWriter out = new BufferedWriter(new FileWriter("c:\\javadata.txt",true));
out.write(globalName+","+date+","+open+","+high+","+low+","+close+","+volume+"\r\n");
out.close();

}
catch(Exception e){};

Now you should be able to use this code, as a building block for any tools you may need for gathering historical data.

Download the full source for this project Here

 

 

 

 

 

Copyright© 2007 StockBotProgramming.com