Indicator · Pine Script v4 · On the chart

Test 014 Array Functions

by ricardosantos79· code credited to RicardoSantos

This script is a technical demonstration of custom array manipulation functions in Pine Script v4.

View on GitHub

GitHub stars261on the repository
Pine versionv4older, still runs
Lines of code568
Settings3inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

568 lines of Pine Script v4 from beta/arrays/test-014-array_functions.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

test-014-array_functions.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=4
study("test-014-array_functions", overlay=true)

int length = input(10)
var float[] prices = array.new_float(size=length, initial_value=open)
var int[] indices = array.new_int(size=length, initial_value=0)
if pivothigh(2, 2)
    e = array.pop(id=prices)
    i = array.pop(id=indices)
    array.insert(id=prices, index=0, value=high[2])
    array.insert(id=indices, index=0, value=bar_index[2])
if pivotlow(2, 2)
    e = array.pop(id=prices)
    i = array.pop(id=indices)
    array.insert(id=prices, index=0, value=low[2])
    array.insert(id=indices, index=0, value=bar_index[2])

f_array_ops(_X, _Y, _op, _value)=>
//| mathematical and logical operations of array elements (X[i] <op> Y[i]) wise.
//| inputs:
//|     _X:     array of x elements
//|     _Y:     array of y elements
//|     _op:    operator string
//|     _value: optional value to do operation instead of _Y array elements, na() to ignore.
    int _sizeY = array.size(id=_Y)
    int _sizeX = array.size(id=_X)
    float[] _Z = array.new_float(size=0, initial_value=0.0)
    if _sizeY == _sizeX
        if na(_value)
            if _op == '+'
            //| addition
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) + array.get(id=_Y, index=_i))
            else if _op == '-'
            //| subtraction
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) - array.get(id=_Y, index=_i))
            else if _op == '*'
            //| multiplication
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) * array.get(id=_Y, index=_i))
            else if _op == '/'
            //| division
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) / array.get(id=_Y, index=_i))
            else if _op == '%'
            //| modulus remainder
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) % array.get(id=_Y, index=_i))
            else if _op == '/%'
            //| floored modulus remainder
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=(array.get(id=_Y, index=_i) + array.get(id=_X, index=_i)) % array.get(id=_Y, index=_i))
            else if _op == '//'
            //| floor division
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=round(array.get(id=_X, index=_i) / array.get(id=_Y, index=_i)))
            else if _op == '**'
            //| exponention
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=pow(array.get(id=_X, index=_i), array.get(id=_Y, index=_i)))
            else if _op == '=='
            //| is equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) == array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else if _op == '!='
            //| is not equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) != array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else if _op == '>'
            //| is higher
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) > array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else if _op == '<'
            //| is lower
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) < array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else if _op == '>='
            //| is higher or equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) >= array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else if _op == '<='
            //| is lower or equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) <= array.get(id=_Y, index=_i) ? 1.0 : 0.0)
            else
                array.clear(id=_Z)
        else
            if _op == '+'
            //| addition
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) + _value)
            else if _op == '-'
            //| subtraction
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) - _value)
            else if _op == '*'
            //| multiplication
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) * _value)
            else if _op == '/'
            //| division
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) / _value)
            else if _op == '%'
            //| modulus remainder
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) % _value)
            else if _op == '/%'
            //| floored modulus remainder
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=(_value + array.get(id=_X, index=_i)) % _value)
            else if _op == '//'
            //| floor division
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=round(array.get(id=_X, index=_i) / _value))
            else if _op == '**'
            //| exponention
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=pow(array.get(id=_X, index=_i), _value))
            else if _op == '=='
            //| is equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) == _value ? 1.0 : 0.0)
            else if _op == '!='
            //| is not equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) != _value ? 1.0 : 0.0)
            else if _op == '>'
            //| is higher
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) > _value ? 1.0 : 0.0)
            else if _op == '<'
            //| is lower
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) < _value ? 1.0 : 0.0)
            else if _op == '>='
            //| is higher or equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) >= _value ? 1.0 : 0.0)
            else if _op == '<='
            //| is lower or equal
                for _i = 0 to _sizeX-1
                    array.push(id=_Z, value=array.get(id=_X, index=_i) <= _value ? 1.0 : 0.0)
            else
                array.clear(id=_Z)
    _Z

