Home » Array Functions » Array.push Function in Pine Script

Array.push Function in Pine Script

Photo of author
Published on

In this tutorial, we will discuss the array.push function in Pine Script, a domain-specific scripting language for the TradingView platform. This function is useful for managing arrays in your trading algorithms. We will go through the syntax, a unique use case, and provide an explanation for each line of code in the example provided.

Syntax of array.push

array.push(array_id, value)

Parameters:

  • array_id (array): The identifier of the array to which the value will be added.
  • value (float|int|bool|color|string): The value to be added to the array.

Returns:

  • None

This function adds a value to the end of an array.

Unique Use Case Example: Dynamic Moving Average Crossover

In this example, we will implement a Dynamic Moving Average Crossover (DMAC) strategy using Pine Script. The primary goal of this strategy is to identify crossovers between two moving averages and utilize them for trading signals.

Here is the full code for our example:

//@version=5
indicator('Dynamic Moving Average Crossover', shorttitle='DMAC', overlay=true)

// User inputs
src = close
fast_len = input.int(9, title='Fast Length', minval=1)
slow_len = input.int(21, title='Slow Length', minval=1)

// Variables
var fast_ma_history = array.new_float()
var slow_ma_history = array.new_float()
var int last_crossover_bar = na
var int bars_since_crossover = na

// Moving average calculations
fast_ma = ta.sma(src, fast_len)
slow_ma = ta.sma(src, slow_len)

// Update moving average history
array.push(fast_ma_history, fast_ma)
array.push(slow_ma_history, slow_ma)

// Determine if a crossover
// occurred
crossover_occurred = ta.crossover(fast_ma, slow_ma) or ta.crossover(slow_ma, fast_ma)

if crossover_occurred
    last_crossover_bar := bar_index
    bars_since_crossover := 0
    bars_since_crossover
else
    bars_since_crossover := na(last_crossover_bar) ? na : bar_index - last_crossover_bar
    bars_since_crossover

// Limit the array size to the slow length
if array.size(fast_ma_history) > slow_len
    array.pop(fast_ma_history)
    array.pop(slow_ma_history)

// Plot moving averages
plot(fast_ma, color=color.new(color.blue, 0), title='Fast MA', linewidth=4)
plot(slow_ma, color=color.new(color.red, 0), title='Slow MA', linewidth=4)

// Plot background color on crossover
bgcolor(crossover_occurred ? color.green : na, transp=90)

// Plot bars since crossover
if barstate.islast
    label.new(x=bar_index, y=high, text=str.tostring(bars_since_crossover), style=label.style_label_down, color=color.black, textcolor=color.white, size=size.huge)

Array.push Function

Code Explanation

Initializing the Script

//@version=5
indicator('Dynamic Moving Average Crossover', shorttitle='DMAC', overlay=true)

The first line sets the Pine Script version to 5. The second line declares the script as an indicator, specifies the title, short title, and sets overlay=true to display the indicator on the price chart.

User Inputs

src = close
fast_len = input.int(9, title='Fast Length', minval=1)
slow_len = input.int(21, title='Slow Length', minval)

These lines define the user inputs for the script. The src variable represents the data source (closing prices), while fast_len and slow_len define the lengths of the fast and slow moving averages, respectively.

var fast_ma_history = array.new_float()
var slow_ma_history = array.new_float()
var int last_crossover_bar = na
var int bars_since_crossover = na

Here, we initialize the arrays to store the historical moving average values (fast_ma_history and slow_ma_history). We also create two integer variables, last_crossover_bar and bars_since_crossover, to track the most recent crossover event and the number of bars since the last crossover.

Moving Average Calculations

fast_ma = ta.sma(src, fast_len)
slow_ma = ta.sma(src, slow_len)

These lines calculate the Simple Moving Averages (SMA) for the fast and slow periods.

Update Moving Average History

array.push(fast_ma_history, fast_ma)
array.push(slow_ma_history, slow_ma)

We use the array.push function to add the calculated moving average values to their respective arrays.

Determine if a Crossover Occurred

crossover_occurred = ta.crossover(fast_ma, slow_ma) or ta.crossover(slow_ma, fast_ma)

This line checks if a crossover between the fast and slow moving averages has occurred.

Update Crossover Bar and Bars Since Crossover

if crossover_occurred
    last_crossover_bar := bar_index
    bars_since_crossover := 0
    bars_since_crossover
else
    bars_since_crossover := na(last_crossover_bar) ? na : bar_index - last_crossover_bar
    bars_since_crossover

This block updates the last_crossover_bar and bars_since_crossover variables based on whether a crossover has occurred.

Limit the Array Size

if array.size(fast_ma_history) > slow_len
    array.pop(fast_ma_history)
    array.pop(slow_ma_history)

Here, we limit the size of the fast_ma_history and slow_ma_history arrays to the slow_len.

Plot Moving Averages

plot(fast_ma, color=color.new(color.blue, 0), title='Fast MA', linewidth=4)
plot(slow_ma, color=color.new(color.red, 0), title='Slow MA', linewidth=4)

These lines plot the fast and slow moving averages on the chart with different colors and widths.

Plot Background Color on Crossover

bgcolor(crossover_occurred ? color.green : na, transp=90)

This line sets the background color to green (with 90% transparency) when a crossover occurs.

Plot Bars Since Crossover

if barstate.islast
    label.new(x=bar_index, y=high, text=str.tostring(bars_since_crossover), style=label.style_label_down, color=color.black, textcolor=color.white, size=size.huge)

On the last bar of the chart, this line plots the number of bars since the last crossover.

Key Takeaways

  • The array.push function is used to add elements to the end of an array in Pine Script.
  • The example demonstrates how to use array.push to store and manage historical moving average values for a Dynamic Moving Average Crossover strategy.
  • The example also showcases how to calculate and display the number of bars since the last crossover event.

Conclusion

In this tutorial, we have explored the array.push function in Pine Script and how it can be used in a trading strategy, specifically a Dynamic Moving Average Crossover strategy. We have provided a complete example with an explanation for each line of code. By utilizing the array.push function, you can effectively manage arrays in your trading algorithms and create more sophisticated strategies based on historical data.

With this knowledge, you can now confidently apply the array.push function in your own Pine Script strategies, expanding your ability to analyze and trade financial markets using the TradingView platform. Remember to experiment with different strategies and techniques to find what works best for your trading style and goals.

Leave a Comment