Chartsv26.10.18
Pro edition

Research studies

The study contract of the library and the template gallery of the desk - definitions from host code, parameter validation with actionable messages, and ready-made research presets.

The library ships the catalogue, and a host extends it without touching the package: a study is a plain definition object with a calculation function. The same contract drives the catalogue, the indicator dialog and the template gallery, so a custom study appears next to the built-in ones with its own parameters.

Pick a card to fill its parameter form; save hands the parameters to the host.
Parameters
Trades0
Wins0
Losses0
Win rate0.0%
P&L0.00
Average P&L0.00
Profit factor-
Max drawdown0.00
Sharpe0.00
Largest win0.00
Largest loss0.00

Closed trades: 0; final equity: 100000.00

Both charts above stream from the same live history: the candles carry the full desk effects and the pane below is the RSI study of that same feed. The preset tpl.rsi-zones fills its form from the declared defaults, and the report recomputes over the live bars every few ticks.

Both charts above are the real library chart: the candle pane streams the newest bar of the shared desk history, the pane below is the same feed read through the RSI study, and the report walks the backtest engine over that very history. The template gallery stays live next to them: a card fills its parameter form and the save hands the resolved parameters to the host.

The study definition

A definition names the study, declares the placement, the inputs and the outputs, and computes values into the output buffers. Nothing is executed from data: the calculation is a function of the host, and the engine only calls it over bars.

import type { IndicatorDefinition } from '@unifin/chart-core';

const drift: IndicatorDefinition = {
  id: 'custom.session-drift',
  title: 'Session drift',
  category: 'custom',
  placement: 'oscillator',
  apiVersion: 1,
  incremental: false,
  inputs: [
    { id: 'window', title: 'Window', kind: 'number', default: 12, min: 2, max: 60, step: 1 },
    { id: 'smooth', title: 'Smooth', kind: 'boolean', default: true },
  ],
  outputs: [{ id: 'drift', kind: 'line' }],
  calculate: ({ bars, params, outputs, from, to }) => {
    const window = typeof params.window === 'number' ? params.window : 12;
    const buffer = outputs.drift;
    if (buffer === undefined) return;
    for (let index = from; index < to; index += 1) {
      buffer[index] = index >= window ? bars[index]!.close - bars[index - window]!.close : Number.NaN;
    }
  },
};

Registration and validation

IndicatorRegistry holds the definitions of the runtime; several registries may live side by side, and a new binding keeps the registrations when the host passes its registry along. Every definition passes the validator first:

import { IndicatorRegistry, validateStudyParams } from '@unifin/chart-core';

const registry = new IndicatorRegistry();
registry.register(drift);

const problems = validateStudyParams(drift, { window: 120 });
// [{ path: 'params.window', message: '120 is above max 60' }]
RefusalProblem you receive
Missing or malformed idid must be a lowercase slug (letters, digits, dot, dash)
Default outside the declared rangeinputs.period default 120 is above max 100
Duplicate registrationid is already registered
Unknown runtime parameterparams.lvl is not declared; declared: period, smoothing
Value of the wrong kindparams.period expects a finite number

The indicator dialog and the element

The React dialog accepts registered studies through studyDialogItems: a definition maps to a row with its category and its editable number, boolean and select inputs. The custom element owns a runtime registry as well and exposes registerStudy, studyIds and the studies registry: a study registered from host code survives a binding recreation and never throws at the host - a broken definition returns false and leaves the contract problems in definitionProblems.

The gallery carries ready-made research: four study presets (RSI with zones, an EMA cloud, volatility bands, an MA fan) and three strategy presets (an EMA cross, a range breakout, a mean reversion). Presets are data: the host picks a card, the parameter form fills from the declared defaults, and saving hands the resolved parameters to the host. The presets come from @unifin/chart-core/studies:

import { STUDY_TEMPLATES, studyTemplate, studyTemplateParams } from '@unifin/chart-core/studies';
import { TemplateGallery } from '@unifin/chart-react/pro';

const rsi = studyTemplate('tpl.rsi-zones');
const defaults = rsi === null ? {} : studyTemplateParams(rsi); // { period: 14, upper: 70, lower: 30 }

A study preset is a full IndicatorDefinition, so it registers unchanged. A strategy preset is declarative text of the strategy language - see Strategies and backtest for the engine that runs it.

On this page