本页源码来自 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/
// © Holtermorter99
//
// CPR & Camarilla Stats — the Central Pivot Range and the Camarilla levels of the previous day, and statistics of
// the chart itself: how often days reach the Camarilla L3/L4 levels and close beyond L4, and whether days with a
// narrow CPR really have larger ranges than days with a wide one. Levels come from completed days only.
//@version=6
indicator("CPR & Camarilla Stats [Holtermorter99]", "CPRC", overlay = true)
// ─────────────────────────────── Inputs ───────────────────────────────
const string G_LVL = "Levels"
const string G_STAT = "Statistics"
const string G_DISP = "Display"
const string G_COL = "Colors"
bool showCpr = input.bool(true, "Show CPR (TC / Pivot / BC)", group = G_LVL)
bool showL34 = input.bool(true, "Show Camarilla L3 / L4", group = G_LVL)
bool showL12 = input.bool(false, "Show Camarilla L1 / L2", group = G_LVL)
int rankLength = input.int(50, "CPR Width Lookback (days)", minval = 10, maxval = 250, group = G_STAT, tooltip = "Each day's CPR width is ranked against the CPRs of this many previous days (percentile, completed days only).")
int narrowPct = input.int(25, "Narrow CPR (≤ percentile)", minval = 1, maxval = 49, group = G_STAT, tooltip = "A CPR whose width percentile is at or below this value is narrow.")
int widePct = input.int(75, "Wide CPR (≥ percentile)", minval = 51, maxval = 99, group = G_STAT, tooltip = "A CPR whose width percentile is at or above this value is wide.")
int adrLength = input.int(14, "ADR Length (days)", minval = 2, maxval = 100, group = G_STAT, tooltip = "Average high-low range of the previous N completed days. Day ranges are compared with the ADR known at the day's open.")
bool showDashboard = input.bool(true, "Show Dashboard", group = G_DISP)
string dashPosInput = input.string("Top Right", "Dashboard Position", options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], inline = "dash", group = G_DISP)
string dashSizeInput = input.string("Small", "", options = ["Tiny", "Small", "Normal"], inline = "dash", group = G_DISP)
color cprColor = input.color(#2962FF, "CPR", group = G_COL)
color upColor = input.color(#26A69A, "Upper Levels", group = G_COL)
color downColor = input.color(#EF5350, "Lower Levels", group = G_COL)
// ─────────────────────────────── Previous-day data ───────────────────────────────
// CPR width (in % of the pivot) of the CPR that a day's high/low/close produce for the NEXT day.
cprWidthPct(float h, float l, float c) =>
float p = (h + l + c) / 3.0
float bc = (h + l) / 2.0
float tc = 2.0 * p - bc
p != 0 ? 100.0 * math.abs(tc - bc) / p : na
// Everything from COMPLETED days: the previous daily bar with lookahead on (no repainting).
// rank: percentile of today's CPR width among the CPRs of the last rankLength days (including today's).
[pH, pL, pC, pAdr, pRank] = request.security(syminfo.tickerid, "D", [high[1], low[1], close[1], ta.sma(high - low, adrLength)[1], ta.percentrank(cprWidthPct(high, low, close), rankLength)[1]], lookahead = barmerge.lookahead_on)
// Intraday charts with standard candles only (Heikin Ashi, Renko, etc. have synthetic prices).
bool intraday = timeframe.isintraday and chart.is_standard
bool newDay = timeframe.change("D")
float pivot = (pH + pL + pC) / 3.0
float bcRaw = (pH + pL) / 2.0
float tcRaw = 2.0 * pivot - bcRaw
float tc = math.max(tcRaw, bcRaw)
float bc = math.min(tcRaw, bcRaw)
float rng = pH - pL
float r1 = pC + rng * 1.1 / 12.0
float r2 = pC + rng * 1.1 / 6.0
float r3 = pC + rng * 1.1 / 4.0
float r4 = pC + rng * 1.1 / 2.0
float s1 = pC - rng * 1.1 / 12.0
float s2 = pC - rng * 1.1 / 6.0
float s3 = pC - rng * 1.1 / 4.0
float s4 = pC - rng * 1.1 / 2.0
float widthPct = pivot != 0 ? 100.0 * (tc - bc) / pivot : na
// ─────────────────────────────── Day tracking and statistics ───────────────────────────────
var float dHi = na
var float dLo = na
var bool dayFull = false
// Levels and classification frozen at the start of the day (they are constant during the day anyway).
var float dR3 = na
var float dR4 = na
var float dS3 = na
var float dS4 = na
var float dTc = na
var float dBc = na
var float dAdr = na
var float dRank = na
var float lastClose = na
var bool dTouch = false // some bar of the day traded inside the CPR
var int nDays = 0
var int hitR3 = 0
var int hitR4 = 0
var int hitS3 = 0
var int hitS4 = 0
var int closeAboveR4 = 0
var int closeBelowS4 = 0
var int touchCpr = 0
// Range / ADR sums by CPR class.
var float sumN = 0.0
var int cntN = 0
var float sumW = 0.0
var int cntW = 0
var float sumA = 0.0
var int cntA = 0
if intraday
if newDay
// Finalize the previous day (only complete days with all inputs known).
if dayFull and not na(dR4) and not na(dAdr) and dAdr > 0 and not na(dRank)
nDays += 1
hitR3 += dHi >= dR3 ? 1 : 0
hitR4 += dHi >= dR4 ? 1 : 0
hitS3 += dLo <= dS3 ? 1 : 0
hitS4 += dLo <= dS4 ? 1 : 0
closeAboveR4 += lastClose > dR4 ? 1 : 0
closeBelowS4 += lastClose < dS4 ? 1 : 0
touchCpr += dTouch ? 1 : 0
float ratio = (dHi - dLo) / dAdr
sumA += ratio
cntA += 1
if dRank <= narrowPct
sumN += ratio
cntN += 1
else if dRank >= widePct
sumW += ratio
cntW += 1
dHi := high
dLo := low
dR3 := r3
dR4 := r4
dS3 := s3
dS4 := s4
dTc := tc
dBc := bc
dAdr := pAdr
dRank := pRank
dayFull := bar_index > 0
dTouch := high >= bc and low <= tc
else
dHi := math.max(nz(dHi, high), high)
dLo := math.min(nz(dLo, low), low)
dTouch := dTouch or (high >= dBc and low <= dTc)
lastClose := close
// ─────────────────────────────── Plots ───────────────────────────────
// Lines break at each new day (na on its first bar) so days are not joined by vertical segments.
bool showNow = intraday and not newDay
pTc = plot(showNow and showCpr ? tc : na, "TC", color = color.new(cprColor, 30), style = plot.style_linebr)
plot(showNow and showCpr ? pivot : na, "Pivot", color = cprColor, style = plot.style_linebr, linewidth = 2)
pBc = plot(showNow and showCpr ? bc : na, "BC", color = color.new(cprColor, 30), style = plot.style_linebr)
fill(pTc, pBc, color = color.new(cprColor, 88), title = "CPR", fillgaps = false)
plot(showNow and showL34 ? r4 : na, "L4 Up (R4)", color = upColor, style = plot.style_linebr, linewidth = 2)
plot(showNow and showL34 ? r3 : na, "L3 Up (R3)", color = color.new(upColor, 30), style = plot.style_linebr)
plot(showNow and showL34 ? s3 : na, "L3 Down (S3)", color = color.new(downColor, 30), style = plot.style_linebr)
plot(showNow and showL34 ? s4 : na, "L4 Down (S4)", color = downColor, style = plot.style_linebr, linewidth = 2)
plot(showNow and showL12 ? r2 : na, "L2 Up (R2)", color = color.new(upColor, 60), style = plot.style_linebr)
plot(showNow and showL12 ? r1 : na, "L1 Up (R1)", color = color.new(upColor, 70), style = plot.style_linebr)
plot(showNow and showL12 ? s1 : na, "L1 Down (S1)", color = color.new(downColor, 70), style = plot.style_linebr)
plot(showNow and showL12 ? s2 : na, "L2 Down (S2)", color = color.new(downColor, 60), style = plot.style_linebr)
plot(widthPct, "CPR Width %", display = display.data_window)
plot(pRank, "CPR Width Percentile", display = display.data_window)
// Alerts on bar close: first close of the day beyond L4.
var bool brokeR4 = false
var bool brokeS4 = false
if newDay
brokeR4 := false
brokeS4 := false
bool alertR4 = barstate.isconfirmed and intraday and not na(r4) and close > r4 and not brokeR4
bool alertS4 = barstate.isconfirmed and intraday and not na(s4) and close < s4 and not brokeS4
if alertR4
brokeR4 := true
if alertS4
brokeS4 := true
alertcondition(alertR4, "Close above L4", "CPRC: first close of the day above Camarilla L4 on {{ticker}}")
alertcondition(alertS4, "Close below L4", "CPRC: first close of the day below Camarilla L4 (S4) on {{ticker}}")
if alertR4 or alertS4
alert("CPRC: first close of the day " + (alertR4 ? "above L4 (" + str.tostring(r4, format.mintick) + ")" : "below L4 (" + str.tostring(s4, format.mintick) + ")") + " on " + syminfo.ticker, alert.freq_once_per_bar_close)
// ─────────────────────────────── Dashboard ───────────────────────────────
dashPosition(string p) =>
switch p
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
"Bottom Left" => position.bottom_left
=> position.top_right
pct(int a, int n) =>
n > 0 ? str.tostring(100.0 * a / n, "#") + "%" : "—"
avgRatio(float s, int n) =>
n > 0 ? str.tostring(s / n, "0.00") + "× (n=" + str.tostring(n) + ")" : "—"
var table dash = na
string dashSize = dashSizeInput == "Tiny" ? size.tiny : dashSizeInput == "Normal" ? size.normal : size.small
if barstate.islast
table.delete(dash)
dash := na
if showDashboard
dash := table.new(dashPosition(dashPosInput), 3, 9, bgcolor = color.new(chart.bg_color, 10), frame_color = color.new(chart.fg_color, 80), frame_width = 1, border_color = color.new(chart.fg_color, 90), border_width = 1)
color dim = color.new(chart.fg_color, 30)
if not intraday
table.cell(dash, 0, 0, "CPRC: use an intraday chart with standard candles", text_color = downColor, text_size = dashSize)
else
string cls = na(pRank) ? "—" : pRank <= narrowPct ? "narrow" : pRank >= widePct ? "wide" : "normal"
table.cell(dash, 0, 0, "Today's CPR", text_color = chart.fg_color, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 0, str.tostring(widthPct, "#.###") + "% · " + cls + (na(pRank) ? "" : " (p" + str.tostring(pRank, "#") + ")"), text_color = chart.fg_color, text_size = dashSize)
table.merge_cells(dash, 1, 0, 2, 0)
table.cell(dash, 0, 1, "Days (n=" + str.tostring(nDays) + ")", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 1, "Up", text_color = upColor, text_size = dashSize)
table.cell(dash, 2, 1, "Down", text_color = downColor, text_size = dashSize)
table.cell(dash, 0, 2, "Reached L3", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 2, pct(hitR3, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 2, 2, pct(hitS3, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 0, 3, "Reached L4", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 3, pct(hitR4, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 2, 3, pct(hitS4, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 0, 4, "Closed beyond L4", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 4, pct(closeAboveR4, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 2, 4, pct(closeBelowS4, nDays), text_color = chart.fg_color, text_size = dashSize)
table.cell(dash, 0, 5, "Traded into CPR", text_color = dim, text_size = dashSize, text_halign = text.align_left, tooltip = "Share of days in which at least one bar's high-low range overlapped the CPR")
table.cell(dash, 1, 5, pct(touchCpr, nDays), text_color = chart.fg_color, text_size = dashSize)
table.merge_cells(dash, 1, 5, 2, 5)
table.cell(dash, 0, 6, "Range / ADR, narrow CPR", text_color = dim, text_size = dashSize, text_halign = text.align_left, tooltip = "Average day range divided by the ADR known at the open, for days whose CPR width percentile was ≤ " + str.tostring(narrowPct))
table.cell(dash, 1, 6, avgRatio(sumN, cntN), text_color = chart.fg_color, text_size = dashSize)
table.merge_cells(dash, 1, 6, 2, 6)
table.cell(dash, 0, 7, "Range / ADR, wide CPR", text_color = dim, text_size = dashSize, text_halign = text.align_left, tooltip = "Same for days whose CPR width percentile was ≥ " + str.tostring(widePct))
table.cell(dash, 1, 7, avgRatio(sumW, cntW), text_color = chart.fg_color, text_size = dashSize)
table.merge_cells(dash, 1, 7, 2, 7)
table.cell(dash, 0, 8, "Range / ADR, all days", text_color = dim, text_size = dashSize, text_halign = text.align_left)
table.cell(dash, 1, 8, avgRatio(sumA, cntA), text_color = chart.fg_color, text_size = dashSize)
table.merge_cells(dash, 1, 8, 2, 8)