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

//@version=5
indicator(title="HistDistDevsv2", overlay=true, max_lines_count = 500)

//Inputs------------------------------------------------------------
ShowLines = input.bool(true, "Show Lines")
LabelBool = input.bool(true, title="Show Labels")
DevLineStyle = input.string("Dashed", title="Line Style", options=["Solid", "Dotted", "Dashed"])
PosDevs = input.color(color.rgb(48, 105, 30, 70), title="Positive Devs Color")
NegDevs = input.color(color.rgb(141, 46, 46, 70), title="Negative Devs Color")
OpenDev = input.color(color.rgb(180, 162, 57, 70), title="Open Line Color")
LabelColor = input.color(color.rgb(0, 0, 0, 70), title="Labels Color")
ShowHist = input.bool(false, title="Show Historical Sessions")
ShowOpen = input.bool(true, title="Show Open")
ShowP5 = input.bool(true, title="Show .5")
Show1 = input.bool(true, title="Show 1")
Show1P5 = input.bool(true, title="Show 1.5")
Show2 = input.bool(false, title="Show 2")
Show2P5 = input.bool(false, title="Show 2.5")
Show3 = input.bool(false, title="Show 3")


//Global Variables------------------------------------------------------------
var xPer = 0.0
var xBar = 0
var xOpen = 0.0
var xDay = 0
var NQper = 0.0
var ESper = 0.0
var YMper = 0.0
var RTYper = 0.0
var CLper = 0.0
var GCper = 0.0
string TFval = ""
bool badTimeframe = false
bool badTicker = false

//Timeframe Switch------------------------------------------------------------
if timeframe.period == "D" or timeframe.isintraday
    TFval := "D"
    NQper := 0.0119
    ESper := 0.0098
    YMper := 0.0095
    RTYper := 0.0135
    CLper := 0.0234
    GCper := 0.0080
else if timeframe.period == "W"
    TFval := "W"
    NQper := 0.0265
    ESper := 0.0229
    YMper := 0.0228
    RTYper := 0.0305
    CLper := 0.0525
    GCper := 0.0202
else if timeframe.period == "M"
    TFval := "M"
    NQper := 0.0510
    ESper := 0.0427
    YMper := 0.0421
    RTYper := 0.0570
    CLper := 0.1183
    GCper := 0.0459
else
    badTimeframe := true

//Asset Selection Switch------------------------------------------------------------
if str.contains(syminfo.ticker, "NQ")
    xPer := NQper
else if str.contains(syminfo.ticker, "ES")
    xPer := ESper
else if str.contains(syminfo.ticker, "YM")
    xPer := YMper
else if str.contains(syminfo.ticker, "RTY")
    xPer := RTYper
else if str.contains(syminfo.ticker, "CL")
    xPer := CLper
else if str.contains(syminfo.ticker, "GC")
    xPer := GCper
else
    badTicker := true

//Post Calculation Variables------------------------------------------------------------
[lineStart, lineEnd] = request.security(syminfo.tickerid, TFval, [time, time_close])
xOpen := request.security(syminfo.tickerid, TFval, open)

var lineStyle = line.style_dashed

if DevLineStyle == "Solid"
    lineStyle := line.style_solid
else if DevLineStyle == "Dotted"
    lineStyle := line.style_dotted

