BG MVC Model View Controller eğitim serisi yayında...

Ana sayfa > Borsa > Pine Script Programlama > Pine Script indikatör örnekleri > pine_script_indicator_atr_001

Pine Script indikatörleri

ATR değerlerini kullanarak, zarar kes ve hedef satış fiyatını belirleme

Bu indikatör, kullanıcı tarafından belirlenen alış fiyatı ve risk/ödül oranına göre, ATR değerlerini kullanarak, zarar kes ve hedef satış fiyatını otomatik olarak hesaplamak için kullanılır.

Kullanıcı, indikatör arayüzü üzerinden aşağıdaki konularda tercih yaparak indikatörü kullanabilir:

  1. Alış yöntemi: Kaynak veya doğrudan alış fiyatı belirleme (Ön tanımlı değer kaynak).
  2. Alış kaynağı: Kapanış, açılış, yüksek, düşük gibi (Ön tanımlı değer kapanış)...
  3. Alış fiyatı: Alış fiyatı, kullanıcı tanımlı olarak seçilmişse, belirtilir (Ön tanımlı değer 0).
  4. ATR uzunluğu (Ön tanımlı değer 14)
  5. ATR çarpanı stop loss (Ön tanımlı değer 1.5).
  6. ATR çarpanı hedef satış (Ön tanımlı değer 3).

* ATR çarpanları risk/ödül oranını otomatik olarak hesaplar (Ön tanımlı değer 2).

ATR değerleri grafiğin zaman periyoduna göre farklılık gösterir.



// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Niteya
//@version=5
// Bu indikatör sadece eğitim amaçlı olarak verilmiştir. Yatırım kararlarınızda kullanmayınız.
// Indicator version 1.0
indicator(title="Niteya ATR Based Buy Position", overlay=true)

buy_method = input.string(title="Buying method", defval="Source", options=["Source", "User defined"])
buy_src = input(close, "Buying source")
buy_val = input.float(defval=0.00, title="Buying price")

atr_period = input.int(title="ATR length", defval=14, minval=1)
smoothing = input.string(title="ATR Moving Average method", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
	switch smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)

atr_mul_stop = input.float(1.50, title="ATR multiplier for stop loss")
atr_mul_target = input.float(3.00, title="ATR multiplier for target")

tr_val = ta.tr(true)
atr_val = ma_function(tr_val, atr_period)

price_src = buy_method=='Source' ? buy_src : buy_val

stop_price_decreased = atr_mul_stop * atr_val
stop_price = price_src - stop_price_decreased
stop_rate = (100*stop_price_decreased)/price_src

target_price_increased = atr_mul_target * atr_val
target_price = price_src + target_price_increased
target_rate = (100*target_price_increased)/price_src

var bool is_lines = false

