Overview of Pine Script Array.abs Function

admin By admin
7 Min Read


Pine Script array.abs function is an invaluable resource for developers and traders who want to design efficient, data-driven trading methods. An extensive description of the array.abs function, including usage, syntax, and practical examples, will be given in this article. You should know exactly how to use the array.abs method in your own Pine Script trading strategy by the end of this article.

“Price is what you pay, value is what you get.”

Warren Buffet

Recognizing the Use Cases and Objectives of the Array.abs Function


To find the absolute values of the items in a given array, use the array.abs function. When working with oscillators, price discrepancies, or any other trading indicator that incorporates both positive and negative numbers, this is especially helpful.

The array.abs function lets you focus on the size of the changes rather than their direction by transforming negative values to positive ones. This can assist you in spotting possible trading opportunities, like noteworthy changes in price or reversals in a trend.

Syntax

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

array.abs(id)

Where id is the input array containing the numeric elements that you want to calculate the absolute values for.

Let’s examine the range with a few real-world instances.The way the abs work.

Example 1: Absolute Price Differences

In this example, we’ll compute the absolute differences between consecutive closing prices using the array.abs function.

priceDiffArray = array.new_float(0)
for i = 1 to 5 by 1
    diff = close[i] - close[i - 1]
    absDiff = math.abs(diff)
    array.push(priceDiffArray, absDiff)
abdPriceDiff = array.abs(priceDiffArray)
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="Absolute Price Difference =  " + str.tostring(array.get(abdPriceDiff, 0)))
label.delete(newLable[1])

Let’s break down the code:

//@version=5
indicator('Absolute Price Differences', overlay=true)

// Initialize an array to store absolute price differences
priceDiffArray = array.new_float(0)

// Loop through the last five bars
for i = 1 to 5 by 1
    // Calculate the price difference between consecutive closing prices
    diff = close[i] - close[i - 1]
    // Calculate the absolute difference
    absDiff = math.abs(diff)
    // Push the absolute difference to the array
    array.push(priceDiffArray, absDiff)

// Calculate the absolute price differences
abdPriceDiff = array.abs(priceDiffArray)

// Create a label to display the absolute price difference
label newLabel = na
if barstate.islast
    newLabel := label.new(x=bar_index, y=close, style=label.style_label_left, 
        color=#E6E6FA, textcolor=color.black, size=size.large, 
        text="Absolute Price Difference =  " + str.tostring(array.get(abdPriceDiff, 0)))

// Delete the previous label to avoid overlapping
label.delete(newLabel[1])

Explanation:

  • The script specifies the Pine Script version and sets the indicator’s title.
  • It initializes an array (priceDiffArray) to store absolute price differences.
  • It loops through the last five bars and calculates the difference between consecutive closing prices (diff). Then, it calculates the absolute difference (absDiff) and pushes it into the priceDiffArray.
  • It calculates the absolute price differences (abdPriceDiff) using the array.abs() function.
  • It creates a label (newLabel) to display the absolute price difference on the chart if it’s the last bar (barstate.islast).
  • Finally, it deletes the previous label to avoid overlapping.

This script allows traders to visualize the absolute price differences between consecutive closing prices over the last five bars directly on the chart.

Example 2: Absolute Moving Average Oscillator

In this example, we will build an oscillator based on the difference of two moving averages, and we will compute the oscillator’s absolute values using the array.abs method.

//@version=5
indicator('Absolute Moving Average Oscillator', overlay=false)

// Calculate short-term and long-term simple moving averages
shortTermMA = ta.sma(close, 9)
longTermMA = ta.sma(close, 21)

// Calculate the oscillator (difference between short-term and long-term moving averages)
oscillator = shortTermMA - longTermMA

// Initialize an array to store absolute oscillator values
absOscillatorArray = array.new_float(0)

// Loop to push oscillator values into the array
for i = 0 to 50 by 1
    array.push(absOscillatorArray, oscillator[i])

// Create a label for displaying absolute oscillator value
label newLabel = na
newAbsOscillatorArray = array.abs(absOscillatorArray)
if barstate.islast
    newLabel := label.new(x=bar_index, y=close, style=label.style_label_left, 
     color=#E6E6FA, textcolor=color.black, size=size.large, 
     text="Absolute Moving Average Oscillator = " + str.tostring(array.get(newAbsOscillatorArray, 0)))

// Delete the previous label to avoid overlapping
label.delete(newLabel[1])

Explanation:

  • The script specifies the Pine Script version and sets the indicator’s title.
  • It calculates the short-term (9-period) and long-term (21-period) simple moving averages (shortTermMA and longTermMA, respectively).
  • It computes the oscillator by subtracting the long-term moving average from the short-term moving average.
  • The script initializes an array (absOscillatorArray) to store absolute oscillator values and then loops to push oscillator values into the array.
  • A label (newLabel) is created to display the absolute oscillator value on the chart if it’s the last bar (barstate.islast).
  • Previous labels are deleted to prevent overlapping.

This indicator allows traders to observe the absolute difference between short-term and long-term moving averages, providing insights into potential trends or reversals in the market.

In summary

For traders who wish to develop strategies that account for the size of price fluctuations, independent of their direction, Pine Script’s array.abs function is a useful tool. The array.abs method was thoroughly explained in this article, along with its use cases, syntax, and useful examples.

You can use the array.abs function in your own Pine Script trading techniques to enhance your decision-making and spot market opportunities now that you have a thorough understanding of it and its uses. You can develop strategies that are more efficient, data-driven, and better matched to your trading requirements by using this function appropriately.

Share This Article
Leave a comment