开云体育

ctrl + shift + ? for shortcuts
© 2025 开云体育

How to make the Java API work?


philipyeh
 

I am a new user of the TWS Java API.
I wrote a test program to test the reqMktData() method.
But I can not get any ticks data. What's problem in my
program? Could anybody help me?
Below is my test java program:

import com.ib.client.*;

public class ATMClient implements EWrapper
{
static final int BID_SIZE = 0;
static final int BID = 1;
static final int ASK = 2;
static final int ASK_SIZE = 3;
static final int LAST = 4;
static final int LAST_SIZE = 5;
EClientSocket m_client = new EClientSocket(this);
int m_id = 100000;
int o_id = 200000;


public static void main (String[] args)
{
ATMClient myapp = new ATMClient();
myapp.start("gw4.ibllc.com", "4000");
}

public void start(String host, String port)
{
m_client.eConnect(host, Integer.parseInt(port));
System.out.println("connect ok!");
Contract m_contract = new Contract();
m_contract.symbol = "ESU2";
m_contract.secType = "FUT";
m_contract.expiry = "200209";
m_contract.strike = 0.0;
m_contract.right = "C";
m_contract.exchange = "GLOBEX";
m_contract.currency = "USD";
m_client.reqMktData(m_id, m_contract);
}


public void tickPrice( int tickerId, int field, double price) {
// received price tick
System.out.println("id=" + tickerId + " " + getField(field)
+ "=" + price);
}

public void tickSize( int tickerId, int field, int size) {
// received size tick
System.out.println("id=" + tickerId + " " + getField(field)
+ "=" + size);
}

public void orderStatus(int orderId, String status, int filled,
int remaining, double price, int permId) {
// received order status
System.out.println("id=" + orderId + " status=" + status
+ " filled=" + filled + " remaining=" + remaining + " price=" +
price + " permId=" + permId);

// make sure id for next order is at least orderId+1
++o_id;
}

public void openOrder( int orderId, Contract contract, Order
order) {
// received open order
System.out.println( "id=" + orderId + " " + order.action
+ " " + order.quantity + " " + contract.symbol);
}

public void nextValidId(int orderId) {
// received next valid order id
o_id = orderId;
}

public void error(String str) {
// received error
System.out.println("*** Error: " + str);
}

public void connectionClosed() {
System.out.println("Connection Closed");
}

private String getField(int tickType) {
switch (tickType) {
case BID_SIZE: return "bidSize";
case BID: return "bidPrice";
case ASK: return "askPrice";
case ASK_SIZE: return "askSize";
case LAST: return "lastPrice";
case LAST_SIZE: return "lastSize";
default: return "unknown";
}
}

public void updateAccountValue(String key,String value,String
currency) {
System.out.println("key=" + key + ", value=" + value + ",
currency=" + currency);
}

public void updatePortfolio(Contract contract, int position,
double marketPrice, double marketValue) {
System.out.println("position=" + position + ", marketPrice="
+ marketPrice + ", marketValue=" + marketValue);
}

public void updateAccountTime(String timeStamp) {
System.out.println("timeStamp=" + timeStamp);
}

}

Thanks.
Philip Yeh.


marinindextrader
 

Not hip to Java, But It looks as if....
.....
.....

I dont code in Java but it seems as if you havnt made a regMktData
call...or am I missing something?

Further I have been unsuccesful passing anything other than variable
values through the reqMkt...

It appears as if your reqMkt object is expecting an object itself as
as an arguement>>> object m_contract...I have tried passing arrays as
arguements and get rejected...

And, the symbol appears incorrect:

ESU2 should be ES

You dont need anthing in the field .right

"C" should be ""

You dont need anything in the field currencey

"USD" should be ""


Here is a literal example of reqMkt call in VB if this helps:

symbol = "ES"
secType = "FUT"
expiry = "200209"
strike = 0
TWSright = ""
exchange = "GLOBEX"
curency = ""

Tws1.reqMktData id, symbol, secType, expiry, strike, TWSright,
exchange, curency

--- In twsapi@y..., "philipyeh" <philipyeh@y...> wrote:
I am a new user of the TWS Java API.
I wrote a test program to test the reqMktData() method.
But I can not get any ticks data. What's problem in my
program? Could anybody help me?
Below is my test java program:

import com.ib.client.*;

public class ATMClient implements EWrapper
{
static final int BID_SIZE = 0;
static final int BID = 1;
static final int ASK = 2;
static final int ASK_SIZE = 3;
static final int LAST = 4;
static final int LAST_SIZE = 5;
EClientSocket m_client = new EClientSocket(this);
int m_id = 100000;
int o_id = 200000;


public static void main (String[] args)
{
ATMClient myapp = new ATMClient();
myapp.start("gw4.ibllc.com", "4000");
}

public void start(String host, String port)
{
m_client.eConnect(host, Integer.parseInt(port));
System.out.println("connect ok!");
Contract m_contract = new Contract();
m_contract.symbol = "ESU2";
m_contract.secType = "FUT";
m_contract.expiry = "200209";
m_contract.strike = 0.0;
m_contract.right = "C";
m_contract.exchange = "GLOBEX";
m_contract.currency = "USD";
m_client.reqMktData(m_id, m_contract);
}


public void tickPrice( int tickerId, int field, double price) {
// received price tick
System.out.println("id=" + tickerId + " " + getField
(field)
+ "=" + price);
}

public void tickSize( int tickerId, int field, int size) {
// received size tick
System.out.println("id=" + tickerId + " " + getField
(field)
+ "=" + size);
}

public void orderStatus(int orderId, String status, int filled,
int remaining, double price, int permId) {
// received order status
System.out.println("id=" + orderId + " status=" + status
+ " filled=" + filled + " remaining=" + remaining + " price=" +
price + " permId=" + permId);

// make sure id for next order is at least orderId+1
++o_id;
}

public void openOrder( int orderId, Contract contract, Order
order) {
// received open order
System.out.println( "id=" + orderId + " " + order.action
+ " " + order.quantity + " " + contract.symbol);
}

public void nextValidId(int orderId) {
// received next valid order id
o_id = orderId;
}

public void error(String str) {
// received error
System.out.println("*** Error: " + str);
}

public void connectionClosed() {
System.out.println("Connection Closed");
}

private String getField(int tickType) {
switch (tickType) {
case BID_SIZE: return "bidSize";
case BID: return "bidPrice";
case ASK: return "askPrice";
case ASK_SIZE: return "askSize";
case LAST: return "lastPrice";
case LAST_SIZE: return "lastSize";
default: return "unknown";
}
}

public void updateAccountValue(String key,String value,String
currency) {
System.out.println("key=" + key + ", value=" + value + ",
currency=" + currency);
}

public void updatePortfolio(Contract contract, int position,
double marketPrice, double marketValue) {
System.out.println("position=" + position + ",
marketPrice="
+ marketPrice + ", marketValue=" + marketValue);
}

public void updateAccountTime(String timeStamp) {
System.out.println("timeStamp=" + timeStamp);
}

}

Thanks.
Philip Yeh.