TL BP/IP Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JaRoRu

//@version=5
indicator(title="TL BP/IP Levels", shorttitle="BP/IP", overlay=true)

currsym = input.string( "US500", "Symbol")
BP = input.int(4660, minval=1, title="BP")
IP1 = input.int(4800, minval=0, title="IP 1")
IP2 = input.int(4700, minval=0, title="IP 2")
IP3 = input.int(4300, minval=0, title="IP 3")
IP4 = input.int(4200, minval=0, title="IP 4")
IP5 = input.int(4100, minval=0, title="IP 5")
BL = input.int(3, minval=0, title="Plot # Bars to the left")
BR = input.int(2, minval=0, title="Plot # Bars to the right")
IM = input.int(30, minval=1, title="Intrday Line Length Multiplier")
BP_col = input.color(color.blue, "BP Color")
BP_line = input.string(title="BP Line Type", defval=line.style_solid, options=[line.style_solid, line.style_dotted, line.style_dashed])
IP_col = input.color(color.blue, "IP Color")
IP_line = input.string(title="IP Line Type", defval=line.style_dotted, options=[line.style_solid, line.style_dotted, line.style_dashed])

if(timeframe.isintraday)
    BL *= IM
    BR *= IM/10

var line0 = line.new(last_bar_index-BL, BP, last_bar_index+BR, BP, color=BP_col, style=BP_line, width=4, extend=extend.none)

var line1 = line.new(last_bar_index-BL, IP1, last_bar_index+BR, IP1, color=IP_col, style=IP_line, width=3, extend=extend.none)

var line2 = line.copy(line1)
line.set_xy1(line2, last_bar_index-BL, IP2)
line.set_xy2(line2, last_bar_index+BR, IP2)

var line3 = line.copy(line1)
line.set_xy1(line3, last_bar_index-BL, IP3)
line.set_xy2(line3, last_bar_index+BR, IP3)

var line4 = line.copy(line1)
line.set_xy1(line4, last_bar_index-BL, IP4)
line.set_xy2(line4, last_bar_index+BR, IP4)

var line5 = line.copy(line1)
line.set_xy1(line5, last_bar_index-BL, IP5)
line.set_xy2(line5, last_bar_index+BR, IP5)

var PTable = table.new(position = position.bottom_center, columns = 6, rows = 1, bgcolor = color.gray, border_width = 1)
table.cell(table_id = PTable, column = 0, row = 0, text_size=size.auto, text_color=color.white, text = "BP " + str.tostring(BP))
table.cell(table_id = PTable, column = 1, row = 0, text_size=size.auto, text_color=color.white, text = "IP1 " + str.tostring(IP1))
table.cell(table_id = PTable, column = 2, row = 0, text_size=size.auto, text_color=color.white, text = "IP2 " + str.tostring(IP2))
table.cell(table_id = PTable, column = 3, row = 0, text_size=size.auto, text_color=color.white, text = "IP3 " + str.tostring(IP3))
table.cell(table_id = PTable, column = 4, row = 0, text_size=size.auto, text_color=color.white, text = "IP4 " + str.tostring(IP4))
table.cell(table_id = PTable, column = 5, row = 0, text_size=size.auto, text_color=color.white, text = "IP5 " + str.tostring(IP5))

//Only show BP/IP Levels on specific ticker symbol
if(currsym != syminfo.ticker)
    line.delete(line0)
    line.delete(line1)
    line.delete(line2)
    line.delete(line3)
    line.delete(line4)
    line.delete(line5)
    table.delete(PTable)
    
if (IP3 == 0)
    line.delete(line3)
    line.delete(line4)
    line.delete(line5)
else if(IP4 == 0)
    line.delete(line4)
    line.delete(line5)
else if (IP5 == 0)
    line.delete(line5)
    
    
//Automatic Alerts for close crossing BP and IP levels
BPO = ta.crossover(close,BP) 
BPU = ta.crossunder(close,BP)
IP1O = ta.crossover(close,IP1)
IP1U = ta.crossunder(close,IP1)
IP2O = ta.crossover(close,IP2)
IP2U = ta.crossunder(close,IP2)
IP3O = ta.crossover(close,IP3)
IP3U = ta.crossunder(close,IP3)
IP4O = ta.crossover(close,IP4)
IP4U = ta.crossunder(close,IP4)
IP5O = ta.crossover(close,IP5)
IP5U = ta.crossunder(close,IP5)

if(BPO)
    alert("US500 (" + str.tostring(close) + ") crossed over BP (" + str.tostring(BP) +  ")", alert.freq_once_per_bar)
else if(BPU)
    alert("US500 (" + str.tostring(close) + ") crossed under BP (" + str.tostring(BP) +  ")", alert.freq_once_per_bar)

if(IP1O)
    alert("US500 (" + str.tostring(close) + ") crossed over IP1 (" + str.tostring(IP1) +  ")", alert.freq_once_per_bar)
else if(IP1U)
    alert("US500 (" + str.tostring(close) + ") crossed under IP1 (" + str.tostring(IP1) +  ")", alert.freq_once_per_bar)
    
if(IP2O)
    alert("US500 (" + str.tostring(close) + ") crossed over IP2 (" + str.tostring(IP2) +  ")", alert.freq_once_per_bar)
else if(IP2U)
    alert("US500 (" + str.tostring(close) + ") crossed under IP2 (" + str.tostring(IP2) +  ")", alert.freq_once_per_bar)
    
if(IP3O)
    alert("US500 (" + str.tostring(close) + ") crossed over IP3 (" + str.tostring(IP3) +  ")", alert.freq_once_per_bar)
else if(IP3U)
    alert("US500 (" + str.tostring(close) + ") crossed under IP3 (" + str.tostring(IP3) +  ")", alert.freq_once_per_bar)
    
if(IP4O)
    alert("US500 (" + str.tostring(close) + ") crossed over IP4 (" + str.tostring(IP4) +  ")", alert.freq_once_per_bar)
else if(IP4U)
    alert("US500 (" + str.tostring(close) + ") crossed under IP4 (" + str.tostring(IP4) +  ")", alert.freq_once_per_bar)
    
if(IP5O)
    alert("US500 (" + str.tostring(close) + ") crossed over IP5 (" + str.tostring(IP5) +  ")", alert.freq_once_per_bar)
else if(IP5U)
    alert("US500 (" + str.tostring(close) + ") crossed under IP5 (" + str.tostring(IP5) +  ")", alert.freq_once_per_bar)
    

//Setup custom alerts for price crossovers
alertcondition(BPO, title='BP Cross Over', message='BP Cross Over')
alertcondition(BPU, title='BP Cross Under', message='BP Cross Under')
alertcondition(IP1O, title='IP1 Cross Over', message='IP1 Cross Over')
alertcondition(IP1U, title='IP1 Cross Under', message='IP1 Cross Under')
alertcondition(IP2O, title='IP2 Cross Over', message='IP2 Cross Over')
alertcondition(IP2U, title='IP2 Cross Under', message='IP2 Cross Under')
alertcondition(IP3O, title='IP3 Cross Over', message='IP3 Cross Over')
alertcondition(IP3U, title='IP3 Cross Under', message='IP3 Cross Under')
alertcondition(IP4O, title='IP4 Cross Over', message='IP4 Cross Over')
alertcondition(IP4U, title='IP4 Cross Under', message='IP4 Cross Under')
alertcondition(IP5O, title='IP5 Cross Over', message='IP5 Cross Over')
alertcondition(IP5U, title='IP5 Cross Under', message='IP5 Cross Under')