本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Halving Cycle NomadaScalper · KokoGhost | v1.2
// Bitcoin calendar study. The 40/80/110-week framework comes from the supplied
// Halving Cycle Profit reference. Rendering and calendar engine by NomadaScalper.
indicator("Halving Cycle NomadaScalper", overlay = true,
max_lines_count = 100, max_labels_count = 140, behind_chart = true)
const int WEEK = 604800000
const int PROJECTED_WEEKS = 208
const int CADENCE = 12
const int MAX_DOTS_PER_CYCLE = 20
const int WASH_STEPS = 7
const int HALVING = 0
const int PROFIT_START = 1
const int PROFIT_END = 2
const int DCA = 3
const string CAL = "Calendar / UTC"
const string MAP = "Chart / milestones"
const string DESK = "Cycle desk"
int openWeeks = input.int(40, "Profit window opens · weeks", minval = 0, group = CAL)
int closeWeeks = input.int(80, "Profit window closes · weeks", minval = 1, group = CAL)
int dcaWeeks = input.int(110, "DCA begins · weeks", minval = 2, group = CAL)
int fifth = input.time(timestamp("10 Apr 2028 00:00 +0000"), "Fifth halving · estimate", group = CAL,
tooltip = "Manual UTC anchor for block 1,050,000. Every date in the fifth cycle moves with this setting.")
bool showFifth = input.bool(true, "Draw projected fifth cycle", group = CAL,
tooltip = "Chart layer only. The desk always identifies calendar estimates. The fifth cycle is bounded at 208 weeks.")
bool markHalving = input.bool(true, "Halving", inline = "events1", group = MAP)
bool markDca = input.bool(true, "DCA", inline = "events1", group = MAP)
bool markOpen = input.bool(true, "Profit open", inline = "events2", group = MAP)
bool markClose = input.bool(true, "Profit close", inline = "events2", group = MAP)
bool showTags = input.bool(true, "Milestone labels", group = MAP, tooltip = "Show one-line names. Hover for the date, week offset and projection details.")
bool showTint = input.bool(true, "Soft profit-window shading", group = MAP,
tooltip = "The green band spans the profit window and deepens toward its end. Projected windows use a lighter shade.")
bool showChecks = input.bool(true, "12-week DCA check-ins", group = MAP,
tooltip = "Dots mark the 12-week cadence after DCA begins. Their vertical position is for display. Very long custom cycles thin the dots to multiples of 12 weeks.")
int cardSize = input.int(11, "Label text size", minval = 10, maxval = 17, group = MAP)
float labelGap = input.float(3.75, "Label distance (ATR)", minval = 0.0, maxval = 6.0, step = 0.25, group = MAP,
tooltip = "Spaces captions away from the nearby five-bar price range. Dates, guides and DCA dots keep their event anchors.")
bool showDesk = input.bool(true, "Show cycle desk", group = DESK)
string deskCorner = input.string("Top right", "Position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = DESK)
const color FACE = #111923
const color PANEL = #18222D
const color RULE = #33404B
const color IVORY = #F4F5F4
const color SOFT = #C2CBD2
const color GOLD_TEXT = #D5BB83
const color JADE_TEXT = #A4CBB9
const color STEEL_TEXT = #ACC6DA
const color BRONZE_FACE = #73552F
const color JADE_FACE = #285B4B
const color CLAY_FACE = #814D46
const color BLUE_FACE = #385B72
// Deep card surfaces carry white type on either chart theme.
color paper = chart.bg_color
bool lightPaper = 0.2126 * color.r(paper) + 0.7152 * color.g(paper) + 0.0722 * color.b(paper) >= 145
color gold = lightPaper ? #886E3F : #C7A76B
color jade = lightPaper ? #4F7568 : #8BB6A3
color steel = lightPaper ? #4F7188 : #8CAEC6
color clay = lightPaper ? #965E55 : #BF8277
color windowInk = lightPaper ? #428767 : #68B092
type Cycle
int number
int start
int stop
int profitOpen
int profitClose
int dca
bool estimate
makeCycle(int number, int start, int stop) =>
Cycle.new(number, start, stop, start + WEEK * openWeeks, start + WEEK * closeWeeks,
start + WEEK * dcaWeeks, number == 5)
dateText(int stamp) => str.format_time(stamp, "dd MMM yyyy", "UTC")
utcText(int stamp) => str.format_time(stamp, "dd MMM yyyy · HH:mm:ss", "UTC") + " UTC"
var array<Cycle> calendar = array.new<Cycle>()
if barstate.isfirst
// Bitcoin block-header times: heights 210,000 / 420,000 / 630,000 / 840,000.
int h1 = timestamp("UTC", 2012, 11, 28, 15, 24, 38)
int h2 = timestamp("UTC", 2016, 7, 9, 16, 46, 13)
int h3 = timestamp("UTC", 2020, 5, 11, 19, 23, 43)
int h4 = timestamp("UTC", 2024, 4, 20, 0, 9, 27)
if openWeeks >= closeWeeks or closeWeeks >= dcaWeeks
runtime.error("Set the weeks in this order: profit open < profit close < DCA.")
if fifth <= h4
runtime.error("The projected fifth halving must follow the fourth halving (20 Apr 2024 UTC).")
calendar.push(makeCycle(1, h1, h2))
calendar.push(makeCycle(2, h2, h3))
calendar.push(makeCycle(3, h3, h4))
calendar.push(makeCycle(4, h4, fifth))
calendar.push(makeCycle(5, fifth, fifth + WEEK * PROJECTED_WEEKS))
// A cycle owns [start, stop). Milestones outside its own interval never spill into the next one.
cycleAt(int stamp) =>
Cycle result = na
for c in calendar
if stamp >= c.start and stamp < c.stop
result := c
break
result
readPhase(Cycle c, int stamp) =>
string phase = "Accumulation (DCA)"
color ink = STEEL_TEXT
int due = c.stop
string nextName = c.number == 5 ? "Horizon end" : "Halving " + str.tostring(c.number + 1)
bool estimatedDue = c.number >= 4
if stamp < c.dca
int target = c.dca
string event = "DCA opens"
phase := "After profit window"
ink := SOFT
if stamp < c.profitClose
target := c.profitClose
event := "Profit close"
phase := "Profit window"
ink := JADE_TEXT
if stamp < c.profitOpen
target := c.profitOpen
event := "Profit open"
phase := "Post-halving"
ink := GOLD_TEXT
if target < c.stop
due := target
nextName := event
estimatedDue := c.estimate
[phase, ink, due, nextName, estimatedDue]
// Keep the live clock inside the elapsed portion of the current bar, including
// delayed feeds. A completed replay bar continues to use its own UTC close.
int asOf = na(time_close) ? (barstate.isrealtime ? math.max(time, timenow) : time) : math.max(time, math.min(timenow, time_close - 1))
Cycle barCycle = cycleAt(asOf)
var int firstLoaded = time
float atr = ta.atr(14)
float barRange = nz(atr, high - low)
float pad = math.max(4 * syminfo.mintick, 0.85 * barRange)
float captionHigh = ta.highest(high, 5)
float captionLow = ta.lowest(low, 5)
cardY(float hi, float lo, float clearance, int role) =>
// Cap the lower margin on positive instruments so a volatile bar stays
// drawable on a logarithmic chart. Price is only a label anchor.
float down = lo > 0 ? math.min(clearance, lo * 0.18) : clearance
role == HALVING or role == DCA ? lo - down : hi + clearance
captionPrice(float hi, float lo, float rangeSize, int role) =>
float clearance = math.max(4 * syminfo.mintick, labelGap * rangeSize)
float down = lo > 0 ? math.min(clearance, lo * 0.50) : clearance
role == HALVING or role == DCA ? lo - down : hi + clearance
var float firstDcaY = cardY(high, low, pad, DCA)
float reference = barstate.isconfirmed ? close : nz(close[1], close)
float refPad = barstate.isconfirmed ? pad : nz(pad[1], pad)
float refRange = barstate.isconfirmed ? barRange : nz(barRange[1], barRange)
type Milestone
Cycle cycle
int stamp
int role
array<label> checks
float priceY = na
float captionY = na
float lastY = na
float lastCaptionY = na
label card = na
line guide = na
type FutureBand
int start
int stop
color ink
line leftEdge = na
line rightEdge = na
linefill fillId = na
var array<Milestone> milestones = array.new<Milestone>()
var array<FutureBand> futureBands = array.new<FutureBand>()
addEvent(Cycle c, int stamp, int role, bool enabled) =>
if enabled and stamp < c.stop
milestones.push(Milestone.new(c, stamp, role, array.new<label>()))
windowColor(float progress, bool estimated) =>
int opacity = int(math.round((estimated ? 12 : 17) + math.max(0.0, math.min(1.0, progress)) * 13))
color.new(windowInk, 100 - opacity)
if barstate.isfirst
for c in calendar
if not c.estimate or showFifth
addEvent(c, c.start, HALVING, markHalving)
addEvent(c, c.profitOpen, PROFIT_START, markOpen)
addEvent(c, c.profitClose, PROFIT_END, markClose)
addEvent(c, c.dca, DCA, markDca)
if showTint and c.profitOpen < c.stop
int duration = c.profitClose - c.profitOpen
for band = 0 to WASH_STEPS - 1
int left = c.profitOpen + int(duration * band / WASH_STEPS)
int right = math.min(c.stop, c.profitOpen + int(duration * (band + 1) / WASH_STEPS))
if left < right
futureBands.push(FutureBand.new(left, right, windowColor((band + 0.5) / WASH_STEPS, c.estimate)))
// Every historical event keeps its own candle. A session gap uses the first
// available candle after the event. Events preceding loaded history get no card.
var int cursor = 0
while cursor < milestones.size()
Milestone event = milestones.get(cursor)
if event.stamp > asOf
break
if event.stamp >= firstLoaded
event.priceY := cardY(high, low, pad, event.role)
event.captionY := captionPrice(captionHigh, captionLow, barRange, event.role)
cursor += 1
// Use the whole pane for the historical band, including the space above and
// below the visible price range. Drawings handle the empty future time axis.
color historicalWash = na
if showTint and not na(barCycle)
if (not barCycle.estimate or showFifth) and asOf >= barCycle.profitOpen and asOf < barCycle.profitClose
float progress = (asOf - barCycle.profitOpen) / float(barCycle.profitClose - barCycle.profitOpen)
historicalWash := windowColor(progress, barCycle.estimate)
bgcolor(historicalWash, title = "Profit-taking window", editable = false)
eventColors(int role) =>
switch role
HALVING => [BRONZE_FACE, gold]
PROFIT_START => [JADE_FACE, jade]
PROFIT_END => [CLAY_FACE, clay]
=> [BLUE_FACE, steel]
eventTitle(Milestone event) =>
string name = switch event.role
HALVING => "Halving " + str.tostring(event.cycle.number)
PROFIT_START => "Profit START"
PROFIT_END => "Profit END"
=> "DCA starts"
name + (event.cycle.estimate ? " · est." : "")
eventTooltip(Milestone event, string title) =>
string info = title + "\n" + utcText(event.stamp)
if event.role != HALVING
int weeks = event.role == PROFIT_START ? openWeeks : event.role == PROFIT_END ? closeWeeks : dcaWeeks
info += "\n+" + str.tostring(weeks) + " weeks after halving " + str.tostring(event.cycle.number)
if event.role == DCA
info += "\nDollar-cost averaging begins."
if event.cycle.estimate
info += "\nProjected from the fifth-halving estimate."
info
drawChecks(Milestone event, float y) =>
int interval = CADENCE * WEEK
int n = int(math.floor((event.cycle.stop - 1 - event.stamp) / float(interval)))
int stride = math.max(1, int(math.ceil(n / float(MAX_DOTS_PER_CYCLE))))
int step = interval * stride
int first = math.max(1, int(math.ceil((firstLoaded - event.stamp) / float(step))))
int due = event.stamp + first * step
while due < event.cycle.stop
event.checks.push(label.new(due, y, "●", xloc = xloc.bar_time, yloc = yloc.price,
style = label.style_label_center, color = na, textcolor = color.new(steel, event.cycle.estimate ? 22 : 9), size = 11,
tooltip = "DCA check-in · " + dateText(due) + (event.cycle.estimate ? " (estimated)" : "")))
due += step
placeEvent(Milestone event, float y, float textY) =>
if na(event.lastY)
if event.stamp >= firstLoaded
[face, ink] = eventColors(event.role)
bool major = event.role == HALVING
if event.role != DCA
event.guide := line.new(event.stamp, y, event.stamp, y + math.max(syminfo.mintick, math.abs(y) * 0.0001),
xloc = xloc.bar_time, extend = extend.both, color = color.new(ink, event.cycle.estimate ? 42 : 20),
style = major ? line.style_dashed : line.style_dotted, width = major ? 2 : 1)
else if showTags
event.guide := line.new(event.stamp, y, event.stamp, textY, xloc = xloc.bar_time,
color = color.new(ink, 62), style = line.style_dotted, width = 1)
if showTags
string title = eventTitle(event)
// Open and close captions face out from the profit window.
bool faceRight = event.role == HALVING or event.role == PROFIT_END
string side = event.cycle.estimate ? label.style_label_center : faceRight ? label.style_label_left : label.style_label_right
event.card := label.new(event.stamp, textY, title, xloc = xloc.bar_time, yloc = yloc.price,
style = side, color = color.new(face, 8), textcolor = IVORY, size = cardSize,
tooltip = eventTooltip(event, title))
else if event.role == DCA
// Keep the DCA opening visible even when all captions are hidden.
event.card := label.new(event.stamp, y, "●", xloc = xloc.bar_time, yloc = yloc.price,
style = label.style_label_center, color = na, textcolor = ink, size = 13)
if event.role == DCA and showChecks
drawChecks(event, y)
else if y != event.lastY or textY != event.lastCaptionY
if not na(event.card)
label.set_y(event.card, showTags ? textY : y)
if not na(event.guide)
line.set_y1(event.guide, y)
line.set_y2(event.guide, event.role == DCA ? textY : y + math.max(syminfo.mintick, math.abs(y) * 0.0001))
if y != event.lastY
for dot in event.checks
label.set_y(dot, y)
event.lastY := y
event.lastCaptionY := textY
bandEdge(int stamp, float price) =>
line.new(stamp, price, stamp, price + math.max(syminfo.mintick, math.abs(price) * 0.0001),
xloc = xloc.bar_time, extend = extend.both, color = na)
updateBand(FutureBand band, int firstEmpty, float price) =>
int x1 = math.max(band.start, firstEmpty)
if x1 < band.stop
if na(band.fillId)
band.leftEdge := bandEdge(x1, price)
band.rightEdge := bandEdge(band.stop, price)
band.fillId := linefill.new(band.leftEdge, band.rightEdge, band.ink)
else if line.get_x1(band.leftEdge) != x1
line.set_x1(band.leftEdge, x1)
line.set_x2(band.leftEdge, x1)
else if not na(band.fillId)
linefill.delete(band.fillId)
line.delete(band.leftEdge)
line.delete(band.rightEdge)
band.fillId := na
band.leftEdge := na
band.rightEdge := na
true
if barstate.islastconfirmedhistory or barstate.islast
for event in milestones
if event.cycle.stop > firstLoaded
float fallback = event.stamp < firstLoaded ? firstDcaY : cardY(reference, reference, refPad, event.role)
float marker = nz(event.priceY, fallback)
float textY = event.stamp < firstLoaded ? marker : nz(event.captionY, captionPrice(reference, reference, refRange, event.role))
placeEvent(event, marker, textY)
int firstEmpty = nz(time_close, asOf + 1)
for band in futureBands
updateBand(band, firstEmpty, reference)
string deskPosition = switch deskCorner
"Top left" => position.top_left
"Bottom left" => position.bottom_left
"Bottom right" => position.bottom_right
=> position.top_right
var table desk = table.new(deskPosition, 2, 3, frame_color = RULE, frame_width = 1)
if barstate.islast and showDesk
Cycle current = barCycle
Cycle firstCycle = calendar.first()
bool beforeCalendar = asOf < firstCycle.start
string head = "BTC / —"
string phase = beforeCalendar ? "Pre-halving" : "Horizon ended"
color phaseInk = SOFT
string weekText = "—"
string nextLine = beforeCalendar ? "Next · Halving 1" : "Next milestone"
string dueText = beforeCalendar ? dateText(firstCycle.start) : "—"
string dueTip = beforeCalendar ? utcText(firstCycle.start) : "The five-cycle calendar ends 208 weeks after the fifth anchor."
if not na(current)
[stateName, stateInk, due, event, estimatedDue] = readPhase(current, asOf)
head := "BTC · Cycle " + str.tostring(current.number) + (current.estimate ? " (est.)" : "")
phase := stateName
phaseInk := stateInk
weekText := "Week " + str.tostring(int(math.floor((asOf - current.start) / float(WEEK))))
nextLine := "Next · " + event
dueText := dateText(due) + (estimatedDue ? " (est.)" : "")
dueTip := utcText(due) + (estimatedDue ? "\nEstimated boundary." : "\nScheduled calendar milestone.")
table.cell(desk, 0, 0, "NomadaScalper", bgcolor = FACE, text_color = GOLD_TEXT, text_size = 12,
text_halign = text.align_left, text_formatting = text.format_bold, tooltip = "Halving Cycle NomadaScalper · Bitcoin cycle calendar")
table.cell(desk, 1, 0, head, bgcolor = FACE, text_color = SOFT, text_size = 12,
text_halign = text.align_right, text_font_family = font.family_monospace, tooltip = "Bitcoin halving cycle. Estimated cycles use the editable fifth-halving date.")
table.cell(desk, 0, 1, phase, bgcolor = PANEL, text_color = phaseInk, text_size = 14,
text_halign = text.align_left, text_formatting = text.format_bold, tooltip = "Calendar phase at " + utcText(asOf))
table.cell(desk, 1, 1, weekText, bgcolor = PANEL, text_color = IVORY, text_size = 12,
text_halign = text.align_right, text_font_family = font.family_monospace, tooltip = "Complete UTC weeks since this cycle's halving.")
table.cell(desk, 0, 2, nextLine, bgcolor = FACE, text_color = SOFT, text_size = 12, text_halign = text.align_left)
table.cell(desk, 1, 2, dueText, bgcolor = FACE, text_color = IVORY, text_size = 12,
text_halign = text.align_right, text_font_family = font.family_monospace, tooltip = dueTip)