// float byvalue = input(0)
// byvalue := byvalue == 0? na : byvalue
// float[] array_ops = f_array_ops(indices, prices, input(''), byvalue)
// bool show_debug = input(false)
// var label debug = label.new(bar_index, open, '')
// if show_debug
//     label.set_xy(id=debug, x=bar_index, y=close)
//     label.set_text(id=debug, text=tostring(indices, '#') + '\n' + tostring(prices) + '\n' + tostring(array_ops))
// else
//     label.delete(id=debug)


f_array_mean(_id)=>
    int _size = array.size(id=_id)
    float _mean = array.sum(id=_id) / _size

// float mean = f_array_mean(prices)
// plot(mean, color=color.yellow, transp=50)

f_array_standardize(_X)=>
//| returns standardized array values against the maximum range in the data set.
//| inputs:  
//|     _X: Array with data points.
    int _size = array.size(id=_X)
    float[] _Z = array.copy(id=_X)
    array.sort(id=_Z, order=order.ascending)
    float _mean = array.sum(id=_Z) / _size
    float _max_range = array.get(id=_Z, index=_size-1) - array.get(id=_Z, index=0)
    for _i = 0 to _size - 1
        array.set(id=_Z, index=_i, value=(array.get(id=_X, index=_i) - _mean) / _max_range)
    _Z

// float[] array_std = f_array_standardize(prices)
// bool show_debug = input(false)
// var label debug = label.new(bar_index, open, '')
// if show_debug
//     label.set_xy(id=debug, x=bar_index, y=close)
//     label.set_text(id=debug, text=tostring(prices) + '\n' + tostring(array_std))
// else
//     label.delete(id=debug)


f_array_reverse(_X)=>
//| returns a new array with the elements of the origin array in reverse order
//| inputs:
//|     _X: Array with origin data points.
    int _size = array.size(id=_X)
    float[] _Z = array.new_float(size=0, initial_value=0.0)
    for _i =_size - 1 to 0
        array.push(id=_Z, value=array.get(id=_X, index=_i))
    _Z

// float[] array_reverse = f_array_reverse(prices)
// bool show_debug = input(false)
// var label debug = label.new(bar_index, open, '')
// if show_debug
//     label.set_xy(id=debug, x=bar_index, y=close)
//     label.set_text(id=debug, text=tostring(prices) + '\n' + tostring(array_reverse))
// else
//     label.delete(id=debug)

f_array_stdev(_id)=>
    int _size = array.size(id=_id)
    float _mean = array.sum(id=_id) / _size
    float _dev = 0.0
    for _i = 0 to _size - 1
        _dev := _dev + abs(array.get(id=_id, index=_i) - _mean)
    _dev := _dev / _size

// float stdev = f_array_stdev(prices)
// plot(stdev, color=color.yellow, transp=50)

f_array_variance(_id)=>
    int _size = array.size(id=_id)
    float _mean = array.sum(id=_id) / _size
    float _var = 0.0
    for _i = 0 to _size - 1
        _var := _var + pow(array.get(id=_id, index=_i) - _mean, 2)
    _var / _size
    
// float variance = f_array_variance(prices)
// plot(variance, color=color.yellow, transp=50, display=display.none)

f_array_covariance(_X, _Y)=>
    int _sizeY = array.size(id=_Y)
    int _sizeX = array.size(id=_X)
    float _meanY = array.sum(id=_Y) / _sizeY
    float _meanX = array.sum(id=_X) / _sizeX
    float _meanXY = 0.0
    if _sizeY == _sizeX
        for _i = 0 to _sizeX - 1
            _meanXY := _meanXY + array.get(id=_X, index=_i) * array.get(id=_Y, index=_i)
        _meanXY := _meanXY / _sizeX
    _meanXY - (_meanX * _meanY)

// float covariance = f_array_covariance(indices, prices)
// plot(covariance, color=color.yellow, transp=50, display=display.none)


f_array_nth(_X, _order, _n)=>
//| returns the highest/lowest nth element in a array:
//| inputs:
//|     _X:     array with origin elements.
//|     _order: order to sort array(lowest: order.ascending, highest: order.descending).
//|     _n:     nth element in the ordered array.
    int _sizeX = array.size(id=_X)
    float[] _T = array.copy(id=_X)
    if _order
        array.sort(id=_T, order=order.ascending)
    else
        array.sort(id=_T, order=order.descending)
    array.get(id=_T, index=_n)
    
