本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// © Zac
// Fast Asia-open gap zones, including Sunday weekend gaps by default.
// Built for 1-second TradingView charts. No dashboard, signals or EMA.
//@version=6
indicator("Zac's Fast Loading Asia Gap Zones + Market Sessions", shorttitle="Zac's Fast Loading Asia Gap Zones + Market Sessions", overlay=true,
max_boxes_count=150)
// ─── GAP SETTINGS ────────────────────────────────────────────────────────────
string TZ = "America/New_York"
string mode = input.string("Every Asia (Sun-Thu)", "Openings to draw",
options=["Every Asia (Sun-Thu)", "Weekday Asia (Mon-Thu)", "Sunday only"])
int openHour = input.int(17, "Asia opening hour (New York)", minval=0, maxval=23)
int openMinute = input.int(0, "Asia opening minute", minval=0, maxval=59)
int minTicks = input.int(1, "Minimum opening gap (ticks)", minval=1)
int keepZones = input.int(120, "Keep latest gap zones", minval=1, maxval=120)
int maxLifeHours = input.int(120, "Maximum zone life (hours; 0 = until filled)", minval=0, maxval=720)
color upColor = input.color(color.rgb(230, 116, 76), "Gap UP color")
color downColor = input.color(color.rgb(44, 177, 162), "Gap DOWN color")
// ─── ZAC'S NEW YORK / LONDON / ASIA SESSION BACKGROUNDS ─────────────────────
// Matches the recent ZACS session color settings; visual only.
// All times are in New York time (automatic DST adjustment).
const string SESSION_BG_GROUP = "Trading Sessions - Backgrounds"
bool showSessionBackgrounds = input.bool(true, "Show Session Backgrounds", group=SESSION_BG_GROUP)
bool showNYBackground = input.bool(true, "Show New York Background", group=SESSION_BG_GROUP)
string nySessionHours = input.session("0930-1700", "New York Session (NY Time)", group=SESSION_BG_GROUP)
color nySessionColor = input.color(color.new(color.blue, 93), "New York Background Color", group=SESSION_BG_GROUP)
bool showLondonBackground = input.bool(true, "Show London Background", group=SESSION_BG_GROUP)
string londonSessionHours = input.session("0300-0930", "London Session (NY Time)", group=SESSION_BG_GROUP)
color londonSessionColor = input.color(color.new(color.green, 93), "London Background Color", group=SESSION_BG_GROUP)
bool showAsiaBackground = input.bool(true, "Show Asia Background", group=SESSION_BG_GROUP)
string asiaSessionHours = input.session("1700-0300", "Asia Session (NY Time)", group=SESSION_BG_GROUP)
color asiaSessionColor = input.color(color.new(color.yellow, 93), "Asia Background Color", group=SESSION_BG_GROUP)
// A single bgcolor call keeps the overlay especially light for 1-second charts.
// If users customize session hours to overlap, priority is NY > London > Asia.
bool nySessionActive = showSessionBackgrounds and showNYBackground and
not na(time(timeframe.period, nySessionHours + ":1234567", TZ))
bool londonSessionActive = showSessionBackgrounds and showLondonBackground and
not na(time(timeframe.period, londonSessionHours + ":1234567", TZ))
bool asiaSessionActive = showSessionBackgrounds and showAsiaBackground and
not na(time(timeframe.period, asiaSessionHours + ":1234567", TZ))
color activeSessionBg = nySessionActive ? nySessionColor :
londonSessionActive ? londonSessionColor :
asiaSessionActive ? asiaSessionColor : na
bgcolor(activeSessionBg, title="ZACS Trading Sessions Background")
// For Sunday on 1-second charts: Friday's 1s candle may be outside available
// history. The previous confirmed 1-minute close supplies a lightweight backup.
// On the FIRST opening minute, these normally point to Friday's last 1m bar.
[previousMinuteClose, previousMinuteTime] = request.security(syminfo.tickerid, "1", [close[1], time[1]],
gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
type OpenGap
box zone
float fillPrice
int direction
int startedAt
var array<OpenGap> active = array.new<OpenGap>()
var array<OpenGap> visible = array.new<OpenGap>()
var int lastHandledDay = na
// ─── OPENING BAR: INCLUDING THE WEEKEND REOPEN ───────────────────────────────
int todaysOpen = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), openHour, openMinute)
int nyDay = dayofweek(time, TZ)
bool weekdayAllowed = nyDay >= dayofweek.monday and nyDay <= dayofweek.thursday
bool dayAllowed = mode == "Sunday only" ? nyDay == dayofweek.sunday :
mode == "Every Asia (Sun-Thu)" ? (nyDay == dayofweek.sunday or weekdayAllowed) : weekdayAllowed
// Normal case: first bar to cross the daily Asia opening time.
bool crossedOpen = not na(time[1]) and time >= todaysOpen and time[1] < todaysOpen
// Weekend reopen can sometimes be earlier than the selected opening hour.
bool actualWeekendReopen = nyDay == dayofweek.sunday and not na(time[1]) and
(dayofweek(time[1], TZ) == dayofweek.friday or dayofweek(time[1], TZ) == dayofweek.saturday) and
time - time[1] >= 12 * 3600000
// On 1s charts the earliest loaded Sunday candle can have no Friday predecessor.
// Verify a real weekend break using the prior minute timestamp, not a clock guess.
bool firstLoadedSundayBar = barstate.isfirst and nyDay == dayofweek.sunday and
not na(previousMinuteTime) and time - previousMinuteTime >= 12 * 3600000
int nyDateKey = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
bool openingBar = dayAllowed and (crossedOpen or actualWeekendReopen or firstLoadedSundayBar) and
(na(lastHandledDay) or lastHandledDay != nyDateKey)
// ─── UPDATE ONLY STILL-OPEN ZONES ───────────────────────────────────────────
int activeCount = array.size(active)
if activeCount > 0
for offset = 0 to activeCount - 1
int i = activeCount - 1 - offset
OpenGap gap = array.get(active, i)
bool filled = gap.direction == 1 ? low <= gap.fillPrice : high >= gap.fillPrice
bool expired = maxLifeHours > 0 and time >= gap.startedAt + maxLifeHours * 3600000
if filled or expired
box.set_extend(gap.zone, extend.none)
box.set_right(gap.zone, expired and not filled ? gap.startedAt + maxLifeHours * 3600000 : time_close)
array.remove(active, i)
// ─── DRAW GAP ON OPEN, WITHOUT WAITING FOR BAR CLOSE ─────────────────────────
if openingBar
lastHandledDay := nyDateKey
bool sunday = nyDay == dayofweek.sunday
float reference = not na(close[1]) ? close[1] : sunday ? previousMinuteClose : na
if not na(reference)
float openingGap = open - reference
if math.abs(openingGap) >= minTicks * syminfo.mintick
int dir = openingGap > 0 ? 1 : -1
color gapColor = dir == 1 ? upColor : downColor
bool filledOnOpeningBar = dir == 1 ? low <= reference : high >= reference
box region = box.new(left=time, top=math.max(open, reference),
right=time_close, bottom=math.min(open, reference), xloc=xloc.bar_time,
border_color=color.new(gapColor, 60), bgcolor=color.new(gapColor, 88),
extend=filledOnOpeningBar ? extend.none : extend.right)
OpenGap g = OpenGap.new(region, reference, dir, time)
array.push(visible, g)
if not filledOnOpeningBar
array.push(active, g)
if array.size(visible) > keepZones
OpenGap old = array.shift(visible)
box.delete(old.zone)
int count = array.size(active)
if count > 0
for offset = 0 to count - 1
int i = count - 1 - offset
if array.get(active, i).startedAt == old.startedAt
array.remove(active, i)