4C Expected Move (Weekly Options)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © FourC
//@version=5

indicator("4C Expected Move (Weekly Options)", shorttitle="4C ExpMove", overlay=true)
em = input.price(1, "Weekly Expected Move - Input next week's EM number based on weekly option pricing [use your broker for this]. It is most accurate to use the EM number for next week AFTER the Friday close and/or before the Monday open of the upcoming week.")
linesright = input(title='Extend Lines Right', defval=true)
show2sigma = input(title='Show 2 Standard Deviation Move', defval=false)
styleOption = input.string(title="Line Style", options=["solid (─)", "dashed (╌)", "dotted (┈)"], defval="solid (─)")
styleOptionx2 = input.string(title="Line Style 2 Standard Deviation", options=["solid (─)", "dashed (╌)", "dotted (┈)"], defval= "dashed (╌)")
linewidth = input(2, "Line Thickness")
wclosecolor = input(title='Last Week Close', defval=color.rgb(0,255,255,0))
posemcolor = input(title='Upper Expected Move', defval=color.rgb(0,255,0,0))
negemcolor = input(title='Lower Expected Move', defval=color.rgb(255,0,0,0))
showlabels = input(title='Show Labels', defval=true)
labelcolor = input(title='Expected Move Label Color', defval=color.white)
labelposition = input(5,'Label Postion (Higher = Further Right)')

lineStyle = (styleOption == "dotted (┈)") ? line.style_dotted :
     (styleOption == "dashed (╌)") ? line.style_dashed :
         line.style_solid

lineStyle2 = (styleOptionx2 == "dotted (┈)") ? line.style_dotted :
     (styleOptionx2 == "dashed (╌)") ? line.style_dashed :
         line.style_solid

wclose = request.security(syminfo.tickerid, "W", close[1], barmerge.gaps_off, barmerge.lookahead_on)

var line wcloseline = na
var line posemline = na
var line negemline = na
var line posemlinex2 = na
var line negemlinex2 = na

var label wcloselabel = na
var label wemposlabel = na
var label wemneglabel = na
var label wemposlabelx2 = na
var label wemneglabelx2 = na

posemx2calc = wclose + (2*em)
negemx2calc = wclose - (2*em)

if wclose[1] != wclose
    line.set_x2(wcloseline, bar_index)
    line.set_x2(posemline, bar_index)
    line.set_x2(negemline, bar_index)
    line.set_x2(posemlinex2, bar_index)
    line.set_x2(negemlinex2, bar_index)
    line.set_extend(wcloseline, extend.none)
    line.set_extend(posemline, extend.none)
    line.set_extend(negemline, extend.none)
    line.set_extend(posemlinex2, extend.none)
    line.set_extend(negemlinex2, extend.none)
    label.delete(wcloselabel)
    label.delete(wemposlabel)
    label.delete(wemneglabel)
    label.delete(wemposlabelx2)
    label.delete(wemneglabelx2)

    //Expected Move Lines
    wcloseline := line.new(bar_index, wclose, bar_index, wclose, color=wclosecolor, style=lineStyle, width=linewidth)
    posemline := line.new(bar_index, wclose + em, bar_index, wclose + em, color=posemcolor, style=lineStyle, width=linewidth)
    negemline := line.new(bar_index, wclose - em, bar_index, wclose - em, color=negemcolor, style=lineStyle, width=linewidth)
    posemlinex2 := line.new(bar_index, posemx2calc, bar_index, posemx2calc, color=posemcolor, style=lineStyle2, width=linewidth)
    negemlinex2 := line.new(bar_index, negemx2calc, bar_index, negemx2calc, color=negemcolor, style=lineStyle2, width=linewidth)
    
    line.delete(wcloseline[1])
    line.delete(posemline[1])
    line.delete(negemline[1])
    line.delete(posemlinex2[1])
    line.delete(negemlinex2[1])
    
    if show2sigma != true

        line.delete(posemlinex2)
        line.delete(negemlinex2)

    if linesright == true
        line.set_x2(wcloseline, bar_index)
        line.set_x2(posemline, bar_index)
        line.set_x2(negemline, bar_index)
        line.set_x2(posemlinex2, bar_index)
        line.set_x2(negemlinex2, bar_index)
        line.set_extend(wcloseline, extend.right)
        line.set_extend(posemline, extend.right)
        line.set_extend(negemline, extend.right)
        line.set_extend(posemlinex2, extend.right)
        line.set_extend(negemlinex2, extend.right)
    
    //Expected Move Labels
    if showlabels == true
        wcloselabel := label.new(bar_index, wclose, 'WC: ' + str.tostring(wclose, '#.##'), style=label.style_none)
        wemposlabel := label.new(bar_index, wclose + em, 'UpperEM: ' + str.tostring(wclose + em, '#.##'), style=label.style_none)
        wemneglabel := label.new(bar_index, wclose - em, 'LowerEM: ' + str.tostring(wclose - em, '#.##'), style=label.style_none)
        wemposlabelx2 := label.new(bar_index, posemx2calc, 'UpperEM x 2: ' + str.tostring(posemx2calc, '#.##'), style=label.style_none)
        wemneglabelx2 := label.new(bar_index, negemx2calc, 'LowerEM x 2: ' + str.tostring(negemx2calc, '#.##'), style=label.style_none)
        label.set_textcolor(wcloselabel, labelcolor)
        label.set_textcolor(wemposlabel, labelcolor)
        label.set_textcolor(wemneglabel, labelcolor)
        label.set_textcolor(wemposlabelx2, labelcolor)
        label.set_textcolor(wemneglabelx2, labelcolor)

    if show2sigma != true
        label.delete(wemposlabelx2)
        label.delete(wemneglabelx2)

if not na(wcloseline) and line.get_x2(wcloseline) != bar_index
    line.set_x2(wcloseline, bar_index)
    line.set_x2(posemline, bar_index)
    line.set_x2(negemline, bar_index)
    line.set_x2(posemlinex2, bar_index)
    line.set_x2(negemlinex2, bar_index)
    label.set_x(wcloselabel, bar_index + labelposition)
    label.set_x(wemposlabel, bar_index + labelposition)
    label.set_x(wemneglabel, bar_index + labelposition)
    label.set_x(wemposlabelx2, bar_index + labelposition)
    label.set_x(wemneglabelx2, bar_index + labelposition)