本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Gold Volatility Regime Meter
// Free, open-source. Research tool, not financial advice.
//
// Idea: trading costs on gold CFDs (spread + slippage) are roughly fixed in USD,
// while the hourly range (ATR) changes a lot between market regimes. When the
// one-month median of the H1 ATR is small, costs eat a large share of every move
// and short-term trend systems tend to bleed. When it is large, costs become
// negligible. This meter shows that ratio on any chart timeframe.
indicator("Gold Volatility Regime Meter", shorttitle = "Gold Regime", overlay = false)
grpA = "Measurement"
atrLen = input.int(14, "ATR length (H1)", minval = 1, group = grpA)
lookback = input.int(500, "Median window (H1 bars)", minval = 50, group = grpA)
grpB = "Costs (your broker)"
spreadUSD = input.float(0.25, "Typical spread (USD/oz)", step = 0.01, group = grpB)
slipUSD = input.float(0.05, "Slippage per side (USD/oz)", step = 0.01, group = grpB)
grpC = "Regime thresholds"
minAtrUSD = input.float(5.0, "Regime ON when median H1 ATR >= (USD)", step = 0.5, group = grpC)
goodCost = input.float(3.0, "Cost/ATR good below (%)", step = 0.5, group = grpC)
badCost = input.float(6.0, "Cost/ATR bad above (%)", step = 0.5, group = grpC)
showTable = input.bool(true, "Show table", group = "Display")
// H1 values requested from closed H1 bars only (no repaint)
[atrH1, medH1] = request.security(syminfo.tickerid, "60",
[ta.atr(atrLen)[1], ta.percentile_nearest_rank(ta.atr(atrLen), lookback, 50)[1]],
lookahead = barmerge.lookahead_on)
roundTrip = spreadUSD + 2 * slipUSD
costPct = medH1 > 0 ? roundTrip / medH1 * 100 : na
regimeOn = medH1 >= minAtrUSD
col = costPct <= goodCost ? color.new(color.teal, 0) : costPct >= badCost ? color.new(color.red, 0) : color.new(color.orange, 0)
plot(medH1, "Median H1 ATR (USD)", color = color.new(col, 55), style = plot.style_area, linewidth = 2)
plot(atrH1, "Current H1 ATR (USD)", color = color.new(color.gray, 30))
hline(minAtrUSD, "Regime threshold", color = color.new(color.blue, 40), linestyle = hline.style_dashed)
bgcolor(regimeOn ? na : color.new(color.red, 92), title = "Regime OFF")
if showTable and barstate.islast
var table t = table.new(position.top_right, 2, 5, bgcolor = color.new(#131722, 5), border_width = 1)
table.cell(t, 0, 0, "H1 ATR now", text_color = color.white)
table.cell(t, 1, 0, str.tostring(atrH1, "#.##") + " USD", text_color = color.white)
table.cell(t, 0, 1, "Median H1 ATR", text_color = color.white)
table.cell(t, 1, 1, str.tostring(medH1, "#.##") + " USD", text_color = color.white)
table.cell(t, 0, 2, "Round-trip cost", text_color = color.white)
table.cell(t, 1, 2, str.tostring(roundTrip, "#.##") + " USD", text_color = color.white)
table.cell(t, 0, 3, "Cost / ATR", text_color = color.white)
table.cell(t, 1, 3, str.tostring(costPct, "#.#") + " %", text_color = col)
table.cell(t, 0, 4, "Regime", text_color = color.white)
table.cell(t, 1, 4, regimeOn ? "ON (costs small)" : "OFF (costs large)", text_color = regimeOn ? color.teal : color.red)
alertcondition(regimeOn and not regimeOn[1], "Regime turned ON", "Gold volatility regime ON: median H1 ATR above threshold")
alertcondition(not regimeOn and regimeOn[1], "Regime turned OFF", "Gold volatility regime OFF: median H1 ATR below threshold")