# setTrailingStop: Attach a trailing stop to one position

> Attach a trailing stop to one position. Parameters, units, copy-ready examples, log lines and errors for the setTrailingStop command.

Source: https://zb-connector.com/docs/commands/settrailingstop/

Attach a trailing stop to one position.

> **Use a demo account first.** Run every step on a MetaTrader 5 demo account. Switch to a live account only after the go-live checklist is done.

## At a glance

-   **Action:** `setTrailingStop` (case-sensitive)
    
-   **Group:** Change stops and trailing
    
-   **From the dashboard:** yes, on the **Send Command** page
    
-   **Finds the position by:** `positionID` (the comment you gave it when you opened it) or `ticket`
    
-   **HTTP 200 means queued, not executed.** See [how delivery works](https://zb-connector.com/docs/reliability/how-delivery-works/) and [find where an alert stopped](https://zb-connector.com/docs/troubleshooting/find-where-it-stopped/).
    

## Fields

| Field | Type | Required | Unit | Description |
| --- | --- | --- | --- | --- |
| `license` | string | Yes | text | Your license key. In examples it is written `YOUR_LICENSE_KEY`. |
| `action` | string | Yes | text | Always `setTrailingStop`. Case-sensitive. |
| `positionID` | string | Yes, or `ticket` | text | The position’s MT5 comment (the `positionID` you sent when opening it). If the value is all digits and no comment matches, it is matched against the position ticket. |
| `ticket` | integer | No | MT5 ticket | MT5 ticket of the position. Used only when `positionID` is empty. Allowed: greater than 0. Needs EA v1.9.1 or later. |
| `trailDistance` | number | Yes | pips | Distance to keep between the best price reached and the stop loss, in pips. Allowed: greater than 0. |

The webhook rejects unknown fields, missing required fields and wrong types with HTTP 422 before anything is queued. Field names are case-sensitive.

## Examples

Replace `YOUR_LICENSE_KEY` with your key. Send the body as the alert message in TradingView, or with any HTTP client.

### Trail a position 30 pips behind the best price

**JSON**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "setTrailingStop",
  "positionID": "eurusd_breakout_1",
  "trailDistance": 30
}
```

**curl**

```bash
curl -X POST "https://dev.api.zb-connector.com/webhook/" \
  -H "Content-Type: application/json" \
  -d '{"license":"YOUR_LICENSE_KEY","action":"setTrailingStop","positionID":"eurusd_breakout_1","trailDistance":30}'
```

**Python**

```python
import requests

payload = """
{
  "license": "YOUR_LICENSE_KEY",
  "action": "setTrailingStop",
  "positionID": "eurusd_breakout_1",
  "trailDistance": 30
}
"""

response = requests.post(
    "https://dev.api.zb-connector.com/webhook/",
    data=payload,
    headers={"Content-Type": "application/json"},
    timeout=10,
)
print(response.status_code, response.text)
```

**Node.js**

```js
const response = await fetch("https://dev.api.zb-connector.com/webhook/", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    "license": "YOUR_LICENSE_KEY",
    "action": "setTrailingStop",
    "positionID": "eurusd_breakout_1",
    "trailDistance": 30
  }),
});
console.log(response.status, await response.text());
```

## What you see

After a successful run the MT5 **Experts** log shows:

```text
✓ addTrailingStop: registered trailDistance=<pips> pips for position <positionID> (ticket=<ticket>)
✓ addTrailingStop: updated trailDistance=<pips> pips for position <positionID>
```

In the dashboard, the command appears in **Command History** on the License Details page. [What each status means](https://zb-connector.com/docs/troubleshooting/find-where-it-stopped/#where-to-look).

## Errors and fixes

| Where you see it | Message or code | Cause | Fix |
| --- | --- | --- | --- |
| MT5 Experts log | `❌ handleCommand: setTrailingStop needs positionID and trailDistance>0` | The position reference is missing or the distance is 0 or less. | Send `positionID` (or `ticket`) and a `trailDistance` greater than 0. |
| MT5 Experts log | `❌ addTrailingStop: position with ID <positionID> not found` | No open position in the EA’s trade scope matches. | Check the position’s comment or ticket in MT5. |

## Notes

-   The EA moves the stop on every price tick, only in the profit direction; it never loosens it.
-   The first move can happen on the next tick. If the position has no stop loss, or the current price minus the distance is tighter than the current stop, the stop is set there straight away.
-   Sending setTrailingStop again for the same position changes the distance.
-   Trailing stops live in the EA’s memory. They are lost when the EA restarts (terminal restart, EA re-attached or recompiled); send the command again after a restart.
-   The trailing stop is removed automatically when the position closes.
-   One pip is 10 points of the symbol (the EA’s definition, the same for every symbol).

## Related

-   [Commands overview](https://zb-connector.com/docs/commands/overview/) and the [cheat sheet](https://zb-connector.com/docs/commands/cheatsheet/)
-   [moveStops](https://zb-connector.com/docs/commands/movestops/): change the stop loss and/or take profit of one position
-   [moveStopsBreakeven](https://zb-connector.com/docs/commands/movestopsbreakeven/): move the stop loss of one position to its entry price, with an optional offset