// int nth = input(0)
// bool order = input(true)
// float nth_element = f_array_nth(prices, order, nth)
// plot(nth_element, color=color.yellow, transp=50)

f_array_contains(_X, _value)=>
//| returns true if atleast 1 element in the array is equal:
//| inputs:
//|     _X:     array with origin elements.
//|     _value: value to compare..
    int _sizeX = array.size(id=_X)
    bool _return  = false
    if _sizeX > 0
        for _i = 0 to _sizeX - 1
            if array.get(id=_X, index=_i) == _value
                _return := true
                break
    _return

// var float[] values = array.new_float(size=2, initial_value=1)
// array.push(id=values, value=2)
// int test_value = f_array_contains(values, input(0)) ? 1 : 0
// plot(test_value, color=color.yellow, transp=50)

f_array_index_of(_X, _value)=>
//| returns the index of the first element in the array that is equal to value:
//| inputs:
//|     _X:     array with origin elements.
//|     _value: value to compare..
    int _sizeX = array.size(id=_X)
    int _return  = -1
    if _sizeX > 0
        for _i = 0 to _sizeX - 1
            if array.get(id=_X, index=_i) == _value
                _return := _i
                break
    _return

// var float[] values = array.new_float(size=2, initial_value=1)
// if barstate.isfirst
//     array.push(id=values, value=2)
//     array.push(id=values, value=5)
//     array.push(id=values, value=7)
// int test_value = f_array_index_of(values, input(0))
// plot(test_value, color=color.yellow, transp=50)


f_array_last_index_of(_X, _value)=>
//| returns the index of the first element in the array that is equal to value:
//| inputs:
//|     _X:     array with origin elements.
//|     _value: value to compare..
    int _sizeX = array.size(id=_X)
    int _return  = -1
    if _sizeX > 0
        for _i = _sizeX - 1 to 0
            if array.get(id=_X, index=_i) == _value
                _return := _i
                break
    _return

// var float[] values = array.new_float(size=2, initial_value=1)
// if barstate.isfirst
//     array.push(id=values, value=2)
//     array.push(id=values, value=5)
//     array.push(id=values, value=7)
// int test_value = f_array_last_index_of(values, input(0))
// plot(test_value, color=color.yellow, transp=50)


f_array_remove_range(_X, _index_from, _index_to)=>
//| returns the index of the first element in the array that is equal to value:
//| inputs:
//|     _X:     array with origin elements.
//|     _value: value to compare..
    _R = array.copy(id=_X)
    int _size = array.size(id=_R)
    if _index_to - _index_from >= 0 and _index_from >= 0 and _index_to < _size
        for _i = _index_to to _index_from
            array.remove(id=_R, index=_i)
    _R

// var float[] values = array.new_float(size=5, initial_value=1)
// if barstate.isfirst
//     array.push(id=values, value=2)
//     array.push(id=values, value=5)
//     array.push(id=values, value=7)

// int index_from = input(1)
// int index_to = input(1)
// float[] array_remove_range = f_array_remove_range(values, index_from, index_to)
// bool show_debug = input(false)
// var label debug = label.new(bar_index, open, '')
// if show_debug
//     label.set_xy(id=debug, x=bar_index, y=close)
//     label.set_text(id=debug, text=tostring(values) + '\n' + tostring(array_remove_range))
// else
//     label.delete(id=debug)


f_array_linreg(_X, _Y)=>
//  https://www.statisticshowto.com/probability-and-statistics/regression-analysis/find-a-linear-regression-equation/#FindaLinear
    int _sizeY = array.size(id=_Y)
    int _sizeX = array.size(id=_X)
    float _sumX = array.sum(id=_X)
    float _sumY = array.sum(id=_Y)
    float _sumXY = 0.0
    float _sumX2 = 0.0
    float _sumY2 = 0.0
    if _sizeY == _sizeX
        for _i = 0 to _sizeY - 1
            float _Xi = array.get(id=_X, index=_i)
            float _Yi = array.get(id=_Y, index=_i)
            _sumXY := _sumXY + (_Xi * _Yi)
            _sumX2 := _sumX2 + pow(_Xi, 2)
            _sumY2 := _sumY2 + pow(_Yi, 2)
    float _a = ((_sumY * _sumX2) - (_sumX * _sumXY)) / ((_sizeX * _sumX2) - pow(_sumX, 2))
    float _b = ((_sizeX * _sumXY) - (_sumX * _sumY)) / ((_sizeX * _sumX2) - pow(_sumX, 2))
    float[] _predictions = array.new_float(size=0, initial_value=0.0)
    float _max_dev = 0.0
    float _min_dev = 0.0
    float _stdev = 0.0
    for _i = 0 to _sizeX - 1
        float _vector = _a + _b * array.get(id=_X, index=_i)
        array.push(id=_predictions, value=_vector)
        //
        float _Yi = array.get(id=_Y, index=_i)
        float _diff = _Yi - _vector
        if _diff > _max_dev
            _max_dev := _diff
        if _diff < _min_dev
            _min_dev := _diff
        _stdev := _stdev + abs(_diff)
    [_predictions, _max_dev, _min_dev, _stdev/_sizeX]

