> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getcertio.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Gap closure plan

# Gap Closure Implementation Plan

## Objective

Transform Certio from a "One-Way Fire & Forget" system into a **"Two-Way Command & Control"** platform, achieving feature parity with TradersPost's dashboard capabilities while maintaining our specific advantage in the MT4/Prop Firm space.

## Core Features (The "Controller" Dashboard)

1. **Live Position Tracking (Two-Way Sync):** Dashboard shows real-time open positions, PnL, and Equity from the MT4 terminal.
2. **Remote Trade Management:** User can click "Close" or "Flatten All" in the Dashboard, which triggers execution on the MT4 terminal.

***

## Phase 1: The "Live state" (Two-Way Sync)

### 1.1 Client Side (MQL4/5 Bridge)

* **Action**: Implement `SendSnapshot()` function.
* **Logic**: Every 5 seconds (and immediately on Order changes), scan all open orders.
* **Payload**:
  ```json theme={null}
  {
    "type": "SNAPSHOT",
    "account": 123456,
    "equity": 10050.50,
    "balance": 10000.00,
    "positions": [
      {"ticket": 123, "symbol": "EURUSD", "type": "BUY", "lots": 1.0, "profit": 50.50}
    ]
  }
  ```
* **Transport**: HTTP POST to `/api/v1/sync`.

### 1.2 Backend (Ingress & Redis)

* **New Endpoint**: `POST /api/v1/sync` (Authenticated).
* **Storage**: Use Redis Hashes for instant access.
  * `HSET user:{secret}:state equity 10050.50`
  * `SET user:{secret}:positions <JSON_BLOB>`
* **API for Frontend**: `GET /api/v1/state` returns the latest cached snapshot.

### 1.3 Frontend (Dashboard UI)

* **New Component**: `PositionTable.tsx`.
* **New Widget**: `AccountStats.tsx` (Live Equity/Balance).
* **Data Fetching**: Poll `/api/v1/state` every 3s (or use SWR).

***

## Phase 2: The "Remote Control" (Action Actions)

### 2.1 Backend (Ingress)

* **New Endpoint**: `POST /api/v1/command`.
* **Request**: `{"command": "CLOSE", "ticket": 123}` or `{"command": "FLATTEN"}`.
* **Logic**: Push command to a high-priority Redis Queue: `commands:{secret}`.

### 2.2 Client Side (MQL Bridge)

* **Logic**: The existing worker (`Certio_Worker`) is already polling. We modify the response of the poll to include "System Commands" alongside "Trade Signals".
* **Execution**:
  * `if (command == "CLOSE") OrderClose(ticket)...`
  * `if (command == "FLATTEN") CloseAll()...`

### 2.3 Frontend (Dashboard UI)

* **UI Update**: Add "Red X" (Close) button to `PositionTable`.
* **UI Update**: Add "Panic Button" (Flatten All) to the header.

***

## Execution Order

1. **Backend Extensions** (Go): Define structs, endpoints, and Redis schema.
2. **MQL Upgrade**: Modify `Certio_Worker.mq4/5` to send snapshots and handle Close commands.
3. **Frontend Build**: Create the new UI components to visualize functionality.

## Verification

* **Success**: I open a trade manually in MT4 -> It appears on Dashboard (Sync).
* **Success**: I click "Close" on Dashboard -> MT4 closes the trade (Control).
