/algo-trading-skills:ibkr-tws-gateway-headless-launchfrom algo-trading-skills
Launches Interactive Brokers Gateway/TWS on headless servers or containers using Xvfb, IBC, port probes, and socket readiness checks. Handles daily resets before API sessions are assumed live.
Ships in algo-trading-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
Invoke this whenever an Interactive Brokers (IBKR) strategy bot runs on a cloud VM, server, or Docker container with no persistent GUI desktop. IBKR publishes no direct-to-server socket API for trading: the Python client (ibapi or ib_insync) talks to a locally running IB Gateway or Trader Workstation (TWS) process, which holds the authenticated session. That process is a Java desktop application, so a headless host needs a virtual display (Xvfb) plus login automation (IBC) — or a container image that bundles both.
This skill covers the transport and process layer: paper/live port guarding, socket readiness probing, detecting the Gateway restarts that drop your socket, and generating a container spec that does not expose order entry to the network.
headless-broker-auth-patterns, which classifies IBKR as a supervised-gateway broker and explains why that constrains what "unattended" can mean.nextValidId), client-id collisions (error 326), and connectivity codes 1100/1101/1102 belong in your API client, not here.config.ini for automated login. IBC's user guide states the config-file method is preferred "because the configuration file can be protected by the operating system", and calls passing the password on the command line "strongly deprecated".Xvfb, or a container image such as ghcr.io/gnzsnz/ib-gateway that ships Xvfb, IBC and a socat relay.4001 live / 4002 paper; TWS 7496 live / 7497 paper.Bind the trading mode to the port before anything else.
IBGatewayConfig(host, port, client_id, is_paper=...). IBGatewayHeadlessManager refuses a paper config on a live default port and a live config on a paper default port.client_id. A duplicate is rejected with TWS API error 326, and a Gateway session accepts at most 32 concurrent clients.Probe the socket, then confirm readiness at the API layer.
wait_for_gateway_ready() before constructing the API client. It returns True or raises — it never returns False, so a caller cannot fall through to a dead socket.nextValidId "is commonly used to indicate that the connection is completed", warning that "function calls made prior to this time could be dropped by TWS". Gate order submission on nextValidId, not on the probe.Run IB Gateway under IBC with a scheduled restart.
AutoRestartTime (IBC config.ini) or AUTO_RESTART_TIME (container). Without it the session simply ages out; with it, one login at the start of the week carries through — IBC's guide notes authentication is needed only "the first time during the week that TWS or Gateway run after 01:00 ET on Sunday."AUTO_RESTART_TIME to an ET value while leaving TIME_ZONE at its Etc/UTC default schedules the wrong instant — set both together.TWOFA_TIMEOUT_ACTION, ExistingSessionDetectedAction). An IBKR Mobile login elsewhere can evict this session.Distinguish the three restart events; only one of them drops your socket.
monitor_gateway_health() detects: it declares a disconnect only after unhealthy_threshold consecutive failed probes, so one dropped probe does not tear down a live strategy, and it reports measured downtime on recovery.Generate the container spec with generate_docker_spec() and deploy it unmodified.
bind_address (loopback by default) and maps to the image's socat relay port (4003 live / 4004 paper), because Gateway itself listens only on the container's 127.0.0.1. Mapping 4001:4001 targets a port nothing serves externally.READ_ONLY_API follows config.read_only_api, which defaults to yes. Turning it off on a live account is logged as a warning.TWS_PASSWORD_FILE) by default rather than an environment variable./dev/tcp; the image ships socat and bash but not netcat, so an nc -z healthcheck can never pass there.Full step-by-step procedure with broker-specific detail: see
references/workflows.md. Broker/framework coverage table and sources: seereferences/standards.md. Printable pre-flight checklist: seeassets/checklist.md.
0.0.0.0. The TWS API socket carries no credential of its own — access control is the Gateway's Trusted-IPs list. A 4001:4001 mapping in Compose publishes order entry on every host interface. The image's own documentation warns that otherwise "every device on [the network] can access your IB account". Bind to loopback, or use an SSH tunnel or a shared Docker network.nextValidId can be silently dropped.restart: always on the Gateway container. An operator who stops the Gateway during an incident will have it resurrected by the next Docker daemon restart. Use unless-stopped so a deliberate stop stays a stop.client_id across strategies. The second connection is rejected (error 326), and if it is the reconnecting process that loses, a restart can leave the bot permanently unable to attach.AUTO_RESTART_TIME is read in the container's TIME_ZONE, which defaults to Etc/UTC.python -m unittest discover -s skills/ibkr-tws-gateway-headless-launch/scripts — all tests must pass.IBGatewayError before any socket is opened.wait_for_gateway_ready() against a closed port and confirm it raises rather than returning False; against a listening socket, confirm it returns True on the first probe without sleeping.monitor_gateway_health() with a bounded max_polls and an injected sleep_fn/clock_fn, close the listening socket mid-run, and confirm disconnect_events/reconnect_events and measured downtime in the returned GatewayHealthReport.generate_docker_spec() to YAML and run docker compose config against it. Confirm the resolved output shows host_ip: 127.0.0.1, target equal to the socat relay port, published equal to the API port, READ_ONLY_API: "yes", restart: unless-stopped, and the secret mounted at the path named by TWS_PASSWORD_FILE.headless-broker-auth-patternssystemd-supervision-for-trading-botswebsocket-subscription-reconciliation-after-reconnecttoken-lifecycle-live-probingWhat SKILL.md sets; muted values are Claude Code's defaults
36 skills in this plugin
/alpaca-paper-live-key-separationUse when a bot connects to the Alpaca Trading API and must never cross paper keys with the live endpoint; pins the base URL per environment, checks the key prefix, probes /v2/account and requires an explicit ALLOW_LIVE_TRADING flag./api-key-least-privilege-audit-toolUse before deploying a live bot, to audit existing broker API keys for scopes they do not need. Flags withdrawal, transfer and admin rights that turn a leaked execution key into theft./broker-account-margin-call-handlingUse when a bot trades a Reg T, portfolio or futures margin account and must act before the broker liquidates: tiered maintenance-margin warnings cross-checked against broker excess liquidity, initial-margin order gating and liquidity-aware de-leveraging./broker-agnostic-adapter-interfaceUse when strategy code must run across several brokers and should not import kiteconnect, alpaca-py or ibapi directly; defines an adapter contract with Decimal prices, typed exceptions and normalised order status across venues./broker-api-changelog-diffing-toolUse before upgrading a broker SDK or OpenAPI spec, to diff two schema snapshots for removed endpoints, newly required parameters, enum mutations and type changes so CI fails before the change reaches an order path./broker-api-deprecation-notice-monitoringUse when a long-running bot calls REST endpoints a broker may retire; parses RFC 8594 Sunset and RFC 9745 Deprecation headers plus changelog feeds into one dated migration deadline with an escalation tier.npx claudepluginhub himanshuj16/algo-trading-skills --plugin algo-trading-broker-integrationOther plugins' skills for the same job