Untitled

Anonymous
Plain Text
10/16/2025 4:24 PM
16.6 KB
17
Indexable
Never
//@version=6
indicator("ENDEKS BAZLI RSN_LINE", overlay=false, max_bars_back=5000, max_lines_count=500, max_labels_count=500)
import TradingView/ta/10

// Kullanıcıdan endeks sesçimi
s2 = input.symbol("XU100", title="Endeks Karşılaştırması")

// Referans endeks seviyesi (örneğin geçmişteki belirli bir seviye)
ref_xu100 = input.float(11288, "Referans XU100 Değeri")

// Haftalık ve Aylık için özel EMA periyodu
weekly_monthly_ema_period = input.int(14, title="Haftalık/Aylık EMA Periyodu", minval=1, maxval=50)

// Hisse ve endeks verileri
[o1, h1, l1, c1] = request.security(syminfo.tickerid, timeframe.period, [open, high, low, close])
[o2, h2, l2, c2] = request.security(s2, timeframe.period, [open, high, low, close])

// Kompozit OHLC
comp_o = (o1 / c2) * ref_xu100
comp_h = (h1 / c2) * ref_xu100
comp_l = (l1 / c2) * ref_xu100
comp_c = (c1 / c2) * ref_xu100

// EMA'lar hesapla
ema8 = ta.ema(comp_c, 8)
ema14 = ta.ema(comp_c, 14)
ema34 = ta.ema(comp_c, 34)
ema55 = ta.ema(comp_c, 55)
ema233 = ta.ema(comp_c, 233)
ema377 = ta.ema(comp_c, 377)

// Haftalık/Aylık için dinamik EMA
weekly_monthly_ema = ta.ema(comp_c, weekly_monthly_ema_period)

// RSI hesapla (sabit 14 periyot)
rsi = ta.rsi(comp_c, 14)

// Zaman dilimine göre referans EMA'yı otomatik seç
var ref_ema = float(na)

if timeframe.isintraday
    if timeframe.multiplier <= 15  // 2 dakika, 5 dakika, 15 dakika
        ref_ema := ema377
    else if timeframe.multiplier <= 120  // 30 dakika, 1 saat, 2 saat
        ref_ema := ema233
    else  // 4 saat
        ref_ema := ema55
else  // Günlük, 3 günlük, haftalık, aylık ve üzeri
    // Haftalık ve aylık için özel EMA kullan
    if timeframe.isweekly or timeframe.ismonthly
        ref_ema := weekly_monthly_ema
    else
        ref_ema := ema34

// EMA kesişmelerini tespit et
ema34_above_ref_ema = ema34 > ref_ema
ema34_above_ref_ema_prev = ema34[1] > ref_ema[1]

// EMA kesişme sinyalleri
bullish_cross = not ema34_above_ref_ema_prev and ema34_above_ref_ema  // Yukarı kesme
bearish_cross = ema34_above_ref_ema_prev and not ema34_above_ref_ema  // Aşağı kesme

// YENI: RSI kesişme sinyalleri
rsi_above_50 = rsi > 50
rsi_above_50_prev = rsi[1] > 50
rsi_bullish_cross = not rsi_above_50_prev and rsi_above_50  // RSI 50 üzerine kesme
rsi_bearish_cross = rsi_above_50_prev and not rsi_above_50  // RSI 50 altına kesme

// Trend durumu (hangi EMA'nın üstte olduğu)
var trend = ema34 > ref_ema ? 1 : -1  // 1: yukarı trend, -1: aşağı trend

// Trend değişimlerini güncelle
if bullish_cross
    trend := 1
else if bearish_cross
    trend := -1

// GÜNCELLENMIŞ: Mum renklerini belirle
barColor = color.blue  // Varsayılan renk

// Önce RSI kesişmeleri kontrol et
if rsi_bullish_cross
    barColor := color.yellow  // RSI 50 üzerine kesme - SARI
else if rsi_bearish_cross
    barColor := color.purple  // RSI 50 altına kesme - MOR
else
    // Normal EMA bazlı renklendirme
    if timeframe.isintraday and timeframe.multiplier >= 240 or not timeframe.isintraday
        if timeframe.isweekly or timeframe.ismonthly
            barColor := comp_c > weekly_monthly_ema ? color.green : color.red
        else
            barColor := comp_c > ema34 ? color.green : color.red
    else
        barColor := trend == 1 ? color.green : color.red

// Mumları çiz
plotcandle(comp_o, comp_h, comp_l, comp_c,
           color=barColor,
           wickcolor=barColor,
           bordercolor=barColor)

// Referans EMA ismini belirle
ref_ema_name = "EMA 34"  // Varsayılan değer

if timeframe.isintraday
    if timeframe.multiplier <= 15
        ref_ema_name := "EMA 377"
    else if timeframe.multiplier <= 120
        ref_ema_name := "EMA 233"
    else
        ref_ema_name := "EMA 55"
else
    if timeframe.isweekly or timeframe.ismonthly
        ref_ema_name := "EMA " + str.tostring(weekly_monthly_ema_period)
    else
        ref_ema_name := "EMA 34"

// EMA Renkleri - ref_ema trend durumuna göre, ema34 sabit renk
ref_ema_color = trend == 1 ? color.green : color.red

// EMA'ları çiz
plot(ema8, title="EMA 8", color=color.lime, linewidth=2)
plot(ema14, title="EMA 14", color=color.aqua, linewidth=2)
plot(ema34, title="EMA 34", color=color.red, linewidth=2, linestyle=plot.linestyle_dashed)
plot(ema55, title="EMA 55", color=color.orange, linewidth=1, display = display.none)
plot(ema233, title="EMA 233", color=color.rgb(120, 8, 180), linewidth=1, display = display.none)
plot(ema377, title="EMA 377", color=color.rgb(6, 35, 164), linewidth=1, display = display.none)

// Referans EMA çizimi
plot(timeframe.isintraday and timeframe.multiplier <= 15 ? ref_ema : na, title="RSN_LINE", color=ref_ema_color, linewidth=3, display = display.none)
plot(timeframe.isintraday and timeframe.multiplier >= 16 and timeframe.multiplier <= 120 ? ref_ema : na, title="RSN_LINE", display = display.none)
plot(timeframe.isintraday and timeframe.multiplier > 120 ? ref_ema : na, title="RSN_LINE", color=ref_ema_color, linewidth=3, display = display.none)
plot(not timeframe.isintraday ? ref_ema : na, title="RSN_LINE", color=ref_ema_color, linewidth=3, display = display.none)

// RSI çizgisi (opsiyonel - görünür değil)
plot(rsi, title="RSI", color=color.new(color.blue, 70), display=display.none)

// Alış ve satış sinyallerini hesapla
entry_price = bullish_cross ? comp_c : na
exit_price = bearish_cross ? comp_c : na

// Son giriş fiyatını sakla
var float last_entry_price = 0.0
if bullish_cross
    last_entry_price := comp_c

// Pozisyon durumu için değişken
var bool in_position = false

// Alış ve satış sinyallerini takip et
if bullish_cross
    in_position := true
    last_entry_price := comp_c
else if bearish_cross
    in_position := false

// ─────────────────────────────────────────────────────────────
// AUTOLINE (adapted to composite price)
// ─────────────────────────────────────────────────────────────
groupAL = "AUTOLINE"
show_ototrend     = input.bool(true, "Autoline", group=groupAL)
show_price_labels = input.bool(false, title="Show Autoline Labels", group=groupAL)
history_bars      = 300
x1                = input.int(title="Trend Line Settings", defval=6, group=groupAL, minval=1)

