I have codes working okay for sending a single historical data request, but want to refactor it to be able to send multiple requests in different threads. Of now, the EClientSocket, EReaderSignal, EReader are instance variables in the HistoricalData class Code: private EClientSocket client; private EReaderSignal readerSignal; private EReader reader; So the TWS connection belongs to the HistoricalData object, Code: private void openConnection(int port) { this.readerSignal = new EJavaSignal(); this.client = new EClientSocket(this, this.readerSignal); this.client.eConnect("127.0.0.1", port, 0); this.reader = new EReader(this.client, this.readerSignal); this.reader.start(); //open a new thread to listen to incoming messages } My question is, shouldn't the connection belong to the class instead or each requesting instance needs its own connection? If I create 3 HistoricalData instances, each opens a thread and sends an asynchronous request for Bid/Ask/Trades, do I need 3 EClientSocket instances and each connecting individually? And there will be one message queue per EClientSocket obj? Conceptually, I would have thought there is one connection and each thread sharing it. But given the EWrapper callbacks are instance methods, the EReader has to know which instance each message in the queue belongs to, so each instance needs its own socket and ereader? Can't the EReader identifies the instance by some request ID? How about EJavaSignal? Can it be static? Such as below, but I guess no as EReaderSignal and EReader are closely connected? Code: static private EReaderSignal signal = new EJavaSignal(); this.client = new EClientSocket(this, signal); this.reader = new EReader(this.client, signal);
Step 1. Open one (!) connection. Step 2 until a million and one. Do all activities for which you need your connection: getting data, send orders, you name it. Each request and/or activity has its own ID by which you can connect the request to the received result. When ready proceed to next step. Step million and two. Close the connection. There is no need to open multiple connections. You can if you want to, but it makes thing unnecessarily complicated. And there is a limit to the number of connections open at the same time (I believe it is 32?).
I want to make 3 threads to request BID/ASK/TRADES using the same variables like ticker/datetime etc. It can also request sequentially by calling reqHistoricalData 3 times and replacing BID/ASK/TRADES, but I feel cleaner by making a BidRequestInstance, AskRequestInstance, TradesRequestInstance, waiting for their results and Future.get(), then combine the results. Is there any problem creating multiple instances, each having its EClientSocket and eConnecting individually?
Why would you request items sequentially? That is not necessary. You can submit all requests at the same time (max 50 per second) and wait and see in what sequence IB's servers send you back the requested data. Some data it will be able to return faster than other data. Overall it will result in you getting your requested information faster than doing it all sequentially (I have tried and compared this). This approach is best done with only one client connected. Using multiple clients makes the administration that your software needs to do to combine the received data more complicated.
you can change the zero (clientid) to something else to open multiple connections. this.client.eConnect("127.0.0.1", port, 0); won't using one connection with multiple threads require synchronization anyway? Easier to just run 3 separate apps that do what you want and forget the threading
That's right, instead of using multiple clients and connections, better open multiple connections using the same client, or one connection and sending requests with different reqId
automated trading. I wrote a system using akka that handled it rather easily. I can send it to you if you have github or bitbucket and if I remember my password. It's pretty old https://akka.io/