Set buffer value

Sets a value in a specific buffer at a given index.

Syntax

this.bufferName.setValue(index, value);

Parameters

  • index - A number representing the position in the buffer to set the value at.

  • value - A number representing the value to store.

Return Value

This method does not return a value.

Description

The setValue method of a buffer object sets a value in a specific buffer at a given index. This is used to store calculated indicator values in the buffer for display on the chart.

Example

// Calculate a simple moving average
let sum = 0;
for (let i = 0; i < period; i++) {
  sum += this.api.Close(i);
}
const average = sum / period;

// Store the calculated value in buffer 0 at the current bar
this.someBuffer.setValue(0, average);

// Store values for multiple bars
for (let i = 0; i < this.api.Bars(); i++) {
  const value = calculateIndicatorValue(i);
  this.someBuffer.setValue(i, value);
}

Last updated