Intraday Quick Stop Loss ATR distance

zerosumbot · study · 67 行 · 点赞 9 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Intraday Quick Stop Loss ATR distance", shorttitle="SL Dist", overlay=false, precision=2)
 
// Inputs
pivotHighLen = input.int(2, "Pivot High Length", minval=1)
pivotLowLen = input.int(2, "Pivot Low Length", minval=1)
atrMultiplier = input.float(2.0, "ATR Multiplier", minval=0.0, step=0.1)
 
// ATR(14) using EMA smoothing
atr = ta.ema(ta.tr(true), 14)
 
// Session tracking
var int sessionBars = 0
var array<float> pivotHighs = array.new<float>()
var array<float> pivotLows = array.new<float>()
 
bool newSession = timeframe.change("D")
 
if newSession or barstate.isfirst
    sessionBars := 1
    array.clear(pivotHighs)
    array.clear(pivotLows)
else
    sessionBars += 1
 
// Find pivot highs/lows using equal-or-better comparisons.
// Candidate pivot is located 'length' candles ago.
highestHigh = ta.highest(high, pivotHighLen * 2 + 1)
lowestLow = ta.lowest(low, pivotLowLen * 2 + 1)
 
// Store only confirmed pivots entirely within today's session.
if barstate.isconfirmed
    if sessionBars >= pivotHighLen * 2 + 1
        if high[pivotHighLen] >= highestHigh
            array.push(pivotHighs, high[pivotHighLen])
 
    if sessionBars >= pivotLowLen * 2 + 1
        if low[pivotLowLen] <= lowestLow
            array.push(pivotLows, low[pivotLowLen])
 
// Find nearest pivot high above current close
float nph = na
 
for level in pivotHighs
    if level > close and (na(nph) or level < nph)
        nph := level
 
// Find nearest pivot low below current close
float npl = na
 
for level in pivotLows
    if level < close and (na(npl) or level > npl)
        npl := level
 
// Calculate SL distances
float short_sl = na
float long_sl = na
 
if not na(atr) and atr > 0
    short_sl := na(nph) ? atrMultiplier : (nph - close) / atr
    long_sl := na(npl) ? atrMultiplier : (close - npl) / atr
 
// Plots
plot(short_sl, title="Short SL", color=color.red, linewidth=1)
plot(-long_sl, title="Long SL", color=color.teal, linewidth=1)
 
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)

← 返回列表