本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CandelaCharts
//@version=6
indicator(title = "CandelaCharts - Volatility Intensity Index", shorttitle = "CandelaCharts - Volatility Intensity Index", overlay = false, precision = 4, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 500, max_bars_back = 500)
// # ========================================================================= #
// # | Colors |
// # ========================================================================= #
//#region
colors_teal = #089981
colors_gold = #F59E0B
colors_orange = #F97316
colors_crimson = #F23645
colors_gray = color.gray
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Inputs |
// # ========================================================================= #
//#region
// # General ----------------------------------------------------------------- #
general_font = input.string("Monospace", "Text Style ", options = ["Default", "Monospace"], inline = "1.0", group = "General")
general_text = input.string("Tiny", "", options = ["Tiny", "Small", "Normal", "Large", "Huge", "Auto"], inline = "1.0", group = "General", tooltip = "Customize global text size and style")
general_brand_show = input.bool(false, "Hide Brand", group = "General")
// # Settings ---------------------------------------------------------------- #
vii_lookback_len = input.int(7, "Lookback Length", minval = 1, group = "Settings", tooltip = "Number of bars used to measure the high-low price range.")
vii_candle_color = input.bool(true, "Color Chart Candles", group = "Settings", tooltip = "Color the main chart candles when volatility intensity reaches or exceeds the High threshold (0.125 by default).")
// # Thresholds -------------------------------------------------------------- #
vii_extreme_th = input.float(0.250, "Extreme Level", minval = 0.0, step = 0.005, inline = "1.0", group = "Thresholds", tooltip = "Minimum intensity ratio for Extreme Volatility.")
vii_extreme_css = input.color(colors_crimson, "", inline = "1.0", group = "Thresholds")
vii_elevated_th = input.float(0.150, "Elevated Level", minval = 0.0, step = 0.005, inline = "2.0", group = "Thresholds", tooltip = "Mid-high dotted reference level separating the lighter and stronger High Volatility zones.")
vii_elevated_css = input.color(colors_orange, "", inline = "2.0", group = "Thresholds")
vii_high_th = input.float(0.125, "High Level", minval = 0.0, step = 0.005, inline = "3.0", group = "Thresholds", tooltip = "Minimum intensity ratio for High Volatility.")
vii_high_css = input.color(colors_orange, "", inline = "3.0", group = "Thresholds")
vii_normal_th = input.float(0.050, "Normal Level", minval = 0.0, step = 0.005, inline = "4.0", group = "Thresholds", tooltip = "Minimum intensity ratio for Normal Volatility.")
vii_normal_css = input.color(colors_gold, "", inline = "4.0", group = "Thresholds")
vii_low_th = input.float(0.000, "Low Level", minval = 0.0, step = 0.005, inline = "5.0", group = "Thresholds", tooltip = "Baseline threshold for Low Volatility.")
vii_low_css = input.color(colors_teal, "", inline = "5.0", group = "Thresholds")
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | UDT |
// # ========================================================================= #
//#region
type UDT_VolatilityState
float floor_price
float ceiling_price
float intensity
int tier
color tier_color
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Constants |
// # ========================================================================= #
//#region
const int TIER_LOW = 0
const int TIER_NORMAL = 1
const int TIER_HIGH = 2
const int TIER_EXTREME = 3
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Functions |
// # ========================================================================= #
//#region
method text_size(string size) =>
out = switch size
"Tiny" => size.tiny
"Small" => size.small
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
"Auto" => size.auto
out
method font_style(string font) =>
out = switch font
"Default" => font.family_default
"Monospace" => font.family_monospace
method classify_tier(float val, float ext_th, float high_th, float norm_th) =>
int tier_out = TIER_LOW
if val >= ext_th
tier_out := TIER_EXTREME
else if val >= high_th
tier_out := TIER_HIGH
else if val >= norm_th
tier_out := TIER_NORMAL
tier_out
method resolve_tier_color(int tier) =>
switch tier
TIER_EXTREME => vii_extreme_css
TIER_HIGH => vii_high_css
TIER_NORMAL => vii_normal_css
=> vii_low_css
method compute_intensity(simple int len) =>
float swing_floor = ta.lowest(low, len)
float swing_ceiling = ta.highest(high, len)
float ratio = swing_floor > 0.0 ? (swing_ceiling - swing_floor) / swing_floor : 0.0
int tier = ratio.classify_tier(vii_extreme_th, vii_high_th, vii_normal_th)
color css = tier.resolve_tier_color()
UDT_VolatilityState.new(swing_floor, swing_ceiling, ratio, tier, css)
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Core Logic |
// # ========================================================================= #
//#region
vol_state = compute_intensity(vii_lookback_len)
is_extreme_regime = vol_state.tier == TIER_EXTREME
is_high_regime = vol_state.tier == TIER_HIGH
is_normal_regime = vol_state.tier == TIER_NORMAL
is_low_regime = vol_state.tier == TIER_LOW
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Visuals |
// # ========================================================================= #
//#region
plot(vol_state.intensity, title = "Volatility Intensity", linewidth = 2, color = vol_state.tier_color, style = plot.style_steplinebr)
color candle_zone_css = is_extreme_regime ? vii_extreme_css : is_high_regime ? vii_high_css : na
barcolor(vii_candle_color ? candle_zone_css : na, title = "High/Extreme Volatility Candles")
h_extreme = hline(vii_extreme_th, title = "Extreme Level", color = vii_extreme_css, linestyle = hline.style_solid, linewidth = 1)
h_elevated = hline(vii_elevated_th, title = "Elevated Level", color = vii_elevated_css, linestyle = hline.style_dotted, linewidth = 1)
h_high = hline(vii_high_th, title = "High Level", color = vii_high_css, linestyle = hline.style_dashed, linewidth = 1)
h_normal = hline(vii_normal_th, title = "Normal Level", color = vii_normal_css, linestyle = hline.style_dotted, linewidth = 1)
h_low = hline(vii_low_th, title = "Low Level", color = vii_low_css, linestyle = hline.style_solid, linewidth = 1)
fill(h_elevated, h_high, color = color.new(vii_high_css, 88), title = "High Volatility Zone (0.125–0.150)")
fill(h_extreme, h_elevated, color = color.new(vii_elevated_css, 75), title = "Upper High Volatility Zone (0.150–0.250)")
alertcondition(is_extreme_regime and barstate.isconfirmed, title = "Extreme Volatility Regime", message = "Volatility Intensity Index entered Extreme regime on {{ticker}} ({{interval}}).")
alertcondition(is_high_regime and barstate.isconfirmed, title = "High Volatility Regime", message = "Volatility Intensity Index entered High regime on {{ticker}} ({{interval}}).")
alertcondition(is_normal_regime and barstate.isconfirmed, title = "Normal Volatility Regime", message = "Volatility Intensity Index entered Normal regime on {{ticker}} ({{interval}}).")
alertcondition(is_low_regime and barstate.isconfirmed, title = "Low Volatility Regime", message = "Volatility Intensity Index entered Low regime on {{ticker}} ({{interval}}).")
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #
// # ========================================================================= #
// # | Brand |
// # ========================================================================= #
//#region
if barstate.isfirst and general_brand_show == false
var table brand = table.new(position.bottom_right, 1, 1, bgcolor = chart.bg_color)
table.cell(brand, 0, 0, "© CandelaCharts", text_color = colors_gray, text_halign = text.align_center, text_size = text_size(general_text), text_font_family = font_style(general_font))
//#endregion
// # ========================================================================= #
// # | End |
// # ========================================================================= #