PClose Levels 2.0
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © financialCode47408

//@version=5
indicator("PClose Levels 2.0", overlay=true)

f_newLine(_color) => line.new(na, na, na, na, xloc.bar_time, extend.both, _color)

f_moveLine(_line, _x, _y) =>
    line.set_xy1(_line, _x,   _y)
    line.set_xy2(_line, _x+1, _y)

var line    line_close = f_newLine(color.blue)
var line    line_emrpos = f_newLine(color.green)
var line    line_erpos = f_newLine(color.green)
var line    line_pos2 = f_newLine(color.green)
var line    line_pos1 = f_newLine(color.green)
var line    line_em = f_newLine(color.white)
var line    line_neg1 = f_newLine(color.red)
var line    line_neg2 = f_newLine(color.red)
var line    line_erneg = f_newLine(color.red)
var line    line_emrneg = f_newLine(color.red)

[pdc,pdt] = request.security(syminfo.tickerid,"D", [close[1],time[1]])
VIX = request.security("VIX", "1", close)
PCLOSE = request.security("SPX", "D", close[1])

if VIX <18.71 
    if barstate.islast
        f_moveLine(line_close, pdt, PCLOSE)
        f_moveLine(line_emrpos, pdt, PCLOSE*1.0067)
        f_moveLine(line_erpos, pdt, PCLOSE*1.0042)
        f_moveLine(line_pos2, pdt, PCLOSE*1.0034)
        f_moveLine(line_pos1, pdt, PCLOSE*1.0018)
        f_moveLine(line_em, pdt, PCLOSE*1.0016)
        f_moveLine(line_neg1, pdt, PCLOSE*-.0010+PCLOSE)
        f_moveLine(line_neg2, pdt, PCLOSE*-.0027+PCLOSE)
        f_moveLine(line_erneg, pdt, PCLOSE*-.0013+PCLOSE)
        f_moveLine(line_emrneg, pdt, PCLOSE*-.0051+PCLOSE)
        
        
if VIX >18.71 and VIX <22.38
    if barstate.islast
        f_moveLine(line_close, pdt, PCLOSE)
        f_moveLine(line_emrpos, pdt, PCLOSE*1.0105)
        f_moveLine(line_erpos, pdt, PCLOSE*1.0082)
        f_moveLine(line_pos2, pdt, PCLOSE*1.0082)
        f_moveLine(line_pos1, pdt, PCLOSE*1.0031)
        f_moveLine(line_em, pdt, PCLOSE*1.0008)
        f_moveLine(line_neg1, pdt, PCLOSE*-.0016+PCLOSE)
        f_moveLine(line_neg2, pdt, PCLOSE*-.0045+PCLOSE)
        f_moveLine(line_erneg, pdt, PCLOSE*-.0042+PCLOSE)
        f_moveLine(line_emrneg, pdt, PCLOSE*-.0087+PCLOSE)
        
if VIX >22.38 and VIX <26.41
    if barstate.islast
        f_moveLine(line_close, pdt, PCLOSE)
        f_moveLine(line_emrpos, pdt, PCLOSE*1.0161)
        f_moveLine(line_erpos, pdt, PCLOSE*1.0105)
        f_moveLine(line_pos2, pdt, PCLOSE*1.0109)
        f_moveLine(line_pos1, pdt, PCLOSE*1.0042)
        f_moveLine(line_em, pdt, PCLOSE*-.0004+PCLOSE)
        f_moveLine(line_neg1, pdt, PCLOSE*-.0042+PCLOSE)
        f_moveLine(line_neg2, pdt, PCLOSE*-.0084+PCLOSE)
        f_moveLine(line_erneg, pdt, PCLOSE*-.0086+PCLOSE)
        f_moveLine(line_emrneg, pdt, PCLOSE*-.0166+PCLOSE)
        
        
if VIX >26.41
    if barstate.islast
        f_moveLine(line_close, pdt, PCLOSE)
        f_moveLine(line_emrpos, pdt, PCLOSE*1.0197)
        f_moveLine(line_erpos, pdt, PCLOSE*1.0114)
        f_moveLine(line_pos2, pdt, PCLOSE*1.0142)
        f_moveLine(line_pos1, pdt, PCLOSE*1.0054)
        f_moveLine(line_em, pdt, PCLOSE*-.0027+PCLOSE)
        f_moveLine(line_neg1, pdt, PCLOSE*-.0048+PCLOSE)
        f_moveLine(line_neg2, pdt, PCLOSE*-.0096+PCLOSE)
        f_moveLine(line_erneg, pdt, PCLOSE*-.0118+PCLOSE)
        f_moveLine(line_emrneg, pdt, PCLOSE*-.0191+PCLOSE)
        
        
line.set_style(line_close, line.style_dashed)
line.set_width(line_close, 2)
line.set_style(line_em, line.style_dotted)
line.set_width(line_em, 2)
line.set_style(line_emrpos, line.style_dashed)
line.set_width(line_emrpos, 2)
line.set_style(line_emrneg, line.style_dashed)
line.set_width(line_emrneg, 2)
line.set_style(line_erpos, line.style_dotted)
line.set_width(line_erpos, 2)
line.set_style(line_erneg, line.style_dotted)
line.set_width(line_erneg, 2)