Misteruniverse
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: January 13th, 2024

Please help to find the output issue in tradingview

I have combined two script in one,
Auto pivot and Envelop , but when I try to plot it in the trading view - and turn auto pivot off in option the
pivot- s1-r1-s2-r2-s3-r3 lines goes missing,
If I select Auto pivot then after few refresh Pivot line goes missing

I am unable to find what is the cause, Kindly help to resolve the issue

Image

Code: Select all

//@version=5
indicator(title="Kat", shorttitle="Kat", overlay=true, max_lines_count = 500, max_labels_count = 500, max_bars_back=500)


///////////////////////////////////////////////////////////////////////////////// INPUTS


AUTO = "Auto"
DAILY = "Daily"
WEEKLY = "Weekly"
MONTHLY = "Monthly"
QUARTERLY = "Quarterly"
YEARLY = "Yearly"
DashSenti = timeframe.period

var string GP1 = "Pivots"
i_auto_pivots = input.bool(defval=true, title="Auto Pivots", group=GP1)
i_pivot_time_frame = input.string(title="‏  ‏Timeframe", defval=AUTO, options=[AUTO, DAILY, WEEKLY, MONTHLY, QUARTERLY, YEARLY], group=GP1)
i_pivots_back = input.int(defval=1, minval=1, maxval=2, title="‏  ‏Pivots Back", group=GP1)

