This tutorial assumes you already had built the minimal RiskGateServer and have it running locally on port 5555. Refer to that [tutorial](https://forge.mql5.io/DouglasRechia/riskgate/src/branch/main/docs/minimal-riskgate-tutorial.md) to see how to create the server.
Let's build the EA that will request approval from the server before placing any trade. For now, the goal is deliberately modest: we want a minimal but working EA that sends a signal to the Service every 30 ticks and logs the response. Here we focus on the structure and the wiring.
The repository [riskgate-ea-example](https://forge.mql5.io/DouglasRechia/riskgate-ea-example) implements the complete tutorial below and can be used as a reference.
## Creating the EA Project
The process mirrors [what we did for the Service](https://forge.mql5.io/DouglasRechia/riskgate-ea-example/src/branch/main/docs/minimal-riskgate-tutorial.md). The first step is to open a terminal in the Data Folder of the MetaTrader 5 installation you want to work with. Open MetaTrader 5, select File > Open Data Folder, then right-click that folder in Windows Explorer and select "Open in Terminal."
In the VSCode terminal, add the riskgate-ea package and install all its dependencies:
```bash
kp add @douglasrechia/riskgate-ea
kp install
```
Three kp commands and the project is ready: the skeleton source file is in place, and the riskgate-ea package together with its transitive dependencies (sockets, logger, and JAson) are downloaded and available under the knitpkg/include directory.
### The Generated Template
The init command produces src/riskgate-ea-example.mq5 with the same structure we saw for the Service: BuildInfo.mqh is included, compile-time constants from the manifest populate the #property description directives, and three empty event handlers are ready to be filled in. After updating copyright and link to the correct values, the file looks like this:
The riskgate-ea package exposes a single class that the EA needs: RiskGateClient. It handles everything related to the socket connection — connecting, sending, receiving, timeout management, reconnection, and fallback policy. The EA never touches a socket directly.
We include the header and declare a global RiskGateClient instance pointing to localhost on port 5555. We also declare a global tick counter to control how often we call the Service:
In OnInit we connect to the Service, and in OnDeinit we disconnect cleanly. If the Service is not yet running when the EA starts, Connect() will log a warning and RiskGateClient will attempt to reconnect automatically on the first call to RequestPositionSize:
```mql5
int OnInit()
{
client.Connect();
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
client.Disconnect();
}
```
The main logic lives in OnTick. Every 30 ticks, the EA builds a CJAVal signal with a symbol, a side, and a stop loss price, calls RequestPositionSize, and logs the response:
Notice how little the EA needs to know. It builds a JSON signal describing its intent, hands it to the client, and reads back three fields from the response. Whether the connection is healthy, whether a reconnection was needed, whether a timeout occurred — none of that is the EA's concern. RiskGateClient handles it internally and always returns a coherent response, either from the live Service or from the configured fallback policy.
Here you can see a full implementation of [src/riskgate-ea-example.mq5](https://forge.mql5.io/DouglasRechia/riskgate-ea-example/src/branch/main/src/riskgate-ea-example.mq5).
### Compiling and Running
Compile the EA with:
```bash
kp compile
```
To attach it to a chart, right-click on Expert Advisors in the MetaTrader 5 Navigator and select Refresh. Open a chart — for example EURUSD — and double-click Experts Advisors/riskgate-ea-example/bin/riskgate-ea-example. As with the Service, enable "Allow DLL imports" in the configuration dialog (required because RiskGateClient uses the Windows socket API via mql-sockets) and click OK.
With the EA attached and the Service already running, the Experts tab will show the exchange between client and server: the EA sending a signal every 30 ticks and logging the approved status, lot size, and reason returned by the handler.
One of the most practical aspects of this architecture becomes immediately visible when you attach the same EA to a second chart — for example GBPUSD. The Experts tab will show two independent client instances, each sending their own signals, both communicating with the same single RiskGate Service. The Service handles them concurrently, calling your RiskGateHandler for each incoming signal. From the handler's perspective, all signals from all EAs and all symbols flow through one place, which is precisely the central visibility that this architecture exists to provide.