# Cancel-on-Disconnect

`D12-F03-A04` · Matching Engines and Venue Logic → Order Controls · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/order-controls/cancel-on-disconnect/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { cancelOnDisconnect } from "fintech-algorithms/matching-engines-and-venue-logic/order-controls/cancel-on-disconnect";
```

## Signature

```ts
cancelOnDisconnect(disconnected_session_id, disconnect_type, trigger_disconnect_types, policy, orders)
```

Decides which orders to pull when a session drops. Not every disconnect should cancel — a deliberate logout and a network failure are different events, and cancelling a hedge because of a brief blip is its own risk.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `disconnected_session_id` | `string` | yes | The session that dropped. |
| `disconnect_type` | `string` | yes | How it dropped — graceful, timeout or transport failure. |
| `trigger_disconnect_types` | `string[]` | yes | Which disconnect types actually trigger cancellation. |
| `policy` | `string` | yes | Cancellation scope: session, participant or firm. |
| `orders` | `Order[]` | yes | Live orders to evaluate. |

## Returns

`{ cancelled, retained, triggered, policy, … }`

Orders cancelled and retained, with whether the disconnect type triggered at all.

## Errors

- When the policy is unrecognised — throws

## Complexity

Time `O(orders)`, space `O(cancelled)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`disconnected_session_id`:

```json
"S-1"
```

`disconnect_type`:

```json
"hard"
```

`trigger_disconnect_types`:

```json
["hard", "graceful", "server"]
```

`policy`:

```json
"cancel_continuous"
```

`orders`:

```json
[
  {
    "order_id": "O-1",
    "session_id": "S-1",
    "book": "continuous",
    "time_in_force": "DAY",
    "quantity": 100
  },
  {
    "order_id": "O-2",
    "session_id": "S-1",
    "book": "continuous",
    "time_in_force": "GTC",
    "quantity": 200
  },
  {
    "order_id": "O-3",
    "session_id": "S-1",
    "book": "auction",
    "time_in_force": "DAY",
    "quantity": 300
  }
]
```

Showing 3 of 4 elements.

### Call

```ts
cancelOnDisconnect(disconnected_session_id, disconnect_type, trigger_disconnect_types, policy, orders)
```

### Returns

object with 9 fields: disconnected_session_id, disconnect_type, triggered, policy, canceled_order_ids, retained_orders, canceled_count, retained_count, …

```json
{
  "disconnected_session_id": "S-1",
  "disconnect_type": "hard",
  "triggered": true,
  "policy": "cancel_continuous",
  "canceled_order_ids": ["O-1", "O-2"],
  "retained_orders": [
    {
      "order_id": "O-3",
      "reason": "auction-order-retained"
    },
    {
      "order_id": "O-4",
      "reason": "different-session"
    }
  ],
  "canceled_count": 2,
  "retained_count": 2,
  "state": "purge-applied"
}
```

## Other exports

`tickSizeValidation`, `priceBandValidation`, `selfTradePrevention`, `fatFingerLimit`, `circuitBreakerTrigger`, `calculate`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **verified** (via D).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/matching-engines-and-venue-logic/order-controls/cancel-on-disconnect/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/matching-engines-and-venue-logic/order-controls/cancel-on-disconnect/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/matching-engines-and-venue-logic/llms.txt