//Draw Once------------------------------------------------------------
if not ShowHist and (not badTimeframe and not badTicker) and ShowLines
    if Show3
        var p3 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p3, lineStart, xOpen + ((xOpen*xPer)*3))
        line.set_xy2(p3, lineEnd, xOpen + ((xOpen*xPer)*3))
        line.set_color(p3, color = PosDevs)
    if Show2P5
        var p25 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p25, lineStart, xOpen + ((xOpen*xPer)*2.5))
        line.set_xy2(p25, lineEnd, xOpen + ((xOpen*xPer)*2.5))
        line.set_color(p25, color = PosDevs)

    if Show2
        var p2 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p2, lineStart, xOpen + ((xOpen*xPer)*2))
        line.set_xy2(p2, lineEnd, xOpen + ((xOpen*xPer)*2))
        line.set_color(p2, color = PosDevs)

    if Show1P5
        var p15 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p15, lineStart, xOpen + ((xOpen*xPer)*1.5))
        line.set_xy2(p15, lineEnd, xOpen + ((xOpen*xPer)*1.5))
        line.set_color(p15, color = PosDevs)

    if Show1    
        var p1 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p1, lineStart, xOpen + ((xOpen*xPer)*1))
        line.set_xy2(p1, lineEnd, xOpen + ((xOpen*xPer)*1))
        line.set_color(p1, color = PosDevs)

    if ShowP5
        var p5 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(p5, lineStart, xOpen + ((xOpen*xPer)*0.5))
        line.set_xy2(p5, lineEnd, xOpen + ((xOpen*xPer)*0.5))
        line.set_color(p5, color = PosDevs)

    if ShowOpen
        var oo = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(oo, lineStart, xOpen)
        line.set_xy2(oo, lineEnd, xOpen)
        line.set_color(oo, color = OpenDev)

    if ShowP5
        var n5 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n5, lineStart, xOpen - ((xOpen*xPer)*0.5))
        line.set_xy2(n5, lineEnd, xOpen - ((xOpen*xPer)*0.5))
        line.set_color(n5, color = NegDevs)

    if Show1
        var n1 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n1, lineStart, xOpen - ((xOpen*xPer)*1))
        line.set_xy2(n1, lineEnd, xOpen - ((xOpen*xPer)*1))
        line.set_color(n1, color = NegDevs)

    if Show1P5
        var n15 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n15, lineStart, xOpen - ((xOpen*xPer)*1.5))
        line.set_xy2(n15, lineEnd, xOpen - ((xOpen*xPer)*1.5))
        line.set_color(n15, color = NegDevs)

    if Show2
        var n2 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n2, lineStart, xOpen - ((xOpen*xPer)*2))
        line.set_xy2(n2, lineEnd, xOpen - ((xOpen*xPer)*2))
        line.set_color(n2, color = NegDevs)

    if Show2P5
        var n25 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n25, lineStart, xOpen - ((xOpen*xPer)*2.5))
        line.set_xy2(n25, lineEnd, xOpen - ((xOpen*xPer)*2.5))
        line.set_color(n25, color = NegDevs)

    if Show3
        var n3 = line.new(na, na, na, na, xloc = xloc.bar_time, style = lineStyle, width = 1)
        line.set_xy1(n3, lineStart, xOpen - ((xOpen*xPer)*3))
        line.set_xy2(n3, lineEnd, xOpen - ((xOpen*xPer)*3))
        line.set_color(n3, color = NegDevs)

//Draw Current and Historical------------------------------------------------------------
if ShowHist and (xOpen != xOpen[1]) and (not badTimeframe and not badTicker) and ShowLines
    var line p3 = na
    var line p25 = na
    var line p2 = na
    var line p15 = na
    var line p1 = na
    var line p5 = na
    var line oo = na
    var line n5 = na
    var line n1 = na
    var line n15 = na
    var line n2 = na
    var line n25 = na
    var line n3 = na

    p3 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p3, lineStart, xOpen + ((xOpen*xPer)*3))
    line.set_xy2(p3, lineEnd, xOpen + ((xOpen*xPer)*3))
    line.set_color(p3, color = PosDevs)

    p25 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p25, lineStart, xOpen + ((xOpen*xPer)*2.5))
    line.set_xy2(p25, lineEnd, xOpen + ((xOpen*xPer)*2.5))
    line.set_color(p25, color = PosDevs)

    p2 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p2, lineStart, xOpen + ((xOpen*xPer)*2))
    line.set_xy2(p2, lineEnd, xOpen + ((xOpen*xPer)*2))
    line.set_color(p2, color = PosDevs)

    p15 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p15, lineStart, xOpen + ((xOpen*xPer)*1.5))
    line.set_xy2(p15, lineEnd, xOpen + ((xOpen*xPer)*1.5))
    line.set_color(p15, color = PosDevs)

    p1 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p1, lineStart, xOpen + ((xOpen*xPer)*1))
    line.set_xy2(p1, lineEnd, xOpen + ((xOpen*xPer)*1))
    line.set_color(p1, color = PosDevs)

    p5 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(p5, lineStart, xOpen + ((xOpen*xPer)*0.5))
    line.set_xy2(p5, lineEnd, xOpen + ((xOpen*xPer)*0.5))
    line.set_color(p5, color = PosDevs)

    oo := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(oo, lineStart, xOpen)
    line.set_xy2(oo, lineEnd, xOpen)
    line.set_color(oo, color = OpenDev)

    n5 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n5, lineStart, xOpen - ((xOpen*xPer)*0.5))
    line.set_xy2(n5, lineEnd, xOpen - ((xOpen*xPer)*0.5))
    line.set_color(n5, color = NegDevs)

    n1 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n1, lineStart, xOpen - ((xOpen*xPer)*1))
    line.set_xy2(n1, lineEnd, xOpen - ((xOpen*xPer)*1))
    line.set_color(n1, color = NegDevs)

    n15 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n15, lineStart, xOpen - ((xOpen*xPer)*1.5))
    line.set_xy2(n15, lineEnd, xOpen - ((xOpen*xPer)*1.5))
    line.set_color(n15, color = NegDevs)

    n2 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n2, lineStart, xOpen - ((xOpen*xPer)*2))
    line.set_xy2(n2, lineEnd, xOpen - ((xOpen*xPer)*2))
    line.set_color(n2, color = NegDevs)

    n25 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n25, lineStart, xOpen - ((xOpen*xPer)*2.5))
    line.set_xy2(n25, lineEnd, xOpen - ((xOpen*xPer)*2.5))
    line.set_color(n25, color = NegDevs)

    n3 := line.new(na, na, na, na, xloc = xloc.bar_time, style = line.style_dashed, width = 1)
    line.set_xy1(n3, lineStart, xOpen - ((xOpen*xPer)*3))
    line.set_xy2(n3, lineEnd, xOpen - ((xOpen*xPer)*3))
    line.set_color(n3, color = NegDevs)



