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)
Code Explanation
Here is an explanation of each line of code in the given script:
//@version=5
: This line specifies that the script uses version 5 of Pine Script.indicator("Moving Average Crossover Strategy", overlay=true)
: This line defines a new indicator called “Moving Average Crossover Strategy” and sets theoverlay
parameter totrue
, meaning that the indicator will be drawn directly on the price chart.length = input.int(14, title="Length", minval=1)
: This line creates an input variable calledlength
with a default value of 14 and a minimum value of 1. The input is displayed in the indicator settings as “Length”.src = close
: This line sets thesrc
(source) variable to the closing price of the bars.fastMA = ta.sma(src, length)
: This line calculates the Simple Moving Average (SMA) of thesrc
variable with the givenlength
and assigns the result to thefastMA
variable.slowMA = ta.sma(src, length * 2)
: This line calculates the SMA of thesrc
variable with double the givenlength
and assigns the result to theslowMA
variable.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 thelongCondition
variable.if (longCondition)
: This line starts an if statement that will execute the following code block iflongCondition
is true.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”.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 theshortCondition
variable.if (shortCondition)
: This line starts an if statement that will execute the following code block ifshortCondition
is true.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”.historicalFastMAs = array.new_float(0)
: This line creates a new empty array of floating-point numbers calledhistoricalFastMAs
.historicalSlowMAs = array.new_float(0)
: This line creates a new empty array of floating-point numbers calledhistoricalSlowMAs
.array.push(historicalFastMAs, fastMA)
: This line adds the current value offastMA
to thehistoricalFastMAs
array.array.push(historicalSlowMAs, slowMA)
: This line adds the current value ofslowMA
to thehistoricalSlowMAs
array.if (array.size(historicalFastMAs) > 100)
: This line starts an if statement that will execute the following code block if the size of thehistoricalFastMAs
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)
whereid
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!