本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// [Abaco-Ind] Previous Ranges MTF - the open, high, low and close of the previous candle of up to three timeframes (the day,
// the week and the month to begin with), held as levels through the current one.
//
// THE RULE
// 1 the levels held through a period are those of the period that CLOSED before it: yesterday's high is known from the
// first bar of today and does not move until the day ends
// 2 the day, the week from Monday and the calendar month are calendar ones; any other timeframe is its own candle
// 3 each of the three timeframes has its own checkbox and its own menu, and four boxes for the levels it shows (O, H, L, C);
// PDH is the previous day's high, PWL the previous week's low, PMC the previous month's close, PWO the previous week's
// open; another timeframe is named by it: P4H-H is the previous 4h candle's high. On a market that trades without a
// break the previous open is the close of the period before it
// 4 one colour per timeframe, set right after its menu, the same orange for all, stronger the longer the timeframe: the first,
// the second, the third
// HOW TO USE: Last puts the levels in force in the empty space past the last candle, an offset of bars after it and then a
// length, each with its name and, if Show Prices is on, its price. All draws every period's levels through the next one (as many periods back as the
// chart's line budget allows), and the ones in force run on with their names. The same levels show on every timeframe of
// the chart.
// THE TOUCH, for alerts and for other indicators: the Abaco touch standard v1 (touch-standard.md). One hidden plot, "Touch", a
// NUMBER because a plot carries nothing else. 0 is no touch. Otherwise its size is the level touched plus the number of levels
// touched on the bar divided by 100, and its sign the direction the price came from: positive from below, negative from above.
// 6.03 is the previous week's low (6) touched from below, three levels touched in all; -12.01 the previous month's open
// touched from above, alone. The levels are numbered by timeframe slot: 1 to 4 the first timeframe's high, low, close and
// open, 5 to 8 the second's, 9 to 12 the third's. With the day, week and month in place: 1 PDH, 2 PDL, 3 PDC, 4 PDO,
// 5 PWH, 6 PWL, 7 PWC, 8 PWO, 9 PMH, 10 PML, 11 PMC, 12 PMO.
// A level arrives when its band (the level plus or minus Touch Tolerance, in % of the close) enters the bar's range and was not
// inside the bar before's. When several arrive on one bar, the one reported is the one of the longest timeframe (the real one
// chosen, not the slot), then the nearest to the previous close, then the lowest number.
// THE ALERT speaks: create it on this indicator with "Any alert() function call". Its message names every level that arrived
// (PDH 1823.77 from below, PWL ...), each once, as it arrives at the tick. It is not tied to the Touch plot's one level.
// The levels themselves are drawings, not plots: Touch is the only plot.
indicator("[Abaco-Ind] Previous Ranges MTF - Day, Week, Month - OHLC", overlay = true, max_lines_count = 500, max_labels_count = 40)
on1 = input.bool(true, "Timeframe 1", inline = "1", display = display.none)
tf1 = input.timeframe("D", "", inline = "1", options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
cDay = input.color(color.new(#c36032, 70), "", inline = "1", display = display.none)
o1 = input.bool(true, "O", inline = "1b", display = display.none, tooltip = "Levels of the previous candle\n- O open\n- H high\n- L low\n- C close")
h1 = input.bool(true, "H", inline = "1b", display = display.none)
l1b = input.bool(true, "L", inline = "1b", display = display.none)
c1b = input.bool(true, "C", inline = "1b", display = display.none)
on2 = input.bool(true, "Timeframe 2", inline = "2", display = display.none)
tf2 = input.timeframe("W", "", inline = "2", options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
cWeek = input.color(color.new(#c36032, 40), "", inline = "2", display = display.none)
o2 = input.bool(true, "O", inline = "2b", display = display.none)
h2 = input.bool(true, "H", inline = "2b", display = display.none)
l2b = input.bool(true, "L", inline = "2b", display = display.none)
c2b = input.bool(true, "C", inline = "2b", display = display.none)
on3 = input.bool(true, "Timeframe 3", inline = "3", display = display.none)
tf3 = input.timeframe("M", "", inline = "3", options = ["1", "3", "5", "15", "30", "60", "120", "240", "D", "W", "M"], display = display.status_line)
cMonth = input.color(#c36032, "", inline = "3", display = display.none)
o3 = input.bool(true, "O", inline = "3b", display = display.none)
h3 = input.bool(true, "H", inline = "3b", display = display.none)
l3b = input.bool(true, "L", inline = "3b", display = display.none)
c3b = input.bool(true, "C", inline = "3b", display = display.none)
showPrice = input.bool(true, "Show Prices", group = "Style", display = display.none, tooltip = "On\n- each label carries its name and its price\nOff\n- the name only")
historyIn = input.string("Last", "Draw", options = ["Last", "All"], group = "Style", display = display.none, tooltip = "Last\n- levels in force only\n- right of the last candle\nAll\n- every past period's levels\n- levels in force run on to the right")
gridGap = input.int(20, "Lines Offset / Length", minval = 0, maxval = 500, inline = "l", group = "Style", display = display.none, tooltip = "Left box, offset\n- bars from the last candle to the line start\nRight box, length\n- bars the lines run\nAll: lines end where Last ends")
gridLen = input.int(20, "", minval = 1, maxval = 500, inline = "l", group = "Style", display = display.none)
lnStyle = input.string("Solid", "Line Style", options = ["Solid", "Dashed", "Dotted"], group = "Style", display = display.none, tooltip = "Style of every line")
lnWidth = input.int(1, "Line Width", minval = 1, maxval = 4, group = "Style", display = display.none, tooltip = "Width of every line, in pixels")
txtSize = input.string("Small", "Text Size", options = ["Tiny", "Small", "Normal"], group = "Style", display = display.none, tooltip = "Size of the names and prices")
tolIn = input.float(0.1, "Touch Tolerance", minval = 0.0, step = 0.05, group = "Alerts", display = display.none, tooltip = "In % of the price\n- a level is touched when the bar's range, low to high, comes within this distance of it\n- used by Touch and its alert only")
// the period that closed: read one period behind, so the one in progress never moves a level
f_prev(simple string tf) =>
request.security(syminfo.tickerid, tf, [high[1], low[1], close[1], open[1], time], lookahead = barmerge.lookahead_on)
[dH, dL, dC, dO, dT] = f_prev(tf1)
[wH, wL, wC, wO, wT] = f_prev(tf2)
[mH, mL, mC, mO, mT] = f_prev(tf3)
// the letter of a timeframe in a level's name: D, W, M, or its own ("4H", "15m")
letterOf(string tf) =>
int sec = timeframe.in_seconds(tf)
tf == "D" ? "D" : tf == "W" ? "W" : tf == "M" ? "M" : sec < 3600 ? str.tostring(sec / 60, "#") + "m-" : str.tostring(sec / 3600, "#") + "H-"
// the twelve levels: the three timeframes, each high, low, close, open
string l1 = letterOf(tf1)
string l2 = letterOf(tf2)
string l3 = letterOf(tf3)
var names = array.from("P" + l1 + "H", "P" + l1 + "L", "P" + l1 + "C", "P" + l1 + "O", "P" + l2 + "H", "P" + l2 + "L", "P" + l2 + "C", "P" + l2 + "O",
"P" + l3 + "H", "P" + l3 + "L", "P" + l3 + "C", "P" + l3 + "O")
vals = array.from(dH, dL, dC, dO, wH, wL, wC, wO, mH, mL, mC, mO)
ons = array.from(on1 and h1, on1 and l1b, on1 and c1b, on1 and o1, on2 and h2, on2 and l2b, on2 and c2b, on2 and o2,
on3 and h3, on3 and l3b, on3 and c3b, on3 and o3)
bool all = historyIn == "All"
bool newD = dT != dT[1]
bool newW = wT != wT[1]
bool newM = mT != mT[1]
colorOf(int i) => i < 4 ? cDay : i < 8 ? cWeek : cMonth
lStyle = lnStyle == "Dashed" ? line.style_dashed : lnStyle == "Dotted" ? line.style_dotted : line.style_solid
lSize = txtSize == "Tiny" ? size.tiny : txtSize == "Normal" ? size.normal : size.small
styleOf(int i) => lStyle
// ── the history, in All: a new period starts a new line and closes the one before, so a level is never joined to the one
// before it ────────────────────────────────────────────────────────────────
var array<line> held = array.new<line>(12, na)
if all
for i = 0 to 11
float y = vals.get(i)
bool newP = i < 4 ? newD : i < 8 ? newW : newM
if newP and ons.get(i) and not na(y)
line prev = held.get(i)
if not na(prev)
prev.set_x2(time)
held.set(i, line.new(time, y, time, y, xloc = xloc.bar_time, color = colorOf(i), style = styleOf(i), width = lnWidth))
// ── the levels in force, past the last candle, with their names ─────────────
var array<line> lines = array.new<line>()
var array<label> labels = array.new<label>()
if barstate.islast
for ln in lines
ln.delete()
for lb in labels
lb.delete()
lines.clear()
labels.clear()
int step = timeframe.in_seconds() * 1000
int xF = time + gridGap * step
int xT = xF + gridLen * step
for i = 0 to 11
float y = vals.get(i)
if ons.get(i) and not na(y)
color col = colorOf(i)
line ln = all ? held.get(i) : na
if all and not na(ln)
ln.set_x2(xT)
else
lines.push(line.new(all ? time : xF, y, xT, y, xloc = xloc.bar_time, color = col, style = styleOf(i), width = lnWidth))
labels.push(label.new(xT, y, names.get(i) + (showPrice ? " - " + str.tostring(y, format.mintick) : ""), xloc = xloc.bar_time,
style = label.style_label_left, color = color(na), textcolor = color.new(col, math.min(color.t(col), 40)),
size = lSize))
// ── the touch (Abaco touch standard v1) ─────────────────────────────────────────
// a level ARRIVES: its band enters this bar's range and was not inside the bar before's
float tol = close * tolIn / 100
f_arrives(float y) =>
not na(y) and low <= y + tol and high >= y - tol and not (low[1] <= y + tol and high[1] >= y - tol)
// one number for the bar: the reported level plus the count / 100, signed by where the price came from. The reported level is
// the one with the longest span, then the nearest to the previous close, then the lowest number
f_pack(array<float> lv, array<bool> hit, array<float> span) =>
int n = 0
int best = -1
for i = 0 to lv.size() - 1
if hit.get(i)
n += 1
if best < 0
best := i
else if span.get(i) > span.get(best) or (span.get(i) == span.get(best) and math.abs(lv.get(i) - close[1]) < math.abs(lv.get(best) - close[1]))
best := i
float out = 0.0
if n > 0
out := (close[1] < lv.get(best) ? 1.0 : -1.0) * (best + 1 + math.min(n, 99) / 100.0)
out
float s1 = timeframe.in_seconds(tf1)
float s2 = timeframe.in_seconds(tf2)
float s3 = timeframe.in_seconds(tf3)
spans = array.from(s1, s1, s1, s1, s2, s2, s2, s2, s3, s3, s3, s3)
array<bool> hits = array.new<bool>(12, false)
for i = 0 to 11
hits.set(i, ons.get(i) and f_arrives(vals.get(i)))
float touch = f_pack(vals, hits, spans)
plot(touch, "Touch", display = display.none, editable = false)
// ── the alert: it speaks, every level that arrived by name, each told once as it arrives (at the tick, not at the close) ──
varip array<bool> told = array.new<bool>(12, false)
if barstate.isnew
told.fill(false)
string said = ""
for i = 0 to 11
if hits.get(i) and not told.get(i)
told.set(i, true)
said += (said == "" ? "" : ", ") + names.get(i) + " " + str.tostring(vals.get(i), format.mintick) + (close[1] < vals.get(i) ? " from below" : " from above")
if said != ""
alert(syminfo.ticker + " " + timeframe.period + ": " + said, alert.freq_all)