本页源码来自 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/
// Market Fear Meter (based on the Williams VIX Fix) with fear/greed zones, regime filter and forward-return stats
// Core Williams VIX Fix code and the "percentile / Bollinger" highlight logic come from
// "CM_Williams_Vix_Fix Finds Market Bottoms" by ChrisMoody (open-source on TradingView, 2014).
// The Williams VIX Fix formula itself is by Larry Williams. All credit to both for the original idea and code.
// New in this version: Pine v6 rewrite, a 0-100 fear/greed gauge with labelled FEAR / GREED zones, text labels
// (FEAR SPIKE, FEAR FADING, GREED FADING) on the gauge and optionally on the price chart, a public regime filter,
// a live status label, alerts, and a forward-return stats table.
//@version=6
indicator("Market Fear Meter", shorttitle = "Fear Meter", overlay = false, precision = 0, max_labels_count = 500)
// ───────── Inputs: original CM_Williams_Vix_Fix settings (same defaults) ─────────
grpW = "Williams VIX Fix (original settings)"
pd = input.int(22, "Lookback for highest close", minval = 2, group = grpW)
bbl = input.int(20, "Bollinger length", minval = 2, group = grpW)
mult = input.float(2.0, "Bollinger std dev", minval = 0.5, maxval = 5, step = 0.1, group = grpW)
lb = input.int(50, "Percentile lookback", minval = 5, group = grpW)
ph = input.float(0.85, "Highest percentile (0.85 = 85%)", minval = 0.5, maxval = 1.0, step = 0.01, group = grpW)
// ───────── Inputs: gauge ─────────
grpG = "Fear / greed gauge"
rankLen = input.int(252, "Ranking window (bars)", minval = 50, group = grpG, tooltip = "WVF and stretch are ranked against this many past bars. 252 = about one trading year on a daily chart.")
stretchMA = input.int(50, "Stretch MA length", minval = 5, group = grpG, tooltip = "Greed side: how far price is above this moving average, ranked.")
useVix = input.bool(false, "Blend in CBOE VIX percentile (US index/ETF charts)", group = grpG)
vixSym = input.symbol("CBOE:VIX", "VIX symbol", group = grpG)
fearLvl = input.int(20, "FEAR zone: gauge at or below", minval = 1, maxval = 45, group = grpG)
greedLvl = input.int(80, "GREED zone: gauge at or above", minval = 55, maxval = 99, group = grpG)
// ───────── Inputs: entries + regime ─────────
grpR = "Fear-fading signals and regime filter"
fadeBars = input.int(1, "Bars after the spike ends", minval = 1, maxval = 5, group = grpR, tooltip = "FEAR FADING prints when the original WVF spike condition was true and has now been false for this many bars.")
regime = input.string("Above 200 MA or shallow drawdown", "Regime filter", options = ["Off", "Above 200 MA", "Above 200 MA or shallow drawdown"], group = grpR)
maLen = input.int(200, "Trend MA length", minval = 20, group = grpR)
ddMax = input.float(20.0, "Shallow drawdown = less than (%)", minval = 1, maxval = 80, group = grpR)
ddLen = input.int(252, "Drawdown lookback (bars)", minval = 20, group = grpR)
// ───────── Inputs: labels + stats ─────────
grpL = "Labels"
showLbl = input.bool(true, "Text labels on the gauge", group = grpL)
onPrice = input.bool(false, "Also show labels on the price chart", group = grpL)
showSpk = input.bool(true, "Show FEAR SPIKE labels", group = grpL)
showBlk = input.bool(true, "Show filtered-out signals (grey)", group = grpL)
grpS = "Stats table"
showTbl = input.bool(true, "Show stats table", group = grpS)
fwd1 = input.int(20, "Horizon 1 (bars)", minval = 1, group = grpS)
fwd2 = input.int(60, "Horizon 2 (bars)", minval = 1, group = grpS)
// ───────── Original WVF (ChrisMoody) ─────────
hiClose = ta.highest(close, pd)
wvf = (hiClose - low) / hiClose * 100
sDev = mult * ta.stdev(wvf, bbl)
midLine = ta.sma(wvf, bbl)
upperBand = midLine + sDev
rangeHigh = ta.highest(wvf, lb) * ph
spike = wvf >= upperBand or wvf >= rangeHigh // original "lime bar" condition
// ───────── Gauge: 0 = extreme fear, 100 = extreme greed ─────────
fearRank = ta.percentrank(wvf, rankLen) // high WVF = fear
stretch = close / ta.sma(close, stretchMA) - 1
stretchRank = ta.percentrank(stretch, rankLen) // far above MA = greed
vixClose = request.security(vixSym, timeframe.period, close) // same timeframe as the chart, no lookahead
vixRank = ta.percentrank(vixClose, rankLen)
fearSide = useVix and not na(vixRank) ? (fearRank + vixRank) / 2 : fearRank
gauge = 0.5 * (100 - fearSide) + 0.5 * stretchRank
// ───────── Regime (public inputs only: 200 MA and drawdown) ─────────
trendMA = ta.sma(close, maLen)
ddPct = (close / ta.highest(high, ddLen) - 1) * 100
regimeOK = switch regime
"Off" => true
"Above 200 MA" => close > trendMA
=> close > trendMA or ddPct > -ddMax
// ───────── Signals ─────────
spikeEnded = not spike and ta.barssince(spike) == fadeBars
fearFade = spikeEnded and regimeOK
fadeBlocked = spikeEnded and not regimeOK
greedFade = ta.crossunder(gauge, greedLvl) // leaving the GREED zone
inFear = gauge <= fearLvl
inGreed = gauge >= greedLvl
spikeStart = spike and not spike[1]
// ───────── Forward-return stats (signal close -> close N bars later) ─────────
var array<int> sigBar = array.new<int>()
var array<float> sigPx = array.new<float>()
var float s1 = 0.0
var int n1 = 0
var int w1 = 0
var float s2 = 0.0
var int n2 = 0
var int w2 = 0
var float b1 = 0.0
var int bn1 = 0
var float b2 = 0.0
var int bn2 = 0
if fearFade
sigBar.push(bar_index)
sigPx.push(close)
if sigBar.size() > 0
for i = 0 to sigBar.size() - 1
age = bar_index - sigBar.get(i)
r = close / sigPx.get(i) - 1
if age == fwd1
s1 += r
n1 += 1
w1 += r > 0 ? 1 : 0
if age == fwd2
s2 += r
n2 += 1
w2 += r > 0 ? 1 : 0
if bar_index >= fwd2
b1 += close / close[fwd1] - 1
bn1 += 1
b2 += close / close[fwd2] - 1
bn2 += 1
// ───────── Gauge pane: zones, line, labels ─────────
C_FEAR = #26a69a // fear zone = green (historically a buy-watch zone)
C_GREED = #ef5350 // greed zone = red (historically a caution zone)
h0 = hline(0, "0", color.new(color.gray, 100))
hFear = hline(fearLvl, "Fear zone edge", color.new(C_FEAR, 50), hline.style_dashed)
h50 = hline(50, "Neutral", color.new(color.gray, 75), hline.style_dotted)
hGreed = hline(greedLvl, "Greed zone edge", color.new(C_GREED, 50), hline.style_dashed)
h100 = hline(100, "100", color.new(color.gray, 100))
fill(h0, hFear, color = color.new(C_FEAR, 85), title = "FEAR zone")
fill(hGreed, h100, color = color.new(C_GREED, 85), title = "GREED zone")
gCol = inFear ? C_FEAR : inGreed ? C_GREED : color.silver
plot(gauge, "Fear / greed gauge", gCol, 2)
addLbl(float yG, string txt, color col, bool atTop, string sz) =>
if showLbl
label.new(bar_index, yG, txt, color = col, textcolor = color.white, style = atTop ? label.style_label_down : label.style_label_up, size = sz)
if onPrice
label.new(bar_index, atTop ? high : low, txt, yloc = atTop ? yloc.abovebar : yloc.belowbar, color = col, textcolor = color.white, style = atTop ? label.style_label_down : label.style_label_up, size = sz, force_overlay = true)
if fearFade
addLbl(gauge, "FEAR FADING", C_FEAR, false, size.small)
if showBlk and fadeBlocked
addLbl(gauge, "fear fading\n(filtered: weak trend)", color.new(color.gray, 20), false, size.tiny)
if showSpk and spikeStart
addLbl(gauge, "FEAR SPIKE", color.new(#00695c, 0), false, size.tiny)
if greedFade
addLbl(gauge, "GREED FADING", C_GREED, true, size.small)
// zone names + status at the right edge
zoneName(float g) => g <= fearLvl ? "Extreme fear" : g < 40 ? "Fear" : g <= 60 ? "Neutral" : g < greedLvl ? "Greed" : "Extreme greed"
var label lFear = na
var label lGreed = na
var label lNow = na
if barstate.islast
label.delete(lFear)
label.delete(lGreed)
label.delete(lNow)
// zone names at the right edge (skipped when the status label already sits in that zone, so they never overlap)
lFear := inFear ? na : label.new(bar_index + 3, fearLvl / 2.0, "FEAR", color = color.new(color.black, 100), textcolor = C_FEAR, style = label.style_label_left, size = size.normal)
lGreed := inGreed ? na : label.new(bar_index + 3, (greedLvl + 100) / 2.0, "GREED", color = color.new(color.black, 100), textcolor = C_GREED, style = label.style_label_left, size = size.normal)
lNow := label.new(bar_index + 1, gauge, "Now: " + zoneName(gauge) + " " + str.tostring(math.round(gauge)), color = gCol, textcolor = color.white, style = label.style_label_left, size = size.normal)
// ───────── Alerts ─────────
alertcondition(spikeStart, "FEAR SPIKE", "Market Fear Meter: fear spike on {{ticker}}")
alertcondition(fearFade, "FEAR FADING", "Market Fear Meter: fear is fading on {{ticker}} (regime filter OK)")
alertcondition(inFear and not inFear[1], "Enters FEAR zone", "Market Fear Meter entered the FEAR zone on {{ticker}}")
alertcondition(greedFade, "GREED FADING", "Market Fear Meter left the GREED zone on {{ticker}}")
// ───────── Stats table ─────────
var table t = table.new(position.top_right, 4, 5, bgcolor = color.new(color.black, 25), border_width = 1)
fmt(x) => na(x) ? "-" : str.tostring(x * 100, "#.#") + "%"
if showTbl and barstate.islast
t.cell(0, 0, "After FEAR FADING: what happened next", text_color = color.white, text_size = size.small)
t.merge_cells(0, 0, 3, 0)
t.cell(0, 1, "Held for", text_color = color.silver, text_size = size.small)
t.cell(1, 1, "Signals", text_color = color.silver, text_size = size.small)
t.cell(2, 1, "Avg return", text_color = color.silver, text_size = size.small)
t.cell(3, 1, "% up", text_color = color.silver, text_size = size.small)
t.cell(0, 2, str.tostring(fwd1) + " bars", text_color = color.white, text_size = size.small)
t.cell(1, 2, str.tostring(n1), text_color = color.white, text_size = size.small)
t.cell(2, 2, fmt(n1 > 0 ? s1 / n1 : na), text_color = color.white, text_size = size.small)
t.cell(3, 2, n1 > 0 ? str.tostring(100.0 * w1 / n1, "#") + "%" : "-", text_color = color.white, text_size = size.small)
t.cell(0, 3, str.tostring(fwd2) + " bars", text_color = color.white, text_size = size.small)
t.cell(1, 3, str.tostring(n2), text_color = color.white, text_size = size.small)
t.cell(2, 3, fmt(n2 > 0 ? s2 / n2 : na), text_color = color.white, text_size = size.small)
t.cell(3, 3, n2 > 0 ? str.tostring(100.0 * w2 / n2, "#") + "%" : "-", text_color = color.white, text_size = size.small)
t.cell(0, 4, "Any bar (for comparison)", text_color = color.silver, text_size = size.small)
t.merge_cells(0, 4, 1, 4)
t.cell(2, 4, fmt(bn1 > 0 ? b1 / bn1 : na) + " / " + fmt(bn2 > 0 ? b2 / bn2 : na), text_color = color.silver, text_size = size.small)
t.cell(3, 4, str.tostring(fwd1) + " / " + str.tostring(fwd2) + " bars", text_color = color.silver, text_size = size.small)