Trailing Stop Loss ATR

Dreadblitz · study · 66 行 · 点赞 2,553 · TradingView 原页

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

Pine Script

//@version=4
study(title="Trailing Stop Loss ATR",shorttitle = "TSLA-X", overlay=true)
//
act = input(true, title="ACTIVATE TSL")
act1 = input(true, title="ACTIVE INFO PANEL")
mode= input(defval="Short", options=["Short", "Long"])
sessSpec = input("0935-1045",title="Session", type=input.session)
perc_atr_day = input(10, title="% Atr Day",step=0.5)
BuySellPrice = input(close, title="Buy-Sell")
//
red=color.new(#FF0000, 0)
green=color.new(#0AAC00, 0)
//
ATR = security(syminfo.tickerid,"D", atr(14))
StopLossRange  = perc_atr_day*ATR/100
//
TrailingStop = 0.0
new_TrailingStop = 0.0
highTimeFrame = "1"
//
is_newbar(res, sess) =>
    t = time(res, sess)
    change(t) > 0 ? 1 : 0
//
if mode=="Short" 
    TrailingStop := BuySellPrice <= close ? BuySellPrice + StopLossRange : 
       close <= open[1] ? close + StopLossRange : TrailingStop[1]
    new_TrailingStop := if is_newbar(highTimeFrame, sessSpec)
        if TrailingStop > new_TrailingStop[1]
            new_TrailingStop[1]
        else
            TrailingStop
    else
        TrailingStop
//
if mode=="Long" 
    TrailingStop := BuySellPrice >= close ? BuySellPrice - StopLossRange : 
      close >= open[1] ? close - StopLossRange : TrailingStop[1]
    
    new_TrailingStop := if is_newbar(highTimeFrame, sessSpec)
        if TrailingStop < new_TrailingStop[1]
            new_TrailingStop[1]
        else
            TrailingStop
    else
        TrailingStop
//
plot_color = is_newbar(highTimeFrame, sessSpec) and mode=="Short" ? red :is_newbar(highTimeFrame, sessSpec) and mode=="Long" ? green : na
plot(act and mode=="Short"?new_TrailingStop:na, linewidth=2, color=plot_color)
plot(act and mode=="Long"?new_TrailingStop:na, linewidth=2, color=plot_color)
//
atr = atr(50) 
 
if new_TrailingStop[0] > new_TrailingStop[1] and act==true and mode=="Long" and act1 
    
    a=label.new(bar_index, new_TrailingStop - 3*atr, style=label.style_none,text="            %: " + tostring(perc_atr_day,"#.##")+"\n               Tsl: " + tostring(StopLossRange,"#.##")+ "\n               ATR: "+ tostring(ATR,"#.##"), size=size.normal, textcolor=color.black)
    label.delete(a[1])
    
if new_TrailingStop[0] < new_TrailingStop[1] and act==true and mode=="Short" and act1 
    
    a=label.new(bar_index, new_TrailingStop + atr, style=label.style_none,text="            %: " + tostring(perc_atr_day,"#.##")+"\n               Tsl: " + tostring(StopLossRange,"#.##")+ "\n               ATR: "+ tostring(ATR,"#.##"), size=size.normal, textcolor=color.black)
    label.delete(a[1])
 
//ALERT
stop= cross(close, new_TrailingStop)
alertcondition(stop,title="STOP",message="STOP")

← 返回列表