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

Candle Anatomy

Install and import#

bash
npm install fintech-algorithms
ts
import { analyzeCandle } from "fintech-algorithms/price-action-and-candlesticks/candle-foundations/candle-anatomy";

Signature#

analyzeCandle(candle, directionEpsilon)

Decomposes one candle into body, upper and lower shadow, and direction. Every candlestick pattern is built on these measurements, so an inconsistent decomposition propagates into every pattern above it.

Parameters#

NameTypeNotes
candleCandleOne OHLC candle with is_closed. An unclosed candle can still be analysed, but its body may yet change — the flag is carried through so downstream code can decide.
directionEpsilonnumberHow close open and close must be to count as unchanged rather than up or down. Without it, floating-point noise makes a doji impossible.
min: 0

Returns#

{ open, high, low, close, body, upper_shadow, lower_shadow, direction, … }

Every measurement a pattern rule needs, in price units.

Errors#

  • When high is below low, or open or close falls outside the range — throws

Complexity: time O(1), space O(1).

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#

candle
{
  "timestamp": "2026-01-05T10:00:00Z",
  "instrument_id": "SYNTH:ABC",
  "interval": "5m",
  "open": 100,
  "high": 108,
  "low": 97,
  "close": 105,
  "is_closed": true,
  "price_basis": "raw-trades",
  "session": "synthetic-utc"
}
directionEpsilon
0

Call#

analyzeCandle(candle, directionEpsilon)

Returns#

object with 23 fields: open, high, low, close, timestamp, instrument_id, interval, is_closed, …

{
  "open": 100,
  "high": 108,
  "low": 97,
  "close": 105,
  "timestamp": "2026-01-05T10:00:00Z",
  "instrument_id": "SYNTH:ABC",
  "interval": "5m",
  "is_closed": true,
  "volume": null,
  "price_basis": "raw-trades",
  "session": "synthetic-utc",
  "body_high": 105,
  "body_low": 100,
  "body": 5
}

Showing 14 of 23 fields.

Other exports#

This module also exports analyzeCandles. 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.

Diagrams#

Candle Anatomy — candle anatomy
Candle Anatomy — candle comparison

Calculation flow#

Candle lifecycle and revision state
stateDiagram-v2
    [*] --> Collecting
    Collecting --> Provisional: first eligible price
    Provisional --> Provisional: new trade updates high, low, or close
    Provisional --> Closed: interval ends
    Closed --> Revised: provider correction
    Revised --> Closed: corrected OHLC republished
    Closed --> [*]
Candle anatomy calculation flow
flowchart TD
    A["Receive OHLC and metadata"] --> B{"All OHLC values finite?"}
    B -->|"No"| X["Reject with validation error"]
    B -->|"Yes"| C{"High and low contain open and close?"}
    C -->|"No"| X
    C -->|"Yes"| D["Compute body high and body low"]
    D --> E["Compute body, shadows, and range"]
    E --> F["Assign direction using epsilon"]
    F --> G{"Range greater than zero?"}
    G -->|"Yes"| H["Compute component ratios and positions"]
    G -->|"No"| I["Set ratios and positions to null"]
    H --> J["Return anatomy and metadata"]
    I --> J

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 Candle Foundations family#