//Draw Labels------------------------------------------------------------
if LabelBool and (not badTimeframe and not badTicker)
    if Show3
        var devp3 = label.new(na, na, "3", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp3, lineEnd, xOpen + ((xOpen*xPer)*3))
        label.set_size(devp3, size.small)
        label.set_textcolor(devp3, textcolor = LabelColor)

    if Show2P5
        var devp25 = label.new(na, na, "2.5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp25, lineEnd, xOpen + ((xOpen*xPer)*2.5))
        label.set_size(devp25, size.small)
        label.set_textcolor(devp25, textcolor = LabelColor)

    if Show2    
        var devp2 = label.new(na, na, "2", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp2, lineEnd, xOpen + ((xOpen*xPer)*2))
        label.set_size(devp2, size.small)
        label.set_textcolor(devp2, textcolor = LabelColor)

    if Show1P5
        var devp15 = label.new(na, na, "1.5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp15, lineEnd, xOpen + ((xOpen*xPer)*1.5))
        label.set_size(devp15, size.small)
        label.set_textcolor(devp15, textcolor = LabelColor)

    if Show1
        var devp1 = label.new(na, na, "1", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp1, lineEnd, xOpen + ((xOpen*xPer)*1))
        label.set_size(devp1, size.small)
        label.set_textcolor(devp1, textcolor = LabelColor)

    if ShowP5
        var devp05 = label.new(na, na, ".5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devp05, lineEnd, xOpen + ((xOpen*xPer)*0.5))
        label.set_size(devp05, size.small)
        label.set_textcolor(devp05, textcolor = LabelColor)

    if ShowOpen
        var devO = label.new(na, na, "O", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devO, lineEnd, xOpen)
        label.set_size(devO, size.small)
        label.set_textcolor(devO, textcolor = LabelColor)

    if ShowP5
        var devn05 = label.new(na, na, ".5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn05, lineEnd, xOpen - ((xOpen*xPer)*0.5))
        label.set_size(devn05, size.small)
        label.set_textcolor(devn05, textcolor = LabelColor)

    if Show1
        var devn1 = label.new(na, na, "1", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn1, lineEnd, xOpen - ((xOpen*xPer)*1))
        label.set_size(devn1, size.small)
        label.set_textcolor(devn1, textcolor = LabelColor)

    if Show1P5    
        var devn15 = label.new(na, na, "1.5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn15, lineEnd, xOpen - ((xOpen*xPer)*1.5))
        label.set_size(devn15, size.small)
        label.set_textcolor(devn15, textcolor = LabelColor)

    if Show2
        var devn2 = label.new(na, na, "2", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn2, lineEnd, xOpen - ((xOpen*xPer)*2))
        label.set_size(devn2, size.small)
        label.set_textcolor(devn2, textcolor = LabelColor)

    if Show2P5
        var devn25 = label.new(na, na, "2.5", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn25, lineEnd, xOpen - ((xOpen*xPer)*2.5))
        label.set_size(devn25, size.small)
        label.set_textcolor(devn25, textcolor = LabelColor)

    if Show3
        var devn3 = label.new(na, na, "3", xloc = xloc.bar_time, color = LabelColor, style = label.style_none)
        label.set_xy(devn3, lineEnd, xOpen - ((xOpen*xPer)*3))
        label.set_size(devn3, size.small)
        label.set_textcolor(devn3, textcolor = LabelColor)