RangePulse: Volatility-Normalized Breakout Strength

TheOnePct · study · 101 行 · 点赞 27 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © shehap20001
//
// RangePulse — Meyers Adaptive Range Expansion  v1.0
// Based on the "Adaptive Intraday Breakout System" in Perry J. Kaufman, Trading Systems and Methods (5th ed.),
// pp. 796-797, from Dennis Meyers, "Range Roving", Active Trader (March 2003).
// Indicator adaptation (no orders, no session-close exit). The low-range sign is mirrored by default:
// the printed formula (low - high[n]) never triggers the bearish condition.
// With exponent a = 0.5 and the rolling max switched off, the two lines equal the classic Random Walk Index.
//@version=6
indicator("RangePulse — Meyers Adaptive Range Expansion", shorttitle = "RangePulse", overlay = false, precision = 2)
 
// ───────────────────────────────────────────── inputs
const string P_TUNED  = "Tuned (n15, a0.45)"
const string P_BOOK   = "Meyers QQQ intraday (n6, a0.75)"
const string P_CUSTOM = "Custom"
const string LR_MIR   = "Mirrored: high[n] - low (recommended)"
const string LR_BOOK  = "literal: low - high[n] (as printed)"
 
const string G_CALC = "Calculation"
const string G_VIS  = "Display"
 
string presetIn = input.string(P_TUNED, "Preset", options = [P_TUNED, P_BOOK, P_CUSTOM], group = G_CALC,
     tooltip = "Tuned: chosen on 2006-2022 gold and BTC data (1H-1D), checked on 2023-2026.\nBook: Meyers' QQQ optimisation.\nCustom: uses the four values below. The four values are ignored unless Custom is selected.")
int    nIn  = input.int(15, "Lookback n", minval = 2, maxval = 200, group = G_CALC,
     tooltip = "Bars between today and the reference bar. Also the ATR length and the rolling-max window.")
float  aIn  = input.float(0.45, "Exponent a", minval = 0.0, maxval = 1.5, step = 0.05, group = G_CALC,
     tooltip = "Ranges are divided by n^a so that one threshold works for different n. 0.5 = random-walk scaling (RWI). At a fixed n, changing a only rescales the lines.")
float  thIn = input.float(1.75, "High-range threshold (TH)", minval = 0.0, step = 0.05, group = G_CALC,
     tooltip = "Bullish needs High-range max above TH. Bearish needs it below TH.")
float  tlIn = input.float(1.60, "Low-range threshold (TL)", minval = 0.0, step = 0.05, group = G_CALC,
     tooltip = "Bearish needs Low-range max above TL. Bullish needs it below TL.")
string lrIn = input.string(LR_MIR, "Low-range formula", options = [LR_MIR, LR_BOOK], group = G_CALC,
     tooltip = "The printed formula gives a negative low range in a falling market, so the bearish condition never fires. Mirrored follows the Random Walk Index convention.")
bool   useMaxIn = input.bool(true, "Rolling max over n bars (Meyers)", group = G_CALC,
     tooltip = "On: each line is the highest normalized range of the last n bars (Meyers). Off with a = 0.5: classic Random Walk Index.")
 
