Synapse Level Index
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=5

                                                        //   © LvNThL    \\ 
//                                                      \\  Description  //
//
//                                                 Synapse Level Index Indicator
//
//                       This indicator simply allows the user to set their desired "Lookback Period", 
//                       and "Lookahead Period" in the Bars Back and Bars Ahead, Pivot Settings. Once 
//                       selected, the indicator tracks the highest high from X Bars Ahead, and the 
//                       lowest low, from Y Bars Back. Then, the indicator calculates the Mean Value.
//                       Then, the indicator proceeds to draw the High to Low range by Eighths. Then,
//                       the indicator proceeds to draw the High to Low range by Sixteenths. Bravo.
//                       Fear and Greed increase at these levels psychologically. Volatility Ensues.
//                       The lowest percentage label is the change in value percentage from the lowest low, 
//                       the middle percentage label is the entire change in value percentage from
//                       either the highest point to lowest point, or lowest to highest point, the top 
//                       percentage label is the change in value percentage from the highest high.
//
//                                                             Enjoy
//
//                                                       Mr. Storm [LvNThL]


indicator("Synapse Level Index", shorttitle = "SLI", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500)

var line line_1 = na
var line line_2 = na
var line line_3 = na
var line line_4 = na
var line line_5 = na
var line line_6 = na
var line line_7 = na
var line line_8 = na 
var line line_9 = na
var line line_10 = na 
var label lbl_1 = na 
var label lbl_2 = na 
var label lbl_3 = na
var label lbl_4 = na
var label lbl_5 = na
var label lbl_6 = na
var label lbl_7 = na
//SLI FUNCTION
int highperiod = input.int(248, title = "Bars Ahead", minval = 2, maxval = 248, step = 2, group = "SLI Settings", tooltip = string("Minimum Value: 2, Maximum Value: 248"))
int lowperiod = input.int(248, title = "Bars Back", minval = 2, maxval = 248, step = 2, group = "SLI Settings", tooltip = string("Minimum Value: 2, Maximum Value: 248"))
float change_in_value_1 = ta.highest(high, highperiod)
float change_in_value_2 = ta.lowest(low, lowperiod)
int change1 = -ta.highestbars(high, highperiod)
int change2 = -ta.lowestbars(low, lowperiod)
var color meancolor = na
// MEAN
float change_in_value_3 = (change_in_value_1 + change_in_value_2)/2

//FOURTHS
change_in_value_4 = (change_in_value_3 + change_in_value_1)/2
change_in_value_5 = (change_in_value_3 + change_in_value_2)/2

//EIGHTHS
change_in_value_6 = (change_in_value_1 + change_in_value_4)/2
change_in_value_7 = (change_in_value_4 + change_in_value_3)/2
change_in_value_8 = (change_in_value_3 + change_in_value_5)/2
change_in_value_9 = (change_in_value_5 + change_in_value_2)/2

if close >= change_in_value_3
    meancolor := color.rgb(0,255,0,0)
else
    meancolor := color.rgb(255,0,0,0)

bool slioverlay = input.bool(true, "SLI Indicator Active", group = "SLI Switches")
bool zigzag = input.bool(true, "ZigZag Active", group = "SLI Switches")
bool extendright = input.bool(false, "Extend Right", group = "SLI Switches")
bool extendleft = input.bool(false, "Extend Left", group = "SLI Switches")
bool extendboth = input.bool(false, "Extend Both", group = "SLI Switches")
bool lightmode = input.bool(false, "Light Mode", group = "SLI Switches")

string extension = extendright ? extend.right : extendleft ? extend.left : extendboth ? extend.both : extend.none

if barstate.islast and zigzag == true or barstate.islast and slioverlay == true

//ZIGZAG LINE 
    line_10 := zigzag ? line.new(bar_index[change2], change_in_value_2,bar_index[change1], change_in_value_1, color = lightmode ? color.black : color.silver, style = line.style_dashed, width = 1, extend = extend.none) : na
    line.delete(line_10[1])
    
    if barstate.islast and slioverlay == true