[D, Dmax, Dmin, Dstdev] = f_array_linreg(indices, prices)

// bool show_debug = input(false)
// var label debug = label.new(bar_index, open, '')
// if show_debug
//     label.set_xy(id=debug, x=bar_index, y=close)
//     label.set_text(id=debug, text=tostring(indices) + '\n' + tostring(D))
// else
//     label.delete(id=debug)

color _ln_col = color.aqua
var line ln_mid = line.new(x1=bar_index, y1=open, x2=bar_index, y2=open, color=_ln_col, style=line.style_solid, width=2)
var line ln_max = line.new(x1=bar_index, y1=open, x2=bar_index, y2=open, color=_ln_col, style=line.style_dotted, width=1)
var line ln_min = line.new(x1=bar_index, y1=open, x2=bar_index, y2=open, color=_ln_col, style=line.style_dotted, width=1)
var line ln_upper = line.new(x1=bar_index, y1=open, x2=bar_index, y2=open, color=_ln_col, style=line.style_dashed, width=1)
var line ln_lower = line.new(x1=bar_index, y1=open, x2=bar_index, y2=open, color=_ln_col, style=line.style_dashed, width=1)

int first = input(0)
int last = input(9)
ln_y1 = array.get(id=D, index=first)
ln_y2 = array.get(id=D, index=last)
line.set_xy1(id=ln_mid, x=array.get(id=indices, index=first), y=ln_y1)
line.set_xy2(id=ln_mid, x=array.get(id=indices, index=last), y=ln_y2)
line.set_xy1(id=ln_max, x=array.get(id=indices, index=first), y=ln_y1 + Dmax)
line.set_xy2(id=ln_max, x=array.get(id=indices, index=last), y=ln_y2 + Dmax)
line.set_xy1(id=ln_min, x=array.get(id=indices, index=first), y=ln_y1 + Dmin)
line.set_xy2(id=ln_min, x=array.get(id=indices, index=last), y=ln_y2 + Dmin)
line.set_xy1(id=ln_upper, x=array.get(id=indices, index=first), y=ln_y1 + Dstdev)
line.set_xy2(id=ln_upper, x=array.get(id=indices, index=last), y=ln_y2 + Dstdev)
line.set_xy1(id=ln_lower, x=array.get(id=indices, index=first), y=ln_y1 - Dstdev)
line.set_xy2(id=ln_lower, x=array.get(id=indices, index=last), y=ln_y2 - Dstdev)



