Appearance
Are you an LLM? You can read better optimized documentation at /safety-and-reliability/connection-resilience.md for this page in Markdown format
Connection Resilience
Trading automation requires constant, reliable connectivity. TTMT maintains connections to two external systems simultaneously: Telegram (for receiving signals) and your broker (for trade execution). Either connection can drop at any time due to network issues, server maintenance, credential expiration, or broker downtime.
TTMT handles all of this automatically. Connections are monitored, refreshed, and reconnected without manual intervention. State is reconciled after every reconnection. Stale data is detected and flagged. You should never need to manually restart your trading service because of a connection issue.
Connection Management
TTMT controls the lifecycle of every broker connection. Each MetaTrader account gets a single dedicated connection, and TTMT enforces strict age and idle limits to prevent stale connections from accumulating.
Connection Lifecycle
Lifecycle Parameters
| Parameter | Value | Purpose |
|---|---|---|
| Max connection age | 4 hours | Force-refresh connections before they become stale |
| Idle timeout | 30 minutes | Clean up connections that are not actively used |
| Credential refresh | 2 hours | Refresh secure credentials before expiry |
| One connection per account | Enforced | Only one connection per MetaTrader account |
Why These Limits Matter
4-hour max age: Long-lived broker connections can develop subtle issues -- missed events, stale subscriptions, memory growth. By forcing a refresh every 4 hours, the system ensures connections are always in a known-good state.
30-minute idle cleanup: If you pause trading on an account, the system does not keep an idle connection open indefinitely. After 30 minutes without activity, the connection is closed and will be re-established on demand.
2-hour credential refresh: TTMT uses secure credentials (not master tokens) for broker access. These credentials are refreshed before they expire, preventing authentication failures during active trading.
One Connection Per Account
Each MetaTrader account has exactly one broker connection at any time. This prevents conflicting operations, duplicate event handlers, and resource leaks that would occur if multiple connections existed for the same account.
Live Connection Manager
The live connection manager handles the real-time data feed from your broker -- the stream of position updates, order fills, price changes, and deal events that TTMT relies on for live monitoring.
Ghost Position Defense
One of the most subtle connection issues is ghost positions: stale position data that does not reflect reality on the broker. These appear during resynchronization events and can cause TTMT to believe positions exist when they have already been closed.
How ghost positions happen:
- The broker connection initiates a resync (periodically or after reconnection).
- During resync, the local position data may temporarily contain outdated information.
- If TTMT reads this data during the resync window, it sees positions that no longer exist.
TTMT's defense (3-layer sync guards):
| Guard | When It Runs | What It Does |
|---|---|---|
| Periodic check guard | Every position sync cycle | Validates positions against broker state before acting on them |
| Price update guard | When new price data arrives | Checks position validity when price data arrives |
| Order completion guard | When an order completes | Verifies position existence after order events |
Authoritative Broadcast
After a full synchronization cycle completes, TTMT performs an authoritative broadcast: it replaces its internal position state entirely with the newly synchronized data. This ensures that any ghost positions from the resync transition are purged.
Health Check System
TTMT monitors health at three independent levels. A failure at any level triggers appropriate responses, from logging to connection refresh to trading pause.
3-Level Health Assessment
| Level | What It Checks | Failure Impact |
|---|---|---|
| Service | Is the Node.js process running and responsive? | Complete outage -- no signals processed, no trades managed |
| Broker Connection | Is the broker connection active and synchronized? | Cannot execute trades or monitor positions |
| Broker | Is the MetaTrader account responsive? | Cannot place orders; existing positions still monitored |
Service Level
The service health check verifies that the Node.js process is alive, the HTTP server is accepting requests, and critical background jobs are running. This is the most basic health check and catches process crashes, out-of-memory conditions, and startup failures.
Broker Connection Level
The broker connection health check verifies that:
- The connection is established
- The live data feed is active and receiving events
- The last synchronization completed successfully
- The connection age is within limits
If the broker connection is degraded, TTMT attempts automatic reconnection before flagging the issue.
Broker Level
The broker health check goes one level deeper -- it verifies that the MetaTrader account itself is responsive. The broker connection might be healthy, but the broker's server could be down for maintenance. This check catches that scenario.
TIP
All three health levels are visible in the dashboard header under the System Health indicator. Green means all three are healthy. Yellow means one level is degraded. Red means a critical level has failed.
Position Sync
Even with reliable connections, state can drift between TTMT's internal records and the broker's reality. Positions can be closed manually, by a trailing stop, or by the broker itself (margin call, end-of-day). The position sync system detects and resolves these discrepancies.
How It Works
TTMT periodically compares its own records against your broker's actual positions to detect opens, closes, and modifications. Positions that have not been updated recently are flagged for investigation.
What Position Sync Catches
| Discrepancy | Detection | Resolution |
|---|---|---|
| Position closed by broker (not by TTMT) | Position in TTMT's records but not on broker | Mark as closed, reconcile P&L |
| Position opened manually | Position on broker but not in TTMT's records | Ignore (TTMT only manages its own trades) |
| Position modified externally | SL/TP values differ between TTMT and broker | Update TTMT's records to match broker |
| Stale position data | Position not updated recently | Flag for investigation, trigger re-query |
Background Safety Check for P&L Recovery
When a position closes outside of TTMT's awareness (e.g., the trailing stop fires during a brief disconnection), a background safety check ensures the trade record is properly finalized.
TTMT periodically queries the broker's trade history to find the deal that closed the position, extracts the actual P&L, and updates TTMT's trade record. If the information is not immediately available, TTMT retries automatically with increasing wait times. This ensures your performance statistics are always accurate, even when closures happen outside TTMT's direct control.
Automatic Recovery From Disconnections
When a connection drops, TTMT follows a structured recovery sequence. No manual intervention is required.
Recovery Sequence
Reconnection Strategy
TTMT retries automatically with increasing wait times between attempts, starting quickly and gradually slowing down. Most disconnections resolve within the first few attempts. If reconnection does not succeed after multiple tries, the issue is flagged for your attention.
What Happens to Existing Positions During Disconnection
Existing positions on the broker are not affected by a TTMT disconnection. They continue to be managed by the broker's server:
- Stop-loss and take-profit orders remain active on the broker.
- Trailing stops (server-side) continue to trail.
- Breakeven stop losses remain in place.
The only thing lost during disconnection is new signal processing. Signals that arrive while disconnected are queued and processed when the connection recovers.
TIP
This is why TTMT uses server-side trailing stops instead of client-side implementations. If the connection drops, server-side trailing continues to protect your positions. A client-side trailing stop would stop working immediately.
Frontend Synchronization State
The dashboard displays real-time position data that comes from the live connection. When synchronization is delayed or interrupted, the frontend detects this and alerts you.
Synchronization State
The frontend tracks whether the position data displayed is current. If the data may be stale:
- The synchronization indicator shows the data is not current.
- A staleness warning banner appears prominently in the Live View.
- Position data is still displayed but marked as potentially outdated.
This prevents you from making decisions based on stale data. When synchronization resumes, the banner clears automatically and the display updates to reflect current state.
Order Fill Recovery
When a limit order fills during a disconnection, TTMT needs to discover this after reconnecting. The system uses a 3-layer defense to ensure no fills are missed.
| Layer | Method | When It Runs |
|---|---|---|
| 1. Live reconnect | Broker resync delivers missed events | Immediately on reconnection |
| 2. Direct query fallback | Direct query to broker's deal/order history | If live resync misses events |
| 3. Admin alert | Notification that manual review is needed | If both automated methods fail |
In practice, Layer 1 catches virtually all missed fills. Layer 2 provides a safety net for edge cases. Layer 3 exists for truly exceptional circumstances.
Summary
| System | What It Protects Against |
|---|---|
| Connection Management | Stale connections, credential expiration, connection leaks |
| Ghost Position Defense | Stale position data during broker resync |
| 3-Level Health Check | Service, connection, and broker failures |
| Position Sync | State drift between TTMT and broker |
| P&L Recovery | Positions closed outside TTMT's awareness |
| Automatic Reconnection | Network interruptions and server restarts |
| Frontend Sync State | Decisions based on stale data |
| Order Fill Recovery | Limit order fills missed during disconnection |
INFO
All connection resilience systems are automatic. You do not need to configure anything or take manual action when connection issues occur. TTMT will reconnect, resync, and resume on its own. The dashboard health indicators keep you informed of the current state.
Related Pages
- Safety & Reliability Overview -- The five layers of defense
- Execution Safety -- Automatic safety pauses and error handling during operations
- Position Protection -- How open positions are managed and protected
- Emergency Controls -- Manual intervention when automatic recovery is insufficient

