本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Source Code Form is subject to the Mozilla Public License, v. 2.0.
// https://mozilla.org/MPL/2.0/
// Copyright (c) 2026 JoulTrades
// Fair value gap concepts are attributed to ICT; selection and lesson design are JoulTrades conventions.
// Window naming and lifecycle states are explicit JoulTrades conventions.
//@version=6
indicator("JoulTrades - First FVG Classroom", shorttitle = "First FVG Classroom", overlay = true, behind_chart = false, max_lines_count = 50, max_labels_count = 60, max_boxes_count = 10)
const string TZ = "America/New_York"
const string LEARN = "Lesson"
const string LOOK = "Appearance"
const string ADV = "Advanced study"
lesson = input.string("AM 09:31-10:00", "Study window (New York)", options = ["AM 09:31-10:00", "PM 13:30-14:00"], tooltip = "Candle 2 names the gap; candle 3 CLOSE confirms it. Default: first gap with displacement. AM reference starts 09:30; PM reference starts 13:29 so the first eligible middle candle is 13:30. The advanced 09:30 exception is excluded.", group = LEARN)
showNumbers = input.bool(true, "Number the three candles", tooltip = "Numbers are added AFTER candle 3 closes. They explain formation retrospectively, not signals on candles 1 or 2.", group = LEARN)
showPanel = input.bool(true, "Show lesson card", group = LEARN)
showQuarters = input.bool(false, "Show quarter levels", group = LEARN)
gapColor = input.color(#CBA652, "Gap and midpoint", group = LOOK)
failedColor = input.color(#CF9389, "Closed beyond boundary", group = LOOK)
panelPosition = input.string("Bottom right", "Lesson card position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = LOOK)
fontSize = input.string("Normal", "Text size", options = ["Small", "Normal", "Large"], group = LOOK)
shade = input.bool(true, "Lightly shade the gap", group = LOOK)
string clockMode = "Middle candle"
selection = input.string("First gap with displacement", "Gap selection", options = ["First gap with displacement", "First geometric gap"], tooltip = "JoulTrades rule: candle 2 must be directional and CLOSE above all earlier highs (bullish) or below all earlier lows (bearish) since the reference start. Candle 2 and candle 3 are excluded from that earlier range. This mechanical test is an implementation choice, not an ICT numerical definition.", group = ADV)
minGapTicks = input.int(4, "Minimum gap (ticks)", minval = 0, tooltip = "Matches the existing JT model default. Both modes use this size filter. Set 0 with geometric mode for the literal first positive wick gap.", group = ADV)
historyDays = input.int(1, "NY dates to retain", minval = 1, maxval = 5, tooltip = "Default: current chart date only. Increase for dated comparisons. Failed gaps remain. Observation ends at 16:00 NY each day: a Classroom review convention.", group = ADV)
if not timeframe.isminutes or timeframe.multiplier != 1
runtime.error("First FVG Classroom: choose the 1-minute (1m) chart interval.")
if not chart.is_standard
runtime.error("First FVG Classroom: use standard Candles or Bars. Synthetic candles change the gap prices.")
string txtSize = fontSize == "Large" ? size.large : fontSize == "Normal" ? size.normal : size.small
string pos = panelPosition == "Bottom right" ? position.bottom_right : panelPosition == "Top left" ? position.top_left : panelPosition == "Bottom left" ? position.bottom_left : position.top_right
bool isAM = lesson == "AM 09:31-10:00"
int startMinute = isAM ? 571 : 810
int endMinute = isAM ? 600 : 840
int offset = clockMode == "Middle candle" ? 1 : 0
int firstThird = startMinute + offset
int lastThird = endMinute - 1 + offset
int firstRequired = firstThird - 2
int nyMinute = hour(time, TZ) * 60 + minute(time, TZ)
string nyDate = str.format_time(time, "yyyy-MM-dd", TZ)
bool newDate = barstate.isfirst or nyDate != nyDate[1]
bool contiguous = not na(time[2]) and time - time[1] == 60000 and time[1] - time[2] == 60000
bool bullGap = contiguous and low > high[2] and low - high[2] >= minGapTicks * syminfo.mintick
bool bearGap = contiguous and high < low[2] and low[2] - high >= minGapTicks * syminfo.mintick
// Same AM displacement proxy as JT 1st FVG; PM uses its own lesson anchor.
var float referenceHigh = na
var float referenceLow = na
if newDate
referenceHigh := na
referenceLow := na
if nyMinute >= firstRequired and nyMinute <= lastThird
referenceHigh := na(referenceHigh) ? high : math.max(referenceHigh, high)
referenceLow := na(referenceLow) ? low : math.min(referenceLow, low)
bool needsDisplacement = selection == "First gap with displacement"
bool bullDisplacement = close[1] > open[1] and close[1] > referenceHigh[2]
bool bearDisplacement = close[1] < open[1] and close[1] < referenceLow[2]
bool qualifiedBull = bullGap and (not needsDisplacement or bullDisplacement)
bool qualifiedBear = bearGap and (not needsDisplacement or bearDisplacement)
float labelPadding = math.max(nz(ta.atr(14), high - low) * 0.25, syminfo.mintick * 4)
type Lesson
string day
int stopAt
int phase = 0
int previous = na
float lo = na
float hi = na
bool bullish = false
int namedAt = na
int knownAt = na
int revisitAt = na
int beyondAt = na
bool missingAfter = false
line lower = na
line middle = na
line upper = na
line q1 = na
line q3 = na
box area = na
label lowerTag = na
label middleTag = na
label upperTag = na
label one = na
label two = na
label three = na
method erase(Lesson s) =>
line.delete(s.lower)
line.delete(s.middle)
line.delete(s.upper)
line.delete(s.q1)
line.delete(s.q3)
box.delete(s.area)
label.delete(s.lowerTag)
label.delete(s.middleTag)
label.delete(s.upperTag)
label.delete(s.one)
label.delete(s.two)
label.delete(s.three)
f_price(float p) =>
str.tostring(p, "#.########")
f_time(int t) =>
na(t) ? "Not observed" : str.format_time(t, "HH:mm", TZ) + " NY"
method paint(Lesson s, int oneAt, int twoAt, float oneLow, float oneHigh, float twoLow, float twoHigh) =>
float mid = (s.lo + s.hi) * 0.5
int x = s.knownAt
s.lower := line.new(x, s.lo, x + 60000, s.lo, xloc = xloc.bar_time, color = gapColor)
s.middle := line.new(x, mid, x + 60000, mid, xloc = xloc.bar_time, color = gapColor, style = line.style_dashed, width = 2)
s.upper := line.new(x, s.hi, x + 60000, s.hi, xloc = xloc.bar_time, color = gapColor)
if showQuarters
s.q1 := line.new(x, s.lo + (s.hi - s.lo) * 0.25, x + 60000, s.lo + (s.hi - s.lo) * 0.25, xloc = xloc.bar_time, color = color.new(gapColor, 50), style = line.style_dotted)
s.q3 := line.new(x, s.lo + (s.hi - s.lo) * 0.75, x + 60000, s.lo + (s.hi - s.lo) * 0.75, xloc = xloc.bar_time, color = color.new(gapColor, 50), style = line.style_dotted)
// Retrospective formation box: created only at candle 3 close, anchored to candle 1.
s.area := box.new(oneAt, s.hi, x + 60000, s.lo, xloc = xloc.bar_time, border_color = color.new(gapColor, 40), bgcolor = color.new(gapColor, shade ? 94 : 100))
string tag = str.substring(s.day, 5, 10) + (isAM ? " AM" : " PM")
string tip = tag + " | " + (s.bullish ? "Bullish" : "Bearish") + " gap\nNamed: " + f_time(s.namedAt) + " (" + clockMode + ")\nConfirmed: " + f_time(s.knownAt) + "\nBox extends back to candle 1 retrospectively; first known at candle 3 close.\nOriginal boundaries stay fixed. A study level, not an entry."
s.middleTag := label.new(x, mid, tag + " | Midpoint " + f_price(mid), xloc = xloc.bar_time, style = label.style_label_right, color = #15171B, textcolor = gapColor, size = txtSize, tooltip = "CE means consequent encroachment: midpoint of this gap.\nLow " + f_price(s.lo) + " | High " + f_price(s.hi) + "\n" + tip)
if showNumbers
string numberTip = "Added retrospectively when candle 3 closed at " + f_time(s.knownAt) + ". Not confirmed on candles 1 or 2."
string numberStyle = s.bullish ? label.style_label_up : label.style_label_down
s.one := label.new(oneAt, s.bullish ? oneLow - labelPadding : oneHigh + labelPadding, "1", xloc = xloc.bar_time, style = numberStyle, color = gapColor, textcolor = #15171B, size = txtSize, tooltip = "Candle 1: first edge.\n" + numberTip)
s.two := label.new(twoAt, s.bullish ? twoLow - labelPadding : twoHigh + labelPadding, "2", xloc = xloc.bar_time, style = numberStyle, color = gapColor, textcolor = #15171B, size = txtSize, tooltip = "Candle 2: middle candle.\n" + numberTip)
s.three := label.new(time, s.bullish ? low - labelPadding : high + labelPadding, "3", xloc = xloc.bar_time, style = numberStyle, color = gapColor, textcolor = #15171B, size = txtSize, tooltip = "Candle 3: wick range does not overlap candle 1. Its CLOSE confirms the gap.\n" + numberTip)
method extendTo(Lesson s, int rightEdge) =>
line.set_x2(s.lower, rightEdge)
line.set_x2(s.middle, rightEdge)
line.set_x2(s.upper, rightEdge)
line.set_x2(s.q1, rightEdge)
line.set_x2(s.q3, rightEdge)
box.set_right(s.area, rightEdge)
label.set_x(s.lowerTag, rightEdge)
label.set_x(s.middleTag, math.max(s.knownAt, math.min(rightEdge, chart.right_visible_bar_time) - 2 * 60000))
label.set_x(s.upperTag, rightEdge)
method refreshTooltip(Lesson s) =>
string detail = s.day + " | " + (s.bullish ? "Bullish" : "Bearish") + " gap\nLow " + f_price(s.lo) + " | High " + f_price(s.hi) + "\nNamed " + f_time(s.namedAt) + " | Confirmed " + f_time(s.knownAt) + "\nRevisit close " + f_time(s.revisitAt) + "\nBeyond close " + f_time(s.beyondAt) + "\n" + (s.missingAfter ? "Missing bars: times are observed, not guaranteed first events." : "Closed-bar observations through 16:00 NY. No entry signal.")
label.set_tooltip(s.middleTag, detail + "\nBox spans from candle 1 retrospectively; only known at candle 3 close.")
var records = array.new<Lesson>()
var Lesson active = na
if barstate.isconfirmed
if newDate
if not na(active) and active.phase == 2 and active.previous + 60000 < active.stopAt
active.missingAfter := true
active.refreshTooltip()
int stop = timestamp(TZ, year(time, TZ), month(time, TZ), dayofmonth(time, TZ), 16, 0)
active := Lesson.new(nyDate, stop)
records.push(active)
while records.size() > historyDays
Lesson oldest = records.shift()
oldest.erase()
if active.phase < 2
if nyMinute == firstRequired
active.phase := 1
active.previous := time
else if nyMinute > firstRequired and nyMinute <= lastThird
if active.phase == 1 and time - active.previous == 60000
active.previous := time
else
active.phase := 4
if active.phase == 1 and nyMinute >= firstThird and nyMinute <= lastThird
if qualifiedBull or qualifiedBear
active.phase := 2
active.bullish := bullGap
active.lo := bullGap ? high[2] : high
active.hi := bullGap ? low : low[2]
active.namedAt := offset == 1 ? time[1] : time
active.knownAt := time_close
active.previous := time
active.paint(time[2], time[1], low[2], high[2], low[1], high[1])
else if nyMinute == lastThird
active.phase := 3
if nyMinute > lastThird and active.phase < 2
active.phase := 4
if active.phase == 2
if time >= active.knownAt and time < active.stopAt
if time - active.previous != 60000
active.missingAfter := true
active.previous := time
if na(active.revisitAt) and low <= active.hi and high >= active.lo
active.revisitAt := time_close
bool beyond = active.bullish ? close < active.lo : close > active.hi
if na(active.beyondAt) and beyond
active.beyondAt := time_close
line.set_color(active.lower, failedColor)
line.set_color(active.middle, failedColor)
line.set_color(active.upper, failedColor)
box.set_bgcolor(active.area, color.new(failedColor, shade ? 94 : 100))
box.set_border_color(active.area, color.new(failedColor, 40))
label.set_text(active.middleTag, str.substring(active.day, 5, 10) + (isAM ? " AM" : " PM") + " | Closed beyond | Mid " + f_price((active.hi + active.lo) * 0.5))
label.set_textcolor(active.middleTag, failedColor)
if time >= active.stopAt and active.previous + 60000 < active.stopAt
active.missingAfter := true
active.refreshTooltip()
active.extendTo(math.min(time_close, active.stopAt))
var card = table.new(pos, 2, 8, bgcolor = #15171B, frame_color = #363636, frame_width = 1, border_width = 0)
f_row(int row, string heading, string value, color valueColor) =>
table.cell(card, 0, row, heading, text_color = #AAA59C, text_size = txtSize, text_halign = text.align_left)
table.cell(card, 1, row, value, text_color = valueColor, text_size = txtSize, text_halign = text.align_left)
if barstate.islast and showPanel
string status = "Waiting for window"
string formation = "Three closed candles"
string confirmation = "Candle 3 must close"
string revisit = "Not observed"
string boundary = "Not observed"
string foot = "1m standard candles | NY time"
color statusColor = gapColor
if not na(active)
if active.phase == 1
status := needsDisplacement ? "Waiting for displacement gap" : "Looking for first gap"
if active.phase == 3
status := "No eligible gap"
foot := "Full window; no qualifying gap"
if active.phase == 4
status := "Incomplete data"
foot := "Missing minute bars; first gap unknown"
if active.phase == 2
status := not na(active.beyondAt) ? "Closed beyond boundary" : not na(active.revisitAt) ? "Revisited" : "Confirmed"
statusColor := not na(active.beyondAt) ? failedColor : gapColor
formation := (active.bullish ? "Bullish" : "Bearish") + " | " + f_time(active.namedAt)
confirmation := f_time(active.knownAt)
revisit := f_time(active.revisitAt)
boundary := f_time(active.beyondAt)
foot := (time >= active.stopAt or (barstate.isconfirmed and time_close >= active.stopAt)) ? "Review ended 16:00 NY | Levels only" : "A marked gap is not a trade signal"
if active.missingAfter
foot := "Data gaps: event times are observed only"
if active.day != nyDate
status := "Waiting for first closed bar"
formation := "New NY date"
confirmation := "Candle 3 must close"
revisit := "Not observed"
boundary := "Not observed"
foot := "Previous-date drawings await bar close"
f_row(0, "JOULTRADES", "FIRST FVG CLASSROOM", #F3EEDF)
f_row(1, isAM ? "AM lesson" : "PM lesson", nyDate + " | " + (isAM ? "09:31-10:00" : "13:30-14:00"), #F3EEDF)
f_row(2, "State", status, statusColor)
f_row(3, needsDisplacement ? "Displacement gap" : "Geometric gap", formation, #F3EEDF)
f_row(4, "Confirmed", confirmation, #F3EEDF)
f_row(5, "Revisit close", revisit, #F3EEDF)
f_row(6, "Beyond close", boundary, #F3EEDF)
f_row(7, "Remember", foot, #AAA59C)