f_array_polyreg(_X, _Y)=>
//  language D: https://rosettacode.org/wiki/Polynomial_regression
    int _sizeY = array.size(id=_Y)
    int _sizeX = array.size(id=_X)
    //
    float _meanX = array.sum(id=_X) / _sizeX
    float _meanY = array.sum(id=_Y) / _sizeX
    float _meanXY = 0.0
    float _meanY2 = 0.0
    float _meanX2 = 0.0
    float _meanX3 = 0.0
    float _meanX4 = 0.0
    float _meanX2Y = 0.0
    if _sizeY == _sizeX
        for _i = 0 to _sizeY - 1
            float _Xi = array.get(id=_X, index=_i)
            float _Yi = array.get(id=_Y, index=_i)
            _meanXY := _meanXY + (_Xi * _Yi)
            _meanY2 := _meanY2 + pow(_Yi, 2)
            _meanX2 := _meanX2 + pow(_Xi, 2)
            _meanX3 := _meanX3 + pow(_Xi, 3)
            _meanX4 := _meanX4 + pow(_Xi, 4)
            _meanX2Y := _meanX2Y + pow(_Xi, 2) * _Yi
        _meanXY := _meanXY / _sizeX
        _meanY2 := _meanY2 / _sizeX
        _meanX2 := _meanX2 / _sizeX
        _meanX3 := _meanX3 / _sizeX
        _meanX4 := _meanX4 / _sizeX
        _meanX2Y := _meanX2Y / _sizeX
    //-----------|covs
    float _sXX = _meanX2 - _meanX * _meanX
    float _sXY = _meanXY - _meanX * _meanY
    float _sXX2 = _meanX3 - _meanX * _meanX2
    float _sX2X2 = _meanX4 - _meanX2 * _meanX2
    float _sX2Y = _meanX2Y - _meanX2 * _meanY
    //-----------|
    float _b = (_sXY * _sX2X2 - _sX2Y * _sXX2) / (_sXX * _sX2X2 - _sXX2 * _sXX2)
    float _c = (_sX2Y * _sXX - _sXY * _sXX2) / (_sXX * _sX2X2 - _sXX2 * _sXX2)
    float _a = _meanY - _b * _meanX - _c * _meanX2
    //-----------|
    float[] _predictions = array.new_float(size=0, initial_value=0.0)
    float _max_dev = 0.0
    float _min_dev = 0.0
    float _stdev = 0.0
    for _i = 0 to _sizeX - 1
        float _Xi = array.get(id=_X, index=_i)
        float _vector = _a + _b * _Xi + _c * _Xi * _Xi
        array.push(id=_predictions, value=_vector)
        //
        float _Yi = array.get(id=_Y, index=_i)
        float _diff = _Yi - _vector
        if _diff > _max_dev
            _max_dev := _diff
        if _diff < _min_dev
            _min_dev := _diff
        _stdev := _stdev + abs(_diff)
    [_predictions, _max_dev, _min_dev, _stdev/_sizeX]


[P, Pmax, Pmin, Pstdev] = f_array_polyreg(indices, prices)

color _pr_col = color.red
var line pr_mid00 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid01 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid02 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid03 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid04 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid05 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid06 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid07 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid08 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_mid09 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_solid, width=2)
var line pr_max00 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max01 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max02 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max03 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max04 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max05 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max06 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max07 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max08 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_max09 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min00 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min01 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min02 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min03 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min04 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min05 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min06 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min07 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min08 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_min09 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dotted, width=1)
var line pr_upper00 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper01 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper02 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper03 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper04 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper05 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper06 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper07 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper08 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_upper09 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower00 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower01 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower02 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower03 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower04 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower05 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower06 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower07 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower08 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
var line pr_lower09 = line.new(x1=bar_index, y1=0.0, x2=bar_index, y2=0.0, color=_pr_col, style=line.style_dashed, width=1)
f_pr_mid_line_selector(_i)=>(_i==0?pr_mid00:(_i==1?pr_mid01:(_i==2?pr_mid02:(_i==3?pr_mid03:(_i==4?pr_mid04:(_i==5?pr_mid05:(_i==6?pr_mid06:(_i==7?pr_mid07:(_i==8?pr_mid08:(_i==9?pr_mid09:pr_mid00))))))))))
f_pr_max_line_selector(_i)=>(_i==0?pr_max00:(_i==1?pr_max01:(_i==2?pr_max02:(_i==3?pr_max03:(_i==4?pr_max04:(_i==5?pr_max05:(_i==6?pr_max06:(_i==7?pr_max07:(_i==8?pr_max08:(_i==9?pr_max09:pr_max00))))))))))
f_pr_min_line_selector(_i)=>(_i==0?pr_min00:(_i==1?pr_min01:(_i==2?pr_min02:(_i==3?pr_min03:(_i==4?pr_min04:(_i==5?pr_min05:(_i==6?pr_min06:(_i==7?pr_min07:(_i==8?pr_min08:(_i==9?pr_min09:pr_min00))))))))))
f_pr_upper_line_selector(_i)=>(_i==0?pr_upper00:(_i==1?pr_upper01:(_i==2?pr_upper02:(_i==3?pr_upper03:(_i==4?pr_upper04:(_i==5?pr_upper05:(_i==6?pr_upper06:(_i==7?pr_upper07:(_i==8?pr_upper08:(_i==9?pr_upper09:pr_upper00))))))))))
f_pr_lower_line_selector(_i)=>(_i==0?pr_lower00:(_i==1?pr_lower01:(_i==2?pr_lower02:(_i==3?pr_lower03:(_i==4?pr_lower04:(_i==5?pr_lower05:(_i==6?pr_lower06:(_i==7?pr_lower07:(_i==8?pr_lower08:(_i==9?pr_lower09:pr_lower00))))))))))