// Colors & styles
line_col_res       = color.new(#dd0000, 0)
label_col_res      = color.new(#dd0000, 0)
label_text_col_res = color.white

line_col_sup       = color.new(#00dd00, 0)
label_col_sup      = color.new(#00dd00, 0)
label_text_col_sup = color.black

style_res = line.style_dashed
style_sup = line.style_dashed
line_width_res = 1
line_width_sup = 1

price_at(t1, p1, t2, p2, t3) =>
    p1 + (p2 - p1) * (t3 - t1) / (t2 - t1)

round_to_tick(x) =>
    mult  = 1 / syminfo.mintick
    value = math.ceil(x * mult) / mult
    value

var array<line>  supports    = array.new_line()
var array<line>  resistances = array.new_line()
var array<label> labels      = array.new_label()

fire_alert_sup = false
fire_alert_res = false
fire_alert_sup := false
fire_alert_res := false

// ─────────────────────────────────────────────────────────────
// Clean previous objects & check alert crosses
// ─────────────────────────────────────────────────────────────
line temp_line = na
if array.size(supports) > 0
    i = array.size(supports) - 1
    while i >= 0
        temp_line := array.get(supports, i)
        prev_line_val = line.get_price(temp_line, bar_index - 1)
        curr_line_val = line.get_price(temp_line, bar_index)
        if comp_c[1] > prev_line_val and comp_c < curr_line_val
            fire_alert_sup := true
        line.delete(temp_line)
        array.remove(supports, i)
        i -= 1

if array.size(resistances) > 0
    i = array.size(resistances) - 1
    while i >= 0
        temp_line := array.get(resistances, i)
        prev_line_val = line.get_price(temp_line, bar_index - 1)
        curr_line_val = line.get_price(temp_line, bar_index)
        if comp_c[1] < prev_line_val and comp_c > curr_line_val
            fire_alert_res := true
        line.delete(temp_line)
        array.remove(resistances, i)
        i -= 1

label temp_label = na
if array.size(labels) > 0
    i = array.size(labels) - 1
    while i >= 0
        temp_label := array.get(labels, i)
        label.delete(temp_label)
        array.remove(labels, i)
        i -= 1

alertcondition(fire_alert_sup, 'Diagonal Support Alert (Composite Price)', 'Composite Price crossed DOWN its diagonal support')
alertcondition(fire_alert_res, 'Diagonal Resistance Alert (Composite Price)', 'Composite Price crossed UP its diagonal resistance')

// ─────────────────────────────────────────────────────────────
// Extremum detection (adapted to composite price series)
// ─────────────────────────────────────────────────────────────
min_values = comp_c
max_values = comp_c
x2 = math.round(int(x1 / 2))

int minimums = 0
minimums := ta.lowestbars(min_values, x1) == -x2 ? x2 : minimums[1] + 1

int maximums = 0
maximums := ta.highestbars(max_values, x1) == -x2 ? x2 : maximums[1] + 1

// ─────────────────────────────────────────────────────────────
// Build lines at last bar
// ─────────────────────────────────────────────────────────────
int minimum1 = 0
int minimum2 = 0
int maximum1 = 0
int maximum2 = 0
int medium   = 0

if barstate.islast and show_ototrend
    // SUPPORTS on composite price
    line last_line = na
    label last_label = na
    for k1 = 0 to 50 by 1
        if minimum1 >= history_bars
            break
        minimum1 := minimum1 + minimums[minimum1]
        minimum2 := minimum1 * 2
        for k2 = 0 to 50 by 1
            if minimum2 >= minimum1 * 8 or minimum2 >= history_bars
                break
            minimum2 := minimum2 + minimums[minimum2]

            if minimum1 >= history_bars or minimum2 >= history_bars
                break

            bar1 = bar_index - minimum1
            bar2 = bar_index - minimum2

            price1 = comp_c[minimum1]
            price2 = comp_c[minimum2]

            current_price = price_at(bar2, price2, bar1, price1, bar_index)

            if current_price < comp_c[1]
                crossed = 0
                medium := 0
                for k3 = 0 to 50 by 1
                    if medium >= minimum2
                        break
                    medium := medium + minimums[medium]
                    if medium >= minimum2
                        break
                    if price_at(bar2, price2, bar1, price1, bar_index - medium) > comp_c[medium]
                        crossed := 1
                        break

                if crossed == 0
                    if not na(last_line)
                        last_price = price_at(line.get_x1(last_line), line.get_y1(last_line), line.get_x2(last_line), line.get_y2(last_line), bar_index)
                        if bar1 == line.get_x2(last_line)
                            if current_price > last_price
                                line.set_xy1(last_line, bar2, price2)
                                line.set_xy2(last_line, bar1, price1)
                                line.set_color(last_line, line_col_sup)
                                if show_price_labels
                                    label.set_xy(last_label, bar_index, current_price)
                                    label.set_text(last_label, str.tostring(round_to_tick(current_price)))
                        else
                            last_line := line.new(bar2, price2, bar1, price1, extend = extend.right, color = line_col_sup, style = style_sup, width = line_width_sup)
                            if show_price_labels
                                last_label := label.new(bar_index, current_price, color = label_col_sup, style = label.style_label_upper_left, text = str.tostring(round_to_tick(current_price)), textcolor = label_text_col_sup)
                            array.push(labels, last_label)
                            array.push(supports, last_line)
                    else
                        last_line := line.new(bar2, price2, bar1, price1, extend = extend.right, color = line_col_sup, style = style_sup, width = line_width_sup)
                        if show_price_labels
                            last_label := label.new(bar_index, current_price, color = label_col_sup, style = label.style_label_upper_left, text = str.tostring(round_to_tick(current_price)), textcolor = label_text_col_sup)
                        array.push(labels, last_label)
                        array.push(supports, last_line)

    // RESISTANCES on composite price
    last_line := na
    last_label := na
    for k1 = 0 to 100 by 1
        if maximum1 >= history_bars
            break
        maximum1 := maximum1 + maximums[maximum1]
        maximum2 := maximum1 * 2
        for k2 = 0 to 50 by 1
            if maximum2 >= maximum1 * 8 or maximum2 >= history_bars
                break
            maximum2 := maximum2 + maximums[maximum2]

            if maximum1 >= history_bars or maximum2 >= history_bars
                break

            bar1 = bar_index - maximum1
            bar2 = bar_index - maximum2

            price1 = comp_c[maximum1]
            price2 = comp_c[maximum2]

            current_price = price_at(bar2, price2, bar1, price1, bar_index)

            if current_price > comp_c[1]
                crossed = 0
                medium := 0
                for k3 = 0 to 100 by 1
                    if medium >= maximum2
                        break
                    medium := medium + maximums[medium]
                    if medium >= maximum2
                        break
                    if price_at(bar2, price2, bar1, price1, bar_index - medium) < comp_c[medium]
                        crossed := 1
                        break

                if crossed == 0
                    if not na(last_line)
                        last_price = price_at(line.get_x1(last_line), line.get_y1(last_line), line.get_x2(last_line), line.get_y2(last_line), bar_index)
                        if bar1 == line.get_x2(last_line)
                            if current_price < last_price
                                line.set_xy1(last_line, bar2, price2)
                                line.set_xy2(last_line, bar1, price1)
                                line.set_color(last_line, line_col_res)
                                if show_price_labels
                                    label.set_xy(last_label, bar_index, current_price)
                                    label.set_text(last_label, str.tostring(round_to_tick(current_price)))
                        else
                            last_line := line.new(bar2, price2, bar1, price1, extend = extend.right, color = line_col_res, style = style_res, width = line_width_res)
                            if show_price_labels
                                last_label := label.new(bar_index, current_price, color = label_col_res, style = label.style_label_lower_left, text = str.tostring(round_to_tick(current_price)), textcolor = label_text_col_res)
                            array.push(labels, last_label)
                            array.push(resistances, last_line)
                    else
                        last_line := line.new(bar2, price2, bar1, price1, extend = extend.right, color = line_col_res, style = style_res, width = line_width_res)
                        if show_price_labels
                            last_label := label.new(bar_index, current_price, color = label_col_res, style = label.style_label_lower_left, text = str.tostring(round_to_tick(current_price)), textcolor = label_text_col_res)
                        array.push(labels, last_label)
                        array.push(resistances, last_line)
Editor is loading...
Leave a Comment