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 br.com.kronus.ibkr.api.IbkrMapper;
import java.time.*;
import java.time.format.DateTimeFormatter;
public class TesteMktDataSimples
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 TesteMktDataSimples
().
iniciar();
}
public void iniciar
() {
System.
out.
println("Conectando ao TWS...");
// MESMOS PARAMETROS QUE FUNCIONARAM NO TesteConexaoSimples
client.
eConnect("127.0.0.1",
7497,
2);
try { Thread.
sleep(2000); } catch (Exception ignored
) {}
System.
out.
println("isConnected = " + client.
isConnected());
if (!client.
isConnected()) {
System.
err.
println("Ainda não conectado ao TWS. Saindo.");
return;
}
// 1 = realtime (se assinado), 3 = delayed
client.
reqMarketDataType(3);
// EReader para processar mensagens
reader =
new EReader
(client, signal
);
reader.
start();
new Thread(() -
> {
while (client.
isConnected()) {
try {
signal.
waitForSignal();
reader.
processMsgs();
} catch (Exception e
) {
e.
printStackTrace();
break;
}
}
},
"IBKR-Reader-Thread").
start();
// --------- CONTRATO PARA TESTE: AAPL ----------
// Contract c = IbkrMapper.contratoForex();
Contract c = IbkrMapper.
contratoAcaoBrasil("PETR4");
/*
Contract c = new Contract();
c.symbol("AAPL");
c.secType("STK");
c.currency("USD");
c.exchange("SMART");
*/
// c.primaryExch("NASDAQ");
// ---------------------------------------------
System.
out.
println("Solicitando market data (reqMktData)...");
client.
reqMktData(reqId, c,
"",
false,
false,
null);
}
// ===========================================
// CALLBACKS DE PREÇO
// ===========================================
@
Override
public void tickPrice
(int tickerId,
int field,
double price, TickAttrib attrib
) {
if (price
<=
0) return;
String campo
;
switch (field
) {
case 1: campo =
"BID"; break;
case 2: campo =
"ASK"; break;
case 4: campo =
"LAST"; break;
default: campo =
"FIELD_" + field
;
}
String hora = LocalTime.
now().
format(DateTimeFormatter.
ofPattern("HH:mm:ss"));
System.
out.
println("[TICK] " + campo +
" = " + price +
" | " + hora
);
}
@
Override
public void tickSize
(int tickerId,
int field, Decimal size
) {
// Se quiser ver tamanhos:
// System.out.println("[SIZE] field=" + field + " size=" + size);
}
// ===========================================
// ERROS
// ===========================================
@
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
);
}
}