本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Aurora Flow | Regime & Volatility", shorttitle = "RTG", overlay = true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 30, commission_type = strategy.commission.percent, commission_value = 0.05, slippage = 2, pyramiding = 0, calc_on_every_tick = false)
// Aurora
// - G-Score: smoothed price Z-score vs. a volatility-Z envelope -> bull / bear state
// - Trend filter: price must be on the right side of the trend EMA
// - Regime filter: skip choppy markets (low path efficiency) and volatility spikes
// - Exit: trailing ATR stop, or the G-Score state flipping against the trade
// ─────────────────────────────── Inputs ───────────────────────────────────
string G_SIG = "Signal"
string G_REG = "Regime Filter"
string G_RISK = "Trade Management"
string G_VIS = "Visuals"
float src = input.source(hlcc4, "Source", group = G_SIG)
int zLen = input.int(48, "Z-Score length", minval = 5, group = G_SIG)
int smoothLen = input.int(8, "Smoothing", minval = 1, group = G_SIG)
float volDecay = input.float(0.94, "Volatility decay (EWMA)", minval = 0.5, maxval = 0.99, step = 0.01, group = G_SIG)
float minThr = input.float(0.5, "Min threshold", minval = 0.1, step = 0.05, group = G_SIG, tooltip = "Floor for the G-Score envelope. Higher = fewer, stronger signals.")
int trendLen = input.int(50, "Trend EMA length", minval = 5, group = G_SIG)
int atrLen = input.int(14, "ATR length", minval = 2, group = G_REG)
int effLen = input.int(30, "Efficiency length", minval = 5, group = G_REG)
float minEff = input.float(0.20, "Min path efficiency (skip chop)", minval = 0.01, maxval = 1.0, step = 0.01, group = G_REG)
float shockRatio = input.float(1.8, "Max ATR / baseline (skip spikes)", minval = 1.1, step = 0.1, group = G_REG)
float stopMult = input.float(2.5, "Trailing stop (ATR)", minval = 0.5, step = 0.1, group = G_RISK)
bool allowLongs = input.bool(true, "Allow longs", group = G_RISK)
bool allowShorts = input.bool(false, "Allow shorts", group = G_RISK)
string colMode = input.string("Standard", "Color mode", options = ["Standard", "Nordic", "Simple"], group = G_VIS)
float bandMult = input.float(1.5, "Fade band width (ATR)", minval = 0.5, step = 0.1, group = G_VIS)
bool showCandles = input.bool(true, "Color candles", group = G_VIS)
bool showDash = input.bool(true, "Dashboard", group = G_VIS)
[colUp, colDn, colNu] = switch colMode
"Standard" => [color.rgb(0, 255, 200), color.rgb(32, 94, 144), color.gray]
"Nordic" => [color.rgb(46, 161, 255), color.rgb(150, 154, 169), color.gray]
"Simple" => [color.lime, color.red, color.gray]
// ─────────────────────────────── Calculations ─────────────────────────────
zScoreOf(float s, int length) =>
(s - ta.sma(s, length)) / ta.stdev(s, length)
float atr = ta.atr(atrLen)
float trendEma = ta.ema(close, trendLen)
// Regime evidence
float travel = math.sum(math.abs(ta.change(close)), effLen)
float eff = travel > syminfo.mintick ? math.min(math.abs(close - close[effLen]) / travel, 1.0) : na
float atrBase = ta.sma(atr, 50)
float volRatio = atrBase > syminfo.mintick ? atr / atrBase : na
// Volatility (EWMA of squared log returns)
float logRet = nz(math.log(src / src[1]), 0.0)
var float ewVar = na
ewVar := volDecay * nz(ewVar[1], logRet * logRet) + (1.0 - volDecay) * logRet * logRet
float vol = math.sqrt(ewVar)
// G-Score
float zPrice = ta.ema(zScoreOf(src, zLen), smoothLen)
float zVol = ta.ema(zScoreOf(vol, zLen), smoothLen * 2)
float thr = math.max(math.abs(zVol), minThr)
int nal = 0
nal := zPrice > thr ? 1 : zPrice < -thr ? -1 : nz(nal[1], 0)
bool ready = not na(zPrice) and not na(thr) and not na(eff) and not na(volRatio)
bool tradable = ready and eff >= minEff and volRatio < shockRatio
string regimeText = not ready ? "WARM-UP" : volRatio >= shockRatio ? "VOLATILE" : eff < minEff ? "CHOP" : "TREND"
// ─────────────────────────────── Signals & orders ─────────────────────────
bool longSetup = tradable and nal == 1 and close > trendEma
bool shortSetup = tradable and nal == -1 and close < trendEma
bool goLong = allowLongs and longSetup and not longSetup[1] and strategy.position_size == 0
bool goShort = allowShorts and shortSetup and not shortSetup[1] and strategy.position_size == 0
if goLong
strategy.entry("Long", strategy.long)
if goShort
strategy.entry("Short", strategy.short)
// Trailing ATR stop (ratchets in the trade's favor only)
var float stopLevel = na
if strategy.position_size > 0
stopLevel := na(stopLevel) ? close - atr * stopMult : math.max(stopLevel, close - atr * stopMult)
strategy.exit("Long Stop", "Long", stop = stopLevel)
else if strategy.position_size < 0
stopLevel := na(stopLevel) ? close + atr * stopMult : math.min(stopLevel, close + atr * stopMult)
strategy.exit("Short Stop", "Short", stop = stopLevel)
else
stopLevel := na
// State-flip exits
if strategy.position_size > 0 and nal == -1
strategy.close("Long", comment = "State flip")
if strategy.position_size < 0 and nal == 1
strategy.close("Short", comment = "State flip")
// ─────────────────────────────── Visuals ──────────────────────────────────
// Ombre color: bear -> neutral -> bull, driven by how far the G-Score is into its envelope
float zNorm = ready ? math.max(-1.0, math.min(1.0, zPrice / thr)) : 0.0
color flowColor = zNorm >= 0 ? color.from_gradient(zNorm, 0.0, 1.0, colNu, colUp) : color.from_gradient(zNorm, -1.0, 0.0, colDn, colNu)
int dim = tradable ? 0 : 55
float spine = trendEma
float bandUp = spine + atr * bandMult
float bandLo = spine - atr * bandMult
pUp = plot(bandUp, "Upper edge", color.new(flowColor, 100))
pLo = plot(bandLo, "Lower edge", color.new(flowColor, 100))
plot(spine, "Glow outer", color.new(flowColor, 90 + (tradable ? 0 : 10)), 12)
plot(spine, "Glow inner", color.new(flowColor, 80 + (tradable ? 0 : 15)), 6)
pMid = plot(spine, "Trend spine", color.new(flowColor, dim), 2)
fill(pUp, pMid, bandUp, spine, color.new(flowColor, 100), color.new(flowColor, 78), title = "Upper fade")
fill(pMid, pLo, spine, bandLo, color.new(flowColor, 78), color.new(flowColor, 100), title = "Lower fade")
barcolor(showCandles ? flowColor : na)
// ─────────────────────────────── Dashboard ────────────────────────────────
var table dash = table.new(position.top_right, 2, 4, bgcolor = color.new(color.black, 10), frame_color = color.new(color.gray, 60), frame_width = 1, border_color = color.new(color.gray, 80), border_width = 1)
if barstate.islast and showDash
color regColor = regimeText == "TREND" ? colUp : regimeText == "VOLATILE" ? color.rgb(239, 92, 72) : color.gray
string posText = strategy.position_size > 0 ? "LONG" : strategy.position_size < 0 ? "SHORT" : "FLAT"
table.cell(dash, 0, 0, "Regime", text_color = color.silver, text_size = size.small)
table.cell(dash, 1, 0, regimeText, text_color = regColor, text_size = size.small)
table.cell(dash, 0, 1, "G-Score", text_color = color.silver, text_size = size.small)
table.cell(dash, 1, 1, nal == 1 ? "BULL" : nal == -1 ? "BEAR" : "NEUTRAL", text_color = flowColor, text_size = size.small)
table.cell(dash, 0, 2, "Efficiency", text_color = color.silver, text_size = size.small)
table.cell(dash, 1, 2, str.tostring(eff, "#.00"), text_color = color.gray, text_size = size.small)
table.cell(dash, 0, 3, "Position", text_color = color.silver, text_size = size.small)
table.cell(dash, 1, 3, posText, text_color = strategy.position_size > 0 ? colUp : strategy.position_size < 0 ? colDn : color.gray, text_size = size.small)