本页源码来自 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/
// © MAlphaCore
//@version=6
// M·Alpha·Fear & Greed — crypto sentiment from 0 (extreme fear) to 100 (extreme greed), computed from chart data only.
// Five parts, each scaled 0–100 against its own history on this chart: momentum (RSI), trend (distance from the
// moving average), volatility (high volatility = fear), volume pressure (volume on up vs. down candles) and
// BTC dominance (money fleeing into Bitcoin = fear for the rest of the market). Weights are adjustable.
indicator("M·Alpha·Fear & Greed", shorttitle = "M·Alpha·F&G", overlay = false, precision = 0)
// ───────────────────────────── Inputs ─────────────────────────────
grCalc = "Calculation"
lookback = input.int(365, "History for scaling (bars)", minval = 50, maxval = 4000, group = grCalc, tooltip = "Each part is ranked against this many past bars. 365 on the daily chart = one year.", display = display.none)
smoothLen = input.int(3, "Smoothing", minval = 1, maxval = 20, group = grCalc, display = display.none)
grParts = "Parts and weights (0 = off)"
wMom = input.float(25, "Momentum (RSI)", minval = 0, group = grParts, inline = "mom", display = display.none)
rsiLen = input.int(14, "Length", minval = 2, group = grParts, inline = "mom", display = display.none)
wTrend = input.float(25, "Trend (distance from MA)", minval = 0, group = grParts, inline = "trend", display = display.none)
maLen = input.int(50, "Length", minval = 2, group = grParts, inline = "trend", display = display.none)
wVol = input.float(20, "Volatility", minval = 0, group = grParts, inline = "vola", display = display.none)
volLen = input.int(30, "Length", minval = 2, group = grParts, inline = "vola", display = display.none)
wPress = input.float(20, "Volume pressure", minval = 0, group = grParts, inline = "press", display = display.none)
pressLen = input.int(14, "Length", minval = 2, group = grParts, inline = "press", display = display.none)
wDom = input.float(10, "BTC dominance", minval = 0, group = grParts, inline = "dom", display = display.none)
domLen = input.int(14, "Length", minval = 2, group = grParts, inline = "dom", display = display.none)
domSym = input.symbol("CRYPTOCAP:BTC.D", "Dominance symbol", group = grParts, display = display.none)
grZones = "Zones"
zExtFear = input.int(25, "Extreme fear below", minval = 1, maxval = 49, group = grZones, display = display.none)
zFear = input.int(45, "Fear below", minval = 1, maxval = 49, group = grZones, display = display.none)
zGreed = input.int(55, "Greed above", minval = 51, maxval = 99, group = grZones, display = display.none)
zExtGreed = input.int(75, "Extreme greed above", minval = 51, maxval = 99, group = grZones, display = display.none)
grStyle = "Style"
colGreed = input.color(#ffeb3b, "Greed", group = grStyle, display = display.none)
colFear = input.color(#b2b5be, "Fear", group = grStyle, display = display.none)
colText = input.color(#e8eaf0, "Text color", group = grStyle, display = display.none)
showTable = input.bool(true, "Show table", group = grStyle, display = display.none)
showParts = input.bool(true, "Show the five parts in the table", group = grStyle, display = display.none)
textSizeIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grStyle, display = display.none)
tablePosIn = input.string("Top right", "Table position", options = ["Top right", "Top left", "Bottom right", "Bottom left"], group = grStyle, display = display.none)
// ───────────────────────────── Calculation ─────────────────────────────
// momentum: RSI is already on a 0–100 scale
sMom = ta.rsi(close, rsiLen)
// trend: how far price is above or below its moving average, ranked against history
sTrend = ta.percentrank(close / ta.sma(close, maLen) - 1, lookback)
// volatility: realized volatility of log returns, ranked and inverted (calm = greed, turbulent = fear)
vola = ta.stdev(math.log(close / close[1]), volLen)
sVol = 100 - ta.percentrank(vola, lookback)
// volume pressure: share of volume on up candles minus down candles, mapped to 0–100
dirVol = volume * math.sign(close - open)
sumVol = math.sum(nz(volume), pressLen)
sumDir = math.sum(nz(dirVol), pressLen)
sPress = sumVol > 0 ? (sumDir / sumVol + 1) * 50 : na
// BTC dominance: rising dominance = fear, ranked and inverted
dom = request.security(domSym, timeframe.period, close, ignore_invalid_symbol = true)
sDom = 100 - ta.percentrank(ta.roc(dom, domLen), lookback)
// weighted mean of the parts that have data on this chart
used(float score, float w) => w > 0 and not na(score) ? w : 0.0
uMom = used(sMom, wMom)
uTrend = used(sTrend, wTrend)
uVol = used(sVol, wVol)
uPress = used(sPress, wPress)
uDom = used(sDom, wDom)
wSum = uMom + uTrend + uVol + uPress + uDom
vSum = uMom * nz(sMom) + uTrend * nz(sTrend) + uVol * nz(sVol) + uPress * nz(sPress) + uDom * nz(sDom)
raw = wSum > 0 ? vSum / wSum : na
fg = ta.ema(raw, smoothLen)
zone(float v) =>
na(v) ? "–" : v < zExtFear ? "Extreme fear" : v < zFear ? "Fear" : v <= zGreed ? "Neutral" : v <= zExtGreed ? "Greed" : "Extreme greed"
// ───────────────────────────── Plot ─────────────────────────────
fgColor = color.from_gradient(nz(fg, 50), 0, 100, colFear, colGreed)
plot(fg, "Fear & Greed", fgColor, 2)
hTop = hline(100, "", color.new(colText, 100))
hExtG = hline(zExtGreed, "Extreme greed", color.new(colGreed, 50), hline.style_dashed)
hMid = hline(50, "Neutral", color.new(colText, 75), hline.style_dotted)
hExtF = hline(zExtFear, "Extreme fear", color.new(colFear, 50), hline.style_dashed)
hBot = hline(0, "", color.new(colText, 100))
fill(hTop, hExtG, color.new(colGreed, 92))
fill(hExtF, hBot, color.new(colFear, 88))
// ───────────────────────────── Alerts ─────────────────────────────
alertcondition(ta.crossunder(fg, zExtFear), "Extreme fear", "M·Alpha·Fear & Greed: extreme fear on {{ticker}} ({{plot_0}})")
alertcondition(ta.crossover(fg, zExtGreed), "Extreme greed", "M·Alpha·Fear & Greed: extreme greed on {{ticker}} ({{plot_0}})")
alertcondition(ta.crossover(fg, zExtFear), "Leaving extreme fear", "M·Alpha·Fear & Greed: leaving extreme fear on {{ticker}} ({{plot_0}})")
alertcondition(ta.crossunder(fg, zExtGreed), "Leaving extreme greed", "M·Alpha·Fear & Greed: leaving extreme greed on {{ticker}} ({{plot_0}})")
// ───────────────────────────── Table ─────────────────────────────
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
tablePos = tablePosIn == "Top left" ? position.top_left : tablePosIn == "Bottom right" ? position.bottom_right : tablePosIn == "Bottom left" ? position.bottom_left : position.top_right
var table t = table.new(tablePos, 2, 7, bgcolor = color.new(#0f1218, 15), border_width = 0)
partRow(int row, string name, float score, float w) =>
off = w <= 0 or na(score)
table.cell(t, 0, row, name, text_color = colText, text_size = textSize, text_halign = text.align_left)
table.cell(t, 1, row, off ? (w <= 0 ? "off" : "no data") : str.tostring(score, "#"), text_color = off ? colFear : color.from_gradient(score, 0, 100, colFear, colGreed), text_size = textSize)
if barstate.islast and showTable
table.cell(t, 0, 0, "M·Alpha·Fear & Greed", text_color = colText, text_size = textSize, text_halign = text.align_left)
table.cell(t, 1, 0, na(fg) ? "warming up" : str.tostring(fg, "#") + " · " + zone(fg), text_color = fgColor, text_size = textSize)
if showParts
partRow(1, "Momentum", sMom, wMom)
partRow(2, "Trend", sTrend, wTrend)
partRow(3, "Volatility", sVol, wVol)
partRow(4, "Volume pressure", sPress, wPress)
partRow(5, "BTC dominance", sDom, wDom)