本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CCE_Charts
//@version=6
indicator("Gianluca ATR Volatility Regime [CCE_Charts]", shorttitle="CCE - ATR Regime", overlay=false)
// Gianluca's calibration
atrLength = input.int(14, "ATR Length", minval=1)
percentileLength = input.int(200, "Percentile Length", minval=2)
// ATR
atr = ta.atr(atrLength)
// ATR percentile
atrPercentile = ta.percentrank(atr, percentileLength)
// Volatility regime
regime =
atrPercentile <= 25 ? "LOW" :
atrPercentile <= 50 ? "MEDIUM" :
atrPercentile <= 75 ? "HIGH" :
"EXTREME"
// Numeric regime
regimeValue =
atrPercentile <= 25 ? 1 :
atrPercentile <= 50 ? 2 :
atrPercentile <= 75 ? 3 :
4
// Percentile plot
plot(
atrPercentile,
title="ATR Percentile",
linewidth=2
)
// Regime boundaries
hline(25, "LOW / MEDIUM", linestyle=hline.style_dashed)
hline(50, "MEDIUM / HIGH", linestyle=hline.style_dashed)
hline(75, "HIGH / EXTREME", linestyle=hline.style_dashed)
// Background
bgcolor(
regimeValue == 1 ? color.new(color.blue, 90) :
regimeValue == 2 ? color.new(color.green, 90) :
regimeValue == 3 ? color.new(color.orange, 90) :
color.new(color.red, 90)
)
// Current regime table
var table regimeTable = table.new(
position.top_right,
2,
2
)
if barstate.islast
table.cell(regimeTable, 0, 0, "ATR %ile")
table.cell(regimeTable, 1, 0, str.tostring(atrPercentile, "#.0"))
table.cell(regimeTable, 0, 1, "REGIME")
table.cell(regimeTable, 1, 1, regime)