fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Candlestick Confirmation and Invalidation State Machine

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#

runConfirmationStateMachine(data)

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

Parameters#

NameTypeNotes
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 }[] }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#

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

Input#

data
{
  "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#

runConfirmationStateMachine(data)

Returns#

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

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

Diagrams#

Candlestick Confirmation and Invalidation State Machine — decision

Calculation flow#

Decision flow
flowchart LR
    A["Validated point-in-time input"] --> B["Detect geometry"]
    B --> C["Arm ordered levels"]
    C --> D{"Boundary satisfied?"}
    D -->|"Yes"| E["Reason-coded ready output"]
    D -->|"No"| F["Explicit rejected or unavailable state"]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

The rest of the Candlestick Scanning and Context family#