本页源码来自 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/
// © arleay
//@version=6
indicator("Yellow Box Range", shorttitle = "YellowBoxRange | @arleay", overlay = true, max_lines_count = 500, max_labels_count = 500, max_boxes_count = 200, max_bars_back = 1000)
const int DAY_MS = 86400000
const int MAX_DAYS = 60
const int MAX_EV = 80
const string HANDLE = "@arle" + "ay" + "nt"
const string MARK = ""
const string CR = "free script by " + HANDLE + " · if you paid for this, you were scammed :(" + MARK
const string G_L = "01. Levels"
const string G_B = "02. Breaks"
const string G_D = "03. Chart"
const string G_S = "04. Style"
const string G_N = "05. Panel"
int iDays = input.int(50, "Days of hourly data", minval = 10, maxval = 250, group = G_L, tooltip = "sessions of 60-minute candles used to measure how far price stretches from the anchor.")
string iAnchor = input.string("Prior close", "Anchor", options = ["Prior close", "Session open"], group = G_L, tooltip = "levels are measured from this price and set once at the start of each session.")
float iPctBox = input.float(70, "Box holds (%)", minval = 50, maxval = 95, step = 1, group = G_L, tooltip = "share of 60-minute candles whose high (top) or low (bottom) stays inside the box.")
float iPctMax = input.float(95, "Max range (%)", minval = 80, maxval = 99.5, step = 0.5, group = G_L, tooltip = "share of 60-minute candles that stay inside the max range. the max trend line is the largest stretch in the sample.")
bool iClose = input.bool(true, "Break needs a close beyond", group = G_B, tooltip = "a box or level counts as broken only on a candle close past it. read on a 1m to 5m chart.")
bool iMarks = input.bool(false, "Mark breaks and rejections", group = G_B)
int iMaxEv = input.int(12, "Max marks", minval = 0, maxval = 60, group = G_B, tooltip = "drawing limit only.")
string iView = input.string("Minimal", "View", options = ["Minimal", "Full"], group = G_D, tooltip = "minimal: today's levels. full: past sessions too, up to the session count below.")
int iShow = input.int(5, "Sessions shown in full view", minval = 1, maxval = MAX_DAYS, group = G_D)
bool iZones = input.bool(true, "Shade average range zones", group = G_D)
bool iMaxT = input.bool(true, "Show max trend lines", group = G_D)
bool iLabels = input.bool(true, "Show labels", group = G_D)
bool iPx = input.bool(true, "Show prices", group = G_D)
color cBox = input.color(color.new(#FFD600, 78), "Yellow box", group = G_S)
color cUpZ = input.color(color.new(#F23645, 88), "Zone above", inline = "z", group = G_S)
color cDnZ = input.color(color.new(#089981, 88), "below", inline = "z", group = G_S)
color cInk = input.color(#000000, "Lines and labels", group = G_S)
string lSz = input.string("Tiny", "Text size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_S)
bool iPan = input.bool(true, "Show", inline = "pn", group = G_N)
string iPanPos = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_N)
string iPanSz = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal", "Large"], group = G_N)
f_px(float p) => na(p) ? "-" : str.tostring(p, format.mintick)
f_sz(string s) =>
switch s
"Tiny" => size.tiny
"Normal" => size.normal
"Large" => size.large
"Huge" => size.huge
=> size.small
f_pos(string s) =>
switch s
"Bottom Right" => position.bottom_right
"Top Left" => position.top_left
"Bottom Left" => position.bottom_left
=> position.top_right
f_lv() =>
var array<float> up = array.new<float>()
var array<float> dn = array.new<float>()
var array<int> per = array.new<int>()
var float anc = na
var int sT = na
var float uB = na
var float dB = na
var float uA = na
var float dA = na
var float uM = na
var float dM = na
var float uT = na
var float dT = na
bool ns = ta.change(time("D")) != 0
if ns
anc := iAnchor == "Prior close" ? nz(close[1], open) : open
sT := time
if per.size() >= 5
uB := up.percentile_nearest_rank(iPctBox)
dB := dn.percentile_nearest_rank(iPctBox)
array<float> ou = array.new<float>()
array<float> od = array.new<float>()
for v in up
if v > uB
ou.push(v)
for v in dn
if v > dB
od.push(v)
uA := ou.size() > 0 ? ou.median() : uB
dA := od.size() > 0 ? od.median() : dB
uM := up.percentile_nearest_rank(iPctMax)
dM := dn.percentile_nearest_rank(iPctMax)
uT := up.max()
dT := dn.max()
per.push(0)
if not na(anc)
up.push(math.max(0.0, high - anc))
dn.push(math.max(0.0, anc - low))
if per.size() > 0
per.set(per.size() - 1, per.get(per.size() - 1) + 1)
while per.size() > iDays
int k = per.shift()
if k > 0
for j = 1 to k
if up.size() > 0
up.shift()
dn.shift()
[anc, sT, uB, dB, uA, dA, uM, dM, uT, dT, per.size()]
[anc, sT, uB, dB, uA, dA, uM, dM, uT, dT, nS] = request.security(syminfo.tickerid, "60", f_lv(), lookahead = barmerge.lookahead_on)
type Day
int t0
int t1
float anc
float yT
float yB
float aT
float aB
float mT
float mB
float tT
float tB
float hi
float lo
float cl
type Ev
int t
float y
string tx
int dir
string tip
var array<Day> days = array.new<Day>()
var array<Ev> evs = array.new<Ev>()
var int zone = 0
var bool rejU = false
var bool rejD = false
var bool rtU = false
var bool rtD = false
var int nDays = 0
var int hYT = 0
var int hYB = 0
var int hAT = 0
var int hAB = 0
var int hMT = 0
var int hMB = 0
var int hIn = 0
int tfSec = timeframe.in_seconds()
int tfMs = tfSec * 1000
bool tfOk = tfSec <= 3600
bool newS = not na(sT) and sT != sT[1]
bool ready = not na(uB) and not na(anc)
f_ev(int t, float y, string tx, int d, string tip) =>
evs.unshift(Ev.new(t, y, tx, d, tip))
while evs.size() > MAX_EV
evs.pop()
if newS and ready
if days.size() > 0
Day p = days.get(0)
p.t1 := time
if not na(p.hi)
nDays += 1
hYT += p.hi >= p.yT ? 1 : 0
hYB += p.lo <= p.yB ? 1 : 0
hAT += p.hi >= p.aT ? 1 : 0
hAB += p.lo <= p.aB ? 1 : 0
hMT += p.hi >= p.mT ? 1 : 0
hMB += p.lo <= p.mB ? 1 : 0
hIn += p.cl <= p.yT and p.cl >= p.yB ? 1 : 0
days.unshift(Day.new(time, na, anc, anc + uB, anc - dB, anc + uA, anc - dA, anc + uM, anc - dM, anc + uT, anc - dT, na, na, na))
while days.size() > MAX_DAYS
days.pop()
zone := 0
rejU := false
rejD := false
rtU := false
rtD := false
if days.size() > 0 and barstate.isconfirmed
Day d = days.get(0)
d.hi := na(d.hi) ? high : math.max(d.hi, high)
d.lo := na(d.lo) ? low : math.min(d.lo, low)
d.cl := close
float upX = iClose ? close : high
float dnX = iClose ? close : low
int z = upX > d.mT ? 3 : upX > d.aT ? 2 : upX > d.yT ? 1 : dnX < d.mB ? -3 : dnX < d.aB ? -2 : dnX < d.yB ? -1 : 0
if z > 0 and z > zone and zone >= 0
string nm = z == 1 ? "YB▲" : z == 2 ? "AVG▲" : "MAX R▲"
f_ev(time, high, nm, 1, z == 1 ? "closed above the yellow box. next level: top of the average range." : z == 2 ? "closed above the average range. continuation, watch the max range." : "closed above the max range. trend day read.")
if z < 0 and z < zone and zone <= 0
string nm = z == -1 ? "YB▼" : z == -2 ? "AVG▼" : "MAX R▼"
f_ev(time, low, nm, -1, z == -1 ? "closed below the yellow box. next level: bottom of the average range." : z == -2 ? "closed below the average range. continuation, watch the max range." : "closed below the max range. trend day read.")
if not rejU and high >= d.aT and close < d.aT and close < open
rejU := true
f_ev(time, high, "AVG ✕", -1, "rejected at the top of the average range. a move back toward the yellow box is the read.")
if not rejD and low <= d.aB and close > d.aB and close > open
rejD := true
f_ev(time, low, "AVG ✕", 1, "rejected at the bottom of the average range. a move back toward the yellow box is the read.")
if z == 0 and zone != 0
f_ev(time, close, "back in", 0, "closed back inside the yellow box.")
if zone >= 1 and z >= 1 and not rtU and low <= d.yT and close > d.yT
rtU := true
f_ev(time, low, "RETEST", 1, "broke above the box and held its top on a retest. the usual plan: target the top of the average range, stop past the bottom of the box.")
if zone <= -1 and z <= -1 and not rtD and high >= d.yB and close < d.yB
rtD := true
f_ev(time, high, "RETEST", -1, "broke below the box and held its bottom on a retest. the usual plan: target the bottom of the average range, stop past the top of the box.")
zone := z
var array<line> dL = array.new<line>()
var array<label> dLb = array.new<label>()
var array<box> dBx = array.new<box>()
f_ln(int x1, float y, int x2, string st, color c) =>
dL.push(line.new(x1, y, x2, y, xloc = xloc.bar_time, color = c, style = st, width = 1))
f_lb(int x, float y, string tx, string sty, color c, string tip) =>
dLb.push(label.new(x, y, tx, xloc = xloc.bar_time, style = sty, color = color.new(c, 100), textcolor = c, size = f_sz(lSz), text_font_family = font.family_monospace, tooltip = tip))
f_bx(int x1, float t, int x2, float b, color bg) =>
dBx.push(box.new(x1, t, x2, b, xloc = xloc.bar_time, border_color = color.new(bg, 100), bgcolor = bg))
f_tag(int x, float y, string tx, color c, bool now, string tip) =>
if iLabels
f_lb(x, y, tx + (iPx and now ? " " + f_px(y) : ""), label.style_label_left, c, tip)
f_drawDay(Day d, int xr, bool now) =>
int x2 = now ? xr : d.t1
f_bx(d.t0, d.yT, x2, d.yB, cBox)
f_ln(d.t0, d.yT, x2, line.style_solid, color.new(cInk, 30))
f_ln(d.t0, d.yB, x2, line.style_solid, color.new(cInk, 30))
if iZones
f_bx(d.t0, d.aT, x2, d.yT, cUpZ)
f_bx(d.t0, d.yB, x2, d.aB, cDnZ)
f_ln(d.t0, d.aT, x2, line.style_dashed, cInk)
f_ln(d.t0, d.aB, x2, line.style_dashed, cInk)
f_ln(d.t0, d.mT, x2, line.style_dotted, cInk)
f_ln(d.t0, d.mB, x2, line.style_dotted, cInk)
f_ln(d.t0, d.anc, x2, line.style_dotted, color.new(cInk, 60))
if iMaxT
f_ln(d.t0, d.tT, x2, line.style_dotted, color.new(cInk, 55))
f_ln(d.t0, d.tB, x2, line.style_dotted, color.new(cInk, 55))
if now
f_tag(x2, d.yT, "YB", cInk, now, "top of the yellow box: where most hourly candles stay below")
f_tag(x2, d.yB, "YB", cInk, now, "bottom of the yellow box: where most hourly candles stay above")
f_tag(x2, d.aT, "AVG", cInk, now, "top of the average range: half of breaks of the box continue past it, half turn back")
f_tag(x2, d.aB, "AVG", cInk, now, "bottom of the average range: half of breaks of the box continue past it, half turn back")
f_tag(x2, d.mT, "MAX R", cInk, now, "max range: a close past it reads as a trend day")
f_tag(x2, d.mB, "MAX R", cInk, now, "max range: a close past it reads as a trend day")
f_tag(x2, d.anc, "ANCHOR", color.new(cInk, 50), now, "price the levels are measured from")
if iMaxT
f_tag(x2, d.tT, "MAX T", color.new(cInk, 40), now, "largest hourly stretch in the sample")
f_tag(x2, d.tB, "MAX T", color.new(cInk, 40), now, "largest hourly stretch in the sample")
if barstate.islast
for b in dBx
box.delete(b)
dBx.clear()
for l in dL
line.delete(l)
dL.clear()
for l in dLb
label.delete(l)
dLb.clear()
if tfOk and days.size() > 0
int xr = time + 10 * tfMs
int nShow = iView == "Full" ? math.min(iShow, days.size()) : 1
for i = 0 to nShow - 1
f_drawDay(days.get(i), xr, i == 0)
if iMarks and iMaxEv > 0
int t0 = days.get(nShow - 1).t0
int n = 0
for e in evs
if e.t >= t0 and n < iMaxEv
n += 1
color c = cInk
bool under = e.tx == "AVG ✕" or e.tx == "RETEST" ? e.dir == 1 : e.dir == -1
string sty = e.dir == 0 ? label.style_label_left : under ? label.style_label_up : label.style_label_down
dLb.push(label.new(e.t, e.y, e.tx, xloc = xloc.bar_time, style = sty, color = color.new(c, 100), textcolor = c, size = f_sz(lSz), text_font_family = font.family_monospace, tooltip = e.tip))
var table panel = table.new(f_pos(iPanPos), 2, 9, bgcolor = color.new(chart.bg_color, 6), frame_width = 0, border_color = color.new(chart.bg_color, 6), border_width = 3)
f_cell(int c, int r, string tx, color tc, string tip) =>
table.cell(panel, c, r, tx, text_color = tc, text_size = f_sz(iPanSz), text_font_family = font.family_monospace, text_halign = c == 1 ? text.align_right : text.align_left, tooltip = tip)
f_pct(int a, int n) => n > 0 ? str.tostring(math.round(100.0 * a / n)) + "%" : "-"
if iPan and barstate.islast
color fg = chart.fg_color
color mu = color.new(chart.fg_color, 50)
f_cell(0, 0, "Yellow Box Range" + MARK, fg, "")
f_cell(1, 0, str.tostring(nz(nS)) + " sessions of 60m", mu, "")
if not tfOk
f_cell(0, 1, "Note", mu, "")
f_cell(1, 1, "use a chart of 60m or lower", fg, "")
else if days.size() > 0
Day d = days.get(0)
string zTx = zone == 0 ? "inside the yellow box" : zone == 1 ? "above the box → AVG " + f_px(d.aT) : zone == 2 ? "above AVG, decision zone" : zone == 3 ? "above MAX R, trend" : zone == -1 ? "below the box → AVG " + f_px(d.aB) : zone == -2 ? "below AVG, decision zone" : "below MAX R, trend"
f_cell(0, 1, "Now", mu, "")
f_cell(1, 1, zTx, fg, "")
f_cell(0, 2, "Box", mu, "")
f_cell(1, 2, f_px(d.yB) + " - " + f_px(d.yT), fg, "")
f_cell(0, 3, "Average", mu, "")
f_cell(1, 3, f_px(d.aB) + " / " + f_px(d.aT), fg, "")
f_cell(0, 4, "Max range", mu, "")
f_cell(1, 4, f_px(d.mB) + " / " + f_px(d.mT), fg, "")
f_cell(0, 5, "Reached", mu, "")
f_cell(1, 5, "box " + f_pct(hYT, nDays) + " / " + f_pct(hYB, nDays) + " · avg " + f_pct(hAT, nDays) + " / " + f_pct(hAB, nDays), fg, "share of past sessions on this chart whose high / low reached each level. n = " + str.tostring(nDays))
f_cell(0, 6, "Max hit", mu, "")
f_cell(1, 6, f_pct(hMT, nDays) + " / " + f_pct(hMB, nDays) + " · n " + str.tostring(nDays), fg, "share of past sessions whose high / low reached the max range")
f_cell(0, 7, "Closed in", mu, "")
f_cell(1, 7, f_pct(hIn, nDays) + " of sessions", fg, "share of past sessions that closed inside the yellow box")
f_cell(0, 8, "Credit", mu, "")
f_cell(1, 8, "free · " + HANDLE + " on X", color.new(chart.fg_color, 65), CR)