softwarespartan / IB4m

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

Unable to connect to TWS #31

Closed Jdo503 closed 4 years ago

Jdo503 commented 6 years ago

Hi,

I am trying to connect to TWS and run the Account Summary but I get the following error….

**-1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.

-1 504 Not connected**

I have followed all the instructions + Enabled ActiveX and Socket Clients on TWS.....

Any ideas please?

softwarespartan commented 6 years ago

Thanks for reaching out.

Can you provide additional details ...

Port number in TWS api configuration vs port number specified in the connection call is often mismatch. Please confirm match

What is OS?

What ver of matlab?

Do you have trading toolbox installed?

Sent from my iPhone

On Jul 9, 2018, at 5:36 AM, Jdo503 notifications@github.com wrote:

Hi,

I am trying to connect to TWS and run the Account Summary but I get the following error….

**-1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket Clients" is enabled on the TWS "Configure->API" menu.

-1 504 Not connected**

I have followed all the instructions + Enabled ActiveX and Socket Clients on TWS.....

Any ideas please?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

softwarespartan commented 5 years ago

were you able to resolve connection issue?

Jdo503 commented 5 years ago

Hi,

Sorry I have been on holiday.

Yes I have been able to resolve it - It was a port number issue.....

Thanks,

J

Despair2000 commented 5 years ago

Sorry for resurrecting this thread.

I have updated to your 9.73 version and try to make it work. One thing that feels strange and you are maybe not aware of is the following problem: When I start the gateway and afterwards matlab, it works fine but if I then exit the gateway and restart it I receive the same error message like Jdo503. I have to close matlab and restart this also to make it work again. No big deal but I thought you might want to know.

softwarespartan commented 5 years ago

hm … yes indeed, I would want to know :) Thanks for the heads up.

This could be an issue with internal thread that has to get started when connecting to TWS. This was a poor design decision from IB but not much we can do about it.

If TWS closes then the socket gets interrupted so the thread might get clobbered. I can double check.

Can you try “clear all” on the matlab side? This should eject the current eclient.Reader and force JVM to make a new one when TWS.Session is created.

-abel

On Aug 29, 2018, at 4:47 AM, Despair2000 notifications@github.com wrote:

Sorry for resurrecting this thread.

I have updated to your 9.73 version and try to make it work. One thing that feels strange and you are maybe not aware of is the following problem: When I start the gateway and afterwards matlab, it works fine but if I then exit the gateway and restart it I receive the same error message like Jdo503. I have to close matlab and restart this also to make it work again. No big deal but I thought you might want to know.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/softwarespartan/IB4m/issues/31#issuecomment-416875651, or mute the thread https://github.com/notifications/unsubscribe-auth/AEWq1PhLV4gBIcYdncqVF8QqYlzisCgvks5uVlU_gaJpZM4VHoLJ.

Despair2000 commented 5 years ago

You were right. "clear all" removes the notification listener and afterwards it works fine to reconnect.

(BTW I send you another email yesterday regarding a problem I experience. Would be grateful about a comment. Thanks in advance! - I can also describe the problem here on GitHub if you prefer?)

SyedShayaan commented 4 years ago

Hi, I am also facing a similar issue where I get the error "Error 1 504 Not connected" System Info: Windows 10 64 bit Python 3.7.3 (anaconda) The code is mainly from IB tutorials.

from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.contract import Contract

class TestApp(EWrapper,EClient): def init(self): EClient.init(self,self) def error(self, reqId , errorCode, errorString): print("Error ", reqId, " " , errorCode, " " ,errorString) def historicalData(self, reqId, bar): print("His Data", reqId, "Date", bar.date, "open", bar.open , "high", bar.high , "low", bar.low)

def main(): app = TestApp() app.connect = ("127.0.0.1" , 7497,0)

contract = Contract()
contract.symbol = "EUR"
contract.secType = "CASH"
contract.exchange = "IDEALPRO"
contract.currency = "USD"

app.reqHistoricalData(1, contract, "","1 D", "1 min", "MIDPOINT", 0, 1, False, [])

app.run()

if name == "main": main()

and I have checked Enabled ActiveX and Socket Clients on TWS and have confirmed that the port is also correct.

Any help will be appreciated.

Thanks

softwarespartan commented 4 years ago

Thanks for reaching out. This repository is for IB4m. If you have IB4m related question, happy to help. You might post your question on Stackoverflow or similar for IB support.

csselo commented 4 years ago

I have the same problem

ERROR -1 502 Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket EClients" 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 error {'args': (-1, 502, 'Couldn\'t connect to TWS. Confirm that "Enable ActiveX and Socket EClients" \nis enabled and connection port is the same as "Socket Port" on the \nTWS "Edit->Global Configuration...->API->Settings" menu. Live Trading ports: \nTWS: 7496; IB Gateway: 4001. Simulated Trading ports for new installations \nof version 954.1 or newer: TWS: 7497; IB Gateway: 4002'), 'self': <main.TestApp object at 0x7f7a105c8a90>, 'class': <class 'main.TestApp'>}

softwarespartan commented 4 years ago

@csselo please confirm the port number specified in the eClient.connect call matches the port number configured in TWS. Probably 9.9 out of 10 times, just need to specify/align the correct ports.

harrodschui commented 4 years ago

Hi, I also got the same error 502.

My port number in TWS and eClient.connect call are matched.. Is there any other solution to solve this problem?

I tried port numbers: 7496, 7497, 4002, 4001 ...still failed to connect.

Note: Tried turning-off firewall, enabled Active X....etc. still not working.

softwarespartan commented 4 years ago

Thanks for reaching out

Can you share the output on the command window and the command you’re running

Also do you have trading toolbox installed?

On May 12, 2020, at 11:17 AM, harrodschui notifications@github.com wrote:

 Hi, I also got the same error 502.

My port number in TWS and eClient.connect call are matched.. Is there any other solution to solve this problem?

I tried port numbers: 7496, 7497, 4002, 4001 ...still failed to connect.

Note: Tried turning-off firewall, enabled Active X....etc. still not working.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or unsubscribe.

harrodschui commented 4 years ago

Hi @softwarespartan , here is my command and output:

Command:: from ibapi.client import EClient from ibapi.wrapper import EWrapper from ibapi.common import *

class TestApp(EWrapper, EClient): def init(self): EClient.init(self, self)

def error(self, reqID: TickerId, errorCode: int, errorString: str):
    print("Error: ", reqID, " ", errorCode, " ", errorString)

def contractDetails(self, reqId:int, contractDetails):
    print("contractDetails: ", reqId, " ", contractDetails)

def main(): app = TestApp()

app.connect("127.0.0.1", port=7496, clientId=0)

app.run()

if name == "main": main()

IB

Output: Error. Id: -1 Code: 502 Msg: Couldn't connect to TWS. Confirm that "Enable ActiveX and Socket EClients" 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

Process finished with exit code 0

P.S. I haven't installed trading toolbox

softwarespartan commented 4 years ago

@harrodschui

Your code does not look like IB4m matlab code :)

Looks like you're using Interactive Brokers Python API.