Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to connect to TWS #31

Closed
Jdo503 opened this issue Jul 9, 2018 · 14 comments
Closed

Unable to connect to TWS #31

Jdo503 opened this issue Jul 9, 2018 · 14 comments

Comments

@Jdo503
Copy link

Jdo503 commented Jul 9, 2018

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
Copy link
Owner

softwarespartan commented Jul 9, 2018 via email

@softwarespartan
Copy link
Owner

were you able to resolve connection issue?

@Jdo503
Copy link
Author

Jdo503 commented Aug 15, 2018

Hi,

Sorry I have been on holiday.

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

Thanks,

J

@Despair2000
Copy link

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
Copy link
Owner

softwarespartan commented Aug 29, 2018 via email

@Despair2000
Copy link

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
Copy link

SyedShayaan commented Nov 13, 2019

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
Copy link
Owner

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
Copy link

csselo commented Dec 18, 2019

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
Copy link
Owner

@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
Copy link

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
Copy link
Owner

softwarespartan commented May 13, 2020 via email

@harrodschui
Copy link

harrodschui commented May 13, 2020

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
Copy link
Owner

@harrodschui

Your code does not look like IB4m matlab code :)

Looks like you're using Interactive Brokers Python API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants