//@version=5
indicator('Le_Professor RSI/MA kesişimi taraması 1', shorttitle='Le Professor Tarama 1', overlay=false)

// RSI Params
rsi_length = input.int(14, title='Length', inline='RSI', group='RSI')
timePeriod = input.timeframe('D', "Resolution", options=['D', 'W', 'M', '1', '3', '5', '10', '15', '30', '45', '60', '120', '240'])
bars_apart = input.int(40, title='Labels Bars Apart', inline='Style', group='Style')

// Liste Seçimi
list_selection = input.string('Liste 1', title="Liste Seçimi", options=['Liste 1', 'Liste 2', 'Liste 3'], group='Liste Seçimi')

// Hisse listeleri
var string[] symbol_list = na
if list_selection == 'Liste 1'
    symbol_list := array.new_string(5)
    array.set(symbol_list, 0, 'BIST:A1CAP')
    array.set(symbol_list, 1, 'BIST:ACSEL')
    array.set(symbol_list, 2, 'BIST:ADEL')
    array.set(symbol_list, 3, 'BIST:ADESE')
    array.set(symbol_list, 4, 'BIST:ADGYO')

if list_selection == 'Liste 2'
    symbol_list := array.new_string(5)
    array.set(symbol_list, 0, 'BIST:ARCLK')
    array.set(symbol_list, 1, 'BIST:AEFES')
    array.set(symbol_list, 2, 'BIST:AFYON')
    array.set(symbol_list, 3, 'BIST:AGESA')
    array.set(symbol_list, 4, 'BIST:AGHOL')

if list_selection == 'Liste 3'
    symbol_list := array.new_string(5)
    array.set(symbol_list, 0, 'BIST:AGROT')
    array.set(symbol_list, 1, 'BIST:AGYO')
    array.set(symbol_list, 2, 'BIST:AHGAZ')
    array.set(symbol_list, 3, 'BIST:AKBNK')
    array.set(symbol_list, 4, 'BIST:AKCNS')

// Verileri hesaplama
Var_Func(source, len) =>
    valpha = 2 / (len + 1)
    vud1 = source > source[1] ? source - source[1] : 0
    vdd1 = source < source[1] ? source[1] - source : 0
    vUD = math.sum(vud1, 9)
    vDD = math.sum(vdd1, 9)
    vCMO = nz((vUD - vDD) / (vUD + vDD))
    VAR = 0.0
    VAR := nz(valpha * math.abs(vCMO) * source) + (1 - valpha * math.abs(vCMO)) * nz(VAR[1])
    VAR

ma(source, len, type) =>
    switch type
        "SMA" => ta.sma(source, len)
        "EMA" => ta.ema(source, len)
        "SMMA (RMA)" => ta.rma(source, len)
        "WMA" => ta.wma(source, len)
        "VWMA" => ta.vwma(source, len)
        "VAR" => Var_Func(source, len)
len1=input.int(20, title="RSI Divergence length", maxval=500)
len = input.int(14, minval=1, title='Length')
src= close
src2 = input(close, 'Source')
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA", "VAR"], group="MOST Settings")
maLengthInput = input.int(5, title="MA Length", group="MOST Settings")
percent = input.float(9.0, 'STOP LOSS Percent', step=0.1, minval=0, group="MOST Settings")
up = ta.rma(math.max(ta.change(src), 0), len)
down = ta.rma(-math.min(ta.change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
rsiMA = ma(rsi, maLengthInput, maTypeInput)

exMov = rsiMA
fark = exMov * percent * 0.01
longStop = exMov - fark
longStopPrev = nz(longStop[1], longStop)
longStop := exMov > longStopPrev ? math.max(longStop, longStopPrev) : longStop
shortStop = exMov + fark
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := exMov < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStop
dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and exMov > shortStopPrev ? 1 : dir == 1 and exMov < longStopPrev ? -1 : dir
MOST = dir == 1 ? longStop : shortStop
cro= ta.crossover(exMov, MOST)
cru= ta.crossunder(exMov, MOST)
direction = 0
direction := cro ? 1 : cru ? -1 : direction[1]

drawLine(X1, Y1, X2, Y2, ExtendType, Color, LineStyle) =>
    var line Line = na
    Line := line.new(X1, Y1, X2, Y2, xloc.bar_index, ExtendType, Color, LineStyle, 1)
    line.delete(Line[1])

rsdcr2(PeriodMinusOne, Deviations, Estimate) =>
    var period = PeriodMinusOne + 1
    var devDenominator = Estimate == 'Unbiased' ? PeriodMinusOne : period
    Ex = 0.0
    Ex2 = 0.0
    Exy = 0.0
    Ey = 0.0
    for i = 0 to PeriodMinusOne by 1
        closeI = nz(rsi[i])
        Ex := Ex + i
        Ex2 := Ex2 + i * i
        Exy := Exy + closeI * i
        Ey := Ey + closeI
        Ey
    ExEx = Ex * Ex
    slope = Ex2 == ExEx ? 0.0 : (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
    linearRegression = (Ey - slope * Ex) / period
    intercept = linearRegression + bar_index * slope
    deviation = 0.0
    for i = 0 to PeriodMinusOne by 1
        deviation := deviation + math.pow(nz(rsi[i]) - (intercept - bar_index[i] * slope), 2.0)
        deviation
    deviation := Deviations * math.sqrt(deviation / devDenominator)
    correlate = ta.correlation(rsi, bar_index, period)
    r2 = math.pow(correlate, 2.0)
    [linearRegression, slope, deviation, correlate, r2]
periodTrend = input.int(89, 'Trend Period', minval=4)
deviationsAmnt = input.float(1.5, ' Deviation', minval=0.1, step=0.1)
estimatorType = input.string('tarafsız', ' Estimator', options=['taraflı', 'tarafsız'])
var extendType = input.string('Right', 'Extend', options=['Right', 'Segment']) == 'Right' ? extend.right : extend.none
periodMinusOne = periodTrend - 1
[linReg, slope, deviation, correlate, r2] = rsdcr2(periodMinusOne, deviationsAmnt, estimatorType)
endPointBar = bar_index - periodTrend + 1
endPointY = linReg + slope * periodMinusOne

screenerFunc(sym) =>
   ///rsi_value1 =  (ta.crossover(rsi, rsiMA) and (-0.2 < correlate) and ( +0.6 > correlate) and(rsi< 50))
    rsi_value1 =   (ta.crossover(rsi, rsiMA) and ta.crossover(rsi, MOST) and (-0.2 < correlate) and ( +0.6 > correlate)),
  
      [rsi_value1]



// Ekran etiketi
var string scr_label1 = 'RSİ-MA YUKARI KESEN: \n-------------------------\n'

// Hisse listelerini kontrol et
for i = 0 to array.size(symbol_list) - 1
    sym = array.get(symbol_list, i)
    rsi = request.security(sym, timePeriod, ta.rsi(close, rsi_length))
    rsiMA = request.security(sym, timePeriod, ma(rsi, 14, "SMA"))
    rsi_value1 = (ta.crossover(rsi, rsiMA) and rsi < 50)
    scr_label1 := rsi_value1 ? scr_label1 + sym + ' ' + '\n' : scr_label1

label.new(bar_index, 0, scr_label1, color=color(na), textcolor=#4fff49, style=label.style_label_down, textalign=text.align_left)

// Uyarı mesajı
alert_message = scr_label1
alert(alert_message, freq=alert.freq_once_per_bar_close)