SetIndexVisibility

Sets the visibility of a buffer programmatically based on custom indicator logic.

Syntax

SetIndexVisibility(index: number, isVisible: boolean): void

Parameters

  • index - A number representing the index of the buffer.

  • isVisible - A boolean indicating whether the buffer should be visible.

Return Value

This method does not return a value.

Description

The SetIndexVisibility method allows your indicator to programmatically control buffer visibility based on custom logic, calculations, or indicator states. This is NOT for basic user preferences (users already have built-in checkboxes for that), but for algorithmic decisions where the indicator itself determines what should be displayed.

When to Use SetIndexVisibility

βœ… Use SetIndexVisibility For:

  • State-based indicators - show different buffers for different market states

  • Conditional logic - only show certain buffers when specific conditions are met

  • Mutually exclusive displays - show one buffer OR another, never both

  • Data-driven visibility - hide buffers when data is invalid or insufficient

  • Dynamic switching - change what's visible based on calculations

❌ Don't Use SetIndexVisibility For:

  • Basic user preferences - users have built-in checkboxes for this

  • Simple on/off toggles - the built-in visibility controls handle this

  • Initial visibility setup in Init() (use SetIndexStyle instead)

SetIndexVisibility vs Built-in Controls

Built-in User Controls (Checkboxes)

Users can already control basic visibility:

  • βœ… "Show/Hide Main Line"

  • βœ… "Show/Hide Signal Line"

  • βœ… Basic on/off toggles

SetIndexVisibility (Custom Logic)

Use for intelligent, condition-based visibility:

  • βœ… Show uptrend buffer OR downtrend buffer (never both)

  • βœ… Hide signal when market is ranging

  • βœ… Show different buffers for different timeframes

  • βœ… Display warning buffer only when conditions are dangerous

Practical Examples

Example 1: State-Based Indicator (Trend vs Range)

export default class TrendRangeIndicator extends IndicatorImplementation {
  public trendBuffer!: TIndexBuffer;
  public rangeBuffer!: TIndexBuffer;
  private isInTrend: boolean = false;

  public Calculate(index: number): void {
    // Determine market state
    this.isInTrend = this.calculateTrendState(index);

    if (this.isInTrend) {
      // Show trend buffer, hide range buffer
      this.api.SetIndexVisibility(0, true); // Trend buffer
      this.api.SetIndexVisibility(1, false); // Range buffer

      this.trendBuffer.setValue(index, this.calculateTrendValue(index));
    } else {
      // Show range buffer, hide trend buffer
      this.api.SetIndexVisibility(0, false); // Trend buffer
      this.api.SetIndexVisibility(1, true); // Range buffer

      this.rangeBuffer.setValue(index, this.calculateRangeValue(index));
    }
  }
}

Example 2: Conditional Signal Display

export default class SmartSignalIndicator extends IndicatorImplementation {
  public mainBuffer!: TIndexBuffer;
  public warningBuffer!: TIndexBuffer;
  public signalBuffer!: TIndexBuffer;

  public Calculate(index: number): void {
    const mainValue = this.calculateMainValue(index);
    this.mainBuffer.setValue(index, mainValue);

    // Only show warning when market is volatile
    const volatility = this.calculateVolatility(index);
    if (volatility > 2.0) {
      this.api.SetIndexVisibility(1, true); // Show warning
      this.warningBuffer.setValue(index, mainValue);
    } else {
      this.api.SetIndexVisibility(1, false); // Hide warning
    }

    // Only show signals when conditions are optimal
    const signalStrength = this.calculateSignalStrength(index);
    if (signalStrength > 0.8 && volatility < 1.5) {
      this.api.SetIndexVisibility(2, true); // Show signal
      this.signalBuffer.setValue(index, this.generateSignal(index));
    } else {
      this.api.SetIndexVisibility(2, false); // Hide signal
    }
  }
}

Example 3: Timeframe-Dependent Display

export default class MultiTimeframeIndicator extends IndicatorImplementation {
  public shortTermBuffer!: TIndexBuffer;
  public longTermBuffer!: TIndexBuffer;

  public OnParamsChange(): void {
    const currentTimeframe = this.api.Timeframe();

    if (currentTimeframe <= 60) {
      // 1 hour or less
      // Show short-term analysis
      this.api.SetIndexVisibility(0, true); // Short-term buffer
      this.api.SetIndexVisibility(1, false); // Long-term buffer
    } else {
      // Show long-term analysis
      this.api.SetIndexVisibility(0, false); // Short-term buffer
      this.api.SetIndexVisibility(1, true); // Long-term buffer
    }
  }
}

Example 4: Data Quality Control

export default class QualityControlIndicator extends IndicatorImplementation {
  public reliableBuffer!: TIndexBuffer;
  public unreliableBuffer!: TIndexBuffer;

  public Calculate(index: number): void {
    const dataQuality = this.assessDataQuality(index);
    const calculatedValue = this.performCalculation(index);

    if (dataQuality > 0.9) {
      // High quality data - show reliable buffer
      this.api.SetIndexVisibility(0, true); // Reliable buffer
      this.api.SetIndexVisibility(1, false); // Unreliable buffer
      this.reliableBuffer.setValue(index, calculatedValue);
    } else {
      // Low quality data - show unreliable buffer with warning style
      this.api.SetIndexVisibility(0, false); // Reliable buffer
      this.api.SetIndexVisibility(1, true); // Unreliable buffer
      this.unreliableBuffer.setValue(index, calculatedValue);
    }
  }
}

Common Use Cases

  1. Mutually Exclusive States: Show buffer A OR buffer B, never both

  2. Conditional Signals: Only display signals when market conditions are suitable

  3. Quality Control: Hide unreliable data, show only when confidence is high

  4. Mode Switching: Different displays for different market modes (trend/range/volatile)

  5. Error Handling: Hide buffers when calculations are invalid or insufficient data

  6. Performance Optimization: Hide complex buffers during high-frequency updates

Key Rules

  1. SetIndexVisibility = Algorithmic decisions by the indicator

  2. Built-in checkboxes = User preferences and basic on/off controls

  3. Use for intelligent logic, not simple user toggles

  4. Perfect for state machines and conditional displays

  5. Great for data quality control and error handling

Last updated