¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Help handling disconnects so I dont have to log into TWS every time I need to reconnect


 

Hi,

Below is the Java method I use to connect to the TWS API. I am still in the process of building my trading system and so I have the need to run my main method, which calls this requestMarketData, via IntelliJ. Once I confirm whatever my recent change was worked or didnt work, I then stop the main method.

Ideally, while I am already logged into TWS, I would simply just run my main method again to connect to the API and test another change. Instead, I need to close TWS, reopen it, login, then run my code. Is there a way to handle this??

public void requestMarketData(String ticker) {
Contract contract = contract(ticker);
int tickId = 1;

client.eConnect(BROKER_CONNECTION_IP, BROKER_CONNECTION_PORT, 0);
client.reqMarketDataType(MARKET_DATA_TYPE);
client.reqMktData(tickId, contract, TICK_STRING,
false /* Snapshot */, false /* Regulatory Snapshot */, null /* MktDataOptions */);

reader = new EReader(client, signal);
reader.start();
new Thread(() -> {
while (client.isConnected()) {
signal.waitForSignal();
try {
reader.processMsgs();
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}).start();

if (!isConnected) return;

client.eDisconnect();
}


 
Edited

We do exactly what you are trying to do all the time, Kyle. We develop and run TWS API Java clients in Eclipse and never have to restart TWS. We can even change classes while clients are running (within certain limits) and Eclipse applies hot patches to the running tasks. Here a few thoughts:

  • No two clients can simultaneously use the same clientId. So if you have a suspended task in IntelliJ that holds an established socket connection with TWS (say a break point was triggered), starting a second client with the same clientId will fail during the connect call.
  • It looks like you have clientId 0 hardwired in your code. If your client does not need the special features that come with clientId 0, a better approach would be to use a somewhat random clientId, such as Instant.now().getEpochSecond()
  • You can always check which clients are currently connected to TWS by selecting the "DATA" button in the top right corner of the TWS window.
  • You need to review the design of your application. It does not make sense that you establish the connection with TWS within your requestMarketData method just to disconnect right after you requested data. Connecting to TWS would generally be one of the first things your application does, while disconnecting would be the last thing just before exiting. Your requestMarketDatashould just do that, request market data.
  • TWS API is completely asynchronous and a request will generally not return anything right away. Not even errors. Responses (including error reports) get delivered to you via the documented callbacks and you need to make sure that you implement and handle all error callbacks. It is a good practice to log errors and warnings liberally so that you have a better understanding of what is going wrong when it does.

Hope this points you in the right direction,

´³¨¹°ù²µ±ð²Ô


 

I appreciate the advice Jurgen, this is super helpful. Going to work on breaking out the connection/market data request/disconnect first as I think I am going to struggle with this a bit.?