SetIndexStyle
Sets the visual style for a buffer.
Important: This method should only be used inside the Init() method for initial buffer configuration.
Syntax
SetIndexStyle(
bufferIndex: number,
type: TDrawStyle,
style: TPenStyle,
width: number,
clr: string,
isVisible?: boolean
): voidParameters
bufferIndex- A number representing the index of the buffer to style.type- A value from theTDrawStyleenum specifying how to draw the buffer.style- A value from theTPenStyleenum specifying the line style.width- A number representing the line width in pixels.clr- A string hex color value for the buffer.isVisible- (Optional) A boolean for initial visibility setup only. For algorithmic visibility control, useSetIndexVisibilityinstead.
Return Value
This method does not return a value.
Description
The SetIndexStyle method sets the visual appearance of a buffer on the chart during indicator initialization. This includes the drawing style (line, histogram, etc.), line style (solid, dashed, etc.), width, color, and initial visibility.
Usage Guidelines:
Use in
Init()only - for setting up buffer appearance during indicator creationFor algorithmic visibility control - use
SetIndexVisibilityinCalculate()based on conditions/calculations
See TDrawStyle for available drawing styles, TPenStyle for line styles
Visibility Parameter vs SetIndexVisibility
Initial Setup (in Init())
Use the isVisible parameter in SetIndexStyle for setting the initial visibility state:
public Init(): void {
// Set initial style with visibility
this.api.SetIndexStyle(0, TDrawStyle.LINE, TPenStyle.SOLID, 2, "#0000ff", true); // Initially visible
this.api.SetIndexStyle(1, TDrawStyle.NONE, TPenStyle.SOLID, 1, "#000000", false); // Initially hidden
}Dynamic Changes (in OnParamsChange() or Calculate())
Use SetIndexVisibility for algorithmic visibility control:
public Calculate(index: number): void {
// Show different buffers based on market state
if (this.isInTrendingMarket(index)) {
this.api.SetIndexVisibility(0, true); // Trend buffer
this.api.SetIndexVisibility(1, false); // Range buffer
} else {
this.api.SetIndexVisibility(0, false); // Trend buffer
this.api.SetIndexVisibility(1, true); // Range buffer
}
}Example
public Init(): void {
// Initial buffer setup with visibility
// Buffer 0: Always visible main line
this.api.SetIndexStyle(0, TDrawStyle.LINE, TPenStyle.SOLID, 2, "#0000ff", true);
// Buffer 1: Initially hidden calculation buffer
this.api.SetIndexStyle(1, TDrawStyle.NONE, TPenStyle.SOLID, 1, "#000000", false);
// Buffer 2: Initially hidden, will be shown conditionally
this.api.SetIndexStyle(2, TDrawStyle.HISTOGRAM, TPenStyle.SOLID, 3, "#00ff00", false);
}
public Calculate(index: number): void {
// Algorithmic visibility control - use SetIndexVisibility
const signalStrength = this.calculateSignalStrength(index);
if (signalStrength > 0.8) {
this.api.SetIndexVisibility(2, true); // Show strong signals only
} else {
this.api.SetIndexVisibility(2, false); // Hide weak signals
}
// DON'T do this in Calculate() or any method thats not Init():
// this.api.SetIndexStyle(2, TDrawStyle.HISTOGRAM, TPenStyle.SOLID, 3, "#00ff00", true); // ❌ Wrong!
}Key Rules
SetIndexStyle= Initial setup inInit()onlySetIndexVisibility= Dynamic changes inOnParamsChange()orCalculate()Both methods control visibility, but serve different purposes in the indicator lifecycle
Last updated