Init

What is it?

The Init method is called once, right when your custom indicator is created or loaded onto the chart. This is where you set everything up β€” like naming your indicator, creating buffers, creating parameters and registering them, and more.


Syntax

public Init(): void {
    // initialization logic here
}

What Happens in Init

This method is like a constructor for your indicator. Here’s what usually happens inside:

  • Set the name and description of the indicator

  • Create buffers that store the calculated values

  • Create and register input parameters like period, color, or line style

  • Set the number of plots, line types, and other configurations


Example

export default class MovingAverage extends IndicatorImplementation {
  // parameters
  public Period!: TOptValue_number;
  public MA!: TIndexBuffer;

  public Init(): void {
    // Create parameters
    this.Period = this.api.createTOptValue_number(8);
    // Setting visible name for an indicator
    this.api.IndicatorShortName("Moving Average");
    // Setting output window to chart, not oscillator  window
    this.api.SetOutputWindow(TOutputWindow.CHART_WINDOW);
    // Registerring parameter Period
    this.api.RegOption("Period", TOptionType.INTEGER, this.Period);
    // Setting available range for it in the menu
    this.api.SetOptionRange("Period", 1, 9999);
    // Creating and tuning the buffer
    this.MA = this.api.CreateIndexBuffer();
    this.api.IndicatorBuffers(1);
    this.api.SetIndexBuffer(0, this.MA);
    this.api.SetIndexLabel(0, "Moving Average");
    this.api.SetIndexStyle(0, TDrawStyle.LINE, TPenStyle.SOLID, 1, "#FF0000");
  }
}

Why It Matters

  • Without Init, your indicator won't show anything.

  • It's required to tell the system how many lines you draw and what inputs you need.

  • Think of it as your setup stage β€” it only runs once when the indicator is loaded.


Pro Tip

Avoid heavy calculations or per-bar logic here. Use Calculate() for that. Init is only for defining the structure and settings of the indicator.

Last updated