Home » Array Functions » array.fill Function in Pinescript

array.fill Function in Pinescript

Photo of author
Published on

This tutorial will cover one of Pine Script’s essential functions, array.fill , and provide you unique use case example to understand how it works. Let’s dive straight in!

What is the array.fill Function?

The array.fill function in Pine Script is used to fill an array with a specific value from a starting index to an ending index. This function is particularly helpful in cases where you want to set or update values within an array over a specified range.

Syntax

The array.fill function has the following syntax:

array.fill(array_id, value, start_index, end_index)

Where:

  • array_id is the identifier of the target array you want to fill.
  • value is the value you want to use to fill the array.
  • start_index is the starting index where the filling should begin.
  • end_index is the ending index where the filling should stop.

Example

//@version=5
indicator('array.fill example' , overlay = true)
a = array.new_float(10)
array.fill(a, close)


sumArray = (array.sum(a))
label newLable = na
if barstate.islast
    newLable := label.new(x=bar_index, y=close, style=label.style_label_left, 
     color=#E6E6FA, textcolor=color.black, size=size.large, 
     text="Sum Of last 10 bars = " + str.tostring((sumArray)))
label.delete(newLable[1])

Explanation Of Example

  • //@version=5: Specifies that the Pine Script version 5 is used.
  • indicator('array.fill example', overlay = true): Defines a new custom indicator with the title ‘array.fill example’, and sets the overlay property to true so it’s displayed directly on the price chart.
  • a = array.new_float(10): Initializes a new array called a with a fixed size of 10 elements, each containing the default float value 0.0.
  • array.fill(a, close): Fills the entire a array with the closing price of the current bar.
  • sumArray = (array.sum(a)): Calculates the sum of the values in the a array and stores it in the sumArray variable.
  • label newLable = na: Initializes a new label called newLable and sets its value to ‘na’ (not available).
  • if barstate.islast: Checks if the current bar is the last bar in the dataset.
    • newLable := label.new(x=bar_index, y=close, style=label.style_label_left, color=#E6E6FA, textcolor=color.black, size=size.large, text="Sum Of last 10 bars = " + str.tostring((sumArray))): If the current bar is the last bar, a new label is created with the sum of the array values in the a array as its text. The label is positioned at the current bar index and closing price level with specific styling and formatting properties.
  • label.delete(newLable[1]): Deletes the previous label, ensuring that only one label is visible on the chart.

Key Takeaways

  • The array.fill function in Pine Script is used to fill an array with a specific value from a starting index to an ending index.
  • The function is beneficial when setting or updating values within an array over a specified range.

Conclusion

In this tutorial, we explored the array.fill function in Pine Script and its application in various use cases. We learned about the function’s syntax, how it fills an array with a specific value within a given range, and demonstrated its utility through examples like the Simple Moving Average, Stochastic Oscillator, and a simple example using labels.

By understanding the array.fill function, you can create and manipulate arrays more effectively in your custom indicators and strategies, enhancing your trading experience on the TradingView platform. Remember to practice and experiment with different scenarios to further grasp the concept and build more efficient and powerful scripts.

Leave a Comment