Supertrend Explorer First_40
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KivancOzbilgic
//derived the screener logic from @ zzzcrypto123
//@version=4


study("Supertrend Explorer", shorttitle="StEx", overlay=true)
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
t1 = input(title="BIST:QNBFB", defval="BIST:QNBFB")
t2 = input(title="BIST:EREGL", defval="BIST:EREGL")
t3 = input(title="BIST:KCHOL", defval="BIST:KCHOL")
t4 = input(title="BIST:FROTO", defval="BIST:FROTO")
t5 = input(title="BIST:ENKAI", defval="BIST:ENKAI")
t6 = input(title="BIST:ISDMR", defval="BIST:ISDMR")
t7 = input(title="BIST:SASA", defval="BIST:SASA")
t8 = input(title="BIST:ASELS", defval="BIST:ASELS")
t9 = input(title="BIST:GARAN", defval="BIST:GARAN")
t10 = input(title="BIST:TUPRS", defval="BIST:TUPRS")
t11 = input(title="BIST:TOASO", defval="BIST:TOASO")
t12 = input(title="BIST:SISE", defval="BIST:SISE")
t13 = input(title="BIST:TCELL", defval="BIST:TCELL")
t14 = input(title="BIST:BIMAS", defval="BIST:BIMAS")
t15 = input(title="BIST:KENT", defval="BIST:KENT")
t16 = input(title="BIST:AKBNK", defval="BIST:AKBNK")
t17 = input(title="BIST:TTKOM", defval="BIST:TTKOM")
t18 = input(title="BIST:ISCTR", defval="BIST:ISCTR")
t19 = input(title="BIST:ISBTR", defval="BIST:ISBTR")
t20 = input(title="BIST:ISKUR", defval="BIST:ISKUR")
t21 = input(title="BIST:ARCLK", defval="BIST:ARCLK")
t22 = input(title="BIST:KLNMA", defval="BIST:KLNMA")
t23 = input(title="BIST:THYAO", defval="BIST:THYAO")
t24 = input(title="BIST:YKBNK", defval="BIST:YKBNK")
t25 = input(title="BIST:SAHOL", defval="BIST:SAHOL")
t26 = input(title="BIST:GUBRF", defval="BIST:GUBRF")
t27 = input(title="BIST:CCOLA", defval="BIST:CCOLA")
t28 = input(title="BIST:PETKM", defval="BIST:PETKM")
t29 = input(title="BIST:KOZAL", defval="BIST:KOZAL")
t30 = input(title="BIST:AEFES", defval="BIST:AEFES")
t31 = input(title="BIST:ENJSA", defval="BIST:ENJSA")
t32 = input(title="BIST:VAKBN", defval="BIST:VAKBN")
t33 = input(title="BIST:PRKAB", defval="BIST:PRKAB")
t34 = input(title="BIST:HEKTS", defval="BIST:HEKTS")
t35 = input(title="BIST:AKSEN", defval="BIST:AKSEN")
t36 = input(title="BIST:TAVHL", defval="BIST:TAVHL")
t37 = input(title="BIST:VESBE", defval="BIST:VESBE")
t38 = input(title="BIST:DOCO", defval="BIST:DOCO")
t39 = input(title="BIST:TTRAK", defval="BIST:TTRAK")
t40 = input(title="BIST:AKSA", defval="BIST:AKSA")


//transparency changed
var TRANSP = 40
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.green, TRANSP))
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, TRANSP), textcolor=color.white)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(color.red, TRANSP))
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, TRANSP), textcolor=color.white)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")
changeCond = trend != trend[1]
alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")
showscr = input(true, title="Show Screener Label")
posX_scr = input(20, title="Pos. Label x-axis")
posY_scr = input(1, title="Pos. Size Label y-axis")
colinput = input(title="Label Color", defval="Blue", options=["White", "Black", "Red", "Green", "Yellow", "Blue"])
col = color.gray
if colinput=="White"
    col:=color.white
if colinput=="Black"
    col:=color.black
if colinput=="Red"
    col:=color.red
if colinput=="Green"
    col:=color.green
if colinput=="Yellow"
    col:=color.yellow
if colinput=="Blue"
    col:=color.blue
    
Supertrend(Multiplier, Periods) =>
    Up=hl2-(Multiplier*atr)
    Dn=hl2+(Multiplier*atr)
    
    TrendUp = 0.0
    TrendUp := close[1]>TrendUp[1] ? max(Up,TrendUp[1]) : Up
    TrendDown = 0.0
    TrendDown := close[1]<TrendDown[1]? min(Dn,TrendDown[1]) : Dn
    Trend = 0.0
    Trend := close > TrendDown[1] ? 1: close< TrendUp[1]? -1: nz(Trend[1],1)
    Tsl = Trend==1? TrendUp: TrendDown
    
    S_Buy = Trend == 1 ? 1 : 0
    S_Sell = Trend != 1 ? 1 : 0
    
    [Trend, Tsl]

[Trend, Tsl] = Supertrend(Multiplier, Periods)
TrendReversal = Trend != Trend[1]

[t01, s01] = security(t1, timeframe.period, Supertrend(Multiplier, Periods))
[t02, s02] = security(t2, timeframe.period, Supertrend(Multiplier, Periods))
[t03, s03] = security(t3, timeframe.period, Supertrend(Multiplier, Periods))
[t04, s04] = security(t4, timeframe.period, Supertrend(Multiplier, Periods))
[t05, s05] = security(t5, timeframe.period, Supertrend(Multiplier, Periods))
[t06, s06] = security(t6, timeframe.period, Supertrend(Multiplier, Periods))
[t07, s07] = security(t7, timeframe.period, Supertrend(Multiplier, Periods))
[t08, s08] = security(t8, timeframe.period, Supertrend(Multiplier, Periods))
[t09, s09] = security(t9, timeframe.period, Supertrend(Multiplier, Periods))
[t010, s010] = security(t10, timeframe.period, Supertrend(Multiplier, Periods))
[t011, s011] = security(t11, timeframe.period, Supertrend(Multiplier, Periods))
[t012, s012] = security(t12, timeframe.period, Supertrend(Multiplier, Periods))
[t013, s013] = security(t13, timeframe.period, Supertrend(Multiplier, Periods))
[t014, s014] = security(t14, timeframe.period, Supertrend(Multiplier, Periods))
[t015, s015] = security(t15, timeframe.period, Supertrend(Multiplier, Periods))
[t016, s016] = security(t16, timeframe.period, Supertrend(Multiplier, Periods))
[t017, s017] = security(t17, timeframe.period, Supertrend(Multiplier, Periods))
[t018, s018] = security(t18, timeframe.period, Supertrend(Multiplier, Periods))
[t019, s019] = security(t19, timeframe.period, Supertrend(Multiplier, Periods))
[t020, s020] = security(t20, timeframe.period, Supertrend(Multiplier, Periods))
[t021, s021] = security(t21, timeframe.period, Supertrend(Multiplier, Periods))
[t022, s022] = security(t22, timeframe.period, Supertrend(Multiplier, Periods))
[t023, s023] = security(t23, timeframe.period, Supertrend(Multiplier, Periods))
[t024, s024] = security(t24, timeframe.period, Supertrend(Multiplier, Periods))
[t025, s025] = security(t25, timeframe.period, Supertrend(Multiplier, Periods))
[t026, s026] = security(t26, timeframe.period, Supertrend(Multiplier, Periods))
[t027, s027] = security(t27, timeframe.period, Supertrend(Multiplier, Periods))
[t028, s028] = security(t28, timeframe.period, Supertrend(Multiplier, Periods))
[t029, s029] = security(t29, timeframe.period, Supertrend(Multiplier, Periods))
[t030, s030] = security(t30, timeframe.period, Supertrend(Multiplier, Periods))
[t031, s031] = security(t31, timeframe.period, Supertrend(Multiplier, Periods))
[t032, s032] = security(t32, timeframe.period, Supertrend(Multiplier, Periods))
[t033, s033] = security(t33, timeframe.period, Supertrend(Multiplier, Periods))
[t034, s034] = security(t34, timeframe.period, Supertrend(Multiplier, Periods))
[t035, s035] = security(t35, timeframe.period, Supertrend(Multiplier, Periods))
[t036, s036] = security(t36, timeframe.period, Supertrend(Multiplier, Periods))
[t037, s037] = security(t37, timeframe.period, Supertrend(Multiplier, Periods))
[t038, s038] = security(t38, timeframe.period, Supertrend(Multiplier, Periods))
[t039, s039] = security(t39, timeframe.period, Supertrend(Multiplier, Periods))
[t040, s040] = security(t40, timeframe.period, Supertrend(Multiplier, Periods))


//** tr01 = t01 != t01[1], up01 = t01 == 1, dn01 = t01 == -1
tr01 = t01 != t01[1], up01 = t01 == 1, dn01 = t01 == -1
tr02 = t02 != t02[1], up02 = t02 == 1, dn02 = t02 == -1
tr03 = t03 != t03[1], up03 = t03 == 1, dn03 = t03 == -1
tr04 = t04 != t04[1], up04 = t04 == 1, dn04 = t04 == -1
tr05 = t05 != t05[1], up05 = t05 == 1, dn05 = t05 == -1
tr06 = t06 != t06[1], up06 = t06 == 1, dn06 = t06 == -1
tr07 = t07 != t07[1], up07 = t07 == 1, dn07 = t07 == -1
tr08 = t08 != t08[1], up08 = t08 == 1, dn08 = t08 == -1
tr09 = t09 != t09[1], up09 = t09 == 1, dn09 = t09 == -1
tr10 = t010 != t010[1], up10 = t010 == 1, dn10 = t010 == -1
tr11 = t011 != t011[1], up11 = t011 == 1, dn11 = t011 == -1
tr12 = t012 != t012[1], up12 = t012 == 1, dn12 = t012 == -1
tr13 = t013 != t013[1], up13 = t013 == 1, dn13 = t013 == -1
tr14 = t014 != t014[1], up14 = t014 == 1, dn14 = t014 == -1
tr15 = t015 != t015[1], up15 = t015 == 1, dn15 = t015 == -1
tr16 = t016 != t016[1], up16 = t016 == 1, dn16 = t016 == -1
tr17 = t017 != t017[1], up17 = t017 == 1, dn17 = t017 == -1
tr18 = t018 != t018[1], up18 = t018 == 1, dn18 = t018 == -1
tr19 = t019 != t019[1], up19 = t019 == 1, dn19 = t019 == -1
tr20 = t020 != t020[1], up20 = t020 == 1, dn20 = t020 == -1
tr21 = t021 != t021[1], up21 = t021 == 1, dn21 = t021 == -1
tr22 = t022 != t022[1], up22 = t022 == 1, dn22 = t022 == -1
tr23 = t023 != t023[1], up23 = t023 == 1, dn23 = t023 == -1
tr24 = t024 != t024[1], up24 = t024 == 1, dn24 = t024 == -1
tr25 = t025 != t025[1], up25 = t025 == 1, dn25 = t025 == -1
tr26 = t026 != t026[1], up26 = t026 == 1, dn26 = t026 == -1
tr27 = t027 != t027[1], up27 = t027 == 1, dn27 = t027 == -1
tr28 = t028 != t028[1], up28 = t028 == 1, dn28 = t028 == -1
tr29 = t029 != t029[1], up29 = t029 == 1, dn29 = t029 == -1
tr30 = t030 != t030[1], up30 = t030 == 1, dn30 = t030 == -1
tr31 = t031 != t031[1], up31 = t031 == 1, dn31 = t031 == -1
tr32 = t032 != t032[1], up32 = t032 == 1, dn32 = t032 == -1
tr33 = t033 != t033[1], up33 = t033 == 1, dn33 = t033 == -1
tr34 = t034 != t034[1], up34 = t034 == 1, dn34 = t034 == -1
tr35 = t035 != t035[1], up35 = t035 == 1, dn35 = t035 == -1
tr36 = t036 != t036[1], up36 = t036 == 1, dn36 = t036 == -1
tr37 = t037 != t037[1], up37 = t037 == 1, dn37 = t037 == -1
tr38 = t038 != t038[1], up38 = t038 == 1, dn38 = t038 == -1
tr39 = t039 != t039[1], up39 = t039 == 1, dn39 = t039 == -1
tr40 = t040 != t040[1], up40 = t040 == 1, dn40 = t040 == -1





