// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Anderson_Guilcapi
//@version=4
study(title="|AG| DETECTOR", shorttitle="|AG| DETECTOR", overlay=true)
tim = input( "W", title="TIME FOR DETECTOR")
type_sw = input( "Camarilla", title=" Pivots Calculation", options=["Traditional", "Camarilla", "Standard D." ,"Fibonacci"])
_i = input( 1, title=" Index Back", minval = 0)
type_color = input( "BLACK", title=" Color", options=["AQUA", "BLACK", "BLUE", "FUCHSIA", "GRAY", "GREEN", "PURPLE", "ORANGE", "RED"])
tra1 = input( 0, title=" Transparency")
type_sw1 = input( "PLOT All", title=" PLOT SETTINGS", options=["LOCKBACK", "PLOT All"])
daily_adr = input( title=" Number of Days Back", type=input.integer, defval=7, minval=0)
andy = input( title="Change Candle Colour?", defval = true)
showlabels = input( true, title="Labels")
//SECURITY FOR TIME
High3 = security(syminfo.tickerid, tim, high [_i], barmerge.gaps_off, barmerge.lookahead_on)
Low3 = security(syminfo.tickerid, tim, low [_i], barmerge.gaps_off, barmerge.lookahead_on)
Close3 = security(syminfo.tickerid, tim, close [_i], barmerge.gaps_off, barmerge.lookahead_on)
//CALCULATIONS
r=High3-Low3
pivot = (High3 + Low3 + Close3 ) / 3.0
//Std
hvar = pow(High3 - pivot, 2)
lvar = pow(Low3 - pivot, 2)
cvar = pow(Close3 - pivot, 2)
dev = sqrt((hvar + lvar + cvar) / 3)
//
h51a= (High3/Low3)*Close3
R5 = (Close3 + (High3-Low3)*(1.1/2)) + 1.168 * ((Close3 + (High3-Low3)*(1.1/2)) - (Close3 + (High3-Low3)*(1.1/4)))
S5 = (Close3 - (High3-Low3)*(1.1/2)) - 1.168 * ((Close3 - (High3-Low3)*(1.1/4)) - (Close3 - (High3-Low3)*(1.1/2)))
dtime_r1ab = type_sw == "Traditional" ? pivot + (pivot - Low3) :na
dtime_s1ab = type_sw == "Traditional" ? pivot - (High3 - pivot) :na
dtime_r2ab = type_sw == "Traditional" ? pivot + (High3 - Low3) :na
dtime_s2ab = type_sw == "Traditional" ? pivot - (High3 - Low3) :na
dtime_r3ab = type_sw == "Traditional" ? (pivot * 2 + (High3 - 2 * Low3)) : type_sw == "Camarilla" ? (Close3 + r*(1.1/4)) : type_sw == "Fibonacci" ? (pivot + ((pivot - Low3)*1.000)) : type_sw == "Standard D." ? (pivot + 2 * dev) :na
dtime_s3ab = type_sw == "Traditional" ? (pivot * 2 - (2 * High3 - Low3)) : type_sw == "Camarilla" ? (Close3 - r*(1.1/4)) : type_sw == "Fibonacci" ? (pivot - ((High3 - pivot)*1.000)) : type_sw == "Standard D." ? (pivot - 2 * dev) :na
dtime_r4ab = type_sw == "Traditional" ? (pivot * 3 + (High3 - 3 * Low3)) : type_sw == "Camarilla" ? (Close3 + r*(1.1/2)) : type_sw == "Fibonacci" ? (pivot + ((High3 - pivot)*1.272)) : type_sw == "Standard D." ? (pivot + 3 * dev) :na
dtime_s4ab = type_sw == "Traditional" ? (pivot * 3 - (3 * High3 - Low3)) : type_sw == "Camarilla" ? (Close3 - r*(1.1/2)) : type_sw == "Fibonacci" ? (pivot - ((High3 - pivot)*1.272)) : type_sw == "Standard D." ? (pivot - 3 * dev) :na
dtime_r5ab = type_sw == "Traditional" ? (pivot * 4 + (High3 - 4 * Low3)) : type_sw == "Camarilla" ? R5 : type_sw == "Fibonacci" ? (pivot + ((High3 - pivot)*1.618)) : type_sw == "Standard D." ? (pivot + 4 * dev) :na
dtime_s5ab = type_sw == "Traditional" ? (pivot * 4 - (4 * High3 - Low3)) : type_sw == "Camarilla" ? S5 : type_sw == "Fibonacci" ? (pivot - ((High3 - pivot)*1.618)) : type_sw == "Standard D." ? (pivot - 4 * dev) :na
dtime_r6ab = h51a
dtime_s6ab = Close3 - (h51a-Close3)
//COLOR VARIABLES
colorag = type_color == "AQUA" ? color.aqua: type_color == "BLACK" ? color.black: type_color == "BLUE" ? color.blue : type_color == "FUCHSIA" ? color.fuchsia : type_color == "GRAY" ? color.gray : type_color == "GREEN" ? color.green : type_color == "PURPLE" ? color.purple : type_color == "ORANGE" ? color.orange : type_color == "RED" ? color.red : na
//LOCKBACK DAYS
new_bar(res) =>
change(time(res)) != 0
new_period(condition, src) =>
result = 0.0
result := condition ? src : result[1]
result
one_day = 1000 * 60 * 60 * 24
new_day = daily_adr > 0 and timenow - time < one_day * daily_adr and new_bar("D")
High1 = new_period(new_day, High3)
ij2 = type_sw1 == "LOCKBACK" ? (timeframe.isintraday or timeframe.isdaily ? High1 : na) : type_sw1 == "PLOT All" ? true : na
diff = iff(type_sw=="Camarilla" or type_sw=="Fibonacci" or type_sw=="Standard D.", true, false )
//RESISTANCE
HTFo = iff((close<= dtime_r6ab) and (close>=dtime_r5ab) and not (close<dtime_r5ab) , dtime_r6ab, na)
HTFo1 = iff((close<= dtime_r6ab) and (close>=dtime_r5ab) and not (close<dtime_r5ab) , dtime_r5ab, na)
HTFo2 = iff((close<= dtime_r5ab) and (close>=dtime_r4ab) and not (close<dtime_r4ab) and not (close>dtime_r5ab) , dtime_r5ab, na)
HTFo3 = iff((close<= dtime_r5ab) and (close>=dtime_r4ab) and not (close<dtime_r4ab) and not (close>dtime_r5ab) , dtime_r4ab, na)
HTFo4 = iff((close<= dtime_r4ab) and (close>=dtime_r3ab) and not (close<dtime_r3ab) and not (close>dtime_r4ab) , dtime_r4ab, na)
HTFo5 = iff((close<= dtime_r4ab) and (close>=dtime_r3ab) and not (close<dtime_r3ab) and not (close>dtime_r4ab) , dtime_r3ab, na)
HTFo6 = iff(diff , iff((close<= dtime_r3ab) and (close>=dtime_s3ab) and not (close<dtime_s3ab) and not (close>dtime_r3ab) , dtime_r3ab, na), iff((close<= dtime_r3ab) and (close>=dtime_r2ab) and not (close<dtime_r2ab) and not (close>dtime_r3ab) , dtime_r3ab, na))
/////////////////////////////////////////////////////////FOR Traditional S&R ////////////////////////////////////////////////////////////////////
//
HTFo6a = iff(diff, na, iff((close<= dtime_r3ab) and (close>=dtime_r2ab) and not (close<dtime_r2ab) and not (close>dtime_r3ab) , dtime_r2ab, na))
HTFo7a = iff(diff, na, iff((close<= dtime_r2ab) and (close>=dtime_r1ab) and not (close<dtime_r1ab) and not (close>dtime_r2ab) , dtime_r2ab, na))
HTFo8a = iff(diff, na, iff((close<= dtime_r2ab) and (close>=dtime_r1ab) and not (close<dtime_r1ab) and not (close>dtime_r2ab) , dtime_r1ab, na))
HTFo9a = iff(diff, na, iff((close<= dtime_r1ab) and (close>=pivot ) and not (close<pivot ) and not (close>dtime_r1ab) , dtime_r1ab, na))
HTFo6a1 = iff(diff, na, iff((close>= dtime_s1ab) and (close<=pivot ) and not (close>pivot ) and not (close<dtime_s1ab) , dtime_s1ab, na))
HTFo7a1 = iff(diff, na, iff((close>= dtime_s2ab) and (close<=dtime_s1ab) and not (close>dtime_s1ab) and not (close<dtime_s2ab) , dtime_s1ab, na))
HTFo8a1 = iff(diff, na, iff((close>= dtime_s2ab) and (close<=dtime_s1ab) and not (close>dtime_s1ab) and not (close<dtime_s2ab) , dtime_s2ab, na))
HTFo9a1 = iff(diff, na, iff((close>= dtime_s3ab) and (close<=dtime_s2ab) and not (close>dtime_s2ab) and not (close<dtime_s3ab) , dtime_s2ab, na))
//PIVOT
HTFo10a = iff(diff, na, pivot )
//Supports
HTFo7 = iff(diff , iff((close>= dtime_s3ab) and (close<=dtime_r3ab) and not (close>dtime_r3ab) and not (close<dtime_s3ab) , dtime_s3ab, na), iff((close>= dtime_s3ab) and (close<=dtime_s2ab) and not (close>dtime_s2ab) and not (close<dtime_s3ab) , dtime_s3ab, na))
HTFo8 = iff((close>= dtime_s4ab) and (close<=dtime_s3ab) and not (close>dtime_s3ab) and not (close<dtime_s4ab) , dtime_s3ab, na)
HTFo9 = iff((close>= dtime_s4ab) and (close<=dtime_s3ab) and not (close>dtime_s3ab) and not (close<dtime_s4ab) , dtime_s4ab, na)
HTFo10 = iff((close>= dtime_s5ab) and (close<=dtime_s4ab) and not (close>dtime_s4ab) and not (close<dtime_s5ab) , dtime_s4ab, na)
HTFo11 = iff((close>= dtime_s5ab) and (close<=dtime_s4ab) and not (close>dtime_s4ab) and not (close<dtime_s5ab) , dtime_s5ab, na)
HTFo12 = iff((close>= dtime_s6ab) and (close<=dtime_s5ab) and not (close>dtime_s5ab) , dtime_s5ab, na)
HTFo13 = iff((close>= dtime_s6ab) and (close<=dtime_s5ab) and not (close>dtime_s5ab) , dtime_s6ab, na)
//PLOT CHAR
plotchar(ij2 and HTFo ? HTFo :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo1 ? HTFo1 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo2 ? HTFo2 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo3 ? HTFo3 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo4 ? HTFo4 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo5 ? HTFo5 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo6 ? HTFo6 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo7 ? HTFo7 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo8 ? HTFo8 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo9 ? HTFo9 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo10 ? HTFo10 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo11 ? HTFo11 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo12 ? HTFo12 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo13 ? HTFo13 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo6a ? HTFo6a :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo7a ? HTFo7a :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo8a ? HTFo8a :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo9a ? HTFo9a :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo10a ? HTFo10a :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo6a1 ? HTFo6a1 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo7a1 ? HTFo7a1 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo8a1 ? HTFo8a1 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
plotchar(ij2 and HTFo9a1 ? HTFo9a1 :na ,char="□", location=location.absolute, size=size.tiny, color=colorag, transp=tra1)
//BAR COLOR CHANGE DEFVAL 720 (12 HOURS)
High1a = security(syminfo.tickerid, "720", high[1], barmerge.gaps_off, barmerge.lookahead_on)
Low1a = security(syminfo.tickerid, "720", low[1], barmerge.gaps_off, barmerge.lookahead_on)
Close1a = security(syminfo.tickerid, "720", close[1], barmerge.gaps_off, barmerge.lookahead_on)
king = (High1a + Low1a + Close1a ) / 3.0
barcolor(iff(andy, close>king ? colorag : color.black, na))
//LABELS
tim1 = iff(tim=="D", "DAILY", iff(tim=="W", "WEEKLY", iff(tim=="M", "MONTHLY", iff(tim=="3M", "QUATERLY", iff(tim=="12M", "YEARLY", iff(tim=="6M", "HALF-YEAR", na))))))
calculation = iff(type_sw=="Fibonacci","Fibonacci", iff(type_sw=="Camarilla", "Camarilla", iff(type_sw=="Traditional", "Traditional", iff(type_sw=="Standard D.", "Standard D.", na))))
// Label for S/R
chper = time - time[1]
chper := change(chper) > 0 ? chper[1] : chper
_COLOR = type_color == "BLACK" ? color.white : color.black
// Labels
if showlabels
var label s1blabel = na, var label s1alabel = na, var label s2blabel = na, var label s2alabel = na, var label s3blabel = na, var label s3alabel = na, var label s4blabel = na, var label s4alabel = na, var label s5blabel = na, var label s5alabel = na, var label s6alabel = na, var label pvilabel = na
var label r1blabel = na, var label r1alabel = na, var label r2blabel = na, var label r2alabel = na, var label r3blabel = na, var label r3alabel = na, var label r4blabel = na, var label r4alabel = na, var label r5blabel = na, var label r5alabel = na, var label r6alabel = na
label.delete(s1blabel), label.delete(s1alabel), label.delete(s2blabel), label.delete(s2alabel), label.delete(s3blabel), label.delete(s3alabel), label.delete(s4blabel), label.delete(s4alabel), label.delete(s5blabel), label.delete(s5alabel), label.delete(s6alabel), label.delete(pvilabel)
label.delete(r1blabel), label.delete(r1alabel), label.delete(r2blabel), label.delete(r2alabel), label.delete(r3blabel), label.delete(r3alabel), label.delete(r4blabel), label.delete(r4alabel), label.delete(r5blabel), label.delete(r5alabel), label.delete(r6alabel)
r6alabel := label.new(x = time + chper * 20, y = HTFo , text = "R6= " + tostring(dtime_r6ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r5alabel := label.new(x = time + chper * 20, y = HTFo1 , text = iff(type_sw=="Standard D.", "R4= ","R5= ") + tostring(dtime_r5ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r5blabel := label.new(x = time + chper * 20, y = HTFo2 , text = iff(type_sw=="Standard D.", "R4= ","R5= ") + tostring(dtime_r5ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r4alabel := label.new(x = time + chper * 20, y = HTFo3 , text = iff(type_sw=="Standard D.", "R3= ","R4= ") + tostring(dtime_r4ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r4blabel := label.new(x = time + chper * 20, y = HTFo4 , text = iff(type_sw=="Standard D.", "R3= ","R4= ") + tostring(dtime_r4ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r3alabel := label.new(x = time + chper * 20, y = HTFo5 , text = iff(type_sw=="Standard D.", "R2= ","R3= ") + tostring(dtime_r3ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r3blabel := label.new(x = time + chper * 20, y = HTFo6 , text = iff(type_sw=="Standard D.", "R2= ","R3= ") + tostring(dtime_r3ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r2alabel := label.new(x = time + chper * 20, y = HTFo6a , text = "R2= " + tostring(dtime_r2ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r2blabel := label.new(x = time + chper * 20, y = HTFo7a , text = "R2= " + tostring(dtime_r2ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r1alabel := label.new(x = time + chper * 20, y = HTFo8a , text = "R1= " + tostring(dtime_r1ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
r1blabel := label.new(x = time + chper * 20, y = HTFo9a , text = "R1= " + tostring(dtime_r1ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
pvilabel := label.new(x = time + chper * 20, y = HTFo10a, text = "P = " + tostring(pivot , "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s1blabel := label.new(x = time + chper * 20, y = HTFo6a1, text = "S1= " + tostring(dtime_s1ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s1alabel := label.new(x = time + chper * 20, y = HTFo7a1, text = "S1= " + tostring(dtime_s1ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s2blabel := label.new(x = time + chper * 20, y = HTFo8a1, text = "S2= " + tostring(dtime_s2ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s2alabel := label.new(x = time + chper * 20, y = HTFo9a1, text = "S2= " + tostring(dtime_s2ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s3blabel := label.new(x = time + chper * 20, y = HTFo7 , text = iff(type_sw=="Standard D.", "S2= ", "S3= ") + tostring(dtime_s3ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s3alabel := label.new(x = time + chper * 20, y = HTFo8 , text = iff(type_sw=="Standard D.", "S2= ", "S3= ") + tostring(dtime_s3ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s4blabel := label.new(x = time + chper * 20, y = HTFo9 , text = iff(type_sw=="Standard D.", "S3= ", "S4= ") + tostring(dtime_s4ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s4alabel := label.new(x = time + chper * 20, y = HTFo10 , text = iff(type_sw=="Standard D.", "S3= ", "S4= ") + tostring(dtime_s4ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s5blabel := label.new(x = time + chper * 20, y = HTFo11 , text = iff(type_sw=="Standard D.", "S4= ", "S5= ") + tostring(dtime_s5ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s5alabel := label.new(x = time + chper * 20, y = HTFo12 , text = iff(type_sw=="Standard D.", "S4= ", "S5= ") + tostring(dtime_s5ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
s6alabel := label.new(x = time + chper * 20, y = HTFo13 , text = "S6= " + tostring(dtime_s6ab, "#.####") + "\n" + tim1 + "\n" + calculation, color = colorag, textcolor=_COLOR, style=label.style_label_left, xloc = xloc.bar_time, yloc=yloc.price)
Show all 162 lines