本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
// Thygoo's Box: the high and low of the first hour of Globex (6 PM to 7 PM New York) drawn as a box that starts at 7 PM
// and runs until 4 PM New York the next day. One box per session. Works on charts of 1 hour or lower (a 1 minute chart is the intended one).
// The box is placed by time, so old boxes cannot hit TradingView's limit on how far back a drawing can start.
// While the current box is still running, its right edge is always drawn a good way ahead of the last candle and grows until 4 PM
// (TradingView only allows drawings a limited number of candles into the future, so on a 1 minute chart the edge is about 7 hours ahead).
indicator("Thygoo's Box", shorttitle="Thygoo Box", overlay=true, max_boxes_count=500)
gB = "Box"
i_col = input.color(#3b82f6, "Box color", group=gB)
i_op = input.int(15, "Fill opacity (0 invisible, 100 solid)", minval=0, maxval=100, group=gB)
i_bop = input.int(60, "Border opacity (0 invisible, 100 solid)", minval=0, maxval=100, group=gB)
i_bw = input.int(1, "Border thickness (0 = no border)", minval=0, maxval=4, group=gB)
i_n = input.string("All", "How many boxes to show", options=["Current", "Last 3", "Last 5", "All"], group=gB, tooltip="Current = only the latest box. Last 3 / Last 5 = the latest three or five. All = every box the chart has history for.")
string tz = "America/New_York"
bool inWin = not na(time(timeframe.period, "1800-1900", tz))
var float wHi = na
var float wLo = na
var int wStart = na
var array<int> done = array.new<int>(1, 0) // the session whose box was already drawn (arrays do not roll back on live ticks)
var array<box> bxs = array.new<box>()
var array<int> ends = array.new<int>() // each box's 4 PM end time
if inWin
if not inWin[1]
wHi := high
wLo := low
wStart := time
else
wHi := math.max(wHi, high)
wLo := math.min(wLo, low)
int ms = timeframe.in_seconds() * 1000
int maxN = i_n == "Current" ? 1 : i_n == "Last 3" ? 3 : i_n == "Last 5" ? 5 : 100000
// the hour is over: draw the box from 7 PM to 4 PM the next day
if not inWin and inWin[1] and not na(wStart) and array.get(done, 0) != wStart
array.set(done, 0, wStart)
int y = year(wStart, tz)
int m = month(wStart, tz)
int d = dayofmonth(wStart, tz)
int xl = timestamp(tz, y, m, d, 19, 0, 0)
int xe = timestamp(tz, y, m, d + 1, 16, 0, 0)
int xr = math.min(xe, time + 450 * ms)
if xr > xl
array.push(bxs, box.new(xl, wHi, xr, wLo, xloc=xloc.bar_time, border_color=color.new(i_col, 100 - i_bop), border_width=i_bw, bgcolor=color.new(i_col, 100 - i_op)))
array.push(ends, xe)
while array.size(bxs) > maxN
box.delete(array.shift(bxs))
array.shift(ends)
// the running box keeps growing until 4 PM
if barstate.islast and array.size(bxs) > 0
box.set_right(array.get(bxs, array.size(bxs) - 1), math.min(array.get(ends, array.size(ends) - 1), time + 450 * ms))