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

array.shift Function in Pine Script

Photo of author
Published on

In this tutorial, we will be exploring the array.shift function in Pine Script, a powerful programming language used to create custom technical analysis tools in the TradingView platform. By the end of this article, you will have a clear understanding of how the array.shift function works, its syntax, and its usage in a unique use case.

Overview of array.shift Function

The array.shift function is used to remove the first element of an array and return its value. This function comes in handy when you need to manipulate an array by removing elements from the beginning of the array.

Syntax

The syntax for the array.shift function is as follows:

array.shift(id) → series <type>

Arguments

  • id (any array type): An array object.

Returns

The value of the removed element.

Basic Example

Let’s start by looking at a simple example of the array.shift function in action:

//@version=5
indicator("array.shift example")
a = array.new_float(5, high)
removedEl = array.shift(a)
plot(array.size(a))
plot(removedEl)

In this example, we create an array of floating-point numbers called a with an initial size of 5, and we initialize it with the high price values. Next, we use the array.shift function to remove the first element of the array and store the removed element’s value in the removedEl variable. Finally, we plot the updated size of the array a and the value of the removed element removedEl.

Unique Use Case: Moving Average Crossover Strategy

Now, let’s explore a unique use case for the array.shift function in Pine Script. In this example, we’ll create a Moving Average Crossover Strategy using the array.shift function to store and manipulate historical moving average values.

Example Code

//@version=5
indicator("Moving Average Crossover Strategy", overlay=true)

length = input.int(14, title="Length", minval=1)
src = close

fastMA = ta.sma(src, length)
slowMA = ta.sma(src, length * 2)

longCondition = ta.crossover(fastMA, slowMA)
if (longCondition)
    label.new(bar_index, low, "Long", color=color.green, style=label.style_label_up, size = size.huge)

shortCondition = ta.crossunder(fastMA, slowMA)
if (shortCondition)
    label.new(bar_index, high, "Short", color=color.red, style=label.style_label_down, size = size.huge)

historicalFastMAs = array.new_float(0)
historicalSlowMAs = array.new_float(0)

array.push(historicalFastMAs, fastMA)
array.push(historicalSlowMAs, slowMA)

if (array.size(historicalFastMAs) > 100)
    array.shift(historicalFastMAs)
    array.shift(historicalSlowMAs)
array.shift Function

Code Explanation

Here is an explanation of each line of code in the given script:

  1. //@version=5: This line specifies that the script uses version 5 of Pine Script.
  2. indicator("Moving Average Crossover Strategy", overlay=true): This line defines a new indicator called “Moving Average Crossover Strategy” and sets the overlay parameter to true, meaning that the indicator will be drawn directly on the price chart.
  3. length = input.int(14, title="Length", minval=1): This line creates an input variable called length with a default value of 14 and a minimum value of 1. The input is displayed in the indicator settings as “Length”.
  4. src = close: This line sets the src (source) variable to the closing price of the bars.
  5. fastMA = ta.sma(src, length): This line calculates the Simple Moving Average (SMA) of the src variable with the given length and assigns the result to the fastMA variable.
  6. slowMA = ta.sma(src, length * 2): This line calculates the SMA of the src variable with double the given length and assigns the result to the slowMA variable.
  7. longCondition = ta.crossover(fastMA, slowMA): This line checks if a crossover occurs between the fast and slow moving averages and assigns the result (true or false) to the longCondition variable.
  8. if (longCondition): This line starts an if statement that will execute the following code block if longCondition is true.
  9. label.new(bar_index, low, "Long", color=color.green, style=label.style_label_up, size = size.huge): Inside the if statement, this line creates a new label on the chart with the text “Long”, colored green, and pointing upwards, at the current bar’s low price level. The size of the label is set to “huge”.
  10. shortCondition = ta.crossunder(fastMA, slowMA): This line checks if a crossunder occurs between the fast and slow moving averages and assigns the result (true or false) to the shortCondition variable.
  11. if (shortCondition): This line starts an if statement that will execute the following code block if shortCondition is true.
  12. label.new(bar_index, high, "Short", color=color.red, style=label.style_label_down, size = size.huge): Inside the if statement, this line creates a new label on the chart with the text “Short”, colored red, and pointing downwards, at the current bar’s high price level. The size of the label is set to “huge”.
  13. historicalFastMAs = array.new_float(0): This line creates a new empty array of floating-point numbers called historicalFastMAs.
  14. historicalSlowMAs = array.new_float(0): This line creates a new empty array of floating-point numbers called historicalSlowMAs.
  15. array.push(historicalFastMAs, fastMA): This line adds the current value of fastMA to the historicalFastMAs array.
  16. array.push(historicalSlowMAs, slowMA): This line adds the current value of slowMA to the historicalSlowMAs array.
  17. if (array.size(historicalFastMAs) > 100): This line starts an if statement that will execute the following code block if the size of the historicalFastMAs array

Key Takeaways

  • The array.shift function is used to remove the first element of an array and return its value.
  • It is useful in situations where you need to manipulate arrays by removing elements from the beginning of the array.
  • The function has a simple syntax: array.shift(id) where id is the array object.
  • You can use array.shift in various use cases, such as maintaining a fixed-size history of data points in a Moving Average Crossover Strategy.

Conclusion

In this tutorial, we have explored the array.shift function in Pine Script, its syntax, and its usage in a unique use case. By understanding and utilizing the array.shift function, you can effectively manipulate arrays in your Pine Script indicators and strategies. Now you are better equipped to create more advanced and efficient trading tools using Pine Script. Good luck and happy coding!

Leave a Comment