Supertrend Volatility Line

suifeng789486 · study · 45 行 · 点赞 8 · TradingView 原页

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

Pine Script

//@version=6
indicator("Supertrend Volatility Line", shorttitle="ST Volatility", overlay=true)
 
// Author: Feng Qing Yang (TradingView: suifeng789486)
// ATR-based Supertrend with the original yellow touch zone.
// The active chart timeframe is used; use standard 5-minute candles
// to match the original indicator. No trade signals or execution.
int ATR_LEN = 14
float FACTOR = 3.0
color GREEN = color.rgb(13, 150, 111)
color RED = color.rgb(220, 72, 85)
 
// Wilder ATR and the same Supertrend recurrence as the research implementation.
float atr = ta.atr(ATR_LEN)
float basicUp = hl2 + FACTOR * atr
float basicDown = hl2 - FACTOR * atr
var float upper = na
var float lower = na
var int direction = 0
if not na(atr)
    if na(upper[1])
        upper := basicUp
        lower := basicDown
        direction := -1
    else
        upper := basicUp < upper[1] or close[1] > upper[1] ? basicUp : upper[1]
        lower := basicDown > lower[1] or close[1] < lower[1] ? basicDown : lower[1]
        direction := direction[1] < 0 ? (close > upper ? 1 : -1) : (close < lower ? -1 : 1)
float trend = direction == 1 ? lower : upper
 
// During a live candle, display the last completed candle's line.
// Update the displayed direction and level only when the candle closes.
int viewSide = barstate.isconfirmed ? direction : direction[1]
float viewTrend = barstate.isconfirmed ? trend : trend[1]
plot(viewSide == 1 ? viewTrend : na, "Bullish volatility line", color=GREEN, linewidth=2, style=plot.style_linebr, display=display.pane)
plot(viewSide == -1 ? viewTrend : na, "Bearish volatility line", color=RED, linewidth=2, style=plot.style_linebr, display=display.pane)
 
// Original touch zone: half an ATR on the price-facing side of the line.
float viewAtr = barstate.isconfirmed ? atr : atr[1]
color GOLD = color.rgb(221, 160, 37)
float zoneTop = viewSide == 1 ? viewTrend + 0.5 * viewAtr : viewTrend
float zoneBottom = viewSide == 1 ? viewTrend : viewTrend - 0.5 * viewAtr
pTop = plot(zoneTop, "Touch zone upper edge", color=color.new(GOLD, 75), style=plot.style_linebr, display=display.pane)
pBottom = plot(zoneBottom, "Touch zone lower edge", color=color.new(GOLD, 75), style=plot.style_linebr, display=display.pane)
fill(pTop, pBottom, color=color.new(GOLD, 87), title="Yellow touch zone")

← 返回列表