YOUSEFUL / DELPHI / COMPONENTS

TAtlasFinStudy

One thing a pane draws: the price bars, the volume, or a technical indicator computed from the closes.

Class Unit Atlas.FinChart · Inherits TCollectionItem · Product Atlas Fin

Description

A study is an item of a pane's TAtlasFinStudies collection; Kind (TAtlasStudyKind) says what it draws. The price kinds draw the bars in UpColor and DownColor: candles, OHLC bars (open tick left, close tick right), a close line, or a close area filled with the line colour mixed 75 % towards the background. Below three pixels per bar, candles and OHLC bars are drawn as high-low lines only. The volume kind draws bars from zero. The indicator kinds (SMA, EMA, Bollinger, RSI and MACD) compute their lines from the closing prices with TAtlasIndicators and cache them until the bars or the study's settings change.

Indicator lines take Color or, left at clDefault, the next colour of a fixed six-colour palette. The palette follows the order of indicator studies across the whole chart, so the same layout always gets the same colours. Bollinger bands draw all three lines in that colour, the middle one dashed. MACD draws its line in that colour, its signal line in a palette colour picked by the study's position in its pane (ignoring Color), and its histogram in the line colour mixed 60 % towards the background.

Hidden studies (Visible False) are neither drawn, listed in the header, nor counted in the pane's scale. All settings are stored in the form file.

Members

MemberKindWhat it does
CreateconstructorCreates a candle study with Period 14, SlowPeriod 26, SignalPeriod 9, Deviations 2, LineWidth 1, Color clDefault and Visible True.
AssignmethodCopies the kind and every setting of another study, without the checks the property setters make, and discards the cached lines.
CaptionmethodThe name shown in the pane header and the collection editor: Candles, OHLC, Close (both close kinds), Volume, SMA(20), EMA(50), BB(20, 2), RSI(14) or MACD(12, 26, 9), with the current settings.
LinesmethodThe computed indicator lines for Data, each as long as Data and holding NaN where the indicator is not yet defined: one line for SMA, EMA and RSI; three for Bollinger (middle, upper, lower) and MACD (line, signal, histogram); none for the price and volume kinds.
KindpropertyWhat the study draws.
PeriodpropertyThe window of SMA, EMA, Bollinger and RSI, and the fast period of MACD; unused by the price and volume kinds.
SlowPeriodpropertyThe slow period of MACD; unused by the other kinds.
SignalPeriodpropertyThe period of the MACD signal line (an EMA of the MACD line); unused by the other kinds.
DeviationspropertyThe width of Bollinger bands in population standard deviations; 2 by default (stored only when different).
ColorpropertyThe colour of an indicator line, close line or close area; clDefault takes the next palette colour.
LineWidthpropertyThe pen width of indicator lines and the close line, clamped to 1..8.
VisiblepropertyFalse hides the study: it is not drawn, not shown in the header and not counted in the pane's scale.

Constructors

Create constructor

constructor Create(Collection: TCollection); override;

Creates a candle study with Period 14, SlowPeriod 26, SignalPeriod 9, Deviations 2, LineWidth 1, Color clDefault and Visible True. Pane.Studies.Add(Kind, Period) is the usual way to create one.

Properties

Kind property

property Kind: TAtlasStudyKind read FKind write SetKind default skCandles;

What the study draws. Changing it recomputes the lines. Switching to skMACD raises EAtlasFinError (MACD needs Period below SlowPeriod; set the periods first) when Period is not below SlowPeriod.

pascal
Chart.Panes[0].Studies[0].Kind := skOhlc;

Period property

property Period: Integer read FPeriod write SetPeriod default 14;

The window of SMA, EMA, Bollinger and RSI, and the fast period of MACD; unused by the price and volume kinds. Must be 1..100000, and below SlowPeriod for MACD; otherwise EAtlasFinError is raised and nothing changes. To raise both MACD periods, set SlowPeriod first. Known limitation: a MACD study with a fast period of 26 or more cannot be read back from a form file, because Period is read before SlowPeriod and checked against the default 26 (loading raises EReadError); set such periods in code at run time.