pot_label = 'Potential Reversal: \n'
pot_label := tr01    ? pot_label + t1 + '\n'  : pot_label
pot_label := tr02    ? pot_label + t2 + '\n'  : pot_label
pot_label := tr03    ? pot_label + t3 + '\n'  : pot_label
pot_label := tr04    ? pot_label + t4 + '\n'  : pot_label
pot_label := tr05    ? pot_label + t5 + '\n'  : pot_label
pot_label := tr06    ? pot_label + t6 + '\n'  : pot_label
pot_label := tr07    ? pot_label + t7 + '\n'  : pot_label
pot_label := tr08    ? pot_label + t8 + '\n'  : pot_label
pot_label := tr09    ? pot_label + t9 + '\n'  : pot_label
pot_label := tr10    ? pot_label + t10 + '\n'  : pot_label
pot_label := tr11    ? pot_label + t11 + '\n'  : pot_label
pot_label := tr12    ? pot_label + t12 + '\n'  : pot_label
pot_label := tr13    ? pot_label + t13 + '\n'  : pot_label
pot_label := tr14    ? pot_label + t14 + '\n'  : pot_label
pot_label := tr15    ? pot_label + t15 + '\n'  : pot_label
pot_label := tr16    ? pot_label + t16 + '\n'  : pot_label
pot_label := tr17    ? pot_label + t17 + '\n'  : pot_label
pot_label := tr18    ? pot_label + t18 + '\n'  : pot_label
pot_label := tr19    ? pot_label + t19 + '\n'  : pot_label
pot_label := tr20    ? pot_label + t20 + '\n'  : pot_label
pot_label := tr21    ? pot_label + t21 + '\n'  : pot_label
pot_label := tr22    ? pot_label + t22 + '\n'  : pot_label
pot_label := tr23    ? pot_label + t23 + '\n'  : pot_label
pot_label := tr24    ? pot_label + t24 + '\n'  : pot_label
pot_label := tr25    ? pot_label + t25 + '\n'  : pot_label
pot_label := tr26    ? pot_label + t26 + '\n'  : pot_label
pot_label := tr27    ? pot_label + t27 + '\n'  : pot_label
pot_label := tr28    ? pot_label + t28 + '\n'  : pot_label
pot_label := tr29    ? pot_label + t29 + '\n'  : pot_label
pot_label := tr30    ? pot_label + t30 + '\n'  : pot_label
pot_label := tr31    ? pot_label + t31 + '\n'  : pot_label
pot_label := tr32    ? pot_label + t32 + '\n'  : pot_label
pot_label := tr33    ? pot_label + t33 + '\n'  : pot_label
pot_label := tr34    ? pot_label + t34 + '\n'  : pot_label
pot_label := tr35    ? pot_label + t35 + '\n'  : pot_label
pot_label := tr36    ? pot_label + t36 + '\n'  : pot_label
pot_label := tr37    ? pot_label + t37 + '\n'  : pot_label
pot_label := tr38    ? pot_label + t38 + '\n'  : pot_label
pot_label := tr39    ? pot_label + t39 + '\n'  : pot_label
pot_label := tr40    ? pot_label + t40 + '\n'  : pot_label





