# closeByID: Close the position (or positions) with a given positionID

> Close the position (or positions) with a given positionID. Parameters, units, copy-ready examples, log lines and errors for the closeByID command.

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

Close the position (or positions) with a given positionID.

> **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:** `closeByID` (case-sensitive)
    
-   **Group:** Close positions
    
-   **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 `closeByID`. 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. |

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.

### Close the position opened with positionID eurusd\_breakout\_1

**JSON**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "closeByID",
  "positionID": "eurusd_breakout_1"
}
```

**curl**

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

**Python**

```python
import requests

payload = """
{
  "license": "YOUR_LICENSE_KEY",
  "action": "closeByID",
  "positionID": "eurusd_breakout_1"
}
"""

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": "closeByID",
    "positionID": "eurusd_breakout_1"
  }),
});
console.log(response.status, await response.text());
```

### Close a position by its MT5 ticket

**JSON**

```json
{
  "license": "YOUR_LICENSE_KEY",
  "action": "closeByID",
  "ticket": 123456789
}
```

**curl**

```bash
curl -X POST "https://dev.api.zb-connector.com/webhook/" \
  -H "Content-Type: application/json" \
  -d '{"license":"YOUR_LICENSE_KEY","action":"closeByID","ticket":123456789}'
```

**Python**

```python
import requests

payload = """
{
  "license": "YOUR_LICENSE_KEY",
  "action": "closeByID",
  "ticket": 123456789
}
"""

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": "closeByID",
    "ticket": 123456789
  }),
});
console.log(response.status, await response.text());
```

## What you see

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

```text
Closed position <ticket> successfully (symbol: <symbol>)
Close Summary: Success = <n>, Failed = <n>
```

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: closeByID requires positionID` | Neither `positionID` nor `ticket` reached the EA. | Send the `positionID` used when the position was opened, or its `ticket`. |
| MT5 Experts log | `Close Summary: Success = 0, Failed = 0` | No open position in the EA’s trade scope matches the positionID. | Check the comment of the position in MT5 (Trade tab) and the EA’s trade scope. |
| Broker, MT5 return code | [10018 Market is closed](https://zb-connector.com/docs/troubleshooting/errors/10018/) | The market for this symbol is closed. | Send the command again when the market is open. |

## Notes

-   Every matching position is closed, so reuse of the same positionID on several open positions closes all of them.
-   Only positions in the EA’s trade scope are considered (by default, trades opened by this EA).

## Related

-   [Commands overview](https://zb-connector.com/docs/commands/overview/) and the [cheat sheet](https://zb-connector.com/docs/commands/cheatsheet/)
-   [close\_percentage](https://zb-connector.com/docs/commands/close_percentage/): close a percentage of one position
-   [closeAll](https://zb-connector.com/docs/commands/closeall/): close every open position (long and short), on all symbols
-   [closeByLotSize](https://zb-connector.com/docs/commands/closebylotsize/): close a given number of lots of one position
-   [closeLong](https://zb-connector.com/docs/commands/closelong/): close every open buy (long) position, on all symbols
