Pivot Points [MisterMoTA]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @author  = MisterMoTA
// © MisterMoTA

//@version=5
indicator('Pivot points [MisterMoTA]', overlay = true)

i_res = input.timeframe("D", "Choose the timeframe for pivots calculation", options=["1", "2", "3", "5", "15", "30", "45", "60", "120", "180", "240", "360", "480", "720", "D", "W", "M"], group = "Pivot Points Settings")

decimals = input.int(2, "Decimals for price displayed in Labels", maxval = 8, group = "Pivot Points Settings")

styleOption = input.string("solid (─)", title="Line Style",
     options=["solid (─)", "dotted (┈)", "dashed (╌)", 
     "arrow left (←)", "arrow right (→)", "arrows both (↔)"], group = "Pivot Points Settings")


lineStyle = styleOption == "dotted (┈)" ? line.style_dotted :
     styleOption == "dashed (╌)" ? line.style_dashed :
     styleOption == "arrow left (←)" ? line.style_arrow_left :
     styleOption == "arrow right (→)" ? line.style_arrow_right :
     styleOption == "arrows both (↔)" ? line.style_arrow_both :
         line.style_solid


hideLabels = input.bool(false, title="Show/Hide Pivot Labels", group = "Pivot Points Settings")
plotPivotPoints = input.bool(true, title = "Show Current Pivot Points", group = "Pivot Points Settings")
oldPivotPoints = input.bool(true, title = "Show Old Pivot Points", group = "Pivot Points Settings")

