RTI Pivot Points Standard
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © superkumar2020

//@version=4
study("Pivot Points Standard", overlay=true)

WchosenColor(c_)=> c_ == "aqua" ? color.aqua : c_ == "black" ? color.black : c_ == "blue" ? color.blue : c_ == "fuchsia" ? color.fuchsia : c_ == "gray" ? color.gray : c_ == "green" ? color.green : c_ == "lime" ? color.lime : c_ == "maroon" ? color.maroon : c_ == "navy" ? color.navy : c_ == "olive" ? color.olive : c_ == "orange" ? color.orange : c_ == "purple" ? color.purple : c_ == "red" ? color.red : c_ == "silver" ? color.silver : c_ == "teal" ? color.teal : c_ == "white" ? color.white : c_ == "yellow" ? color.yellow : color.black
WhigherTF = "W"
Wmy_color = input(title="Weekly Color", defval="black", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
WprevCloseHTF = security(syminfo.tickerid, WhigherTF, close[1], lookahead=true)
WprevOpenHTF = security(syminfo.tickerid, WhigherTF, open[1], lookahead=true)
WprevHighHTF = security(syminfo.tickerid, WhigherTF, high[1], lookahead=true)
WprevLowHTF = security(syminfo.tickerid, WhigherTF, low[1], lookahead=true)

WpLevel = (WprevHighHTF + WprevLowHTF + WprevCloseHTF) / 3
Wr1Level = WpLevel * 2 - WprevLowHTF
Ws1Level = WpLevel * 2 - WprevHighHTF
Wr2Level = WpLevel + (WprevHighHTF - WprevLowHTF)
Ws2Level = WpLevel - (WprevHighHTF - WprevLowHTF)
Wr3Level = WpLevel + 2*(WprevHighHTF - WprevLowHTF)
Ws3Level = WpLevel - 2*(WprevHighHTF - WprevLowHTF)


var line Wr1Line = na
var line WpLine = na
var line Ws1Line = na
var line Wr2Line = na
var line Ws2Line = na
var line Wr3Line = na
var line Ws3Line = na
var line Wm3Line = na

var label Wr3L = na
var label Wr1L = na
var label WpL = na
var label Ws1L = na
var label Ws2L = na
var label Ws3L = na
var label Wr2L = na

if WpLevel[1] != WpLevel
    line.set_x2(Wr1Line, bar_index)
    line.set_x2(WpLine, bar_index)
    line.set_x2(Ws1Line, bar_index)
    line.set_x2(Wr2Line, bar_index)
    line.set_x2(Ws2Line, bar_index)
    line.set_x2(Wr3Line, bar_index)
    line.set_x2(Ws3Line, bar_index)
    line.set_extend(Wr1Line, extend.none)
    line.set_extend(WpLine, extend.none)
    line.set_extend(Ws1Line, extend.none)
    line.set_extend(Wr2Line, extend.none)
    line.set_extend(Ws2Line, extend.none)
    line.set_extend(Wr3Line, extend.none)
    line.set_extend(Ws3Line, extend.none)
    Wr1Line := line.new(bar_index, Wr1Level, bar_index, Wr1Level, width=1, extend=extend.right, color=color.green)
    WpLine := line.new(bar_index, WpLevel, bar_index, WpLevel, width=2, extend=extend.right, color=color.yellow)
    Ws1Line := line.new(bar_index, Ws1Level, bar_index, Ws1Level, width=1,extend=extend.right, color=color.red)
    Wr2Line := line.new(bar_index, Wr2Level, bar_index, Wr2Level, width=1, extend=extend.right,color=color.green)
    Ws2Line := line.new(bar_index, Ws2Level, bar_index, Ws2Level, width=1,extend=extend.right,color=color.red)
    Wr3Line := line.new(bar_index, Wr3Level, bar_index, Wr3Level, width=1, extend=extend.right,color=color.green)
    Ws3Line := line.new(bar_index, Ws3Level, bar_index, Ws3Level, width=1,extend=extend.right,color=color.red)
    Wr1L := label.new(bar_index, Wr1Level, WhigherTF+".R1", style=label.style_none)
    var label label1 = na
    WpL := label.new(bar_index, WpLevel, WhigherTF+".P", style=label.style_none)
    label.set_textalign(label1, text.align_right)
    Ws1L := label.new(bar_index, Ws1Level, WhigherTF+".S1", style=label.style_none)
    Wr2L := label.new(bar_index, Wr2Level, WhigherTF+".R2", style=label.style_none)
    Ws2L := label.new(bar_index, Ws2Level, WhigherTF+".S2", style=label.style_none)
    Wr3L := label.new(bar_index, Wr3Level, WhigherTF+".R3", style=label.style_none)
    Ws3L := label.new(bar_index, Ws3Level, WhigherTF+".S3", style=label.style_none)
    label.delete(Wr3L[1])
    label.delete(Ws3L[1])
    label.delete(Wr2L[1])
    label.delete(Wr1L[1])
    label.delete(WpL[1])
    label.delete(Ws1L[1])
    label.delete(Ws2L[1])
    line.delete(Wr2Line[1])
    line.delete(Wr1Line[1])
    line.delete(Wm3Line[1])
    line.delete(WpLine[1])
    line.delete(Ws1Line[1])
    line.delete(Ws2Line[1])
    line.delete(Wr3Line[1])
    line.delete(Ws3Line[1])
    
if not na(WpLine) and line.get_x2(WpLine) != bar_index
    line.set_x2(Wr1Line, bar_index)
    line.set_x2(WpLine, bar_index)
    line.set_x2(Ws1Line, bar_index)
    line.set_x2(Wr2Line, bar_index)
    line.set_x2(Ws2Line, bar_index)
    line.set_x2(Wr3Line, bar_index)
    line.set_x2(Ws3Line, bar_index)

MchosenColor(c_)=> c_ == "aqua" ? color.aqua : c_ == "black" ? color.black : c_ == "blue" ? color.blue : c_ == "fuchsia" ? color.fuchsia : c_ == "gray" ? color.gray : c_ == "green" ? color.green : c_ == "lime" ? color.lime : c_ == "maroon" ? color.maroon : c_ == "navy" ? color.navy : c_ == "olive" ? color.olive : c_ == "orange" ? color.orange : c_ == "purple" ? color.purple : c_ == "red" ? color.red : c_ == "silver" ? color.silver : c_ == "teal" ? color.teal : c_ == "white" ? color.white : c_ == "yellow" ? color.yellow : color.black
MhigherTF = "M"
Mmy_color = input(title="Monthly Color", defval="green", options=["aqua", "black", "blue", "fuchsia", "gray", "green", "lime", "maroon", "navy", "olive", "orange", "purple", "red", "silver", "teal", "white", "yellow"])
MprevCloseHTF = security(syminfo.tickerid, MhigherTF, close[1], lookahead=true)
MprevOpenHTF = security(syminfo.tickerid, MhigherTF, open[1], lookahead=true)
MprevHighHTF = security(syminfo.tickerid, MhigherTF, high[1], lookahead=true)
MprevLowHTF = security(syminfo.tickerid, MhigherTF, low[1], lookahead=true)

MpLevel = (MprevHighHTF + MprevLowHTF + MprevCloseHTF) / 3
Mr1Level = MpLevel * 2 - MprevLowHTF
Ms1Level = MpLevel * 2 - MprevHighHTF
Mr2Level = MpLevel + (MprevHighHTF - MprevLowHTF)
Ms2Level = MpLevel - (MprevHighHTF - MprevLowHTF)
Mr3Level = MpLevel + 2*(MprevHighHTF - MprevLowHTF)
Ms3Level = MpLevel - 2*(MprevHighHTF - MprevLowHTF)


var line Mr1Line = na
var line MpLine = na
var line Ms1Line = na
var line Mr2Line = na
var line Ms2Line = na
var line Mr3Line = na
var line Ms3Line = na
var line Mm3Line = na

var label Mr3L = na
var label Mr1L = na
var label MpL = na
var label Ms1L = na
var label Ms2L = na
var label Ms3L = na
var label Mr2L = na

if MpLevel[1] != MpLevel
    line.set_x2(Mr1Line, bar_index)
    line.set_x2(MpLine, bar_index)
    line.set_x2(Ms1Line, bar_index)
    line.set_x2(Mr2Line, bar_index)
    line.set_x2(Ms2Line, bar_index)
    line.set_x2(Mr3Line, bar_index)
    line.set_x2(Ms3Line, bar_index)
    line.set_extend(Mr1Line, extend.none)
    line.set_extend(MpLine, extend.none)
    line.set_extend(Ms1Line, extend.none)
    line.set_extend(Mr2Line, extend.none)
    line.set_extend(Ms2Line, extend.none)
    line.set_extend(Mr3Line, extend.none)
    line.set_extend(Ms3Line, extend.none)
    Mr1Line := line.new(bar_index, Mr1Level, bar_index, Mr1Level, width=1, extend=extend.right, color=color.green)
    MpLine := line.new(bar_index, MpLevel, bar_index, MpLevel, width=3, extend=extend.right, color=MchosenColor(Mmy_color))
    Ms1Line := line.new(bar_index, Ms1Level, bar_index, Ms1Level, width=1,extend=extend.right, color=#FF5252)
    Mr2Line := line.new(bar_index, Mr2Level, bar_index, Mr2Level, width=1, extend=extend.right,style=line.style_dotted, color=color.green)
    Ms2Line := line.new(bar_index, Ms2Level, bar_index, Ms2Level, width=1,extend=extend.right, color=#FF5252)
    Mr3Line := line.new(bar_index, Mr3Level, bar_index, Mr3Level, width=1, extend=extend.right,style=line.style_dashed, color=color.green)
    Ms3Line := line.new(bar_index, Ms3Level, bar_index, Ms3Level, width=1,extend=extend.right, color=#FF5252)
    Mr1L := label.new(bar_index, Mr1Level, MhigherTF+".R1", style=label.style_none)
    var label label1 = na
    MpL := label.new(bar_index, MpLevel, MhigherTF+".P", style=label.style_none)
    label.set_textalign(label1, text.align_right)
    Ms1L := label.new(bar_index, Ms1Level, MhigherTF+".S1", style=label.style_none)
    Mr2L := label.new(bar_index, Mr2Level, MhigherTF+".R2", style=label.style_none)
    Ms2L := label.new(bar_index, Ms2Level, MhigherTF+".S2", style=label.style_none)
    Mr3L := label.new(bar_index, Mr3Level, MhigherTF+".R3", style=label.style_none)
    Ms3L := label.new(bar_index, Ms3Level, MhigherTF+".S3", style=label.style_none)
    label.delete(Mr3L[1])
    label.delete(Ms3L[1])
    label.delete(Mr2L[1])
    label.delete(Mr1L[1])
    label.delete(MpL[1])
    label.delete(Ms1L[1])
    label.delete(Ms2L[1])
    line.delete(Mr2Line[1])
    line.delete(Mr1Line[1])
    line.delete(Mm3Line[1])
    line.delete(MpLine[1])
    line.delete(Ms1Line[1])
    line.delete(Ms2Line[1])
    line.delete(Mr3Line[1])
    line.delete(Ms3Line[1])
    
if not na(MpLine) and line.get_x2(MpLine) != bar_index
    line.set_x2(Mr1Line, bar_index)
    line.set_x2(MpLine, bar_index)
    line.set_x2(Ms1Line, bar_index)
    line.set_x2(Mr2Line, bar_index)
    line.set_x2(Ms2Line, bar_index)
    line.set_x2(Mr3Line, bar_index)
    line.set_x2(Ms3Line, bar_index)