You have a Python script, a broker account and a terminal full of errors. Asking Claude or ChatGPT to rewrite the whole bot can make the problem harder to isolate.
Start with a smaller question: can this Python environment establish a connection to the intended TWS or IB Gateway session?
A successful connection does not prove that market data, order permissions or a strategy work. It does narrow the problem.
1. Write down the connection you intend to make
Record four things before changing anything: the broker application, account environment, host and configured socket port.
| Application | Default paper port | Default live port |
|---|---|---|
| Trader Workstation (TWS) | 7497 | 7496 |
| IB Gateway | 4002 | 4001 |
These are defaults, not a substitute for inspecting your settings. The client must match the port actually configured in the running application. IBKR documents these settings in its TWS configuration lesson.
Start troubleshooting in paper mode. If the script and broker run on the same machine, the package's example host is 127.0.0.1. On a VM, that address means the VM itself—not your laptop.
Changing a port alone is not a safe paper-to-live conversion. The main Trend Join Long bot supports deliberately configured paper and live use; its account and port guards must remain consistent.
2. Confirm that the broker application is ready
Open the intended TWS or Gateway session and finish signing in. A browser login to your account is not a running TWS API server.
In TWS, inspect Global Configuration → API → Settings. Check the socket-client setting and port. For a connection-only diagnosis, leave Read-Only API enabled: placing orders is a separate permission decision.
If you use a remote host, investigate the intended network route and trusted-client configuration. Do not expose the API port to the public internet or disable your firewall just to silence a connection error.
IBKR's connection verification guide explains why a missing server, disabled socket interface or mismatched port can produce error 502.
3. Run the smallest existing test
For the shipped Trend Join Long package on Windows, open PowerShell in the trading-bot directory. If the package environment is already installed, run:
.\.venv\Scripts\python.exe .\test_connect.py
The shipped test connects to 127.0.0.1:7497 using client ID 99, prints connection state and managed accounts, then disconnects. It does not submit orders.
This introductory test is intended for paper TWS. Its connection values are written directly in the file: it does not read your .env. That distinction matters if you changed IBKR_PORT and expected this helper to follow it. It also means you should confirm the TWS session and account yourself; a socket port is not an account safety check.
If the environment is not installed, the package's Windows setup launcher is:
py -3.12 setup_and_test.py
That launcher creates or reuses .venv, installs missing dependencies and runs the same paper connection helper. Use the package instructions for setup; don't install unrelated API libraries suggested by a different tutorial.
A connection result is diagnostic evidence, not a reason to run buy_one.py or the autonomous cycle as a test.
4. Follow the actual error
| Evidence | Next inspection |
|---|---|
Python cannot import ib_async |
Confirm the interpreter and package environment before changing broker settings. |
| Error 502 or connection refused | Check that the intended application is running, API sockets are enabled, and host/port match. |
| Error 326 | Another API connection is using that client ID. Identify it; don't randomly change the bot's order-owning client. |
| Error 504 | The client is not connected. Find the earlier connection failure. |
| Connection succeeds, main bot aborts on an account guard | Inspect intended account mode and matching configuration; do not remove the guard. |
IBKR's error-code reference distinguishes these broker/API conditions. Python import failures are a different layer.
5. Give your AI assistant evidence, not a vague symptom
Use this request after removing account IDs, credentials and tokens:
Diagnose only the connection failure. I am using paper TWS on the same Windows machine as Python. My configured socket port is [port] and the client ID is [ID]. I ran [command] with [Python version]. Here is the complete redacted exception and the small connection function. Explain the first failing step and propose one change at a time. Do not place orders, switch to live mode, remove account guards or rewrite the strategy.
The useful outcome is a repeatable connection check and a known next step. If connection succeeds but the bot remains inactive, move on to its run history, inputs and decision logs.
For the larger build workflow, our free Claude Code/IBKR guide includes the prompts and downloadable instructions. The optional source package is linked there for readers who want the existing implementation.