Requesting live quote data using TWS API and Java
Author: Joe
Tutorial Objective: Request live 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 live quote 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.
If you have already completed the java requesting historical quote
data tutorial then this one will seem very familiar. The first thing
we will want to do is add the following line into the function error
if you have not done so already:
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 live
quote 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 to the main function:
m_client.reqMktData(0,contract,"");
This function call has 2 very important parameters.
- Parameter 1: This is an ID value. Essentially anytime you make
a request for market data for a new security, give the function
a new id. When data for that security is returned it does not
tell you what symbol the data belongs to. Instead it will return
that ID back. You then match up your ID's to determine which symbol
the market data coming in goes to.
- Parameter 2: The contract structure that specifies which product
we wish to request data for.
Now that the request has been made we need to ensure that the program
will not exit before the quotes come in. Add the following lines:
while(1==1)
{
}
When live quote data comes in, the data will come in through one
of two functions. If the data deals with a price such as close,
last, bid, ask.... then the tickPrice() function is called. If the
data deals with a size such as volume, bid size, ask size... then
the tickSize() function will be called. Knowing this add the following
lines to the tickPrice() function:
System.out.println("Price Update: Id:
"+tickerId+" Type: "+field+" Price: "+price);
In the above print statement you see a variable called "field".
This value specifies which type of price data is being sent:
- field=9 means close price
Now that we can capture price data updates we need to add the following
line to the tickSize() function:
System.out.println("Volume Update: Id:
"+tickerId+" Type: "+field+" Size: "+size);
In this function the "field" value has the following
meanings:
- field=5 means last trade size
Now you should be able to make TWS send you all the live price/volume
information you need to make your trading decisions.
Download the full source for this tutorial Here
|