int pr_fractions = 10
int pr_size = array.size(id=P)
int pr_step = max(pr_size / pr_fractions, 1)
for _i = 0 to pr_size - pr_step - 1 by pr_step
    int _next_step_index = _i + pr_step
    int _line = _i / pr_step

    line.set_xy1(id=f_pr_mid_line_selector(_line), x=array.get(id=indices, index=_i), y=array.get(id=P, index=_i))
    line.set_xy2(id=f_pr_mid_line_selector(_line), x=array.get(id=indices, index=_i + pr_step), y=array.get(id=P, index=_i + pr_step))
    line.set_xy1(id=f_pr_max_line_selector(_line), x=array.get(id=indices, index=_i), y=array.get(id=P, index=_i) + Pmax)
    line.set_xy2(id=f_pr_max_line_selector(_line), x=array.get(id=indices, index=_i + pr_step), y=array.get(id=P, index=_i + pr_step) + Pmax)
    line.set_xy1(id=f_pr_min_line_selector(_line), x=array.get(id=indices, index=_i), y=array.get(id=P, index=_i) + Pmin)
    line.set_xy2(id=f_pr_min_line_selector(_line), x=array.get(id=indices, index=_i + pr_step), y=array.get(id=P, index=_i + pr_step) + Pmin)
    line.set_xy1(id=f_pr_upper_line_selector(_line), x=array.get(id=indices, index=_i), y=array.get(id=P, index=_i) + Pstdev)
    line.set_xy2(id=f_pr_upper_line_selector(_line), x=array.get(id=indices, index=_i + pr_step), y=array.get(id=P, index=_i + pr_step) + Pstdev)
    line.set_xy1(id=f_pr_lower_line_selector(_line), x=array.get(id=indices, index=_i), y=array.get(id=P, index=_i) - Pstdev)
    line.set_xy2(id=f_pr_lower_line_selector(_line), x=array.get(id=indices, index=_i + pr_step), y=array.get(id=P, index=_i + pr_step) - Pstdev)

Overview

Test 014 Array Functions identifies pivot highs and pivot lows and dynamically manages their values and corresponding bar indices inside fixed-size float and integer arrays. It also defines helper functions to perform element-wise mathematical and logical operations between arrays. Rendered on the price chart, configurable settings allow adjusting the storage array length and specified index ranges.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

Support & resistance

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
lengthint10length
firstint0first
lastint9last
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

948 stars

Moving Average Channel plots separate simple moving averages for upper and lower bands on the price chart. It fills the space between bands with semi-transparent color and colors bars green above the upper band, red below the lower band, or gray in between. Us

Indicator v4 Moving averagesSupport & resistance
Updated 10 days ago · GPL-3.0 GitHub repository of Moving Average Channel

ICT Equal Highs and Lows

by ArunKBhaskar
575 stars

This overlay indicator finds pivot highs and lows on the price chart and compares the last two highs and the last two lows. When the percentage difference between a pair is below the Equal HL threshold, it treats them as equal levels and draws a box. Left and

Indicator v5 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Equal Highs and Lows
330 stars

This indicator monitors multiple cryptocurrency symbols for trading signals. It calculates and plots entry prices, take profit, and stop loss levels for each symbol including BTCUSDT, ETHUSDT, BNBUSDT and others. Price crossovers trigger buy, sell or close ale

Indicator v5 Support & resistanceScreeners & dashboards
Updated 2 months ago · None stated GitHub repository of Multi Alert Long/Short [Alorse]

This overlay indicator draws a fixed-range volume profile on the price chart, splitting traded activity into up and down volume at each price level. It also plots a sentiment profile, a developing point of control, and value-area high and low around a configur

Indicator v5 VolumeSupport & resistance
Updated 2 months ago · MIT GitHub repository of Volume Profile and Indicator by DGT

BZ-tracker

by Allen1212
303 stars

The BZ-tracker indicator marks ARK Invest buy and sell transactions on stock charts with labels. It plots green labels for purchases and red labels for sales, each annotated with a multiplier such as X1 or X2 to show the number of ARK ETFs trading the stock on

Indicator v4 Support & resistanceScreeners & dashboards
Updated 2 years ago · None stated GitHub repository of BZ-tracker

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers