Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
8.5 kB
7
Indexable
Never
//@version=5

indicator(title='Range PA', overlay=true)

onoff=input.bool(true,title = "Range Yapısı")

bool range_sv = input.bool(true, title='Range Çiz')
bool range_eq_sv = input.bool(true, title='0.5 Seviyesini Çiz')
bool range_q_sv = input.bool(true, title='0.25 ve 0.75 Seviyelerini Çİz')
bool log_sv = input.bool(false, title='Logaritmik Ölçü')
bool r_a_sv = input.bool(true, title='Yeni Range Uyarısı')
bool rt_a_sv = input.bool(true, title='Range Test Etti Uyarısı')

var float[] pvh1_price = array.new_float(30, na) // high
var int[] pvh1_time = array.new_int(30, na)
var float[] pvl1_price = array.new_float(30, na) // low
var int[] pvl1_time = array.new_int(30, na)
var float[] pvh2_price = array.new_float(10, na) // higher high
var int[] pvh2_time = array.new_int(10, na)
var float[] pvl2_price = array.new_float(10, na) // lower low
var int[] pvl2_time = array.new_int(10, na)

var line[] range_h_lines = array.new_line() // Range lines
var line[] range_25_lines = array.new_line()
var line[] range_m_lines = array.new_line()
var line[] range_75_lines = array.new_line()
var line[] range_l_lines = array.new_line()

var label range_high_label = na
var label range_low_label = na
var label range_mid_label = na

var float temp_pv_0 = na
var float temp_pv_1 = na
var float temp_pv_2 = na
var int temp_time = na
var float last_range_h = na
var float last_range_l = na
var line range_m = na
var line range_25 = na
var line range_75 = na
var int rh_a_time = 0
var int rl_a_time = 0

bool pvh = high < high[1] and high[1] > high[2]
bool pvl = low > low[1] and low[1] < low[2]
int pv1_time = bar_index[1]
float pv1_high = high[1]
float pv1_low = low[1]
bool new_ph_2nd = false
bool new_pl_2nd = false
string alert = na

