本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// @version=4
study(title="ATR Trailing Stops S/R [LM]", shorttitle="Atr Stop S/R [LM]", overlay = true)
/// Inputs ///
i_rep = input(defval=true, title="Allow Repainting", type=input.bool, group="Safety settings", inline="Safety settings", tooltip="function repaint")
i_bar = input(defval=false, title="Allow Bar Color Change", type=input.bool, group="Safety settings", inline="Color signals")
i_res1 = input(defval="", title="1st Resolution", type=input.resolution, group="Main Time Frame Settings")
i_nATRPeriod1 = input(defval=24, title="ATR Period", type=input.integer, minval=1, maxval=900, step=1, group="Main Time Frame Settings", inline="Atr settings")
i_nATRMultip1 = input(defval=2, title="ATR Multiplier", type=input.float, minval=0.01, maxval=100, step=0.1, group="Main Time Frame Settings", inline="Atr settings")
i_showLines1 = input(defval=true, title="Show S/R", type=input.bool, group="Main Time Frame Settings", inline="Bool")
i_extendLines1 = input(defval=true, title="Extend S/R", type=input.bool, group="Main Time Frame Settings", inline="Bool")
i_lineCount1 = input(defval=10, title="Line Count", type=input.integer, group="Main Time Frame Settings", inline="Count")
i_lineCandleCount1 = input(defval=40, title="Candle Count", type=input.integer, group="Main Time Frame Settings", inline="Count")
i_bullColor1 = input(defval=color.green, title="Bull Color", type=input.color, group="Main Time Frame Settings", inline="Color")
i_bearColor1 = input(defval=color.red , title="Bear Color", type=input.color, group="Main Time Frame Settings", inline="Color")
f_security(_symbol, _res, _src, _repaint) =>
security(_symbol, _res, _src[_repaint ? 0 : barstate.isrealtime ? 1 : 0])[_repaint ? 0 : barstate.isrealtime ? 0 : 1]
f_donchian(_len) => avg(lowest(_len), highest(_len))
f_drawLine(_x1, _x2, _yValue, _lineColor) =>
line.new(x1 = _x1,
y1 = _yValue,
x2 = _x2,
y2 = _yValue,
color = _lineColor,
style=line.style_dotted,
width = 2)
f_drawLabel(_x, _y, _textColor) =>
label.new(_x, _y, " " +tostring(_y), xloc.bar_index, yloc.price, #00000000, label.style_none, _textColor)
f_extendArray(_lineArray, _labelArray, _extendLines) =>
if (array.size(_lineArray)>0)
for _i = array.size(_lineArray) - 1 to 0
x2 = line.get_x2(array.get(_lineArray, _i))
yValue = line.get_y1(array.get(_lineArray, _i))
if (_extendLines or (bar_index - 1 == x2 and not (high > yValue and low < yValue)))
line.set_x2(array.get(_lineArray, _i), bar_index)
label.set_x(array.get(_labelArray, _i), bar_index)
f_checkCandleCount(_plot, _count) =>
bool isPreviousValueSame = true
for i = 1 to _count
isPreviousValueSame := isPreviousValueSame and _plot[i] == _plot[i-1]
isPreviousValueSame and _plot[_count] != _plot[_count + 1]
f_atr(_length, _close, _open, _high, _low) =>
trueRange = na(_high[1])? _high-_low : max(max(_high - _low, abs(_high - _close[1])), abs(_low - _close[1]))
rma(trueRange, _length)
/// 1st Resolution ///
cls1 = f_security(syminfo.tickerid, i_res1, close, i_rep)
opn1 = f_security(syminfo.tickerid, i_res1, open, i_rep)
hgh1 = f_security(syminfo.tickerid, i_res1, high, i_rep)
low1 = f_security(syminfo.tickerid, i_res1, low, i_rep)
xATR = f_atr(i_nATRPeriod1, cls1, opn1, hgh1, low1)
nLoss1 = i_nATRMultip1 * xATR
xATRTrailingStop1 = 0.0
xATRTrailingStop1 :=iff(cls1 > nz(xATRTrailingStop1[1], 0) and cls1[1] > nz(xATRTrailingStop1[1], 0), max(nz(xATRTrailingStop1[1]), cls1 - nLoss1),
iff(cls1 < nz(xATRTrailingStop1[1], 0) and cls1[1] < nz(xATRTrailingStop1[1], 0), min(nz(xATRTrailingStop1[1]), cls1 + nLoss1),
iff(cls1 > nz(xATRTrailingStop1[1], 0), cls1 - nLoss1, cls1 + nLoss1)))
/// Color ///
bullColor1 = color.new(i_bullColor1, 40)
bearColor1 = color.new(i_bearColor1, 40)
atrColor = cls1 > xATRTrailingStop1[1] ? bullColor1 : bearColor1
/// S/R lines ///
var firstArray = array.new_line()
var firstLabelArray = array.new_label()
if (i_showLines1 and f_checkCandleCount(xATRTrailingStop1, i_lineCandleCount1-1))
line l = f_drawLine(bar_index[i_lineCandleCount1], bar_index, xATRTrailingStop1, atrColor)
label lab = f_drawLabel(bar_index, xATRTrailingStop1, atrColor)
if (array.size(firstArray)==i_lineCount1)
line.delete(array.shift(firstArray))
label.delete(array.shift(firstLabelArray))
array.push(firstArray, l)
array.push(firstLabelArray, lab)
if (i_showLines1)
f_extendArray(firstArray, firstLabelArray, i_extendLines1)
/// Plot ///
plot(xATRTrailingStop1[1], title="ATR Trailing Stop", color=atrColor, linewidth=1, style=plot.style_line, editable=true, display=display.all)
/// Alerts ///
alertcondition(crossover(cls1, xATRTrailingStop1[1]), "Buy Signal1", "Bullish Change Detected1")
alertcondition(crossunder(cls1,xATRTrailingStop1[1]), "Sell Signal1", "Bearish Change Detected1")