Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| Download
| RSS feed
package br.com.kronus.ibkr.robos;
import com.ib.client.*;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TesteRealtimeIBKR
extends DefaultEWrapper
{
private final EJavaSignal signal =
new EJavaSignal
();
private final EClientSocket client =
new EClientSocket
(this, signal
);
private EReader reader
;
private final int reqId =
1001;
public static void main
(String[] args
) {
new TesteRealtimeIBKR
().
iniciar();
}
public void iniciar
() {
System.
out.
println("Conectando ao TWS...");
client.
eConnect("127.0.0.1",
7496,
2); // 7496 = live, 7497 = paper
if (!client.
isConnected()) {
System.
err.
println("Falha ao conectar!");
return;
}
// tipo de market data: 1 = realtime (se assinado), 3 = delayed
client.
reqMarketDataType(1);
// Cria o EReader que vai processar as mensagens da IBKR
reader =
new EReader
(client, signal
);
reader.
start();
// Thread que fica processando as mensagens
new Thread(() -
> {
while (client.
isConnected()) {
try {
signal.
waitForSignal();
reader.
processMsgs();
} catch (Exception e
) {
e.
printStackTrace();
}
}
},
"IBKR-Reader-Thread").
start();
try { Thread.
sleep(1000); } catch (Exception ignored
) {}
// --------------------------------------
// CONTRATO DO MINI ÍNDICE (ajuste se precisar)
// --------------------------------------
Contract c =
new Contract
();
c.
symbol("PETR4"); // símbolo na IB
c.
secType("FUT");
c.
exchange("B3"); // teste "BMF" ou "BVMF" se der erro 200
c.
currency("BRL");
c.
lastTradeDateOrContractMonth("202512"); // Z25 = Dez/2025
// --------------------------------------
System.
out.
println("Solicitando market data tick-by-tick...");
client.
reqTickByTickData(reqId, c,
"Last",
0,
true);
}
// ===========================================
// CALLBACK DOS TICKS
// ===========================================
@
Override
public void tickByTickAllLast
(int reqId,
int tickType,
long time,
double price, Decimal size, TickAttribLast attrib,
String exchange,
String specialConditions
) {
ZonedDateTime zdt = Instant.
ofEpochSecond(time
)
.
atZone(ZoneId.
of("America/Maceio"));
String hora = zdt.
format(DateTimeFormatter.
ofPattern("HH:mm:ss"));
System.
out.
println(
"[TICK] Preço: " + price +
" | Hora IBKR: " + hora +
" | Epoch IBKR: " + time
);
long agora = Instant.
now().
getEpochSecond();
long difSegundos =
Math.
abs(agora - time
);
if (difSegundos
<=
5) {
System.
out.
println("🔥 DADOS EM TEMPO REAL! (OK)");
} else if (difSegundos
>=
800 && difSegundos
<=
1000) {
System.
out.
println("⏳ DADOS ATRASADOS ~15 MIN (DELAYED)");
} else {
System.
out.
println("⚠️ Diferença de tempo: " + difSegundos +
"s");
}
}
// ===========================================
// ERROS (apenas esse já basta)
// ===========================================
@
Override
public void error
(int id,
long code,
int arg2,
String arg3,
String msg
) {
System.
err.
println("IBKR ERROR: id=" + id +
" code=" + arg2 +
" msg=" + arg3
);
}
}