softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

WHATIF-order causes disconnect #143

Open Despair2000 opened 2 years ago

Despair2000 commented 2 years ago

@softwarespartan

Hi Abel,

I stumbled over a strange problem and think the problem might be within IB4m.

It happens me annoyingly often that directly after sending a WHATIF-order I get an error 511 or 512 "Oder Sending Error" - Java exception. And then I'm disconnected from the TWS. This happens more often when there are several API calls in a row but it also happened when there was no call before some time.

I think the full codes I saw were 249 512 (this I'm sure of) and 21 511 (not so sure on the 21).

I tried to look into this a little bit and found, "error codes 511 and 512 (and other 5xx codes) are used in the Java and C# APIs and are client side errors. In other words, they get raised locally in the client when an API request caused an exception. These error codes do not come from TWS. (...) In any case, 5xx errors indicate that something happened when the client was writing bytes into the socket that it uses to communicate with TWS."

Maybe I should add that I used WHATIF-orders for some time on my live account without problems. It was now when I started a new project and tried it on the paper account that I ran into this problem.

Do you have anything smart to say to this problem? :-)

Despair2000 commented 2 years ago

@softwarespartan @abelatnvidia

This problem is very hard to grab since it is hard to reproduce. One week ago (when I wrote the initial post) I was really plagued by it and my program exited countless times (surely 15-20 times). Friday I ran the exact same program again and it happened only once that the error occurred. So I assume it depends on what else is going on on the computer (especially java related).

Would be awesome if you would find the time to have a look Abel!

Thanks in advance!

softwarespartan commented 2 years ago

Apologies this was missed. Likely overdue for an IB API update. This is probably a bug in the API itself. I should have some time over next few weeks to refresh.

-Abel

On Dec 12, 2021, at 3:00 AM, Despair2000 @.***> wrote:

@softwarespartan https://github.com/softwarespartan @abelatnvidia https://github.com/abelatnvidia This problem is very hard to grab since it is hard to reproduce. One week ago (when I wrote the initial post) I was really plagued by it and my program exited countless times (surely 15-20 times). Friday I ran the exact same program again and it happened only once that the error occurred. So I assume it depends on what else is going on on the computer (especially java related).

Would be awesome if you would find the time to have a look Abel!

Thanks in advance!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/143#issuecomment-991851325, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABC2VVGULO53XXQ2K4STGYDUQRJATANCNFSM5JLLHXCQ. Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

Despair2000 commented 2 years ago

Now I got a new variation. God knows what caused it:

2 510 Request Market Data Sending Error - java.lang.NullpointerException

And disconnected.

I get it again and again. I'm just trying to request market data for an option. I had market data for this option streaming all day without problems. Then an error (in my program) occurred and I had to restart the program but now I can't request the data anymore. Requesting data for a stock is working without problems. Maybe I should add that the option is expiring today.

Despair2000 commented 2 years ago

Happy new year @softwarespartan @abelatnvidia,

Around new year IB send out emails and asked to upgrade to a newer version of TWS (or API?) because of some sort of vulnerability (you maybe also got this email). I was already worried that IB4m maybe won't work afterwards (I think to remember that 29th dec was the date) but so far it still works. I'm always little confused about API-version vs TWS version. If I understand it right IB4m is still based on the 973 API (?) while the latest TWS version is already 1012. What does this mean? The newer TWS versions are apparently backwards compatible with the older API versions but we can't use functionality that came with the newer API versions?

On this note, have you been able to think a little bit about the update you named? The disconnections are still bugging me.

Despair2000 commented 2 years ago

I can add the following info.

1.) The error occurs also on the live account and not only on the paper account. 2.) I think (I'm not 100% but pretty sure) the error so far occurred only when placing a WHATIF-order for options that expire the same day.

softwarespartan commented 2 years ago

@Despair2000 I have added latest IB-API updates to IB4m. You can try these updates out by adding the tws-101201.jar file. There are many IB-API updates to watch out for -- IB has been actively developing the API. Let me know if this update resolves your disconnect issue. Also don't hesitate to let me know if you find any bugs. Cheers!

softwarespartan commented 2 years ago

@Despair2000 p.s. apologies this took so long to get rolling, we just welcomed our new baby boy a few weeks ago so it has been a busy time :)

Despair2000 commented 2 years ago

Congratulations! My son just turned 1 so I know what you are talking about :-)

I will test your update and come with feedback.

Despair2000 commented 2 years ago

I just tried a first time to connect with the new jar-file and got the following error:

Unrecognized function or variable 'getEventWithRemove'.

Error in TWS.processNotification (line 4) event = getEventWithRemove(getSource(n),n.uuid);

Error in connectIB (line 29) eConnect(session.eClientSocket,IP,port,clientID); pause(0.5)

