//@version=6
strategy("Strategy.exit Trail_Offset ve Price Örnekleri", overlay=true)
// Temel parametreler
initial_capital = 10000
position_size = 1000
// Giriş koşulları
long_condition = ta.crossover(ta.sma(close, 10), ta.sma(close, 20))
short_condition = ta.crossunder(ta.sma(close, 10), ta.sma(close, 20))
// ============================================
// ÖRNEK 1: TRAIL_OFFSET kullanımı
// ============================================
if long_condition
strategy.entry("Long", strategy.long, qty=position_size)
// Trailing stop - fiyat %2 düştüğünde pozisyonu kapat
strategy.exit("Long Exit", "Long",
trail_offset=close * 0.02) // %2 trailing offset
// ============================================
// ÖRNEK 2: PRICE ile sabit stop loss
// ============================================
if short_condition
strategy.entry("Short", strategy.short, qty=position_size)
// Sabit fiyat seviyesinde stop loss
stop_price = close * 1.03 // %3 üzerinde stop
strategy.exit("Short Exit", "Short",
stop=stop_price)
// ============================================
// ÖRNEK 3: TRAIL_OFFSET ve PRICE birlikte
// ============================================
var float entry_price = na
var float trail_stop = na
// Long pozisyon için gelişmiş örnek
if long_condition
strategy.entry("Advanced Long", strategy.long, qty=position_size)
entry_price := close
trail_stop := close * 0.95 // %5 altında başlangıç stop
// Trailing stop güncelleme
if strategy.position_size > 0
// Yeni yüksek seviye varsa trail stop'u güncelle
if close > entry_price
new_trail = close * 0.98 // %2 trailing offset
trail_stop := math.max(trail_stop, new_trail)
// Exit koşulu
strategy.exit("Advanced Long Exit", "Advanced Long",
stop=trail_stop,
trail_offset=close * 0.02)
// ============================================
// ÖRNEK 4: Dinamik trail_offset
// ============================================
// Volatiliteye göre dinamik trailing offset
atr_value = ta.atr(14)
dynamic_trail_offset = atr_value * 2 // ATR'nin 2 katı
if ta.crossover(close, ta.sma(close, 50))
strategy.entry("Dynamic Long", strategy.long, qty=position_size)
strategy.exit("Dynamic Long Exit", "Dynamic Long",
trail_offset=dynamic_trail_offset)
// ============================================
// ÖRNEK 5: Zaman bazlı çıkış ile price
// ============================================
var int entry_bar = na
if ta.crossover(ta.rsi(close, 14), 30)
strategy.entry("RSI Long", strategy.long, qty=position_size)
entry_bar := bar_index
// 10 bar sonra veya stop loss'ta çık
if strategy.position_size > 0 and not na(entry_bar)
bars_since_entry = bar_index - entry_bar
if bars_since_entry >= 10
strategy.exit("RSI Long Exit", "RSI Long",
stop=close * 0.95) // %5 stop loss
else
strategy.exit("RSI Long Exit", "RSI Long",
trail_offset=close * 0.03) // %3 trailing
// ============================================
// GÖRSELLEŞTIRME
// ============================================
// Trailing stop çizgisi
plot(strategy.position_size > 0 ? trail_stop : na,
color=color.red, linewidth=2, title="Trailing Stop")
// Giriş seviyesi
plot(strategy.position_size != 0 ? entry_price : na,
color=color.blue, linewidth=1, title="Entry Price")
// Pozisyon büyüklüğü tablosu
if barstate.islast
var table info_table = table.new(position.top_right, 2, 4, bgcolor=color.white, border_width=1)
table.cell(info_table, 0, 0, "Position Size", text_color=color.black)
table.cell(info_table, 1, 0, str.tostring(strategy.position_size), text_color=color.black)
table.cell(info_table, 0, 1, "Entry Price", text_color=color.black)
table.cell(info_table, 1, 1, str.tostring(entry_price), text_color=color.black)
table.cell(info_table, 0, 2, "Trail Stop", text_color=color.black)
table.cell(info_table, 1, 2, str.tostring(trail_stop), text_color=color.black)
table.cell(info_table, 0, 3, "ATR", text_color=color.black)
table.cell(info_table, 1, 3, str.tostring(atr_value), text_color=color.black)