# Candlestick Confirmation and Invalidation State Machine

`D06-F05-A08` · Price Action and Candlesticks → Candlestick Scanning and Context · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/price-action-and-candlesticks/candlestick-scanning-and-context/candlestick-confirmation-and-invalidation-state-machine/
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 { runConfirmationStateMachine } from "fintech-algorithms/price-action-and-candlesticks/candlestick-scanning-and-context/candlestick-confirmation-and-invalidation-state-machine";
```

## Signature

```ts
runConfirmationStateMachine(data)
```

advance directional candidates through closed-bar transitions with ordered levels, finite expiry, and complete reason-coded history.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `data` | `{ detection_index: number; direction: string; confirmation_level: number; invalidation_level: number; expires_after_bars: number; events: { bar_index: number; bar_closed: boolean; close: number }[] }` | yes | Topic input record; the required fields are fixed by this topic data-contract. |

## Returns

`{ state, direction, confirmation_level, invalidation_level, transitions, terminal }`

One lifecycle record. `state` progresses from `awaiting-confirmation` to `confirmed`, `invalidated`, or `expired`; `terminal` identifies those final states and `transitions` preserves the full reason-coded trace.

## Complexity

Time `O(n log n) worst case; see README for topic-specific n`, space `O(n)`.

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`data`:

```json
{
  "detection_index": 10,
  "direction": "bullish",
  "confirmation_level": 105,
  "invalidation_level": 95,
  "expires_after_bars": 3,
  "events": [
    {
      "bar_index": 11,
      "bar_closed": true,
      "close": 102
    },
    {
      "bar_index": 12,
      "bar_closed": true,
      "close": 106
    }
  ]
}
```

### Call

```ts
runConfirmationStateMachine(data)
```

### Returns

object with 4 fields: state, terminal, confirmation_level, invalidation_level

```json
{
  "state": "confirmed",
  "terminal": true,
  "confirmation_level": 105,
  "invalidation_level": 95
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

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.12.0.
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/price-action-and-candlesticks/candlestick-scanning-and-context/candlestick-confirmation-and-invalidation-state-machine/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/price-action-and-candlesticks/candlestick-scanning-and-context/candlestick-confirmation-and-invalidation-state-machine/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/price-action-and-candlesticks/llms.txt