mainPivotColor = input.color(color.new(#1df10e, 0), "Main Pivot Color", inline="Main Pivot Color", group = "Pivot Points Settings")

r1color = input.color(color.new(#fae606, 0), "R1 Support Color", group = "Pivot Points Settings")
r2color = input.color(color.new(#41b4fc, 0), "R2 Support Color", group = "Pivot Points Settings")
r3color = input.color(color.new(#f37221, 5), "R3 Support  Color", group = "Pivot Points Settings")
r4color = input.color(color.new(color.red, 0), "R4 Support Color", group = "Pivot Points Settings")
r5color = input.color(color.new(#d30755, 0), "R5 Support  Color", group = "Pivot Points Settings")

s1color = input.color(color.new(#fae606, 0), "S1 Support Color", group = "Pivot Points Settings")
s2color = input.color(color.new(#41b4fc, 0), "S2 Support Color", group = "Pivot Points Settings")
s3color = input.color(color.new(#04ac0a, 0), "S3 Support Color", group = "Pivot Points Settings")
s4color = input.color(color.new(#048008, 0), "S4 Support Color", group = "Pivot Points Settings")
s5color = input.color(color.new(#025b05, 0), "S5 Support Color", group = "Pivot Points Settings")

var string decs =na
if decimals == 0
    decs := "#"
else if decimals == 1
    decs :="#.#"
else if decimals == 2
    decs := "#.##"
else if decimals == 3
    decs :="#.###"
else if decimals == 4
    decs :="#.####"
else if decimals == 5
    decs :="#.#####"
else if decimals == 6
    decs :="#.######"
else if decimals == 7
    decs :="#.#######"
else if decimals == 8
    decs :="#.########"

//Pivot points 

var topLine = line.new(x1=na, y1=na, x2=na, y2=na, color=color.rgb(76, 175, 79, 5), style=lineStyle, width=2)
var bottomLine = line.new(x1=na, y1=na, x2=na, y2=na,   color=color.rgb(255, 82, 82, 5), style=lineStyle, width=2)


var mainPivot = line.new(x1=na, y1=na, x2=na, y2=na, color=mainPivotColor, style=lineStyle, width=1)
var sup1 = line.new(x1=na, y1=na, x2=na, y2=na, color=s1color, style=lineStyle, width=1)
var sup2 = line.new(x1=na, y1=na, x2=na, y2=na, color=s2color, style=lineStyle, width=1)
var supp3 = line.new(x1=na, y1=na, x2=na, y2=na, color=s3color, style=lineStyle, width=1)
var supp4 = line.new(x1=na, y1=na, x2=na, y2=na, color=s4color, style=lineStyle, width=1)
var supp5 = line.new(x1=na, y1=na, x2=na, y2=na, color=s5color, style=lineStyle, width=1)

var ress1 = line.new(x1=na, y1=na, x2=na, y2=na, color=r1color, style=lineStyle, width=1)
var ress2 = line.new(x1=na, y1=na, x2=na, y2=na, color=r2color, style=lineStyle, width=1)
var res3 = line.new(x1=na, y1=na, x2=na, y2=na, color=r3color, style=lineStyle, width=1)
var res4 = line.new(x1=na, y1=na, x2=na, y2=na,  color=r4color, style=lineStyle, width=1)
var res5 = line.new(x1=na, y1=na, x2=na, y2=na, color=r5color, style=lineStyle, width=1)

Val_high = request.security(syminfo.tickerid, i_res, high[1]) 
Val_low =  request.security(syminfo.tickerid, i_res, low[1]) 
dclose = request.security(syminfo.tickerid, i_res, close[1])

PivotPoint = (Val_high + Val_low + dclose ) / 3

R1 = PivotPoint * 2 -Val_low
R2 = PivotPoint + (Val_high -Val_low)    
R3 = PivotPoint * 2 + (Val_high - 2 * Val_low)
R4 = PivotPoint * 3 + (Val_high - 3 * Val_low)
R5 = PivotPoint * 4 + (Val_high - 4 * Val_low)
       
S1 = PivotPoint * 2 - Val_high
S2 = PivotPoint - (Val_high -Val_low) 
S3 = PivotPoint * 2 - (2 * Val_high - Val_low)
S4 = PivotPoint * 3 - (3 * Val_high - Val_low)
S5 = PivotPoint * 4 - (4 * Val_high - Val_low)

if plotPivotPoints
    line.set_xy1(id=topLine, x= bar_index, y=Val_high)
    line.set_xy2(id=topLine, x= bar_index+50, y=Val_high)

    line.set_xy1(id=bottomLine, x=bar_index, y=Val_low)
    line.set_xy2(id=bottomLine, x= bar_index+50, y=Val_low)

    line.set_xy1(id=mainPivot, x= bar_index, y=PivotPoint)
    line.set_xy2(id=mainPivot, x=  bar_index+50, y=PivotPoint)
        
    line.set_xy1(id=sup1, x= bar_index, y=S1)
    line.set_xy2(id=sup1, x=  bar_index+50, y=S1)

    line.set_xy1(id=sup2, x= bar_index, y=S2)
    line.set_xy2(id=sup2, x=  bar_index+50, y=S2)

    line.set_xy1(id=supp3, x= bar_index, y=S3)
    line.set_xy2(id=supp3, x=  bar_index+50, y=S3)

    line.set_xy1(id=supp4, x= bar_index, y=S4)
    line.set_xy2(id=supp4, x=  bar_index+50, y=S4)

    line.set_xy1(id=supp5, x= bar_index, y=S5)
    line.set_xy2(id=supp5, x=  bar_index+50, y=S5)
        
    line.set_xy1(id=ress1, x= bar_index, y=R1)
    line.set_xy2(id=ress1, x=  bar_index+50, y=R1)

    line.set_xy1(id=ress2, x= bar_index, y=R2)
    line.set_xy2(id=ress2, x=  bar_index+50, y=R2)

    line.set_xy1(id=res3, x= bar_index, y=R3)
    line.set_xy2(id=res3, x=  bar_index+50, y=R3)
    
    line.set_xy1(id=res4, x= bar_index, y=R4)
    line.set_xy2(id=res4, x=  bar_index+50, y=R4)

    line.set_xy1(id=res5, x= bar_index, y=R5)
    line.set_xy2(id=res5, x=  bar_index+50, y=R5)

z = plot(oldPivotPoints ? PivotPoint: na, color=color.new(#1df10e, 65), style=plot.style_circles, title=' Mean Pivot', linewidth=1, display = display.pane)
x1 = plot(oldPivotPoints ? R1: na, color=r1color, style=plot.style_circles, title=' R1', linewidth=1, display = display.pane)
x2 = plot(oldPivotPoints ? S1: na, color=s1color, style=plot.style_circles, title=' S1', linewidth=1, display = display.pane)
y1 = plot(oldPivotPoints ? R2: na, color=r2color, style=plot.style_circles, title=' R2', linewidth=1, display = display.pane)
y2 = plot(oldPivotPoints ? S2: na, color=s2color, style=plot.style_circles, title=' S2', linewidth=1, display = display.pane)
y3 = plot(oldPivotPoints ? S3: na, color=s3color, style=plot.style_circles, title=' S3', linewidth=1, display = display.pane)
x3 = plot(oldPivotPoints ? R3: na, color=r3color, style=plot.style_circles, title=' R3', linewidth=1, display = display.pane)
y4 = plot(oldPivotPoints ? S4: na, color=s4color, style=plot.style_circles, title=' S4', linewidth=1, display = display.pane)
x4 = plot(oldPivotPoints ? R4: na, color=r4color, style=plot.style_circles, title='R4', linewidth=1, display = display.pane)
y5 = plot(oldPivotPoints ? S5: na, color=s5color, style=plot.style_circles, title='S5', linewidth=1, display = display.pane)
x5 = plot(oldPivotPoints ? R5: na, color=r5color, style=plot.style_circles, title='R5', linewidth=1, display = display.pane)


if hideLabels == false    

    l0 = label.new( bar_index+50, Val_high, str.tostring(Val_high, decs) + " is Last Candle High at " + i_res, color=#00000000, textcolor=color.green, style=label.style_label_left, size=size.normal)
    l10 = label.new( bar_index+50, Val_low, str.tostring(Val_low, decs) + " is Last Candle Low at " + i_res, color=#00000000, textcolor=color.red, style=label.style_label_left, size=size.normal)

    label.delete(l0[1])
    label.delete(l10[1])

    lm = label.new( bar_index+50, PivotPoint, 'Main Pivot : ' + str.tostring(PivotPoint, decs), color=#00000000, textcolor=mainPivotColor, style=label.style_label_left, size=size.normal)
    l1 = label.new( bar_index+50, S1, 'S1 : ' + str.tostring(S1, decs), color=#00000000, textcolor=s1color, style=label.style_label_left, size=size.normal)
    l2 = label.new( bar_index+50, S2, 'S2 : ' + str.tostring(S2, decs), color=#00000000, textcolor=s2color, style=label.style_label_left, size=size.normal)
    l3 = label.new( bar_index+50, S3, 'S3 pivot is ' + str.tostring(S3, decs), color=#00000000, textcolor=s3color, style=label.style_label_left, size=size.normal)
    l4 = label.new( bar_index+50, S4, 'S4 pivot is ' + str.tostring(S4, decs), color=#00000000, textcolor=s4color, style=label.style_label_left, size=size.normal)
    l5 = label.new( bar_index+50, S5, 'S5 pivot is ' + str.tostring(S5, decs), color=#00000000, textcolor=s5color, style=label.style_label_left, size=size.normal)
        
    r11 = label.new( bar_index+50, R1, 'R1 : ' + str.tostring(R1, decs), color=#00000000, textcolor=r1color, style=label.style_label_left,size=size.normal)
    r22 = label.new( bar_index+50, R2, 'R2 : ' + str.tostring(R2, decs), color=#00000000, textcolor=r2color, style=label.style_label_left, size=size.normal)
    r33 = label.new( bar_index+50, R3, 'R3 pivot is ' + str.tostring(R3, decs), color=#00000000, textcolor=r3color, style=label.style_label_left, size=size.normal)
    r44 = label.new( bar_index+50, R4, 'R4 pivot is ' + str.tostring(R4, decs), color=#00000000, textcolor=r4color, style=label.style_label_left, size=size.normal)
    r55 = label.new( bar_index+50, R5, 'R5 pivot is ' + str.tostring(R5, decs), color=#00000000, textcolor=r5color, style=label.style_label_left, size=size.normal)
        
    label.delete(lm[1])     

    label.delete(l1[1])
    label.delete(l2[1])
    label.delete(l3[1])
    label.delete(l4[1])
    label.delete(l5[1])

    label.delete(r11[1])
    label.delete(r22[1])
    label.delete(r33[1])
    label.delete(r44[1])
    label.delete(r55[1])