Home » Array Functions » array.clear Function in Pine Script

array.clear Function in Pine Script

Photo of author
Published on

In this tutorial, we will be delving into the **array.clear**function in Pine Script. This powerful function allows you to clear an array, making it empty and ready to be reused. By the end of this tutorial, you will understand how to use the **array.clear**function and its applications in Pine Script.

The **array.clear**function in Pine Script is used to empty an array, effectively removing all its elements. The syntax for the function is:

array.clear(id)
Where arr is the array you want to clear.

When to Use array.clear

You might want to use the array.clear function in situations where you need to:

  • Reset the array to its initial state.
  • Reuse an array for a different purpose.
  • Free up memory by clearing large arrays that are no longer needed.

Use Case Example

Now that we have a good understanding of the array.clear function, let’s look at two unique use case examples to see how it can be applied in Pine Script.

Example Trading Volume Accumulation

//@version=5
indicator('Volume Accumulation', shorttitle='VA', overlay=false)

interval = input.int(20, minval=1, title='Interval (Bars)')

// Initialize the volume accumulation array
volume_accumulation = array.new_float()

// Add the current volume to the array
for i = 1 to interval by 1
    array.push(volume_accumulation, volume[i])

// Calculate the accumulated volume
accumulated_volume = array.sum(volume_accumulation)

// Reset the volume accumulation array every 'interval' bars
if bar_index % interval == 0
    array.clear(volume_accumulation)
    array.push(volume_accumulation, volume)

// Plot the accumulated volume
plot(accumulated_volume, color=color.new(color.blue, 0), linewidth=2, title='Accumulated Volume', offset=-1)

Here Is the output of this indicator on charts

In this code:

  1. We create an input for the interval at which the volume accumulation will be reset.
  2. We initialize a float array called volume_accumulation to store the trading volumes.
  3. We push the current trading volume onto the volume_accumulation array.
  4. We calculate the accumulated volume using the array.sum() function.
  5. We use a conditional statement to check if the current bar index is divisible by the specified interval. If so, we use array.clear(volume_accumulation) to reset the array, and then immediately push the current trading volume onto the now empty volume_accumulation array.
  6. Finally, we plot the accumulated volume on the chart.

This example demonstrates how to use the array.clear function to accumulate trading volume over a specified interval and reset the accumulation every n bars. This can provide insights into volume trends during different time periods.

Conclusion

The array.clear function in Pine Script is a useful tool for managing arrays and maintaining clean, efficient code. We have demonstrated its utility in above example. By understanding how to use the array.clear function, you can create more dynamic and efficient indicators and strategies in Pine Script.

Remember that the array.clear function is particularly helpful in situations where you need to reset an array to its initial state, reuse an array for a different purpose, or free up memory by clearing large arrays that are no longer needed.

We hope this tutorial has provided you with valuable insights into the array.clear function in Pine Script and how it can be applied in your own projects. With this knowledge, you can now confidently incorporate the array.clear function into your Pine Script toolbox and create more advanced and efficient trading tools. Happy coding!

Leave a Comment