Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Connect to TWS in main not in Thread (Java)
twsapi
Hi board,
I have a question regarding the thread programming in Java with the IB API. In the example application (Example 1) you see that first the main class is called and within that class a new thread is called e.g. new Example1(args[0]).start(); If I move the connect call to TWS e.g. connectToTWS(); into the main class the thread will not see/be able to access the open connection to TWS. My question is now, why is the thread not able to use the opened connection within main but need its own connection call? And can I actually use just one connection call in main to be used by multiple threads? What I want to achieve is that the main class opens the initial call to TWS and holds this open while different threads can use this open connection. As every call of connectToTWS(); requires me to allow access in the TWS client I only want to authorize my program once not every time a thread actually opens the connection. Not much of automation when I need to authorize it manually every time :) Thanks so much for your help! Regards |
btw12342001
It's just an example so don't expect it to do something else. Your problem isn't really with the threads but the run method (called with .start). That method connects and requests data. If you want more data with the same connection you will have to write it yourself somehow.
toggle quoted message
Show quoted text
You could simply add more lines like Contract contract = createContract..... eClientSocket.reqMktData.... but then you'd have to redo the way it handles returned data. --- In TWSAPI@..., twsapi <no_reply@...> wrote:
|
twsapi
Hi btw12342001,
toggle quoted message
Show quoted text
thanks so much for the tip! That actually helps me a lot. I still did not completely understood your earlier reply on how to handle returned data. In which method is the returned data handled? I guess if the connection is not limited to just one thread you also run into synchronization issues between the threads. But as far as I understood you can initiate 6 or 8 connections but IB recommends to use as little as possible, is that correct? Regards --- In TWSAPI@..., "btw12342001" <newguy@...> wrote:
Not much of automation when I need to authorize it manually every time :)To fix the authorizing part you just need to set trusted IP addresses in TWS. Add 127.0.0.1 for the local machine. |
Hi,
toggle quoted message
Show quoted text
Breaking in, I have a vague memory there may be people who said they open multiple connections in order to have a dedicated thread for each, but it is highly unusual and probably not necessary. Bottom line is if you do not have specific reason to make your life complicated, don't make your threading model more complex than it needs to be for your well-understood purposes. This certainly does not require multiple connections and you can still spawn threads to handle computations that can be factored in that way, if you like. I suggest you start with a fewest-thread-possible approach. Personally I choose 1 thread and do not use a separate reader thread. Sometimes the environment may present you with a separate reader thread and you can write some code to funnel the results from that back to the main thread. There have been endless arguments here about the incredible importance or incredible unnecessity of having additional threads for this or that. It is a big topic and has been discussed for weeks on end numerous times. If you want to learn about it I suggest your read the archives, but in the meantime stick with 1 thread, or 2 if that comes naturally in your context and you can manage it. A common suggestion is to use a single thread to dispatch data *quickly* to other threads so the main thread is close enough to always available to handle the next incoming message. Anything processed quickly can be handled on that thread and anything not can be put in a queue and handled elsewhere. (Meanwhile just start coding without worrying about that until you need to.) Regarding returned data the easy solution is to always override ALL callback methods and log the results from every callback. Then you have no doubt what data is returned through which callback, in case the documentation is vague about it (or non-existent). -Kurt On 7/16/13 12:45 AM, "twsapi" <no_reply@...> wrote:
Hi btw12342001, |
btw12342001
--- In TWSAPI@..., twsapi <no_reply@...> wrote:
Call me brian :). I have no idea how to change that login name. thanks so much for the tip! That actually helps me a lot. I still did not completely understood your earlier reply on how to handle returned data. In which method is the returned data handled?If you scroll down in Example1.java you will see the tickPrice method. This is overriding the method of the same name in ExampleBase. All data from the TWS socket gets sent to those methods in ExampleBase unless they are overridden. If you want more than one symbol you have to assign an separate id in the request and then do something in tickPrice based on the returned tickerId. I guess if the connection is not limited to just one thread you also run into synchronization issues between the threads. But as far as I understood you can initiate 6 or 8 connections but IB recommends to use as little as possible, is that correct?Yes, only use one connection. You don't even need the thread part, you could delete the "extends thread" part in ExampleBase and then just call run, however there's a thread.sleep in the run method and you would have to fix the logic. I don't know what your purpose is so it's hard to tell you what to do. I wrote a very simple 1 file, console based program in the files section under java. Maybe that will be easier to understand, it's also a netbeans project. |
twsapi
Hi Brian :), Kurt,
toggle quoted message
Show quoted text
Thanks so much for your help! I will limit myself to just two threads and actually based on also Kurt's comment synchronization can be a bitch so I rather have two open connections in each thread than fumbling around with thread concurrency :) But guys, thanks so much you really helped me a lot! Regards Call me brian :). I have no idea how to change that login name. |
I mainly suggested you choose wisely for your own needs.
toggle quoted message
Show quoted text
If synchronization *is* a bitch and you are using 2 threads and one handles incoming socket messages and the other is handling incoming GUI message, then you may already have some issues on your hands if your have a user interface that initiates socket events and displays results, if not for other reasons. That's why for my preferences use 1 thread rather than 2, knowing I can change this if it became a problem. OTOH with Java's native threading support, if you know how to use it, maybe it is no issue at all. ASIDE: If btw... is a problem for Brian then "twsapi" could be a confusing name for you to be using here (hint hint). It looks like you may both be using yahoo for posting (with "twsapi" also having the no_reply feature), or even as your email client. If you have an outside email client usually you can just set your name for the From field and yahoo seems to honor it in what it passes through. What you can do if you are fully inside yahoo, I don't know, but no surprise if it is crippled to f'ing hell. If you can't get a decent name through you can always name yourself in your signature. I think "twsapi" is still fully anonymous at this point. -Kurt On 7/19/13 2:20 AM, "twsapi" <no_reply@...> wrote:
Hi Brian :), Kurt, |
to navigate to use esc to dismiss