英文:
Interactive Broker Java API returns nothing from historicalData
问题
以下是代码的翻译部分:
假设以下代码将返回并打印历史市场数据列表,如开盘价、收盘价、最高价、最低价和成交量。与交互经纪API的连接应该正常。但在Java控制台中,它没有打印来自回调的市场数据。看起来它没有经过historicalData函数。如何激活该函数并将市场数据存储在对象中?
void run() {
m_client.eConnect("localhost", 7497, 123);
final EReader reader = new EReader(m_client, m_signal);
reader.start();
new Thread() {
@Override
public void run() {
while (m_client.isConnected()) {
m_signal.waitForSignal();
try {
reader.processMsgs();
} catch (Exception e) {
System.out.println("异常:" + e.getMessage());
}
}
}
}.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void nextValidId(int orderId) {
System.out.println("id " + orderId);
nextOrderID = orderId;
//Contract c = new StkContract("AAPL");
Contract c = new Contract();
c.symbol("EUR");
c.exchange("IDEALPRO");
c.secType("CASH");
c.currency("USD");
m_client.reqHistoricalData(1, c,
LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)+ " 16:00:00",
"1 D", "1 min", "MIDPOINT", 1, 1, false, null);
m_client.reqMktData(1, c, "", false, false, null);
}
@Override
public void error(int id, int errorCode, String errorMsg) {
System.out.println(id + " " + errorCode + " " + errorMsg);
}
@Override
public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
//如果在下一个日历日运行,这将起作用
if (LocalDate.now().minusDays(1).format(DateTimeFormatter.BASIC_ISO_DATE).equals(date)){
this.date = date;
this.high = high;
this.low = low;
this.open = open;
this.close = close;
System.out.println(date + " h: " + high + " l: " +low);
}
System.out.println("正在请求数据...");
System.out.println(EWrapperMsgGenerator.historicalData(reqId, date, open, high, low, close, volume, count, WAP));
System.out.println("数据输入结束...");
}
希望这对你有所帮助。如果你有其他问题,请随时提出。
英文:
Supposed the below code would returns and print a list of historical market data like the open, close, high, low, volume. The connection to Interactive Broker API should be fine. But in Java console, it did not print the market data from callbacks. Looks like it did not go through the historicalData function. How do I activate that function and store the market data in an object?
id 1
-1 2104 Market data farm connection is OK:usfarm.nj
-1 2104 Market data farm connection is OK:cashfarm
-1 2104 Market data farm connection is OK:usfarm
-1 2106 HMDS data farm connection is OK:cashhmds
-1 2106 HMDS data farm connection is OK:hkhmds
-1 2106 HMDS data farm connection is OK:ushmds
-1 2158 Sec-def data farm connection is OK:secdefil
...
Sample Code:
void run() {
m_client.eConnect("localhost", 7497, 123);
final EReader reader = new EReader(m_client, m_signal);
reader.start();
new Thread() {
@Override
public void run() {
while (m_client.isConnected()) {
m_signal.waitForSignal();
try {
reader.processMsgs();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
}.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void nextValidId(int orderId) {
System.out.println("id "+orderId);
nextOrderID = orderId;
//Contract c = new StkContract("AAPL");
Contract c = new Contract();
c.symbol("EUR");
c.exchange("IDEALPRO");
c.secType("CASH");
c.currency("USD");
m_client.reqHistoricalData(1, c,
LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE)+ " 16:00:00",
"1 D", "1 min", "MIDPOINT", 1, 1, false, null);
m_client.reqMktData(1, c, "", false, false, null);
}
@Override
public void error(int id, int errorCode, String errorMsg) {
System.out.println(id + " " + errorCode + " " + errorMsg);
}
@Override
public void historicalData(int reqId, String date, double open, double high, double low, double close, int volume, int count, double WAP, boolean hasGaps) {
//if being run on the next calendar day, this works
if (LocalDate.now().minusDays(1).format(DateTimeFormatter.BASIC_ISO_DATE).equals(date)){
this.date = date;
this.high = high;
this.low = low;
this.open = open;
this.close = close;
System.out.println(date + " h: " + high + " l: " +low);
}
System.out.println("Requesting Data...");
System.out.println(EWrapperMsgGenerator.historicalData(reqId, date, open, high, low, close, volume, count, WAP));
System.out.println("Data Input Ended...");
}
专注分享java语言的经验与见解,让所有开发者获益!
评论