Search code examples
averagepine-scriptstockpine-script-v5

Create an average or median value of only select set of numbers


enter image description hereI trade stocks with tradingview. I started a simple Pinecode to display % rate of change between price periods; but only those that are greater than zero. I got that far but I want now to create an AVERAGE or median value of them. How can I do this?

//@version=5
indicator(title="only+ROC", shorttitle="+ROC", format=format.price, precision=2, timeframe="", 
timeframe_gaps=true)
length = input.int(1, minval=1)
source = input(close, "Source")
roc = 100 * (source - source[length])/source[length]
hline(0, color=#787B86, title="Zero Line")
plot(series=roc > 0 ? roc : na)

Solution

  • avg_len = input.int(10, title = "averaging length")
    med_len = input.int(10, title = "median length")
    
    avg = ta.sma(roc, avg_len)
    med = ta.median(roc, med_len)
    

    Follow up question :

    //@version=5
    indicator("ROC vals", overlay = true)
    
    time_input_A = input.time(timestamp("20 Jul 2020 00:00 +0000"), title = "Start point", confirm = true)
    time_input_B = input.time(timestamp("21 Jul 2020 00:00 +0000"), title = "End point", confirm = true)
    
    length = input.int(1, title = "roc length")
    
    time_A = math.min(time_input_A, time_input_B)
    time_B = math.max(time_input_A, time_input_B)
    
    in_range = time >= time_A and time <= time_B
    post_bar = time > time_B and time[1] <= time_B
    
    roc = ta.roc(close, length)
    
    var float[] roc_vals = array.new_float()
    
    var int start_index = na
    var int end_index = na
    var float start_close = na
    var float end_close = na
    
    
    if in_range
        array.unshift(roc_vals, roc)
        if na(start_index)
            start_index := bar_index
        if na(start_close)
            start_close := close
            
    
    float[] pos_roc_vals = array.new_float()
    float[] neg_roc_vals = array.new_float()
    
    if array.size(roc_vals) > 0
        for i = 0 to array.size(roc_vals) - 1
            roc_val_i = array.get(roc_vals, i)
            if roc_val_i > 0
                array.push(pos_roc_vals, roc_val_i)
            if roc_val_i < 0
                array.push(neg_roc_vals, roc_val_i)
    
    if post_bar
        pos_roc_avg = array.avg(pos_roc_vals)
        pos_roc_med = array.median(pos_roc_vals)
    
        neg_roc_avg = array.avg(neg_roc_vals)
        neg_roc_med = array.median(neg_roc_vals)
    
        roc_text = "+ROC\nAverage : " + str.tostring(pos_roc_avg) + "\nMedian : " + str.tostring(pos_roc_med) + "\n\n-ROC\nAverage : " + str.tostring(neg_roc_avg) + "\nMedian : " + str.tostring(neg_roc_med)
    
        label.new(x = bar_index, y = close, style = label.style_label_left, color = color.yellow, textcolor = color.black, text = roc_text)
        
        end_index := bar_index[1]
        end_close := close[1]
        
        box.new(left = start_index, top = math.max(start_close, end_close), right = end_index, bottom = math.min(start_close, end_close), border_color = color.gray, border_width = 2, border_style = line.style_dashed, bgcolor = na)