JayK23
JayK23

Reputation: 263

How is a function applied to data in PineScript?

I'm trying to convert the following PineScript indicator to Python but i'm having troubles understanding how is the function applied to src. The function wpnr returns a single value, which i assume is the value of the indicator on every candle. But how can it returns a different value if the function is provided always the same data with src?

//@version=4
study('Weighted percentile nearest rank', 'WPNR', true, resolution='', resolution_gaps=true)
//by gorx1 & wallneradam

multisort(base_array, second_array, reverse) => //[
    n           = array.size(base_array)
    index_array = array.new_int(n)
    
    if n > 0
        for i = 0 to n - 1
            array.set(index_array, i, i)
        
        n -= 1
        
        for i = 0 to n > 0 ? n - 1 : na
            for j = 0 to n - i - 1
                t  = array.get(base_array, j) 
                t1 = array.get(base_array, j + 1) 
                    
                if (reverse ? t < t1 : t > t1)
                    array.set(base_array, j, t1)
                    array.set(base_array, j + 1, t)
                    
                    _t = array.get(second_array, j)
                    array.set(second_array, j, array.get(second_array, j + 1))
                    array.set(second_array, j + 1, _t)
                    
                    _ti = array.get(index_array, j)
                    array.set(index_array, j, array.get(index_array, j + 1))
                    array.set(index_array, j + 1, _ti)
    
    index_array //]

wpnr(src, len, n) => //[
    data    = array.new_float()
    weights = array.new_float()
    
    for i = 0 to len - 1
        array.push(data   , src[i] )
        array.push(weights, len - i) //the actual linear weights
        // array.push(weights, 1      ) //unit weights, if u wanna do a raincheck
    
    multisort(data, weights, false)
    
    sum         = 0.0
    weights_cum = array.new_float()
    
    for i = 0 to len - 1
        sum := sum + array.get(weights, i)
        array.push(weights_cum, sum)
    
    wpnr  = 0.0
    thres = array.sum(weights) / 100 * n

    for i = 0 to len - 1
        if array.get(weights_cum, i) >= thres
            wpnr := array.get(data, i)
            break
    
    wpnr //]

src = input(close, 'Source')
len = input(50   , 'Length')
n   = input(50   , '%'     )

out = wpnr(src, len, n)

plot(out, 'WPNR', color.rgb(255, 255, 255))

Upvotes: 0

Views: 329

Answers (1)

G.Lebret
G.Lebret

Reputation: 3108

src is the close value = the value of the close of each past bar AND the actual value of the real-time bar.
On past candle :

  • pinescript code is played only at the close of the candle.
    wpnr will be called one time only, with the close value of the bar.

On real time candle :

  • Each time the value or volume of the real-time bar change, the pinescript plays again.
  • So you function wpnr will return different values, as it will be called each time the value of the bar change.

Upvotes: 1

Related Questions