Hadi Mazloumzadeh
Hadi Mazloumzadeh

Reputation: 9

Plot exhibition problem in pinescript v.5

I have to section on plot for exhibition one upper ad lower bands and other center line which doesn't exhibit on chart properly when have center line bands are vanished so try in some modes and had no result on chart the calculation are work properly and I have triangles on right position but can not have center line

//@version=5
indicator("Nadaraya-Watson Full combine(Repaint Only)", " Nadaraya-Watson Full", overlay = true, max_lines_count = 500, max_labels_count = 500, max_bars_back=500)

// setting
h = input.float(8., 'Bandwidth', minval = 0)
mult = input.float(3., 'Envelope Multiplier', minval = 0)
src = input(close, 'Source')

// style
upCss = input.color(color.teal, 'Up Color', inline = 'style', group = 'Style')
dnCss = input.color(color.red, 'Down Color', inline = 'style', group = 'Style')
midCss = input.color(color.blue, 'Middle Band', inline = 'style', group = 'Style')

// gauss function
gauss(x, h) => math.exp(-(math.pow(x, 2) / (h * h * 2)))

// new variables
n = bar_index
var ln = array.new_line(0)
var mid_ln = array.new_line(0)  
if barstate.isfirst
    for i = 0 to 499
        array.push(ln, line.new(na, na, na, na))
        array.push(mid_ln, line.new(na, na, na, na)) 

// plot and compute (NWE + Envelope)
float y2 = na
float y1 = na
float y1_d = na
line l = na

nwe = array.new<float>(0)
sae = 0.

if barstate.islast
    for i = 0 to math.min(499, n - 1)
        sum = 0.
        sumw = 0.
        
        for j = 0 to math.min(499, n - 1)
            w = gauss(i - j, h)
            sum += src[j] * w
            sumw += w

        y2 := sum / sumw
        sae += math.abs(src[i] - y2)
        nwe.push(y2)

        // computing mean line(NWE)
        d = y2 - y1
        l := array.get(ln, i)
        line.set_xy1(l, n - i + 1, y1)
        line.set_xy2(l, n - i, y2)
        line.set_color(l, y2 > y1 ? upCss : dnCss)
        line.set_width(l, 2)

        // mean line draw
        mid_l = array.get(mid_ln, i)
        line.set_xy1(mid_l, n - i + 1, y1)
        line.set_xy2(mid_l, n - i, y2)
        line.set_color(mid_l, midCss)
        line.set_width(mid_l, 2)

        if d * y1_d < 0
            label.new(n - i + 1, src[i], y1_d < 0 ? '▲' : '▼'
              , color = color(na)
              , style = y1_d < 0 ? label.style_label_up : label.style_label_down
              , textcolor = y1_d < 0 ? upCss : dnCss
              , textalign = text.align_center)

        y1 := y2
        y1_d := d

    sae := sae / math.min(499, n - 1) * mult

    // upper and lower bands
    for i = 0 to math.min(499, n - 1)
        if  -i - 2 * int(-i / 2)
            line.new(n - i + 1, y1 + sae, n - i, nwe.get(i) + sae, color = upCss)
            line.new(n - i + 1, y1 - sae, n - i, nwe.get(i) - sae, color = dnCss)
        
        // signals
        if src[i] > nwe.get(i) + sae and src[i + 1] < nwe.get(i) + sae
            label.new(n - i, src[i], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center)
        if src[i] < nwe.get(i) - sae and src[i + 1] > nwe.get(i) - sae
            label.new(n - i, src[i], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center)

        y1 := nwe.get(i)

// Dashboard
var tb = table.new(position.top_right, 1, 1
  , bgcolor = #1e222d
  , border_color = #373a46
  , border_width = 1
  , frame_color = #373a46
  , frame_width = 1)

tb.cell(0, 0, 'Repainting Mode Enabled', text_color = color.white, text_size = size.small)

Upvotes: 0

Views: 21

Answers (0)

Related Questions