var string GP2 = "Pivot Style"
i_pivot_color = input.color(defval=color.new(#fb8c00, 60), title="Line Color", group=GP2, inline = "pivot_line")
i_line_width = input.int(defval=2, minval=1, maxval=2, title="Line Width", group=GP2, inline = "pivot_line")
i_pivot_labels = input.bool(defval=true, title="Pivot Labels", group=GP2, inline = "pivot_label")
i_pivot_label_pos = input.string(defval="Left", options=["Left", "Right"], title="", group=GP2, inline = "pivot_label")


///////////////////////////////////////////////////////////////////////////////// HELPER FUNCTIONS


timeOpenPeriod(tf, period) => request.security(syminfo.tickerid, tf, time[period])
timeClosePeriod(tf, period) => request.security(syminfo.tickerid, tf, time_close[period])
highPeriod(tf, period) => request.security(syminfo.tickerid, tf, high[period])
lowPeriod(tf, period) => request.security(syminfo.tickerid, tf, low[period])
closePeriod(tf, period) => request.security(syminfo.tickerid, tf, close[period])


// Traditional Pivots Formulas
PP(h, l, c) => (h + l + c) / 3    
R1(h, l, c) => PP(h, l, c) * 2 - l    
S1(h, l, c) => PP(h, l, c) * 2 - h    
R2(h, l, c) => PP(h, l, c) + (h - l)    
S2(h, l, c) => PP(h, l, c) - (h - l)    
R3(h, l, c) => PP(h, l, c) * 2 + (h - 2 * l)    
S3(h, l, c) => PP(h, l, c) * 2 - (2 * h - l)    
R4(h, l, c) => PP(h, l, c) * 3 + (h - 3 * l)    
S4(h, l, c) => PP(h, l, c) * 3 - (3 * h - l)   
R5(h, l, c) => PP(h, l, c) * 4 + (h - 4 * l)    
S5(h, l, c) => PP(h, l, c) * 4 - (4 * h - l)


draw_line(ln, i, pivot, tmo, tc, col) =>
    array.push(ln, line.new(x1 = array.get(tmo, i), y1 = array.get(pivot, i), x2 = array.get(tc, i), y2 = array.get(pivot, i), xloc=xloc.bar_time, color=col, style=line.style_solid, width=i_line_width))


draw_label(lb, i, pivot, tmo, tc, txt, txt_color) =>
    if i_pivot_labels
        offset = '‏  ‏  ‏  ‏  ‏'
        labels_align_str_left= i_pivot_label_pos == "Left" ? txt + offset : offset + txt
        x_pos = i_pivot_label_pos == "Left" ? array.get(tmo, i) : array.get(tc, i)
        array.push(lb, label.new(x = x_pos, y = array.get(pivot, i), text=labels_align_str_left, textcolor=txt_color, style=label.style_label_center, color=#00000000, xloc=xloc.bar_time))



get_pivot_resolution() =>
    resolution = "M"
    if i_pivot_time_frame == AUTO
        if timeframe.isintraday
            resolution := timeframe.multiplier <= 15 ? "D" : "W"
        else if timeframe.isweekly or timeframe.ismonthly
            resolution := "12M"
    else if i_pivot_time_frame == DAILY
        resolution := "D"
    else if i_pivot_time_frame == WEEKLY
        resolution := "W"
    else if i_pivot_time_frame == MONTHLY
        resolution := "M"
    else if i_pivot_time_frame == QUARTERLY
        resolution := "3M"
    else if i_pivot_time_frame == YEARLY
        resolution := "12M"
    resolution


///////////////////////////////////////////////////////////////////////////////// CREATE ARRAYS

// Call timeframe resolution helper function
resolution = get_pivot_resolution()


// Create blank arrays
pivotTimeOpenArray = array.new_int(0)
pivotTimeCloseArray = array.new_int(0)
pivotHighArray = array.new_float(0)
pivotLowArray = array.new_float(0)
pivotCloseArray = array.new_float(0)


//Pine Script forces us to enter all the values of the array manually, one line at a time.
// Manually insert price values into the array
array.push(pivotTimeOpenArray, timeOpenPeriod(resolution, 0))
array.push(pivotTimeOpenArray, timeOpenPeriod(resolution, 1))


array.push(pivotTimeCloseArray, timeClosePeriod(resolution, 0))
array.push(pivotTimeCloseArray, timeClosePeriod(resolution, 1))


array.push(pivotHighArray, highPeriod(resolution, 0))
array.push(pivotHighArray, highPeriod(resolution, 1))



array.push(pivotLowArray, lowPeriod(resolution, 0))
array.push(pivotLowArray, lowPeriod(resolution, 1))



array.push(pivotCloseArray, closePeriod(resolution, 0))
array.push(pivotCloseArray, closePeriod(resolution, 1))




///////////////////////////////////////////////////////////////////////////////// ARRAY OTHER

// Create blank arrays

var lines_pivot = array.new_line()
var labels_pivot = array.new_label()

ppMonArray = array.new_float()
r1MonArray = array.new_float()
s1MonArray = array.new_float()
r2MonArray = array.new_float()
s2MonArray = array.new_float()
r3MonArray = array.new_float()
s3MonArray = array.new_float()
r4MonArray = array.new_float()
s4MonArray = array.new_float()
r5MonArray = array.new_float()
s5MonArray = array.new_float()

float pivotHigh = na
float pivotLow = na


///////////////////////////////////////////////////////////////////////////////// PLOT LINES



// Only execute calculations on the last bar 
if barstate.islast
    
    // Loop over each month back
    for pivot_offset = 0 to i_pivots_back - 1
        
        // Calculate Traditional Pivots values and push to arrays
        // Notice that the high, low, close arrays have +1
        // This is because Pivot lines are calculate on the previous candle, but drawn on the current candle.
        array.push(ppMonArray, PP(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(r1MonArray, R1(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1))) 
        array.push(s1MonArray, S1(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(r2MonArray, R2(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1))) 
        array.push(s2MonArray, S2(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(r3MonArray, R3(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(s3MonArray, S3(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(r4MonArray, R4(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1))) 
        array.push(s4MonArray, S4(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        array.push(r5MonArray, R5(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1))) 
        array.push(s5MonArray, S5(array.get(pivotHighArray, pivot_offset + 1), array.get(pivotLowArray, pivot_offset + 1), array.get(pivotCloseArray, pivot_offset + 1)))
        
        // Current months high and low values
        pivotHigh := array.get(pivotHighArray, pivot_offset)
        pivotLow  := array.get(pivotLowArray, pivot_offset)
        
        
        // **************************
        // Automatic Pivots
        // **************************
        // Logic that will only draw pivot lines one above the months high/low.
        // This makes the chart less cluttered and more readable.
        
        if i_auto_pivots
        
            // Draw Monthly Pivot lines
            // PP
            draw_line(lines_pivot, pivot_offset, ppMonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, ppMonArray, pivotTimeOpenArray, pivotTimeCloseArray, "P", i_pivot_color)
            
            // R1
            if pivotHigh > array.get(ppMonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, r1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, r1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R1", i_pivot_color)
            
            // S1
            if pivotLow < array.get(ppMonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, s1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, s1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S1", i_pivot_color)
            
            // R2
            if pivotHigh > array.get(r1MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, r2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, r2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R2", i_pivot_color)
            
            // S2
            if pivotLow < array.get(s1MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, s2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, s2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S2", i_pivot_color)
            
            // R3
            if pivotHigh > array.get(r2MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, r3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, r3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R3", i_pivot_color)
            
            // S3
            if pivotLow < array.get(s2MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, s3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, s3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S3", i_pivot_color)
            
            // R4
            if pivotHigh > array.get(r3MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, r4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, r4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R4", i_pivot_color)
            
            // S4
            if pivotLow < array.get(s3MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, s4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, s4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S4", i_pivot_color)
            
            // R5
            if pivotHigh > array.get(r4MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, r5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, r5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R5", i_pivot_color)
            
            // S5
            if pivotLow < array.get(s4MonArray, pivot_offset)
                draw_line(lines_pivot, pivot_offset, s5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
                draw_label(labels_pivot, pivot_offset, s5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S5", i_pivot_color)

        else
            // **************************
            // Turn off auto pivots
            // Display all pivot lines
            // **************************
            
            // Draw Monthly Pivot lines
            // PP
            draw_line(lines_pivot, pivot_offset, ppMonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, ppMonArray, pivotTimeOpenArray, pivotTimeCloseArray, "P", i_pivot_color)
            
            // R1
            draw_line(lines_pivot, pivot_offset, r1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, r1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R1", i_pivot_color)
            
            // S1
            draw_line(lines_pivot, pivot_offset, s1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, s1MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S1", i_pivot_color)
            
            // R2
            draw_line(lines_pivot, pivot_offset, r2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, r2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R2", i_pivot_color)
            
            // S2
            draw_line(lines_pivot, pivot_offset, s2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, s2MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S2", i_pivot_color)
            
            // R3
            draw_line(lines_pivot, pivot_offset, r3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, r3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R3", i_pivot_color)
            
            // S3
            draw_line(lines_pivot, pivot_offset, s3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, s3MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S3", i_pivot_color)
            
            // R4
            draw_line(lines_pivot, pivot_offset, r4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, r4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R4", i_pivot_color)
            
            // S4
            draw_line(lines_pivot, pivot_offset, s4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, s4MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S4", i_pivot_color)
            
            // R5
            draw_line(lines_pivot, pivot_offset, r5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, r5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "R5", i_pivot_color)
            
            // S5
            draw_line(lines_pivot, pivot_offset, s5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, i_pivot_color)
            draw_label(labels_pivot, pivot_offset, s5MonArray, pivotTimeOpenArray, pivotTimeCloseArray, "S5", i_pivot_color)

//@version=5
//indicator("Envelope")
//------------------------------------------------------------------------------
//Settings
//-----------------------------------------------------------------------------{
envh = input.float(8.,'Bandwidth', minval = 0)
envmult = input.float(3., minval = 0)
envsrc = input(close, 'Source')

repaint = input(true, 'Repainting Smoothing', tooltip = 'Repainting is an effect where the indicators historical output is subject to change over time. Disabling repainting will cause the indicator to output the endpoints of the calculations')

//Style
upCss = input.color(color.teal, 'Colors', inline = 'inline1', group = 'Style')
dnCss = input.color(color.red, '', inline = 'inline1', group = 'Style')

//-----------------------------------------------------------------------------}
//Functions
//-----------------------------------------------------------------------------{
//Gaussian window
gauss(x, envh) => math.exp(-(math.pow(x, 2)/(envh * envh * 2)))

//-----------------------------------------------------------------------------}
//Append lines
//-----------------------------------------------------------------------------{
envn = bar_index

var envln = array.new_line(0) 

if barstate.isfirst and repaint
    for envi = 0 to 499
        array.push(envln,line.new(na,na,na,na))

//-----------------------------------------------------------------------------}
//End point method
//-----------------------------------------------------------------------------{
var coefs = array.new_float(0)
var den = 0.

if barstate.isfirst and not repaint
    for envi = 0 to 499
        envw = gauss(envi, envh)
        coefs.push(envw)

    den := coefs.sum()

envout = 0.
if not repaint
    for envi = 0 to 499
        envout += envsrc[envi] * coefs.get(envi)
envout /= den
envmae = ta.sma(math.abs(envsrc - envout), 499) * envmult

envupper = envout + envmae
envlower = envout - envmae
 
//-----------------------------------------------------------------------------}
//Compute and display envnwe
//-----------------------------------------------------------------------------{
float envy2 = na
float envy1 = na

envnwe = array.new<float>(0)
if barstate.islast and repaint
    envsae = 0.
    //Compute and set envnwe point 
    for envi = 0 to math.min(499,envn - 1)
        sum = 0.
        envsumw = 0.
        //Compute weighted mean 
        for envj = 0 to math.min(499,envn - 1)
            envw = gauss(envi - envj, envh)
            sum += envsrc[envj] * envw
            envsumw += envw

        envy2 := sum / envsumw
        envsae += math.abs(envsrc[envi] - envy2)
        envnwe.push(envy2)
    
    envsae := envsae / math.min(499,envn - 1) * envmult
    for envi = 0 to math.min(499,envn - 1)
        if envi%2
            line.new(envn-envi+1, envy1 + envsae, envn-envi, envnwe.get(envi) + envsae, color = upCss)
            line.new(envn-envi+1, envy1 - envsae, envn-envi, envnwe.get(envi) - envsae, color = dnCss)
        
        if envsrc[envi] > envnwe.get(envi) + envsae and envsrc[envi+1] < envnwe.get(envi) + envsae
            label.new(envn-envi, envsrc[envi], '▼', color = color(na), style = label.style_label_down, textcolor = dnCss, textalign = text.align_center)
        if envsrc[envi] < envnwe.get(envi) - envsae and envsrc[envi+1] > envnwe.get(envi) - envsae
            label.new(envn-envi, envsrc[envi], '▲', color = color(na), style = label.style_label_up, textcolor = upCss, textalign = text.align_center)
        
        envy1 := envnwe.get(envi)

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

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

//-----------------------------------------------------------------------------}
//Plot
//-----------------------------------------------------------------------------}
plot(repaint ? na : envout + envmae, 'envupper', upCss)
plot(repaint ? na : envout - envmae, 'envlower', dnCss)

//Crossing Arrows
plotshape(ta.crossunder(close, envout - envmae) ? low : na, "Crossunder", shape.labelup, location.absolute, color(na), 0 , text = '▲', textcolor = upCss, size = size.tiny)
plotshape(ta.crossover(close, envout + envmae) ? high : na, "Crossover", shape.labeldown, location.absolute, color(na), 0 , text = '▼', textcolor = dnCss, size = size.tiny)

//-----------------------------------------------------------------------------}

Return to “Pine Script Q&A”