if barstate.islast 
    // ATR values table
    var table buy_table = table.new(position.middle_left, columns=3, rows=8, bgcolor=color.rgb(236, 236, 236), border_width=1, border_color=color.white)

    table.merge_cells(buy_table, 1, 1, 2, 1)
    table.merge_cells(buy_table, 1, 2, 2, 2)
    table.merge_cells(buy_table, 1, 7, 2, 7)

    table.cell(buy_table, 0, 0, "Value", text_color=color.rgb(244, 244, 244), text_halign=text.align_center, bgcolor=color.rgb(65, 65, 65))
    table.cell(buy_table, 1, 0, "Stop", text_color=color.rgb(244, 244, 244), text_halign=text.align_center, bgcolor=color.red)
    table.cell(buy_table, 2, 0, "Target", text_color=color.rgb(244, 244, 244), text_halign=text.align_center, bgcolor=color.green)    

    table.cell(buy_table, 0, 1, "ATR", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='Purchase price')
    table.cell(buy_table, 0, 2, "Buy price", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='Purchase price')
    table.cell(buy_table, 0, 3, "ATR multiplier", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='The amount of price reduced from buy price for stop loss price calculation and the amount of price added for target level price calculation.')
    table.cell(buy_table, 0, 4, "Amount", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='The amount of price reduced from buy price for stop loss price calculation and the amount of price added for target level price calculation.')
    table.cell(buy_table, 0, 5, "Price", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='Stop loss and target level sell price')
    table.cell(buy_table, 0, 6, "Rate (%)", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange, tooltip='Decrease rate for stop loss and increase rate for target level of buy price.')
    table.cell(buy_table, 0, 7, "Risk/Reward ratio", text_color=color.rgb(244, 244, 244), text_halign=text.align_left, bgcolor=color.orange)

    table.cell(buy_table, 1, 1, str.tostring(atr_val, '0.000'), text_halign=text.align_center)
    table.cell(buy_table, 1, 2, str.tostring(price_src, '0.000'), text_halign=text.align_center)
    table.cell(buy_table, 1, 3, str.tostring(atr_mul_stop, '0.000'), text_halign=text.align_right)
    table.cell(buy_table, 1, 4, str.tostring(stop_price_decreased, '0.000'), text_halign=text.align_right, tooltip=str.tostring(atr_mul_stop, '0.000') + ' * ' + str.tostring(atr_val, '0.000'))
    table.cell(buy_table, 1, 5, str.tostring(stop_price, '0.000'), text_halign=text.align_right, tooltip=str.tostring(price_src, '0.000') + ' - ' + str.tostring(stop_price_decreased, '0.000'))
    table.cell(buy_table, 1, 6, str.tostring(stop_rate, '0.000'), text_halign=text.align_right, tooltip='(100 * ' + str.tostring(stop_price_decreased, '0.000') + ') / ' + str.tostring(price_src, '0.000'))
    table.cell(buy_table, 1, 7, str.tostring(target_rate/stop_rate, '0.00'), text_halign=text.align_center, tooltip=str.tostring(target_rate, '0.00') + ' / ' + str.tostring(stop_rate, '0.00'))

    table.cell(buy_table, 2, 3, str.tostring(atr_mul_target, '0.000'), text_halign=text.align_right)
    table.cell(buy_table, 2, 4, str.tostring(target_price_increased, '0.000'), text_halign=text.align_right, tooltip=str.tostring(atr_mul_target, '0.000') + ' * ' + str.tostring(atr_val, '0.000'))
    table.cell(buy_table, 2, 5, str.tostring(target_price, '0.000'), text_halign=text.align_right, tooltip=str.tostring(price_src, '0.000') + ' + ' + str.tostring(target_price_increased, '0.000'))
    table.cell(buy_table, 2, 6, str.tostring(target_rate, '0.000'), text_halign=text.align_right, tooltip='(100 * ' + str.tostring(target_price_increased, '0.000') + ') / '+ str.tostring(price_src, '0.000'))

    // Buy position box
    if not is_lines
        bar_past = bar_index[40]
        bar_past_label = bar_index[20]

        var line target_line = line.new(bar_index, target_price, bar_past, target_price, extend=extend.none, color=color.green, width=1)
        var line buy_line = line.new(bar_index, price_src, bar_past, price_src, extend=extend.none, color=color.blue, width=1)
        var line stop_line = line.new(bar_index, stop_price, bar_past, stop_price, extend=extend.none, color=color.red, width=1)

        linefill.new(target_line, buy_line, color=color.new(#59d68f, 50)) 
        linefill.new(buy_line, stop_line, color=color.new(#fd9292, 50)) 

        label_target = label.new(bar_past_label, target_price, text='Target: ' + str.tostring(target_price, '0.000') + ' (%' + str.tostring(target_rate, '0.000')  + ')', color=color.new(color.green, 0), style=label.style_label_center, textcolor=color.white, size=size.normal)        
        label_src = label.new(bar_past_label, price_src, text='Buy: ' + str.tostring(price_src, '0.000') + ' - Risk/Reward ratio: ' + str.tostring(target_rate/stop_rate, '0.00'), color=color.new(color.blue, 0), style=label.style_label_center, textcolor=color.white, size=size.normal)
        label_stop = label.new(bar_past_label, stop_price, text='Stop: ' + str.tostring(stop_price, '0.000') + ' (%' + str.tostring(stop_rate, '0.000')  + ')', color=color.new(color.red, 0), style=label.style_label_center, textcolor=color.white, size= size.normal)

        is_lines := true 	

Yukarıdaki indikatörün bir uygulaması aşağıda grafikte gösterilmektedir: