本页源码来自 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/
// © BackQuant
//@version=6
indicator("Signed Realized Variance Trend [BackQuant]", shorttitle="Signed RV Trend [BackQuant]", overlay=false)
// Constants
const string calc = "Calculation"
const string trend = "Trend Settings"
const string ui = "Plotting and Coloring"
const color GREEN = #00ff00
const color RED = #ff0000
const color WHITE = #ffffff
// Inputs
float src = input.source(close, "Source", group=calc)
int energyLen = input.int(30, "Energy Length", minval=2, group=calc)
string smoothType = input.string("EMA", "Energy Smoothing", options=["EMA", "RMA"], group=calc)
int postSmooth = input.int(3, "Trend Smoothing", minval=1, group=calc, tooltip="Optional smoothing applied to the final signed variance ratio. Set to 1 to disable.")
float flipThreshold = input.float(0.10, "Trend Threshold", minval=0.0, maxval=1.0, step=0.01, group=trend, tooltip="Trend state only changes after the oscillator clears the opposite threshold.")
float strongLevel = input.float(0.65, "Strong Trend Level", minval=0.0, maxval=1.0, step=0.05, group=trend, inline="LVL")
float extremeLevel = input.float(0.85, "Extreme Level", minval=0.0, maxval=1.0, step=0.05, group=trend, inline="LVL")
bool showOsc = input.bool(true, "Show Oscillator", group=ui)
bool showStatic = input.bool(true, "Show Static Levels", group=ui)
bool showSignal = input.bool(true, "Show Smoothed Signal", group=ui)
int signalLen = input.int(8, "Signal Length", minval=1, group=ui)
bool paintCandles = input.bool(true, "Color Candles", group=ui)
color longCol = input.color(GREEN, "Bullish", group=ui, inline="COL")
color shortCol = input.color(RED, "Bearish", group=ui, inline="COL")
// Helpers
smooth(float source, int length, string mode) =>
switch mode
"EMA" => ta.ema(source, length)
"RMA" => ta.rma(source, length)
=> ta.ema(source, length)
// Returns
float logReturn = src > 0.0 and src[1] > 0.0 ? math.log(src / src[1]) : 0.0
// Realized Variance Energy
float returnSquared = logReturn * logReturn
float signedSquared = math.sign(logReturn) * returnSquared
float signedEnergy = smooth(signedSquared, energyLen, smoothType)
float totalEnergy = smooth(returnSquared, energyLen, smoothType)
// Signed Realized Variance Ratio
float rawTrend = totalEnergy > 0.0 ? signedEnergy / totalEnergy : 0.0
rawTrend := math.max(-1.0, math.min(1.0, rawTrend))
float rvTrend = postSmooth > 1 ? ta.ema(rawTrend, postSmooth) : rawTrend
float signal = ta.ema(rvTrend, signalLen)
// Persistent Trend State
var int trendState = 0
if trendState == 0
trendState := rvTrend >= 0.0 ? 1 : -1
else if trendState != 1 and rvTrend > flipThreshold
trendState := 1
else if trendState != -1 and rvTrend < -flipThreshold
trendState := -1
bool bullFlip = trendState == 1 and trendState[1] == -1
bool bearFlip = trendState == -1 and trendState[1] == 1
color trendCol = trendState == 1 ? longCol : shortCol
// Oscillator Gradient
float absTrend = math.abs(rvTrend)
color plotCol = WHITE
if absTrend >= extremeLevel
plotCol := WHITE
else if rvTrend > 0.0
if absTrend < 0.20
plotCol := color.new(#1dcaff, 70)
else if absTrend < 0.35
plotCol := color.new(#1e9b25, 65)
else if absTrend < 0.50
plotCol := color.new(longCol, 65)
else if absTrend < strongLevel
plotCol := color.new(longCol, 35)
else
plotCol := longCol
else if rvTrend < 0.0
if absTrend < 0.20
plotCol := color.new(#e65100, 70)
else if absTrend < 0.35
plotCol := color.new(#771515, 65)
else if absTrend < 0.50
plotCol := color.new(shortCol, 65)
else if absTrend < strongLevel
plotCol := color.new(shortCol, 35)
else
plotCol := shortCol
else
plotCol := color.new(WHITE, 70)
// Main Oscillator
plot(showOsc ? rvTrend : na, "Signed Realized Variance", plotCol, 3, plot.style_columns)
// Signal
plot(showSignal ? signal : na, "Signal", color.new(WHITE, 15), 2)
// Static Levels
pBullStrong = plot(showStatic ? strongLevel : na, "Bull Strong", color.new(longCol, 20), 1)
pBullExtreme = plot(showStatic ? extremeLevel : na, "Bull Extreme", color.new(longCol, 20), 1)
pBearStrong = plot(showStatic ? -strongLevel : na, "Bear Strong", color.new(shortCol, 20), 1)
pBearExtreme = plot(showStatic ? -extremeLevel : na, "Bear Extreme", color.new(shortCol, 20), 1)
fill(pBullStrong, pBullExtreme, showStatic ? color.new(longCol, 82) : na, title="Bull Extreme Zone")
fill(pBearStrong, pBearExtreme, showStatic ? color.new(shortCol, 82) : na, title="Bear Extreme Zone")
hline(0.0, "Zero", color.new(WHITE, 70))
// Candles
plotcandle(
open, high, low, close,
"Trend Candles",
trendCol,
trendCol,
true,
bordercolor=trendCol,
display=paintCandles ? display.all : display.none,
force_overlay=true)
// Data Window
plot(logReturn, "Log Return", display=display.data_window, editable=false)
plot(returnSquared, "Squared Return", display=display.data_window, editable=false)
plot(signedEnergy, "Signed Realized Variance", display=display.data_window, editable=false)
plot(totalEnergy, "Total Realized Variance", display=display.data_window, editable=false)
plot(rawTrend, "Raw Signed Variance Ratio", display=display.data_window, editable=false)
plot(rvTrend, "Signed Variance Trend", display=display.data_window, editable=false)
// Alerts
alertcondition(bullFlip, "Signed RV Bullish", "Signed Realized Variance Trend turned bullish on {{ticker}}")
alertcondition(bearFlip, "Signed RV Bearish", "Signed Realized Variance Trend turned bearish on {{ticker}}")