本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Source Code Form is subject to the Mozilla Public License, v. 2.0.
// https://mozilla.org/MPL/2.0/
//
// Independent adaptation of the Camarilla Pivot Levels. Not Stefany's
// original indicator, source code, or an endorsed reproduction.
// REFERENCES / VERIFIED OBSERVATIONS
// https://x.com/stefanytrades/status/2113361391352693203
// https://x.com/stefanytrades/status/2095195423870799881
//@version=6
indicator("Camarilla Framework [MMT]", shorttitle = "CAM [MMT]", overlay = true, precision = 2, max_lines_count = 500, max_labels_count = 500, dynamic_requests = true)
const string GD = "Source / Sessions"
const string GL = "Daily Levels"
const string GH = "Higher Timeframe Levels"
const string GV = "Appearance"
string dataMode = input.string("RTH", "Daily HLC source", options = ["RTH", "ETH"], group = GD)
string rthSession = input.session("0930-1600", "RTH window", group = GD)
string ethSession = input.session("1800-1700", "ETH window", tooltip = "ES/NQ full electronic session. ETH does not stop at the RTH close.", group = GD)
string tzInput = input.string("America/New_York", "Session timezone", options = ["America/New_York", "America/Chicago", "America/Los_Angeles", "America/Denver", "Europe/London", "Europe/Berlin", "Asia/Tokyo", "Australia/Sydney", "Exchange"], group = GD)
int sourceBars = input.int(100000, "1-minute source history", minval = 20000, maxval = 100000, step = 10000, tooltip = "Monthly aggregation needs at least one full prior month plus the current month. Availability depends on your data and TradingView plan.", group = GD)
bool showDaily = input.bool(true, "Daily layer", group = GL)
bool show3 = input.bool(true, "R3 / S3", inline = "dl", group = GL)
bool show4 = input.bool(true, "R4 / S4", inline = "dl", group = GL)
bool show6 = input.bool(true, "R6 / S6", inline = "dl", group = GL)
bool showCP = input.bool(true, "CP = prior close", group = GL)
int priorDays = input.int(0, "Prior daily sets to retain", minval = 0, maxval = 20, group = GL)
string weeklyMode = input.string("CP only", "Weekly layer", options = ["Off", "CP only", "All levels"], group = GH)
string monthlyMode = input.string("CP only", "Monthly layer", options = ["Off", "CP only", "All levels"], group = GH)
int priorWeeks = input.int(0, "Prior weekly sets", minval = 0, maxval = 10, group = GH)
int priorMonths = input.int(0, "Prior monthly sets", minval = 0, maxval = 6, group = GH)
bool show4H = input.bool(false, "Previous 4H H/L/C (additional reference)", group = GH)
color dailyColor = input.color(color.rgb(194, 166, 117), "Daily R / S", inline = "dcol", group = GV)
color dailyCPColor = input.color(color.rgb(119, 119, 119), "Daily CP", inline = "dcol", group = GV)
color weeklyColor = input.color(color.rgb(83, 83, 83), "Weekly", inline = "hcol", group = GV)
color monthlyColor = input.color(color.rgb(151, 113, 72), "Monthly", inline = "hcol", group = GV)
color fourHColor = input.color(color.rgb(103, 131, 148), "4H", group = GV)
int lineWidth = input.int(1, "Line width", minval = 1, maxval = 4, group = GV)
int lineTransparency = input.int(45, "Line transparency", minval = 0, maxval = 90, group = GV)
string lineStyleInput = input.string("Solid", "Line style", options = ["Solid", "Dashed", "Dotted"], group = GV)
bool extendRight = input.bool(false, "Extend active lines right", tooltip = "Only active sets extend. Historical sets end at the next publication.", group = GV)
bool showLabels = input.bool(true, "Labels", inline = "lab", group = GV)
bool showPrices = input.bool(true, "Prices", inline = "lab", group = GV)
string priceFormatInput = input.string("Two decimals", "Label price format", options = ["Two decimals", "Minimum tick"], tooltip = "The reference chart shows unrounded calculated prices such as 7738.61 on ES. Two decimals preserves that appearance; Minimum tick rounds label text only.", group = GV)
bool showScalePrices = input.bool(true, "Show current daily / HTF CP prices on scale", group = GV)
string labelSizeInput = input.string("Small", "Label size", options = ["Tiny", "Small", "Normal"], group = GV)
int labelOffset = input.int(6, "Daily label offset (bars)", minval = 0, maxval = 50, group = GV)
int layerSpacing = input.int(12, "Extra offset per HTF layer (bars)", minval = 0, maxval = 40, group = GV)
bool latestLabelsOnly = input.bool(true, "Labels on latest sets only", group = GV)
string tz = tzInput == "Exchange" ? syminfo.timezone : tzInput
string sourceSession = dataMode == "RTH" ? rthSession : ethSession
string sourceTicker = ticker.modify(ticker.standard(syminfo.tickerid), session = session.extended)
string lnStyle = lineStyleInput == "Dashed" ? line.style_dashed : lineStyleInput == "Dotted" ? line.style_dotted : line.style_solid
string labSize = labelSizeInput == "Tiny" ? size.tiny : labelSizeInput == "Normal" ? size.normal : size.small
if barstate.isfirst
if not chart.is_standard or not timeframe.isintraday or timeframe.in_seconds() < 60 or timeframe.in_seconds() > 3600
runtime.error("Use standard time-based candles from 1 to 60 minutes (3m, 5m, or 15m recommended).")
// Runs inside the requested 1m context. Every minute is processed, even though
// request.security returns only the last snapshot within each chart bar.
f_source() =>
int sh = int(str.tonumber(str.substring(sourceSession, 0, 2)))
int sm = int(str.tonumber(str.substring(sourceSession, 2, 4)))
int eh = int(str.tonumber(str.substring(sourceSession, 5, 7)))
int em = int(str.tonumber(str.substring(sourceSession, 7, 9)))
int startMinute = sh * 60 + sm
int endMinute = eh * 60 + em
int minuteNow = hour(time, tz) * 60 + minute(time, tz)
bool overnight = startMinute >= endMinute
bool active = not na(time("1", sourceSession, tz))
int dayAdjustment = overnight and minuteNow < startMinute ? -1 : 0
int sourceStart = timestamp(tz, year(time, tz), month(time, tz), dayofmonth(time, tz) + dayAdjustment, sh, sm)
int sourceEnd = timestamp(tz, year(sourceStart, tz), month(sourceStart, tz), dayofmonth(sourceStart, tz) + (overnight ? 1 : 0), eh, em)
int wk = time("W")
int mo = time("M")
var float dh = na
var float dl = na
var float dc = na
var int dayStart = na
var int dayEnd = na
var bool dayTrusted = false
var bool dayPublished = false
var float outDH = na
var float outDL = na
var float outDC = na
var int outDT = na
var int weekKey = na
var float wh = na
var float wl = na
var float wc = na
var bool weekTrusted = false
var float outWH = na
var float outWL = na
var float outWC = na
var int outWT = na
var int monthKey = na
var float mh = na
var float ml = na
var float mc = na
var bool monthTrusted = false
var float outMH = na
var float outML = na
var float outMC = na
var int outMT = na
// Finalize a source day whose last scheduled minute was absent (holiday,
// early close, or missing bar), before overwriting its accumulator.
if not dayPublished and dayTrusted and not na(dc) and time >= dayEnd
outDH := dh
outDL := dl
outDC := dc
outDT := time
dayPublished := true
if na(weekKey) or wk != weekKey
if not na(weekKey) and weekTrusted and not na(wc)
outWH := wh
outWL := wl
outWC := wc
outWT := time
weekTrusted := not na(weekKey)
weekKey := wk
wh := na
wl := na
wc := na
if na(monthKey) or mo != monthKey
if not na(monthKey) and monthTrusted and not na(mc)
outMH := mh
outML := ml
outMC := mc
outMT := time
monthTrusted := not na(monthKey)
monthKey := mo
mh := na
ml := na
mc := na
if active
if na(dayStart) or sourceStart != dayStart
dh := high
dl := low
dc := close
dayStart := sourceStart
dayEnd := sourceEnd
// Suppress the first loaded day if history begins mid-session.
dayTrusted := time == sourceStart
dayPublished := false
else
dh := math.max(dh, high)
dl := math.min(dl, low)
dc := close
wh := na(wh) ? high : math.max(wh, high)
wl := na(wl) ? low : math.min(wl, low)
wc := close
mh := na(mh) ? high : math.max(mh, high)
ml := na(ml) ? low : math.min(ml, low)
mc := close
// Include the last selected minute's close; never an outside-session bar.
if time_close >= dayEnd and dayTrusted and not dayPublished
outDH := dh
outDL := dl
outDC := dc
outDT := time_close
dayPublished := true
[outDH, outDL, outDC, outDT, outWH, outWL, outWC, outWT, outMH, outML, outMC, outMT]
[srcDH, srcDL, srcDC, srcDT, srcWH, srcWL, srcWC, srcWT, srcMH, srcML, srcMC, srcMT] = request.security(sourceTicker, "1", f_source(), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, calc_bars_count = sourceBars)
[prev4HH, prev4HL, prev4HC, current4HTime] = request.security(sourceTicker, "240", [high[1], low[1], close[1], time], lookahead = barmerge.lookahead_on)
// Adopt snapshots only at chart confirmation. This prevents the last source
// minute's unconfirmed close from briefly becoming the next session's CP.
var float dH = na
var float dL = na
var float dC = na
var int dT = na
var float wH = na
var float wL = na
var float wC = na
var int wT = na
var float mH = na
var float mL = na
var float mC = na
var int mT = na
if barstate.isconfirmed
dH := srcDH
dL := srcDL
dC := srcDC
dT := srcDT
wH := srcWH
wL := srcWL
wC := srcWC
wT := srcWT
mH := srcMH
mL := srcML
mC := srcMC
mT := srcMT
f_price(float value) =>
str.tostring(value, priceFormatInput == "Two decimals" ? "0.00" : format.mintick)
f_levels(float h, float l, float c) =>
float r = h - l
float r6 = l > 0 ? c * h / l : na
array.from(r6, c + r * 1.1 / 2, c + r * 1.1 / 4, c, c - r * 1.1 / 4, c - r * 1.1 / 2, 2 * c - r6)
// One object owns a whole period; cleanup never deletes half a level set.
type PivotSet
array<line> lines
array<label> labels
int stamp
f_delete(PivotSet item) =>
for ln in item.lines
line.delete(ln)
for lb in item.labels
label.delete(lb)
f_finish(PivotSet item, int endTime) =>
for ln in item.lines
line.set_extend(ln, extend.none)
line.set_x2(ln, math.max(line.get_x1(ln) + 1, endTime))
for lb in item.labels
if latestLabelsOnly
label.delete(lb)
else
label.set_x(lb, endTime)
if latestLabelsOnly
array.clear(item.labels)
f_addLevel(PivotSet item, int startTime, float value, string name, color col, color textCol) =>
if not na(value)
array.push(item.lines, line.new(startTime, value, startTime + 1, value, xloc = xloc.bar_time, color = color.new(col, lineTransparency), width = lineWidth, style = lnStyle, extend = extendRight ? extend.right : extend.none))
if showLabels
string txt = name + (showPrices ? " " + f_price(value) : "")
array.push(item.labels, label.new(startTime, value, txt, xloc = xloc.bar_time, style = label.style_label_left, color = col, textcolor = textCol, size = labSize))
f_layer(array<PivotSet> history, float h, float l, float c, int stamp, string prefix, string mode, color col, int keep, int offset, bool daily) =>
if mode != "Off" and not na(stamp) and not na(c) and h >= l and l > 0
bool changed = array.size(history) == 0
if not changed
changed := array.get(history, array.size(history) - 1).stamp != stamp
if changed
int startTime = stamp
if array.size(history) > 0
f_finish(array.get(history, array.size(history) - 1), startTime)
PivotSet fresh = PivotSet.new(array.new<line>(), array.new<label>(), stamp)
array<float> levels = f_levels(h, l, c)
array<string> names = array.from("R6", "R4", "R3", "CP", "S3", "S4", "S6")
for i = 0 to 6
bool enabled = mode == "All levels" or i == 3
if daily
enabled := i == 3 ? showCP : (i == 0 or i == 6) ? show6 : (i == 1 or i == 5) ? show4 : show3
if enabled
color useCol = daily and i == 3 ? dailyCPColor : col
color txtCol = daily and i != 3 ? color.black : color.white
f_addLevel(fresh, startTime, array.get(levels, i), prefix + array.get(names, i), useCol, txtCol)
array.push(history, fresh)
while array.size(history) > keep + 1
f_delete(array.shift(history))
PivotSet current = array.get(history, array.size(history) - 1)
int edge = (na(time_close) ? time : time_close) + int(timeframe.in_seconds() * 1000) * offset
for ln in current.lines
line.set_x2(ln, math.max(line.get_x1(ln) + 1, edge))
for lb in current.labels
label.set_x(lb, edge)
var array<PivotSet> dailySets = array.new<PivotSet>()
var array<PivotSet> weeklySets = array.new<PivotSet>()
var array<PivotSet> monthlySets = array.new<PivotSet>()
f_layer(dailySets, dH, dL, dC, dT, "", showDaily ? "All levels" : "Off", dailyColor, priorDays, labelOffset, true)
f_layer(weeklySets, wH, wL, wC, wT, "Weekly ", weeklyMode, weeklyColor, priorWeeks, labelOffset + layerSpacing, false)
f_layer(monthlySets, mH, mL, mC, mT, "Monthly ", monthlyMode, monthlyColor, priorMonths, labelOffset + layerSpacing * 2, false)
var PivotSet fourHSet = PivotSet.new(array.new<line>(), array.new<label>(), na)
if show4H and not na(prev4HC)
if na(fourHSet.stamp) or current4HTime != fourHSet.stamp
f_delete(fourHSet)
fourHSet := PivotSet.new(array.new<line>(), array.new<label>(), current4HTime)
f_addLevel(fourHSet, current4HTime, prev4HH, "Prev 4H H", fourHColor, color.white)
f_addLevel(fourHSet, current4HTime, prev4HL, "Prev 4H L", fourHColor, color.white)
f_addLevel(fourHSet, current4HTime, prev4HC, "Prev 4H C", fourHColor, color.white)
int edge4H = time_close + int(timeframe.in_seconds() * 1000) * (labelOffset + layerSpacing * 3)
for ln in fourHSet.lines
line.set_x2(ln, math.max(line.get_x1(ln) + 1, edge4H))
for lb in fourHSet.labels
label.set_x(lb, edge4H)
array<float> dayLevels = f_levels(dH, dL, dC)
float r6 = array.get(dayLevels, 0)
float r4 = array.get(dayLevels, 1)
float r3 = array.get(dayLevels, 2)
float cp = dC
float s3 = array.get(dayLevels, 4)
float s4 = array.get(dayLevels, 5)
float s6 = array.get(dayLevels, 6)
// Scale tags reproduce the reference's colored price-axis markers without
// duplicating the horizontal line drawings on the chart pane.
plot(showScalePrices and showDaily and show6 ? r6 : na, "Daily R6 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and showDaily and show4 ? r4 : na, "Daily R4 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and showDaily and show3 ? r3 : na, "Daily R3 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and showDaily and showCP ? cp : na, "Daily CP scale", color = dailyCPColor, display = display.price_scale)
plot(showScalePrices and showDaily and show3 ? s3 : na, "Daily S3 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and showDaily and show4 ? s4 : na, "Daily S4 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and showDaily and show6 ? s6 : na, "Daily S6 scale", color = dailyColor, display = display.price_scale)
plot(showScalePrices and weeklyMode != "Off" ? wC : na, "Weekly CP scale", color = weeklyColor, display = display.price_scale)
plot(showScalePrices and monthlyMode != "Off" ? mC : na, "Monthly CP scale", color = monthlyColor, display = display.price_scale)
// Crossing only, not acceptance/retest signals. Check BOTH closes against the
// SAME fixed pivot and suppress the chart bar on which a pivot changes.
bool dailyStable = not na(dT) and not na(dT[1]) and dT == dT[1]
bool weeklyStable = not na(wT) and not na(wT[1]) and wT == wT[1]
bool monthlyStable = not na(mT) and not na(mT[1]) and mT == mT[1]
f_up(float level, bool stable) =>
barstate.isconfirmed and stable and not na(level) and close > level and close[1] <= level
f_down(float level, bool stable) =>
barstate.isconfirmed and stable and not na(level) and close < level and close[1] >= level
alertcondition(f_up(cp, dailyStable), "Daily CP reclaimed", "{{ticker}} closed above daily CP ({{close}})")
alertcondition(f_down(cp, dailyStable), "Daily CP lost", "{{ticker}} closed below daily CP ({{close}})")
alertcondition(f_up(r3, dailyStable), "Above R3", "{{ticker}} closed above daily R3 ({{close}})")
alertcondition(f_down(r3, dailyStable), "Below R3", "{{ticker}} closed below daily R3 ({{close}})")
alertcondition(f_up(r4, dailyStable), "Above R4", "{{ticker}} closed above daily R4 ({{close}})")
alertcondition(f_down(r4, dailyStable), "Below R4", "{{ticker}} closed below daily R4 ({{close}})")
alertcondition(f_up(s3, dailyStable), "Above S3", "{{ticker}} closed above daily S3 ({{close}})")
alertcondition(f_down(s3, dailyStable), "Below S3", "{{ticker}} closed below daily S3 ({{close}})")
alertcondition(f_up(s4, dailyStable), "Above S4", "{{ticker}} closed above daily S4 ({{close}})")
alertcondition(f_down(s4, dailyStable), "Below S4", "{{ticker}} closed below daily S4 ({{close}})")
alertcondition(f_up(r6, dailyStable), "Above R6", "{{ticker}} closed above daily R6 ({{close}})")
alertcondition(f_down(s6, dailyStable), "Below S6", "{{ticker}} closed below daily S6 ({{close}})")
alertcondition(f_up(wC, weeklyStable), "Weekly CP reclaimed", "{{ticker}} closed above weekly CP ({{close}})")
alertcondition(f_down(wC, weeklyStable), "Weekly CP lost", "{{ticker}} closed below weekly CP ({{close}})")
alertcondition(f_up(mC, monthlyStable), "Monthly CP reclaimed", "{{ticker}} closed above monthly CP ({{close}})")
alertcondition(f_down(mC, monthlyStable), "Monthly CP lost", "{{ticker}} closed below monthly CP ({{close}})")