Introduction to array.concat function
Pine Script is a domain-specific scripting language designed to create customized technical analysis tools, such as indicators and strategies, for the TradingView platform. One of the most versatile and powerful functions provided by Pine Script is the array.concat function. In this tutorial, we will dive deep into the array.concat function, exploring its syntax, use cases, and examples to help you understand how to effectively utilize it in your own Pine Script projects.
Syntax
The array.concat function combines two or more arrays, creating a new array that includes elements from all the original arrays. The syntax for the array.concat function is as follows:
newArray = array.concat(array1, array2, ..., arrayN)
Where newArray is the resulting array after combining array1, array2, …, arrayN. Note that the original arrays are not modified; instead, a new array is created.
When to use array.concat function
The array.concat function is particularly useful when you need to merge data from multiple sources, such as historical price data, indicator values, or other custom arrays. Combining arrays can help you perform calculations or comparisons across various data sets and create more powerful and insightful technical analysis tools.
Examples
Combining Two Simple Arrays
//@version=5 indicator('Array Concat Example 1', overlay=true) array1 = array.new_float(0) array2 = array.new_float(0) array.push(array1 , 1) array.push(array1 , 2) array.push(array1 , 3) array.push(array2 , 9) array.push(array2 , 8) array.push(array2 , 7) mergedArray = array.concat(array1, array2) label arrayLabel = na if barstate.isconfirmed arrayLabel := label.new( bar_index , high , str.tostring(mergedArray) , size = size.huge , textcolor = color.white) label.delete(arrayLabel[2])
Explanation:
- Initialize two empty float arrays:
array1
andarray2
. - Make two simple arrays and insert values.
- Use
array.concat
to mergearray1
andarray2
into a new array calledmergedArray
. - Plot the
mergedArray
values on the chart.
Most Common FAQs
array.concat
function modify the original arrays? No, the array.concat
the function does not modify the original arrays. Instead, it creates a new array containing elements from the input arrays.
No, you can only concatenate arrays of the same type. Attempting to concatenate arrays of different types will result in a runtime error.
array.concat
? Yes, the order of the input arrays determines the order of elements in the concatenated array. The elements from the first input array will appear first, followed by elements from the second input array, and so on.
Key Takeaways
- The
array.concat
function is a powerful tool for combining data from multiple sources in Pine Script. - It creates a new array without modifying the original arrays.
- The order of input arrays is important, as it determines the order of elements in the concatenated array.
- You can only concatenate arrays of the same type.
Conclusion
Understanding and effectively utilizing the array.concat
function is crucial for creating powerful and versatile technical analysis tools in Pine Script. This tutorial has provided an in-depth overview of the function, its syntax, when to use it, and examples of real-world applications. By applying the knowledge gained from this tutorial, you can create more advanced indicators and strategies that leverage the power of combined data sets.