com.mathworks.jmi.MatlabException: Undefined function 'getEventWithRemove' for input arguments of type 'com.tws.Handler'. at com.mathworks.jmi.NativeMatlab.SendMatlabMessage(Native Method) at com.mathworks.jmi.NativeMatlab.sendMatlabMessage(NativeMatlab.java:273) at com.mathworks.jmi.MatlabLooper.sendMatlabMessage(MatlabLooper.java:120) at com.mathworks.jmi.Matlab.mtFevalConsoleOutput(Matlab.java:1835) at com.proxy.ProxyBuilder$Handler$2.run(ProxyBuilder.java:127)

Despair2000 commented 2 years ago

I now changed line 4 of the process notification to:

event = getEvent(getSource(n),n.uuid);

After doing this the above error doesn't show up but I can't connect anyway because I get the following error:

-1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket Clients" is enabled and connection port is the same as "Socket Port" on the TWS "Edit->Global Configuration...->API->Settings" menu. Live Trading ports: TWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations of version 954.1 or newer: TWS: 7497; IB Gateway: 4002

I receive this although all the mentioned settings are correct and work fine with the TWS973.jar. I tried on two computers and it's the same on both. After changing back to the old jar-file I can connect as usual. So something seems to be wrong.

Despair2000 commented 2 years ago

...or am I missing something here now? To use the new jar-file one just have to change it on the javaclasspath, right? Or are there some more changes I should have done?

softwarespartan commented 2 years ago

@Despair2000 can you check which version of Matlab and TWS you are using? My first few thoughts are that this update uses the bleeding edge IB API so might need most recent version of TWS. Typically, error 502 is a socket issue. I know the old JAR file works for you but, I would just double check/confirm that you are indeed connecting on a port that is valid for live trading, vs paper trading, vs gateway, etc.

RE: getEvent, apologies this was an src merge bug. I pushed updated jar that should resolve this.

Despair2000 commented 2 years ago

I use Matlab 2021b and also the latest TWS (10.12.2j).

I just tried again with the jar-file you uploaded and I get the same error. When you tell me to double check the ports and so on I don't really know what you mean. When I switch to the new jar-file the only think I change is the entry in the javaclasspass. The function I use to connect with the port and IP and the settings of the TWS I don't touch.

P.S.: The"getEvent-error" is now gone with the new jar-file.

Despair2000 commented 2 years ago

I really tried everything I could imagine but I can not establish a connection with the new jar-file. I tried with the TWS and gateway (both live and paper), both on PC and MAC. :-(

Can you login with the new jar-file?

Despair2000 commented 2 years ago

@softwarespartan I haven't made any progress here and have no idea how I can make the new jar-file work. :-(

softwarespartan commented 2 years ago

Apologies for delay! Is it possible to send a log file or any diagnostic info ? For example, even if TWS was not even open, would generate the same error.

On Feb 19, 2022, at 5:01 AM, Despair2000 @.***> wrote:

 @softwarespartan I haven't made any progress here and have no idea how I can make the new jar-file work. :-(

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you were mentioned.

Despair2000 commented 2 years ago

I was just thinking myself about activating the log-file. I have never used the log-file before and obviously I'm doing something wrong. I activated the API-logging in the global configuration of the TWS and as google tells me there should be created a log.txt in the installation directory of the TWS. Sadly this is not the case. There is a file called launcher.log but this seems to be only logging the login to the TWS.

However the same error is thrown if the TWS isn't even running. So I'm not sure if there would be something in the log-file if I could find it :-/

So you can login with this jar-file?

Despair2000 commented 2 years ago

Now I found at least also the logs in the TWS. I will try to provide tomorrow more logs of what is going on. I looked over it but there was nothing that caught my intention. Then I don't really know what to look for either.

Despair2000 commented 2 years ago

So I now tried again after I found the logs in the TWS and when I attempt to login with IB4m there isn't logged anything (almost). This is although I activated detailed logging. Obviously the log I posted yesterday showed a lot of other things that did not belong to my login attempt. I now started the TWS and tried to login twice with the API. The following was logged:

============================== Log file: /Users/boriskleine/Jts/milbcblkagmloocjldnbedbojialplcealbooiaf/api.0.20220220.100521.ibgzenc ============================= 10:05:21:715 -> --- 15-1-DUXXXXXXX- 10:05:21:720 -> --- 9-1-1-

============================== Log file: /Users/boriskleine/Jts/milbcblkagmloocjldnbedbojialplcealbooiaf/api.0.20220220.100549.ibgzenc ============================= 10:05:49:263 -> --- 15-1-DUXXXXXXX- 10:05:49:266 -> --- 9-1-1-

Despair2000 commented 2 years ago

I understand that you are busy with other issues. Just drop a line when we can investigated this issue further (I did not make any progress). I will so long keep on using the 973 jar-file.

Despair2000 commented 2 years ago

@softwarespartan Hi Abel, I'm still using here the old jar-file. Any chance that we can look at this problem?