本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Timeframe Master
//
// The candle one timeframe up, drawn behind the chart. On a 5 minute chart that is the 1 hour candle,
// on a 1 hour chart the daily candle, on a daily chart the monthly candle. Each bigger candle is built
// from the chart's own bars: its open is the first bar's open, its close the last bar's close, its
// high and low the extremes in between. Nothing is requested from another timeframe and nothing looks
// ahead. On a stock chart with extended hours on, the bars before the open and after the close are
// chart bars too, so the daily and bigger candles include them.
//
// Each one is drawn as a lantern. The body runs the full width of the bars it covers and the wicks are
// a narrow column down its middle. The candle is cut into rows from its low to its high, and every row
// glows by how many of the bars inside it traded there, so the bright rows are where price spent its
// time. The row that saw the most is the core, marked with a line. The candle still forming is drawn
// the same way, inside a dashed outline of the whole candle out to where it is due to close.
//
// The candles do the drawing, the panel does the words, the alerts carry the events.
indicator("Timeframe Master [SimpleAlgo]", "Timeframe Master [SimpleAlgo]", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_polylines_count = 100)
// ───────────────────────── Settings ─────────────────────────
grpT = "Timeframe"
tfIn = input.string("Auto", "Bigger timeframe", options = ["Auto", "5 minutes", "15 minutes", "30 minutes", "1 hour", "2 hours", "4 hours", "1 day", "1 week", "1 month"], group = grpT, tooltip = "The candle drawn behind the chart. Auto picks the smallest of the usual timeframes that holds at least ten of the chart's bars: 15 minutes behind a 1 minute chart, 30 minutes behind 2 or 3 minutes, 1 hour behind 4 or 5 minutes, 2 hours behind 10 minutes, 4 hours behind 15 or 20 minutes, the day behind 30 minutes to 2 hours, the week behind 3 to 12 hours, the month behind daily charts. A choice that holds fewer than four of the chart's bars, not a whole number of them, or far too many, falls back to Auto. On a stock chart with extended hours on, daily and bigger candles include the bars before the open and after the close. Switch extended hours off for the regular session's candle.")
keepIn = input.int(120, "Bigger candles to draw", minval = 4, maxval = 120, group = grpT, tooltip = "The most recent bigger candles stay on the chart, counting the one still forming. TradingView limits how much one script can draw, so about 115 is the most that fit.")
grpL = "Look"
lanternIn = input.bool(true, "Lantern", group = grpL, tooltip = "On: every row of a bigger candle glows by how long price spent there. Off: the quiet look, a plain body lit at its close with a thin wick.")
coreIn = input.bool(true, "Core line", group = grpL, inline = "c")
colCoreIn = input.color(#FFD166, "", group = grpL, inline = "c", tooltip = "The price inside each bigger candle where its bars spent the most time. It is a record of that candle, not a level to trade from. Needs the lantern.")
themeIn = input.string("Mint and Pink", "Colours", options = ["Mint and Pink", "Ice", "Neon", "Custom"], group = grpL)
cUpIn = input.color(#00FFA3, "", group = grpL, inline = "col")
cDnIn = input.color(#FF3D71, "", group = grpL, inline = "col")
brightIn = input.int(3, "Brightness", minval = 1, maxval = 5, group = grpL)
sizeIn = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = grpL)
showPanel = input.bool(true, "Panel", group = grpL, inline = "p")
panelPos = input.string("Bottom left", "", options = ["Top left", "Top right", "Bottom left", "Bottom right"], group = grpL, inline = "p")
[colUp, colDn] = switch themeIn
"Mint and Pink" => [#00FFA3, #FF3D71]
"Ice" => [#7DD3FC, #FFFFFF]
"Neon" => [#FFFFFF, #D946EF]
=> [cUpIn, cDnIn]
color colPanel = #262B38
// fixed rules
// longReach true: the glow of the finished candles is drawn with polylines, so about 115 of them fit on the chart.
// longReach false: every glowing row is a box, as in the first versions, and 30 to 45 candles fit
bool longReach = true
int boxRoom = 470 // TradingView keeps at most 500 boxes and 500 lines per script. The finished candles stay under these,
int lineRoom = 470 // which leaves room for the one still forming
int glowSteps = 16 // how many steps of glow a row can have, faintest to brightest
// ───────────────────────── The bigger timeframe ─────────────────────────
int tfSec = timeframe.in_seconds()
int secM = timeframe.in_seconds("M")
// Auto: the smallest of the usual timeframes that holds at least ten of the chart's bars. Inside a day it also has to hold a whole
// number of them, or the bigger candles would start and end in the middle of the chart's bars
string autoIn = 15 >= tfSec * 10 ? "15S" : 60 >= tfSec * 10 and 60 % tfSec == 0 ? "1" : 300 >= tfSec * 10 and 300 % tfSec == 0 ? "5" : 900 >= tfSec * 10 and 900 % tfSec == 0 ? "15" : 1800 >= tfSec * 10 and 1800 % tfSec == 0 ? "30" : 3600 >= tfSec * 10 and 3600 % tfSec == 0 ? "60" : 7200 >= tfSec * 10 and 7200 % tfSec == 0 ? "120" : 14400 >= tfSec * 10 and 14400 % tfSec == 0 ? "240" : ""
string autoTf = autoIn != "" ? autoIn : 86400 >= tfSec * 10 ? "D" : 604800 >= tfSec * 10 ? "W" : secM >= tfSec * 10 ? "M" : secM * 3 >= tfSec * 10 ? "3M" : "12M"
string pickTf = switch tfIn
"5 minutes" => "5"
"15 minutes" => "15"
"30 minutes" => "30"
"1 hour" => "60"
"2 hours" => "120"
"4 hours" => "240"
"1 day" => "D"
"1 week" => "W"
"1 month" => "M"
=> autoTf
int pickSec = timeframe.in_seconds(pickTf)
// a choice has to hold at least four of the chart's bars, a whole number of them inside a day, and not far too many
bool pickOk = pickSec >= tfSec * 4 and pickSec <= tfSec * 1500 and (pickSec >= 86400 or pickSec % tfSec == 0)
string bigTf = pickOk ? pickTf : autoTf
int bigSec = timeframe.in_seconds(bigTf)
string bigName = switch bigTf
"15S" => "15s"
"1" => "1m"
"5" => "5m"
"15" => "15m"
"30" => "30m"
"60" => "1h"
"120" => "2h"
"240" => "4h"
"D" => "daily"
"W" => "weekly"
"M" => "monthly"
"3M" => "quarterly"
=> "yearly"
tfText() =>
string out = timeframe.period
if timeframe.isseconds
out := str.tostring(timeframe.multiplier) + "s"
else if timeframe.isminutes
out := timeframe.multiplier >= 60 and timeframe.multiplier % 60 == 0 ? str.tostring(int(timeframe.multiplier / 60)) + "h" : str.tostring(timeframe.multiplier) + "m"
else if timeframe.isdaily
out := timeframe.multiplier == 1 ? "daily" : str.tostring(timeframe.multiplier) + " day"
else if timeframe.isweekly
out := timeframe.multiplier == 1 ? "weekly" : str.tostring(timeframe.multiplier) + " week"
else if timeframe.ismonthly
out := timeframe.multiplier == 1 ? "monthly" : str.tostring(timeframe.multiplier) + " month"
out
bool newBig = timeframe.change(bigTf)
int bigCloseT = time_close(bigTf)
int barsEst = math.max(1, int(math.ceil(bigSec / (tfSec * 1.0))))
// how many are kept: the number asked for, and never so many that the oldest would sit more than 9000 bars back, which TradingView does not allow
int keepN = math.max(4, math.min(keepIn, int(9000 / barsEst)))
bool recent = bar_index >= last_bar_index - (keepN + 2) * barsEst // only the candles that can still be on the chart are drawn
// a stock chart with extended hours on: its daily and bigger candles take in the bars before the open and after the close
bool extHours = bigSec >= 86400 and timeframe.isintraday and syminfo.session == session.extended and syminfo.type != "futures"
// ───────────────────────── Drawing one bigger candle ─────────────────────────
float bright = 0.5 + 0.25 * (brightIn - 1)
// With long reach the glowing rows of the finished candles are not boxes. They are kept here as plain rectangles and drawn together on
// the last bar, as a few polylines. Each rectangle has its bars, its prices, a key (colour, body or wick, step of glow) and the candle it belongs to
var rX0 = array.new_int()
var rX1 = array.new_int()
var rY0 = array.new_float()
var rY1 = array.new_float()
var rKey = array.new_int()
var rGrp = array.new_int()
var rectCnt = array.new_int() // how many rectangles each finished candle took, oldest first
var polys = array.new<polyline>()
// rows a bigger candle is cut into, low to high: more rows when more bars went into it
rowsFor(int n) =>
math.max(8, math.min(18, int(n * 0.6) + 3))
// a row's glow as a transparency, from its step of glow (0 is the faintest) and the candle's brightness. 100 would be clear glass
trOf(int q, float gain) =>
int(math.round(100 - math.min(100 * gain * (0.035 + 0.515 * q / (glowSteps - 1.0)), 80)))
// One glowing rectangle of a bigger candle. keep: remember it for the polylines, under its key and its candle. Otherwise it is a box, pushed onto bx
putRect(bool keep, int xa, float yTop, int xb, float yBot, color col, int keyBase, int q, float gain, int grp, array<box> bx) =>
if keep
array.push(rX0, xa)
array.push(rX1, xb)
array.push(rY0, yBot)
array.push(rY1, yTop)
array.push(rKey, keyBase + q)
array.push(rGrp, grp)
else
array.push(bx, box.new(xa, yTop, xb, yBot, xloc = xloc.bar_index, border_width = 0, bgcolor = color.new(col, trOf(q, gain))))
true
// One stretch of a bigger candle, from ya up to yb between bars xa and xb, drawn row by row. qs holds each row's step of glow.
// Neighbouring rows that glow the same are drawn as one rectangle
slab(float pl, float stepY, int rows, array<int> qs, bool keep, color col, int keyBase, float gain, int grp, float ya, float yb, int xa, int xb, array<box> bx) =>
if yb > ya and xb > xa and stepY > 0
// the small allowance keeps a price that sits exactly on a row's edge on the right side of it
int ra = math.max(0, math.min(rows - 1, int(math.floor((ya - pl) / stepY + 0.000000001))))
int rb = math.max(ra, math.min(rows - 1, int(math.ceil((yb - pl) / stepY - 0.000000001)) - 1))
float y0 = ya
for r = ra to rb
bool ends = r == rb
if not ends
ends := array.get(qs, r + 1) != array.get(qs, r)
if ends
float y1 = r == rb ? yb : pl + (r + 1) * stepY
if y1 > y0
putRect(keep, xa, y1, xb, y0, col, keyBase, array.get(qs, r), gain, grp, bx)
y0 := y1
true
// x0 to x1 are the bars it has covered so far, xEnd is where it is due to end. Everything made is pushed onto bx and ln.
// keep: a finished candle with long reach, its glow goes to the polylines under the candle number grp.
// Returns the core: the middle of the row its bars touched most often
drawBig(bool live, bool keep, int grp, float po, float ph, float pl, float pc, int x0, int x1, int xEnd, int rows, array<float> his, array<float> los, array<box> bx, array<line> ln) =>
bool up = pc >= po
color col = up ? colUp : colDn
int keyBody = up ? 0 : glowSteps // keys: rising bodies, falling bodies, then the same again for the wicks
int keyWick = keyBody + glowSteps * 2
float blo = math.min(po, pc)
float bhi = math.max(po, pc)
float gain = bright * (live ? 1.12 : 1.0)
int xm = int((x0 + xEnd) / 2)
float core = na
if lanternIn
// how many of the bars inside touched each row
float stepY = (ph - pl) / rows
cnt = array.new_float(rows, 0.0)
int n = array.size(his)
if stepY > 0 and n > 0
for i = 0 to n - 1
int a = math.max(0, math.min(rows - 1, int(math.floor((array.get(los, i) - pl) / stepY + 0.000000001))))
int b = math.max(0, math.min(rows - 1, int(math.ceil((array.get(his, i) - pl) / stepY - 0.000000001)) - 1))
if b >= a
for r = a to b
array.set(cnt, r, array.get(cnt, r) + 1)
float topCnt = math.max(array.max(cnt), 1.0)
// each row's step of glow: 0 for a row hardly visited, glowSteps - 1 for the row visited most
qs = array.new_int(rows, 0)
for r = 0 to rows - 1
array.set(qs, r, int(math.round(math.pow(array.get(cnt, r) / topCnt, 1.6) * (glowSteps - 1))))
// the wick column: a narrow strip down the middle of the whole candle, lit only where bars have printed
int ww = math.max(1, int(math.round((xEnd - x0) * 0.22)))
int xw0 = math.max(x0, xm - int(ww / 2))
int xwF = xw0 + ww
int xw1 = math.min(xwF, x1)
if stepY > 0
slab(pl, stepY, rows, qs, keep, col, keyBody, gain, grp, blo, bhi, x0, x1, bx) // through the body the rows run the full width
slab(pl, stepY, rows, qs, keep, col, keyWick, gain, grp, pl, blo, xw0, xw1, bx) // through the wicks they are the narrow column
slab(pl, stepY, rows, qs, keep, col, keyWick, gain, grp, bhi, ph, xw0, xw1, bx)
if keep
// the edges of the wicks, as two empty boxes: lines are what long reach runs short of
if xw1 > xw0
if blo > pl
array.push(bx, box.new(xw0, blo, xw1, pl, xloc = xloc.bar_index, border_color = color.new(col, 55), border_width = 1, bgcolor = color.new(col, 100)))
if ph > bhi
array.push(bx, box.new(xw0, ph, xw1, bhi, xloc = xloc.bar_index, border_color = color.new(col, 55), border_width = 1, bgcolor = color.new(col, 100)))
else
// the edges of the wicks. On the candle still forming they are drawn at their full width, dashed until its bars get there
int xwE = live ? xwF : xw1
bool ghost = live and xwF > x1
string wSty = ghost ? line.style_dashed : line.style_solid
color wCol = color.new(col, ghost ? 45 : 55)
if xwE > xw0
if blo > pl
array.push(ln, line.new(xw0, pl, xw0, blo, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
array.push(ln, line.new(xw0, pl, xwE, pl, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
if live or xwF <= x1
array.push(ln, line.new(xwE, pl, xwE, blo, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
if ph > bhi
array.push(ln, line.new(xw0, bhi, xw0, ph, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
array.push(ln, line.new(xw0, ph, xwE, ph, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
if live or xwF <= x1
array.push(ln, line.new(xwE, bhi, xwE, ph, xloc = xloc.bar_index, color = wCol, style = wSty, width = 1))
// the core: the row that saw the most bars
int rc = array.indexof(cnt, array.max(cnt))
core := pl + (rc + 0.5) * stepY
if coreIn
bool inBody = core >= blo and core <= bhi
int xa = inBody ? x0 : xw0
int xb = inBody ? x1 : xw1
if xb > xa
array.push(ln, line.new(xa, core, xb, core, xloc = xloc.bar_index, color = color.new(colCoreIn, 84), width = 6))
array.push(ln, line.new(xa, core, xb, core, xloc = xloc.bar_index, color = color.new(colCoreIn, 5), width = 2))
// TradingView draws boxes above lines, so the row the core runs through tints the line. A sliver of a box on top keeps its colour true
array.push(bx, box.new(xa, core + stepY * 0.04, xb, core - stepY * 0.04, xloc = xloc.bar_index, border_color = color.new(colCoreIn, 5), border_width = 1, bgcolor = color.new(colCoreIn, 5)))
else
// the quiet look: a thin wick down the middle and a body lit at its close, fading back to its open
array.push(ln, line.new(xm, pl, xm, ph, xloc = xloc.bar_index, color = color.new(col, 58), width = 2))
if x1 > x0 and pc != po
for j = 0 to 7
float ya = po + (pc - po) * j / 8.0
float yb = po + (pc - po) * (j + 1) / 8.0
// the step of glow that fades from 6% at the open to 32% at the close
int q = int(math.round((0.025 + 0.26 * math.pow((j + 0.5) / 8.0, 1.3)) / 0.515 * (glowSteps - 1)))
putRect(keep, x0, math.max(ya, yb), x1, math.min(ya, yb), col, keyBody, q, gain, grp, bx)
// the body's outline, with the close as its brightest edge
if x1 > x0
if bhi > blo
array.push(bx, box.new(x0, bhi, x1, blo, xloc = xloc.bar_index, border_color = color.new(col, 40), border_width = 1, bgcolor = color.new(col, 100)))
array.push(ln, line.new(x0, pc, x1, pc, xloc = xloc.bar_index, color = color.new(col, 84), width = 6))
array.push(ln, line.new(x0, pc, x1, pc, xloc = xloc.bar_index, color = color.new(col, 5), width = 2))
// the part of the candle that has not happened yet: an empty frame out to where it is due to close
if live and xEnd > x1
if bhi > blo
array.push(bx, box.new(x1, bhi, xEnd, blo, xloc = xloc.bar_index, border_width = 0, bgcolor = color.new(col, 95)))
array.push(ln, line.new(x1, pc, xEnd, pc, xloc = xloc.bar_index, color = color.new(col, 45), style = line.style_dashed, width = 1))
array.push(ln, line.new(x1, po, xEnd, po, xloc = xloc.bar_index, color = color.new(col, 45), style = line.style_dashed, width = 1))
array.push(ln, line.new(xEnd, blo, xEnd, bhi, xloc = xloc.bar_index, color = color.new(col, 45), style = line.style_dashed, width = 1))
core
// ───────────────────────── The glow of the finished candles ─────────────────────────
// Drawn from the rectangles kept above, one polyline for each key. TradingView's renderer drops the points of a polyline that lie outside
// the visible bars, so the path has to stay right whatever window of bars survives. It is a row of combs, one per candle: up the left edge
// of the candle's first rectangle, along its top, a bridge up the right edge to the next rectangle, around that one, and so on; then back
// down the right edge. The bottom edge of each first rectangle is walked on the way back. Every link joins two points on the same bar, or
// is walked both ways, so the links enclose nothing. Top and bottom edges carry extra points, so a candle cut by the edge of the screen
// is still lit from the cut onward
// the bars strictly between xa and xb, one every step, pushed left to right at price y
edgeLR(array<chart.point> pts, int xa, int xb, float y, int step) =>
int x = xa + step
while x < xb
array.push(pts, chart.point.from_index(x, y))
x += step
true
// the same bars, right to left
edgeRL(array<chart.point> pts, int xa, int xb, float y, int step) =>
int n = int(math.ceil((xb - xa) / (step * 1.0))) - 1
if n > 0
for k = n to 1
array.push(pts, chart.point.from_index(xa + k * step, y))
true
// the way back along the bottom edges, candle by candle from the newest, then the polyline itself.
// gA, gB, gY and gS hold each candle's left bar, right bar, bottom price and step
finishPoly(array<chart.point> pts, array<int> gA, array<int> gB, array<float> gY, array<int> gS, color c, int tr) =>
int ng = array.size(gA)
if ng > 0
for g = ng - 1 to 0
int xa = array.get(gA, g)
int xb = array.get(gB, g)
float y = array.get(gY, g)
array.push(pts, chart.point.from_index(xb, y))
edgeRL(pts, xa, xb, y, array.get(gS, g))
array.push(pts, chart.point.from_index(xa, y))
array.push(polys, polyline.new(pts, closed = true, xloc = xloc.bar_index, line_color = color.new(c, 100), fill_color = color.new(c, tr)))
true
rebuildPolys() =>
for p in polys
polyline.delete(p)
array.clear(polys)
int n = array.size(rKey)
if n > 0
// every rectangle's place in the store, sorted by key: the rectangles of one key end up together, oldest candle first
ord = array.new_int(n, 0)
for i = 0 to n - 1
array.set(ord, i, array.get(rKey, i) * 100000 + i)
array.sort(ord)
int j = 0
while j < n
int key = int(array.get(ord, j) / 100000)
color c = key % (glowSteps * 2) < glowSteps ? colUp : colDn
int tr = trOf(key % glowSteps, bright)
pts = array.new<chart.point>()
gA = array.new_int()
gB = array.new_int()
gY = array.new_float()
gS = array.new_int()
while j < n and int(array.get(ord, j) / 100000) == key
// one candle's rectangles of this key: they share their left and right bars
int i0 = array.get(ord, j) % 100000
int g = array.get(rGrp, i0)
int j1 = j
while j1 + 1 < n and int(array.get(ord, j1 + 1) / 100000) == key and array.get(rGrp, array.get(ord, j1 + 1) % 100000) == g
j1 += 1
// a polyline holds 10000 points at most, and the way back adds to these
if array.size(pts) >= 4500
finishPoly(pts, gA, gB, gY, gS, c, tr)
pts := array.new<chart.point>()
gA := array.new_int()
gB := array.new_int()
gY := array.new_float()
gS := array.new_int()
int xa = array.get(rX0, i0)
int xb = array.get(rX1, i0)
// extra points along the top and bottom: eight stretches, and never more than 20 bars apart, so any screenful of bars holds some
int step = math.max(1, math.min(int(math.ceil((xb - xa) / 8.0)), 20))
float ya1 = array.get(rY0, i0)
float yb1 = array.get(rY1, i0)
// up the left edge of the first rectangle and along its top
array.push(pts, chart.point.from_index(xa, ya1))
array.push(pts, chart.point.from_index(xa, yb1))
edgeLR(pts, xa, xb, yb1, step)
array.push(pts, chart.point.from_index(xb, yb1))
if j1 > j
// each further rectangle: the bridge along the right edge to its bottom corner, then its bottom, its left and its top
for q = j + 1 to j1
int i = array.get(ord, q) % 100000
float ya = array.get(rY0, i)
float yb = array.get(rY1, i)
array.push(pts, chart.point.from_index(xb, ya))
edgeRL(pts, xa, xb, ya, step)
array.push(pts, chart.point.from_index(xa, ya))
array.push(pts, chart.point.from_index(xa, yb))
edgeLR(pts, xa, xb, yb, step)
array.push(pts, chart.point.from_index(xb, yb))
// back down the right edges, each bridge walked the other way
for q = j1 to j + 1
int iq = array.get(ord, q) % 100000
int ip = array.get(ord, q - 1) % 100000
array.push(pts, chart.point.from_index(xb, array.get(rY0, iq)))
array.push(pts, chart.point.from_index(xb, array.get(rY1, ip)))
array.push(pts, chart.point.from_index(xb, ya1))
array.push(gA, xa)
array.push(gB, xb)
array.push(gY, ya1)
array.push(gS, step)
j := j1 + 1
finishPoly(pts, gA, gB, gY, gS, c, tr)
true
// ───────────────────────── The bigger candle ─────────────────────────
// built bar by bar from the chart. A new one starts on the first bar of the bigger timeframe's next period
var bool started = false // the first one in the chart's history is skipped: it may have started before the chart did
var float bigO = na
var float bigH = na
var float bigL = na
var float bigC = na
var int bigStart = na
var float prevO = na
var float prevH = na
var float prevL = na
var float prevC = na
var int prevBars = na
var hiArr = array.new_float() // the highs and lows of the chart's bars inside the bigger candle
var loArr = array.new_float()
var allBoxes = array.new<box>() // finished candles, oldest first, with how many pieces each one took
var allLines = array.new<line>()
var boxCnt = array.new_int()
var lineCnt = array.new_int()
var liveBoxes = array.new<box>() // the candle still forming, redrawn as it grows
var liveLines = array.new<line>()
var int lastDone = -1 // the bar on which the newest finished candle was drawn
var int polysFor = -2 // the lastDone the polylines were last built for
clearLive() =>
for b in liveBoxes
box.delete(b)
for e in liveLines
line.delete(e)
array.clear(liveBoxes)
array.clear(liveLines)
true
if newBig
if started and array.size(hiArr) > 0
if recent
// the candle that just ended is drawn for good
clearLive()
int b0 = array.size(allBoxes)
int e0 = array.size(allLines)
int r0 = array.size(rKey)
drawBig(false, longReach, bar_index, bigO, bigH, bigL, bigC, bigStart, bar_index - 1, bar_index - 1, rowsFor(array.size(hiArr)), hiArr, loArr, allBoxes, allLines)
array.push(boxCnt, array.size(allBoxes) - b0)
array.push(lineCnt, array.size(allLines) - e0)
array.push(rectCnt, array.size(rKey) - r0)
lastDone := bar_index
// the oldest ones make room for it: down to the number asked for, and under TradingView's limits
while array.size(boxCnt) > 1 and (array.size(boxCnt) > keepN - 1 or array.size(allBoxes) > boxRoom or array.size(allLines) > lineRoom)
int nb = array.shift(boxCnt)
if nb > 0
for k = 1 to nb
box.delete(array.shift(allBoxes))
int ne = array.shift(lineCnt)
if ne > 0
for k = 1 to ne
line.delete(array.shift(allLines))
int nr = array.shift(rectCnt)
if nr > 0
for k = 1 to nr
array.shift(rX0)
array.shift(rX1)
array.shift(rY0)
array.shift(rY1)
array.shift(rKey)
array.shift(rGrp)
prevO := bigO
prevH := bigH
prevL := bigL
prevC := bigC
prevBars := array.size(hiArr)
started := true
bigO := open
bigH := high
bigL := low
bigStart := bar_index
array.clear(hiArr)
array.clear(loArr)
else if started
bigH := math.max(bigH, high)
bigL := math.min(bigL, low)
if started
bigC := close
array.push(hiArr, high)
array.push(loArr, low)
// the candle still forming, on the last bar only
float coreNow = na
if barstate.islast and started
clearLive()
// where it is due to end: by the clock inside a day, by the length of the last one for weeks and months
int msToEnd = bigCloseT - time_close
int leftByTime = msToEnd > 0 ? int(math.ceil(msToEnd / (tfSec * 1000.0))) : 0
int leftByPrev = na(prevBars) ? 0 : math.max(prevBars - array.size(hiArr), 0)
int barsLeft = bigSec <= 86400 ? leftByTime : leftByPrev
int xEnd = bar_index + math.min(barsLeft, 400)
// its rows are counted for the size it is growing into, so they do not jump as its bars arrive
int barsNow = array.size(hiArr)
int rowsNow = rowsFor(na(prevBars) ? barsNow : math.max(barsNow, prevBars))
coreNow := drawBig(true, false, 0, bigO, bigH, bigL, bigC, bigStart, bar_index, xEnd, rowsNow, hiArr, loArr, liveBoxes, liveLines)
// the glow of the finished candles, drawn again only when one has been added
if barstate.islast and longReach and polysFor != lastDone
rebuildPolys()
polysFor := lastDone
// ───────────────────────── Words ─────────────────────────
fmtP(float x) =>
syminfo.mintick >= 1 ? str.tostring(x, "#,##0") : syminfo.mintick >= 0.01 ? str.tostring(x, "#,##0.00") : str.tostring(x, format.mintick)
// a candle's move from open to close: Up 0.75%, Down 2.31%
moveTxt(float po, float pc) =>
(pc >= po ? "Up " : "Down ") + str.tostring(math.abs(pc / po - 1) * 100, "0.00") + "%"
var table panel = na
if barstate.islast and started and showPanel
color blank = color.new(#000000, 100)
// time left in the bigger candle, when the market is open
string leftTxt = ""
int msLeft = bigCloseT - timenow
if msLeft > 0
int mins = int(msLeft / 60000)
if mins >= 2880
leftTxt := " · " + str.tostring(int(mins / 1440)) + " days left"
else if mins >= 60
int hh = int(mins / 60)
int mm = mins % 60
leftTxt := " · " + str.tostring(hh) + "h " + (mm < 10 ? "0" : "") + str.tostring(mm) + "m left"
else if mins >= 1
leftTxt := " · " + str.tostring(mins) + (mins == 1 ? " minute left" : " minutes left")
float pos = bigH > bigL ? (close - bigL) / (bigH - bigL) : 0.5
string spot = pos >= 2.0 / 3 ? "in the top third of its range" : pos <= 1.0 / 3 ? "in the bottom third of its range" : "in the middle of its range"
string r1 = moveTxt(bigO, bigC) + leftTxt
string r2 = (close >= bigO ? "Above" : "Below") + " its open, " + spot
if na(panel)
tablePos = switch panelPos
"Top left" => position.top_left
"Top right" => position.top_right
"Bottom right" => position.bottom_right
=> position.bottom_left
panel := table.new(tablePos, 2, 6, frame_width = 0, border_width = 0)
txtSize = sizeIn == "Small" ? size.small : sizeIn == "Large" ? size.large : size.normal
color txt = #D1D4DC
color dim = color.new(#D1D4DC, 40)
color bg = color.new(colPanel, 15)
table.cell(panel, 0, 0, "TIMEFRAME MASTER", text_color = txt, text_size = txtSize, text_halign = text.align_left, bgcolor = bg)
table.cell(panel, 1, 0, bigName + " candles behind a " + tfText() + " chart" + (extHours ? ", extended hours included" : ""), text_color = dim, text_size = txtSize, text_halign = text.align_right, bgcolor = bg)
table.cell(panel, 0, 1, "This " + bigName + " candle", text_color = dim, text_size = txtSize, text_halign = text.align_left, bgcolor = bg)
table.cell(panel, 1, 1, r1, text_color = bigC >= bigO ? colUp : colDn, text_size = txtSize, text_halign = text.align_right, bgcolor = bg)
table.cell(panel, 0, 2, "Price is", text_color = dim, text_size = txtSize, text_halign = text.align_left, bgcolor = bg)
table.cell(panel, 1, 2, r2, text_color = txt, text_size = txtSize, text_halign = text.align_right, bgcolor = bg)
int rw = 3
if not na(coreNow)
table.cell(panel, 0, rw, "Its core", text_color = dim, text_size = txtSize, text_halign = text.align_left, bgcolor = bg)
table.cell(panel, 1, rw, fmtP(coreNow) + ", where it has spent the most time", text_color = txt, text_size = txtSize, text_halign = text.align_right, bgcolor = bg)
rw += 1
if not na(prevO)
float ppos = prevH > prevL ? (prevC - prevL) / (prevH - prevL) : 0.5
string pwhere = ppos >= 0.75 ? "closed near its high" : ppos <= 0.25 ? "closed near its low" : "closed mid range"
table.cell(panel, 0, rw, "Last " + bigName + " candle", text_color = dim, text_size = txtSize, text_halign = text.align_left, bgcolor = bg)
table.cell(panel, 1, rw, moveTxt(prevO, prevC) + ", " + pwhere, text_color = txt, text_size = txtSize, text_halign = text.align_right, bgcolor = bg)
rw += 1
// at the bottom of the chart, one clear row under the panel: TradingView draws its event icons along that edge
if panelPos == "Bottom left" or panelPos == "Bottom right"
table.cell(panel, 0, rw, ".", text_color = blank, text_size = txtSize, bgcolor = blank)
// ───────────────────────── Alerts ─────────────────────────
bool turnedUp = started and not newBig and close > bigO and close[1] <= bigO
bool turnedDn = started and not newBig and close < bigO and close[1] >= bigO
alertcondition(newBig and started, "A new bigger candle", "Timeframe Master: a new bigger candle has opened")
alertcondition(turnedUp, "Bigger candle turned up", "Timeframe Master: price is back above the bigger candle's open")
alertcondition(turnedDn, "Bigger candle turned down", "Timeframe Master: price has dropped below the bigger candle's open")