# CreateIndexBuffer

Creates a new buffer for storing indicator values.

**Important**: This method should **only be used inside the `Init()` method** during indicator initialization.

## Syntax

```typescript
CreateIndexBuffer(): TIndexBuffer
```

## Parameters

This method does not take any parameters.

## Return Value

Returns a [TIndexBuffer](https://fto-2.gitbook.io/fto-indicators-docs/types/index-buffer) object that can be used to store indicator values.

## Description

The `CreateIndexBuffer` method creates a new buffer for storing indicator values. Buffers are essential for displaying data on charts and must be properly declared, created, and configured during indicator setup.

**Buffer Management Rules:**

* **Declare** all buffers as class-level fields using the `!` syntax
* **Create** buffers in `Init()` using `CreateIndexBuffer()`
* **Register** buffer count with `IndicatorBuffers()`
* **Assign** buffers to indexes with `SetIndexBuffer()`
* **Configure** buffer appearance with `SetIndexStyle()`

See [TIndexBuffer](https://fto-2.gitbook.io/fto-indicators-docs/types/index-buffer) for more information about working with indicator buffers.

## Proper Buffer Declaration

All buffers must be declared as **class-level fields** with the non-null assertion operator (`!`):

```typescript
// ✅ Correct - declare as class-level fields
export default class MyIndicator extends IndicatorImplementation {
  public mainBuffer!: TIndexBuffer;
  public signalBuffer!: TIndexBuffer;
  public helperBuffer!: TIndexBuffer;

  public Init(): void {
    this.mainBuffer = this.api.CreateIndexBuffer();
    this.signalBuffer = this.api.CreateIndexBuffer();
    this.helperBuffer = this.api.CreateIndexBuffer();
  }
}
// ❌ Wrong - don't use api methods (any api methods, including CreateIndexBuffer) in class level fields
export default class MyIndicator extends IndicatorImplementation {
  public mainBuffer = this.api.CreateIndexBuffer();
  public signalBuffer = this.api.CreateIndexBuffer();
  public helperBuffer = this.api.CreateIndexBuffer();

  public Init(): void {}
}
```

## Complete Buffer Setup Workflow

### Step 1: Declare Buffers (Class Level)

```typescript
export default class MovingAverageIndicator extends IndicatorImplementation {
    // Declare all buffers as class properties
    public maBuffer!: TIndexBuffer;
    public signalBuffer!: TIndexBuffer;
```

### Step 2: Create and Configure in Init()

```typescript
    public Init(): void {
        // 1. Register total number of buffers FIRST
        this.api.IndicatorBuffers(2);

        // 2. Create buffer instances
        this.maBuffer = this.api.CreateIndexBuffer();
        this.signalBuffer = this.api.CreateIndexBuffer();

        // 3. Assign buffers to indexes
        this.api.SetIndexBuffer(0, this.maBuffer);
        this.api.SetIndexBuffer(1, this.signalBuffer);

        // 4. Configure buffer appearance
        this.api.SetIndexStyle(0, TDrawStyle.LINE, TPenStyle.SOLID, 2, "#0000ff");
        this.api.SetIndexStyle(1, TDrawStyle.LINE, TPenStyle.DASH, 1, "#ff0000");

        // 5. Set buffer labels
        this.api.SetIndexLabel(0, "Moving Average");
        this.api.SetIndexLabel(1, "Signal Line");
    }
```

### Step 3: Use Buffers in Calculate()

```typescript
    public Calculate(index: number): void {
        // Use buffers to store and retrieve values
        const closePrice = this.api.Close(index);
        const maValue = this.calculateMA(index);

        // Set buffer values
        this.maBuffer.setValue(index, maValue);
        this.signalBuffer.setValue(index, maValue * 1.1);

        // Get buffer values (from previous bars)
        const previousMA = this.maBuffer.getValue(index + 1);
    }
}
```

## Buffer Creation Rules

### ✅ Must Be Done in Init()

* **Create** all buffers with `CreateIndexBuffer()`
* **Register** buffer count with `IndicatorBuffers()`
* **Assign** buffers to indexes with `SetIndexBuffer()`
* **Configure** initial styles with `SetIndexStyle()`

### ❌ Cannot Be Done Outside Init()

* **Creating new buffers** in `Calculate()` or other methods
* **Changing buffer count** after initialization
* **Reassigning buffer indexes** during runtime

## Complete Example

```typescript
export default class AdvancedIndicator extends IndicatorImplementation {
  // 1. Declare all buffers as class-level fields
  public fastMA!: TIndexBuffer;
  public slowMA!: TIndexBuffer;
  public signalBuffer!: TIndexBuffer;
  public helperBuffer!: TIndexBuffer;

  public Init(): void {
    // 2. Register total buffer count
    this.api.IndicatorBuffers(4);

    // 3. Create buffer instances
    this.fastMA = this.api.CreateIndexBuffer();
    this.slowMA = this.api.CreateIndexBuffer();
    this.signalBuffer = this.api.CreateIndexBuffer();
    this.helperBuffer = this.api.CreateIndexBuffer();

    // 4. Assign to indexes
    this.api.SetIndexBuffer(0, this.fastMA);
    this.api.SetIndexBuffer(1, this.slowMA);
    this.api.SetIndexBuffer(2, this.signalBuffer);
    this.api.SetIndexBuffer(3, this.helperBuffer);

    // 5. Configure styles
    this.api.SetIndexStyle(0, TDrawStyle.LINE, TPenStyle.SOLID, 1, "#0000ff");
    this.api.SetIndexStyle(1, TDrawStyle.LINE, TPenStyle.SOLID, 1, "#ff0000");
    this.api.SetIndexStyle(
      2,
      TDrawStyle.HISTOGRAM,
      TPenStyle.SOLID,
      2,
      "#00ff00"
    );
    this.api.SetIndexStyle(3, TDrawStyle.NONE, TPenStyle.SOLID, 1, "#000000"); // Hidden

    // 6. Set labels
    this.api.SetIndexLabel(0, "Fast MA");
    this.api.SetIndexLabel(1, "Slow MA");
    this.api.SetIndexLabel(2, "Signal");
  }

  public Calculate(index: number): void {
    // Use buffers for calculations and display
    const fastValue = this.calculateFastMA(index);
    const slowValue = this.calculateSlowMA(index);

    this.fastMA.setValue(index, fastValue);
    this.slowMA.setValue(index, slowValue);
    this.helperBuffer.setValue(index, fastValue - slowValue);

    // Conditional signal display
    if (Math.abs(fastValue - slowValue) > 0.01) {
      this.signalBuffer.setValue(index, fastValue);
    }
  }
}
```

## Key Rules Summary

1. **Declare buffers as class-level fields** with `!` syntax
2. **Create buffers only in `Init()`** using `CreateIndexBuffer()`
3. **Assign buffers to indexes** with `SetIndexBuffer()`
4. **Configure appearance** with `SetIndexStyle()`
5. **Use buffers in `Calculate()`** for data storage and retrieval
6. **Never create buffers outside `Init()`** - this will cause errors