bool showBg      = input.bool(true, "Shade bullish / bearish states", group = G_VIS)
bool showMarks   = input.bool(true, "Mark the first bar of each state", group = G_VIS)
bool marksOnMain = input.bool(true, "Show the marks on the price chart", group = G_VIS)
bool showTable   = input.bool(true, "Status table", group = G_VIS)
color cBull = input.color(#26a69a, "Bullish", group = G_VIS, inline = "c")
color cBear = input.color(#ef5350, "Bearish", group = G_VIS, inline = "c")
 
// ───────────────────────────────────────────── settings from preset
bool isCustom = presetIn == P_CUSTOM
bool isBook   = presetIn == P_BOOK
int   n  = isCustom ? nIn  : isBook ? 6    : 15
float a  = isCustom ? aIn  : isBook ? 0.75 : 0.45
float th = isCustom ? thIn : isBook ? 0.50 : 1.75
float tl = isCustom ? tlIn : isBook ? 1.05 : 1.60
 
// ───────────────────────────────────────────── calculation (Kaufman p. 796)
float atrN = ta.atr(n)
float hr   = high - low[n]                                        // HR = H(t) - L(t-n)
float lr   = lrIn == LR_MIR ? high[n] - low : low - high[n]       // LR (mirrored or as printed)
float den  = math.pow(n, a) * atrN
float nhr  = den > 0 ? hr / den : na                              // NHR = HR / n^a / ATR(n)
float nlr  = den > 0 ? lr / den : na                              // NLR = LR / n^a / ATR(n)
float nhrMax = ta.highest(nhr, n)                                 // computed every bar (Pine consistency)
float nlrMax = ta.highest(nlr, n)
float hMax = useMaxIn ? nhrMax : nhr                              // HRmax = highest(NHR, n)
float lMax = useMaxIn ? nlrMax : nlr                              // LRmax = highest(NLR, n)
 
// ───────────────────────────────────────────── states (Kaufman p. 797)
bool bull = hMax > th and lMax < tl
bool bear = lMax > tl and hMax < th
bool bullStart = bull and not bull[1] and barstate.isconfirmed
bool bearStart = bear and not bear[1] and barstate.isconfirmed
 
// ───────────────────────────────────────────── plots
plot(hMax, "High-range max", color = cBull, linewidth = 2)
plot(lMax, "Low-range max",  color = cBear, linewidth = 2)
hline(th, "TH", color = cBull, linestyle = hline.style_dashed)
hline(tl, "TL", color = cBear, linestyle = hline.style_dashed)
 
bgcolor(showBg and bull ? color.new(cBull, 88) : showBg and bear ? color.new(cBear, 88) : na, title = "State shading")
 
plotshape(showMarks and not marksOnMain and bullStart, "Bullish start (pane)", shape.triangleup,   location.bottom, cBull, size = size.tiny)
plotshape(showMarks and not marksOnMain and bearStart, "Bearish start (pane)", shape.triangledown, location.top,    cBear, size = size.tiny)
plotshape(showMarks and marksOnMain and bullStart, "Bullish start (chart)", shape.triangleup,   location.belowbar, cBull, size = size.tiny, force_overlay = true)
plotshape(showMarks and marksOnMain and bearStart, "Bearish start (chart)", shape.triangledown, location.abovebar, cBear, size = size.tiny, force_overlay = true)
 
// ───────────────────────────────────────────── alerts (closed bars only)
alertcondition(bullStart, "RangePulse bullish start", "RangePulse: bullish range expansion started on {{ticker}} {{interval}} at {{close}}")
alertcondition(bearStart, "RangePulse bearish start", "RangePulse: bearish range expansion started on {{ticker}} {{interval}} at {{close}}")
alertcondition(bullStart or bearStart, "RangePulse any start", "RangePulse: new state on {{ticker}} {{interval}} at {{close}}")
 
// ───────────────────────────────────────────── status table
var table tbl = table.new(position.top_right, 2, 4, bgcolor = color.new(color.gray, 85), border_width = 1)
if showTable and barstate.islast
    string st  = bull ? "Bullish" : bear ? "Bearish" : "Neutral"
    color  stc = bull ? cBull : bear ? cBear : color.gray
    table.cell(tbl, 0, 0, "State", text_color = chart.fg_color, text_size = size.small)
    table.cell(tbl, 1, 0, st, text_color = stc, text_size = size.small)
    table.cell(tbl, 0, 1, "High-range max", text_color = chart.fg_color, text_size = size.small)
    table.cell(tbl, 1, 1, str.tostring(hMax, "#.##") + " / " + str.tostring(th, "#.##"), text_color = cBull, text_size = size.small)
    table.cell(tbl, 0, 2, "Low-range max", text_color = chart.fg_color, text_size = size.small)
    table.cell(tbl, 1, 2, str.tostring(lMax, "#.##") + " / " + str.tostring(tl, "#.##"), text_color = cBear, text_size = size.small)
    table.cell(tbl, 0, 3, "Settings", text_color = chart.fg_color, text_size = size.small)
    table.cell(tbl, 1, 3, "n " + str.tostring(n) + "  a " + str.tostring(a, "#.##"), text_color = chart.fg_color, text_size = size.small)

← 返回列表