//TOP LINE
        line_1 := line.new(bar_index[24], change_in_value_1, bar_index + 1, change_in_value_1, color = color.rgb(255,0,0,0), style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_1[1])
        lbl_2 := label.new(bar_index + 1, change_in_value_1, close < change_in_value_1 ? string("▽ ") + str.tostring((close / change_in_value_1)-1, "#.##%") : close >= change_in_value_1 ? string("△ ") + str.tostring((change_in_value_1 / close)-1, "#.##%") : string("Unknown"), style = label.style_label_left, color = color.rgb(0,0,0,100), textcolor = lightmode ? color.black : color.silver, size = size.small)
        label.delete(lbl_2[1])

//REMAINING TOP HALF LINES
        line_6 := line.new(bar_index[24], change_in_value_6, bar_index + 1, change_in_value_6, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_6[1])

        line_4 := line.new(bar_index[24], change_in_value_4, bar_index + 1, change_in_value_4, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_4[1])

        line_7 := line.new(bar_index[24], change_in_value_7, bar_index + 1, change_in_value_7, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_7[1])

//MEAN LINE
        line_3 := line.new(bar_index[24], change_in_value_3, bar_index + 1, change_in_value_3, color = meancolor, style = line.style_dashed, width = 1, extend = extension)
        line.delete(line_3[1])
        lbl_1 := label.new(bar_index + 1, change_in_value_3, change1 < change2 ? string("△ ") + str.tostring((change_in_value_1 / change_in_value_2)-1, "#.##%") : change1 >= change2 ? string("▽ ") + str.tostring((change_in_value_2 / change_in_value_1)-1, "#.##%") : string("Unknown"), style = label.style_label_left, color = color.rgb(0,0,0,100), textcolor = lightmode ? color.black : color.silver, size = size.small)
        label.delete(lbl_1[1])

//BOTTOM LINE
        line_2 := line.new(bar_index[24], change_in_value_2, bar_index + 1, change_in_value_2, color = color.rgb(0,255,0,0), style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_2[1])
        lbl_3 := label.new(bar_index + 1, change_in_value_2, close < change_in_value_2 ? string("▽ ") + str.tostring((close / change_in_value_2)-1, "#.##%") : close >= change_in_value_2 ? string("△ ") + str.tostring((close / change_in_value_2)-1, "#.##%") : string("Unknown"), style = label.style_label_left, color = color.rgb(0,0,0,100), textcolor = lightmode ? color.black : color.silver, size = size.small)
        label.delete(lbl_3[1])

//REMAINGIN BOTTOM HALF LINES
        line_8 := line.new(bar_index[24], change_in_value_8, bar_index + 1, change_in_value_8, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_8[1])

        line_5 := line.new(bar_index[24], change_in_value_5, bar_index + 1, change_in_value_5, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_5[1])

        line_9 := line.new(bar_index[24], change_in_value_9, bar_index + 1, change_in_value_9, color = lightmode ? color.black : color.silver, style = line.style_dotted, width = 1, extend = extension)
        line.delete(line_9[1])

//TOP HALF LINE FILL   
        linefill.new(line1 = line_1, line2 = line_6, color = color.rgb(255,0,0,95))
        linefill.new(line1 = line_6, line2 = line_4, color = color.rgb(255,0,0,96))
        linefill.new(line1 = line_4, line2 = line_7, color = color.rgb(255,0,0,97))
        linefill.new(line1 = line_7, line2 = line_3, color = color.rgb(255,0,0,98))

//BOTTOM HALF LINE FILL
        linefill.new(line1 = line_2, line2 = line_9, color = color.rgb(0,255,0,95))
        linefill.new(line1 = line_9, line2 = line_5, color = color.rgb(0,255,0,96))
        linefill.new(line1 = line_5, line2 = line_8, color = color.rgb(0,255,0,97))
        linefill.new(line1 = line_8, line2 = line_3, color = color.rgb(0,255,0,98))