scr_label = 'Confirmed Reversal: \n'
scr_label := tr01[1] ? scr_label + t1 + '\n'  : scr_label
scr_label := tr02[1] ? scr_label + t2 + '\n'  : scr_label
scr_label := tr03[1] ? scr_label + t3 + '\n'  : scr_label
scr_label := tr04[1] ? scr_label + t4 + '\n'  : scr_label
scr_label := tr05[1] ? scr_label + t5 + '\n'  : scr_label
scr_label := tr06[1] ? scr_label + t6 + '\n'  : scr_label
scr_label := tr07[1] ? scr_label + t7 + '\n'  : scr_label
scr_label := tr08[1] ? scr_label + t8 + '\n'  : scr_label
scr_label := tr09[1] ? scr_label + t9 + '\n'  : scr_label
scr_label := tr10[1] ? scr_label + t10 + '\n'  : scr_label
scr_label := tr11[1] ? scr_label + t11 + '\n'  : scr_label
scr_label := tr12[1] ? scr_label + t12 + '\n'  : scr_label
scr_label := tr13[1] ? scr_label + t13 + '\n'  : scr_label
scr_label := tr14[1] ? scr_label + t14 + '\n'  : scr_label
scr_label := tr15[1] ? scr_label + t15 + '\n'  : scr_label
scr_label := tr16[1] ? scr_label + t16 + '\n'  : scr_label
scr_label := tr17[1] ? scr_label + t17 + '\n'  : scr_label
scr_label := tr18[1] ? scr_label + t18 + '\n'  : scr_label
scr_label := tr19[1] ? scr_label + t19 + '\n'  : scr_label
scr_label := tr20[1] ? scr_label + t20 + '\n'  : scr_label
scr_label := tr21[1] ? scr_label + t21 + '\n'  : scr_label
scr_label := tr22[1] ? scr_label + t22 + '\n'  : scr_label
scr_label := tr23[1] ? scr_label + t23 + '\n'  : scr_label
scr_label := tr24[1] ? scr_label + t24 + '\n'  : scr_label
scr_label := tr25[1] ? scr_label + t25 + '\n'  : scr_label
scr_label := tr26[1] ? scr_label + t26 + '\n'  : scr_label
scr_label := tr27[1] ? scr_label + t27 + '\n'  : scr_label
scr_label := tr28[1] ? scr_label + t28 + '\n'  : scr_label
scr_label := tr29[1] ? scr_label + t29 + '\n'  : scr_label
scr_label := tr30[1] ? scr_label + t30 + '\n'  : scr_label
scr_label := tr31[1] ? scr_label + t31 + '\n'  : scr_label
scr_label := tr32[1] ? scr_label + t32 + '\n'  : scr_label
scr_label := tr33[1] ? scr_label + t33 + '\n'  : scr_label
scr_label := tr34[1] ? scr_label + t34 + '\n'  : scr_label
scr_label := tr35[1] ? scr_label + t35 + '\n'  : scr_label
scr_label := tr36[1] ? scr_label + t36 + '\n'  : scr_label
scr_label := tr37[1] ? scr_label + t37 + '\n'  : scr_label
scr_label := tr38[1] ? scr_label + t38 + '\n'  : scr_label
scr_label := tr39[1] ? scr_label + t39 + '\n'  : scr_label
scr_label := tr40[1] ? scr_label + t40 + '\n'  : scr_label


f_colorscr (_valscr ) => 
     _valscr  ? #00000000 : na
     
f_printscr (_txtscr ) => 
     var _lblscr  = label(na), 
     label.delete(_lblscr ), 
     _lblscr  := label.new(
     time + (time-time[1])*posX_scr , 
     ohlc4[posY_scr], 
     _txtscr ,
     xloc.bar_time, 
     yloc.price, 
     f_colorscr (  showscr ),
     textcolor =  showscr ? col : na, 
     size = size.normal, 
     style=label.style_label_center
     )
f_printscr ( scr_label + '\n' + pot_label)

st_security(_symbol, _res, _src) => security(_symbol, _res, _src[barstate.isrealtime ? 1 : 0])
ST_Trend = st_security(syminfo.tickerid, timeframe.period, Trend)
ST_Tsl = st_security(syminfo.tickerid, timeframe.period, Tsl)