if barstate.isconfirmed
    if pvh
        array.pop(pvh1_price)
        array.pop(pvh1_time)
        array.unshift(pvh1_price, pv1_high)
        array.unshift(pvh1_time, pv1_time)
        if array.size(pvh1_price) > 2
            temp_pv_0 := array.get(pvh1_price, 0)
            temp_pv_1 := array.get(pvh1_price, 1)
            temp_pv_2 := array.get(pvh1_price, 2)
            if temp_pv_0 < temp_pv_1 and temp_pv_1 > temp_pv_2
                array.pop(pvh2_price)
                array.pop(pvh2_time)
                array.unshift(pvh2_price, temp_pv_1)
                array.unshift(pvh2_time, array.get(pvh1_time, 1))
                new_ph_2nd := true
    if pvl
        array.pop(pvl1_price)
        array.pop(pvl1_time)
        array.unshift(pvl1_price, pv1_low)
        array.unshift(pvl1_time, pv1_time)
        if array.size(pvl1_price) > 2
            temp_pv_0 := array.get(pvl1_price, 0)
            temp_pv_1 := array.get(pvl1_price, 1)
            temp_pv_2 := array.get(pvl1_price, 2)
            if temp_pv_0 > temp_pv_1 and temp_pv_1 < temp_pv_2
                array.pop(pvl2_price)
                array.pop(pvl2_time)
                array.unshift(pvl2_price, temp_pv_1)
                array.unshift(pvl2_time, array.get(pvl1_time, 1))
                new_pl_2nd := true
    if range_sv and onoff and (new_ph_2nd or new_pl_2nd) and (array.size(pvh2_price) > 1 and array.size(pvl2_price) > 1) and (array.get(pvh2_price, 0) < array.get(pvh2_price, 1) and array.get(pvl2_price, 0) > array.get(pvl2_price, 1) and array.get(pvh2_price, 0) > array.get(pvl2_price, 1) and array.get(pvl2_price, 0) < array.get(pvh2_price, 1)) and (array.get(pvl2_price, 1) > nz(last_range_h) or na(last_range_l) ? true : (array.get(pvh2_price, 1) < last_range_l))
        temp_time := math.min(array.get(pvh2_time, 1), array.get(pvl2_time, 1))
        last_range_h := array.get(pvh2_price, 1)
        last_range_l := array.get(pvl2_price, 1)
        temp_pv_0 := log_sv ? math.exp((math.log(last_range_h) + math.log(last_range_l)) / 2) : (last_range_h + last_range_l) / 2
        temp_pv_1 := log_sv ? math.exp((math.log(last_range_h) + math.log(temp_pv_0)) / 2) : (last_range_h + temp_pv_0) / 2
        temp_pv_2 := log_sv ? math.exp((math.log(last_range_l) + math.log(temp_pv_0)) / 2) : (last_range_l + temp_pv_0) / 2
        array.push(range_h_lines, line.new(x1=temp_time, y1=last_range_h, x2=bar_index, y2=last_range_h, color=color.rgb(205, 29, 240), width=1, extend=extend.right))
        array.push(range_l_lines, line.new(x1=temp_time, y1=last_range_l, x2=bar_index, y2=last_range_l, color=color.rgb(205, 29, 240), width=1, extend=extend.right))
        if range_eq_sv and onoff
            array.push(range_m_lines, line.new(x1=temp_time, y1=temp_pv_0, x2=bar_index, y2=temp_pv_0, color=color.rgb(205, 29, 240), width=1, extend=extend.right))
        if range_q_sv and onoff
            array.push(range_25_lines, line.new(x1=temp_time, y1=temp_pv_1, x2=bar_index, y2=temp_pv_1, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
            array.push(range_75_lines, line.new(x1=temp_time, y1=temp_pv_2, x2=bar_index, y2=temp_pv_2, style=line.style_dashed, color=color.gray, width=1, extend=extend.right))
        if r_a_sv
            alert := alert + 'Yeni Range : ' + str.tostring(last_range_h) + ' - ' + str.tostring(last_range_l) + '. Mean = ' + str.tostring(temp_pv_0) + '\n'
        // Remove previous labels if they exist
        if na(range_high_label) == false
            label.delete(range_high_label)
        if na(range_low_label) == false
            label.delete(range_low_label)
        if na(range_mid_label) == false
            label.delete(range_mid_label)
        // Create new labels at the end of the lines
        range_high_label := label.new(x=temp_time, y=last_range_h, text='Range High', xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.rgb(0, 0, 0), textcolor=color.black, textalign=text.align_center)
        range_low_label := label.new(x=temp_time, y=last_range_l, text='Range Low', xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.rgb(0, 0, 0), textcolor=color.black, textalign=text.align_center)
        if range_eq_sv
            range_mid_label := label.new(x=temp_time, y=temp_pv_0, text='Range Mid', xloc=xloc.bar_index, yloc=yloc.price, style=label.style_none, color=color.rgb(0, 0, 0), textcolor=color.black, textalign=text.align_center)
    if array.size(range_h_lines) > 0
        for i = array.size(range_h_lines) - 1 to 0 by 1
            range_h = array.get(range_h_lines, i)
            top = line.get_y1(range_h)
            range_l = array.get(range_l_lines, i)
            bottom = line.get_y1(range_l)
            temp_time := line.get_x1(range_h)
            if array.size(pvh1_price) > 0 and array.get(pvh1_price, 0) > top
                if rt_a_sv and close < top
                    if array.get(pvh1_time, 0) != rh_a_time
                        rh_a_time := array.get(pvh1_time, 0)
                        alert := alert + 'Range High Test @ ' + str.tostring(array.get(pvh1_price, 0)) + ' \n'
            if array.size(pvl1_price) > 0 and array.get(pvl1_price, 0) < bottom
                if rt_a_sv and close > bottom
                    if array.get(pvl1_time, 0) != rl_a_time
                        rl_a_time := array.get(pvl1_time, 0)
                        alert := alert + 'Range Low Test @ ' + str.tostring(array.get(pvl1_price, 0)) + ' \n'
            if range_eq_sv
                range_m := array.get(range_m_lines, i)
            if range_q_sv
                range_25 := array.get(range_25_lines, i)
                range_75 := array.get(range_75_lines, i)
            if array.size(pvh1_price) > 0 and array.size(pvl1_price) > 0 and (array.get(pvh1_price, 0) < bottom or array.get(pvl1_price, 0) > top)
                line.delete(range_h)
                array.remove(range_h_lines, i)
                line.delete(range_l)
                array.remove(range_l_lines, i)
                if range_eq_sv
                    line.delete(range_m)
                    array.remove(range_m_lines, i)
                if range_q_sv
                    line.delete(range_25)
                    array.remove(range_25_lines, i)
                    line.delete(range_75)
                    array.remove(range_75_lines, i)
                last_range_h := na
                last_range_l := na

alert := not na(alert) ? (alert + 'Current price = ' + str.tostring(close) + '\n') : na
exec = not na(alert) ? true : false
if exec==true 
    alert(alert, alert.freq_once_per_bar_close)
Leave a Comment