pascal
Study.SlowPeriod := 60;   // first
Study.Period := 30;       // then the fast period

SlowPeriod property

property SlowPeriod: Integer read FSlowPeriod write SetSlowPeriod default 26;

The slow period of MACD; unused by the other kinds. Must be 1..100000 and, for MACD, above Period; otherwise EAtlasFinError is raised.

SignalPeriod property

property SignalPeriod: Integer read FSignalPeriod write SetSignalPeriod default 9;

The period of the MACD signal line (an EMA of the MACD line); unused by the other kinds. Must be 1..100000.

Deviations property

property Deviations: Double read FDeviations write SetDeviations stored IsDeviationsStored;

The width of Bollinger bands in population standard deviations; 2 by default (stored only when different). Must be 0..100; NaN or a value outside raises EAtlasFinError.

pascal
Chart.Panes[0].Studies.Add(skBollinger, 20).Deviations := 2.5;   // header: BB(20, 2.5)

Color property

property Color: TColor read FColor write SetColor default clDefault;

The colour of an indicator line, close line or close area; clDefault takes the next palette colour. The price bars and volume use the chart's UpColor and DownColor instead, and the MACD signal line always uses a palette colour.

LineWidth property

property LineWidth: Integer read FLineWidth write SetLineWidth default 1;

The pen width of indicator lines and the close line, clamped to 1..8. Candles, OHLC bars and volume bars ignore it.

Visible property

property Visible: Boolean read FVisible write SetVisible default True;

False hides the study: it is not drawn, not shown in the header and not counted in the pane's scale.

pascal
Chart.Panes[0].Studies[1].Visible := False;

Methods

Assign method

procedure Assign(Source: TPersistent); override;

Copies the kind and every setting of another study, without the checks the property setters make, and discards the cached lines.

Caption method

function Caption: string;

The name shown in the pane header and the collection editor: Candles, OHLC, Close (both close kinds), Volume, SMA(20), EMA(50), BB(20, 2), RSI(14) or MACD(12, 26, 9), with the current settings.

Lines method

function Lines(Data: TAtlasBarSeries): TArray<TArray<Double>>;

The computed indicator lines for Data, each as long as Data and holding NaN where the indicator is not yet defined: one line for SMA, EMA and RSI; three for Bollinger (middle, upper, lower) and MACD (line, signal, histogram); none for the price and volume kinds. The result is cached and shared with the chart: treat it as read-only. The cache is recognised by Data.Version alone, so pass the series the chart draws (Chart.Data); for any other series call TAtlasIndicators directly, or a series that happens to have the same version gets the cached lines of the other one.

pascal
var Rsi: Double;
Rsi := Chart.Panes[2].Studies[0].Lines(Chart.Data)[0][Chart.Data.Count - 1];

Examples

MACD with custom periods

Adds MACD(8, 21, 5) in its own pane. The fast period is given to Add; it must stay below the slow period, which is 26 until it is changed.

pascal
uses Atlas.FinChart;

procedure AddFastMacd(Chart: TAtlasFinChart);
var
  Macd: TAtlasFinStudy;
begin
  Macd := Chart.Panes.Add('MACD', 120).Studies.Add(skMACD, 8);
  Macd.SlowPeriod := 21;
  Macd.SignalPeriod := 5;
  Assert(Macd.Caption = 'MACD(8, 21, 5)');
end;

Switch the price style

The first study of the price pane changes between candles, OHLC bars, a close line and a close area.

pascal
uses Atlas.FinChart;

procedure SetPriceStyle(Chart: TAtlasFinChart; Style: Integer);
const
  Kinds: array[0..3] of TAtlasStudyKind = (skCandles, skOhlc, skCloseLine, skCloseArea);
begin
  Chart.Panes[0].Studies[0].Kind := Kinds[Style];
end;

TAtlasFinStudies · TAtlasStudyKind · TAtlasIndicators · TAtlasFinPane

Also in Atlas Fin

TAtlasFinChart