本页源码来自 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("True FVG+", shorttitle = "True FVG+ | @arleay", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500)
const string NY = "America/New_York"
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_G = "Gaps"
const string G_V = "Validation"
const string G_T = "Stats"
const string G_D = "Display"
const string G_S = "Style"
const string G_I = "Panel"
int iMinTk = input.int(2, "Min gap (ticks)", minval = 1, group = G_G)
string iEnd = input.string("Close through", "Gap ends on", options = ["Close through", "Wick through", "Close past CE"], group = G_G, tooltip = "close through: a candle closes past the far edge. the box stops at that bar.")
int iAge = input.int(48, "Max age (hours)", minval = 1, maxval = 500, group = G_G, tooltip = "a gap still open after this stops and counts as expired.")
string iProf = input.string("Day + prior day", "Profile", options = ["Day + prior day", "Day", "Week + prior week"], group = G_V, tooltip = "the volume profile each gap is measured against, as it stood on the bar the gap formed.")
bool iUseLtf = input.bool(true, "Build profile from 1m bars", group = G_V, tooltip = "spreads each 1 minute bar's volume over its own range. off, or where 1 minute history ends: the chart bar's volume is spread over the chart bar.")
string iVal = input.string("Both", "Validate with", options = ["Both", "Local", "Overall"], group = G_V, tooltip = "local: the gap is thin next to the volume above and below it. overall: the gap is thin against the whole profile. both: needs both.")
float iValley = input.float(0.5, "Local limit (x thinner side)", minval = 0.1, maxval = 1.0, step = 0.05, group = G_V, tooltip = "volume per price inside the gap as a share of the heaviest volume per price on its thinner side. lower is stricter.")
float iGlobal = input.float(0.5, "Overall limit (x profile average)", minval = 0.1, maxval = 1.5, step = 0.05, group = G_V, tooltip = "volume per price inside the gap as a share of the profile's average volume per price. lower is stricter.")
float iSide = input.float(2.0, "Side window (x gap height)", minval = 0.5, maxval = 6.0, step = 0.5, group = G_V, tooltip = "how far above and below the gap the side volume is read. at least 3 rows.")
int iBinTicks = input.int(0, "Row size (ticks, 0 = auto)", minval = 0, group = G_V, tooltip = "price step volume is stored at. auto uses about 0.005% of price. a gap narrower than one row is not measured.")
float iReact = input.float(1.0, "Held after (x ATR)", minval = 0.25, maxval = 5.0, step = 0.25, group = G_T, tooltip = "a touched gap counts as held once price moves this many 14 bar ATRs away from it before the gap ends. failed: it ends first.")
string iMode = input.string("Minimal", "View", options = ["Minimal", "Full"], group = G_D, tooltip = "minimal: live LVN gaps only. full adds gaps that failed the test, faded, and ended LVN gaps.")
int iLiveN = input.int(4, "Live LVN gaps per side", minval = 1, maxval = 20, group = G_D, tooltip = "display cap, newest first. every gap is still tracked and counted.")
int iOtherN = input.int(3, "Other gaps per side (Full)", minval = 0, maxval = 20, group = G_D)
int iEndN = input.int(2, "Ended LVN gaps per side (Full)", minval = 0, maxval = 20, group = G_D)
bool iCE = input.bool(true, "CE line", inline = "d1", group = G_D)
bool iTags = input.bool(true, "Tags", inline = "d1", group = G_D)
bool iTagPct = input.bool(false, "Ratio in tag", inline = "d1", group = G_D)
color cBull = input.color(#1B9E86, "Bull", inline = "s1", group = G_S)
color cBear = input.color(#DA597D, "Bear", inline = "s1", group = G_S)
color cOther = input.color(#787B86, "Other", inline = "s1", group = G_S)
int iTr = input.int(88, "Box transparency", minval = 50, maxval = 98, group = G_S)
color cLine = input.color(#000000, "Lines and labels", group = G_S)
string tSz = input.string("Tiny", "Label size", options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = G_S)
bool iInfo = input.bool(true, "Show", inline = "i1", group = G_I)
bool iCompact = input.bool(false, "Compact", inline = "i1", group = G_I)
string iInfoPos = input.string("Top Right", "Position", options = ["Top Right", "Bottom Right", "Top Left", "Bottom Left"], group = G_I)
bool iHist = input.bool(false, "Show historical LVN gaps", inline = "h1", group = G_D, tooltip = "keeps ended LVN gaps on the chart, faded, in either view. each stops at the bar that ended it.")
int iHistN = input.int(20, "", minval = 1, maxval = 100, inline = "h1", group = G_D, tooltip = "most recent ended LVN gaps drawn per side.")
string iTfMode = input.string("Chart", "Gap timeframe", options = ["Chart", "Aligned HTF"], group = G_G, tooltip = "aligned htf: gaps come from the higher timeframe paired with the chart. 1m → 15m, 3m → 30m, 5m → 1H, 15m → 4H, 1H → D, 4H → W, D → M.")
string iHtfProf = input.string("Auto", "Aligned profile", options = ["Auto", "Use Profile setting"], group = G_V, tooltip = "aligned htf only. auto: day + prior day for gaps up to 1H, week + prior week for 4H, month + prior month for D and above.")
const string G_P = "Profile display"
bool iVp = input.bool(false, "Show profile", group = G_P, tooltip = "draws the profile gaps are measured against, from where it starts.")
color cVp = input.color(color.new(#000000, 90), "Rows", group = G_P)
int iVpRows = input.int(200, "Rows", minval = 20, maxval = 250, group = G_P, tooltip = "rows drawn. stored rows are grouped to fit.")
int iVpW = input.int(25, "Width (% of profile period)", minval = 5, maxval = 100, group = G_P)
f_sz(string s) =>
switch s
"Huge" => size.huge
"Large" => size.large
"Normal" => size.normal
"Small" => size.small
=> size.tiny
f_pos(string s) =>
switch s
"Top Right" => position.top_right
"Bottom Right" => position.bottom_right
"Top Left" => position.top_left
=> position.bottom_left
f_px(float p) => str.tostring(p, format.mintick)
f_clock(int t) => str.format_time(t, "MMM dd HH:mm", NY)
f_pct(float r) => na(r) ? "n/a" : str.tostring(r * 100, "#") + "%"
type Acc
map<int, float> v
int lo
int hi
float tot = 0.0
int n = 0
int startT
type Gap
int dir
float top
float bot
int t
int madeT
float atr
string src
bool meas = false
int why = 0
bool ok = false
float rLoc
float rGlo
int state = 0
int endT
bool touched = false
int outcome = 0
bool counted = false
string tf = ""
bool full = iMode == "Full"
int chSec = timeframe.in_seconds()
string htf = chSec <= 60 ? "15" : chSec <= 180 ? "30" : chSec <= 300 ? "60" : chSec <= 900 ? "240" : chSec <= 3600 ? "D" : chSec <= 14400 ? "W" : "M"
int htfSec = timeframe.in_seconds(htf)
bool htfMode = iTfMode == "Aligned HTF"
string tfName = not htfMode ? "" : htfSec < 3600 ? str.tostring(int(htfSec / 60)) + "m" : htfSec < 86400 ? str.tostring(int(htfSec / 3600)) + "H" : htf
int pKind = htfMode and iHtfProf == "Auto" ? (htfSec <= 3600 ? 1 : htfSec <= 14400 ? 2 : 3) : iProf == "Day" ? 0 : iProf == "Day + prior day" ? 1 : 2
bool two = pKind != 0
float ageMs = iAge * 3600000.0 * (htfMode ? math.max(1.0, htfSec / 3600.0) : 1.0)
string ltf = timeframe.in_seconds() > 60 ? "1" : timeframe.period
bool ltfOn = iUseLtf and timeframe.in_seconds() > 60
var float binSz = na
if na(binSz)
binSz := iBinTicks > 0 ? iBinTicks * syminfo.mintick : math.max(syminfo.mintick, math.round(close * 0.00005 / syminfo.mintick) * syminfo.mintick)
[hA, lA, vA] = request.security_lower_tf(syminfo.tickerid, ltf, [high, low, volume])
bool ltfOk = ltfOn and vA.size() > 0
float atrV = ta.atr(14)
float hi2 = high[2]
float lo2 = low[2]
int t2 = time[2]
f_htf() =>
[time[1], high[1], low[1], close[1], high[3], low[3], time[3], ta.atr(14)[1]]
[hT, hH1, hL1, hC1, hH3, hL3, hT3, hAtr] = request.security(syminfo.tickerid, htf, f_htf(), lookahead = barmerge.lookahead_on)
bool hNew = not na(hT) and hT != hT[1]
f_newAcc() =>
Acc.new(v = map.new<int, float>())
f_spread(Acc a, float lo, float hi, float vv) =>
if not na(lo) and not na(hi)
int r0 = int(math.floor(lo / binSz + 0.000001))
int r1 = int(math.floor(hi / binSz + 0.000001))
a.lo := na(a.lo) ? r0 : math.min(a.lo, r0)
a.hi := na(a.hi) ? r1 : math.max(a.hi, r1)
if vv > 0
int cnt = r1 - r0 + 1
int stp = math.max(1, int(math.ceil(cnt / 300.0)))
float share = vv * stp / cnt
int r = r0
while r <= r1
a.v.put(r, nz(a.v.get(r)) + share)
r += stp
a.tot += vv
a.tot
f_addBar(Acc a) =>
a.n += 1
if na(a.startT)
a.startT := time
if ltfOk
for i = 0 to vA.size() - 1
f_spread(a, lA.get(i), hA.get(i), nz(vA.get(i)))
else
f_spread(a, low, high, nz(volume))
a.n
f_bv(Acc a1, Acc a2, int k) =>
nz(a1.v.get(k)) + (two ? nz(a2.v.get(k)) : 0.0)
f_rangeVol(Acc a1, Acc a2, float p0, float p1) =>
float s = 0.0
int k0 = int(math.floor(p0 / binSz))
int k1 = int(math.floor(p1 / binSz))
for k = k0 to k1
float ov = math.min((k + 1) * binSz, p1) - math.max(k * binSz, p0)
if ov > 0
s += f_bv(a1, a2, k) * ov / binSz
s
f_peak(Acc a1, Acc a2, int k0, int k1) =>
float best = 0.0
for k = k0 + 1 to k1 - 1
best := math.max(best, (f_bv(a1, a2, k - 1) + f_bv(a1, a2, k) + f_bv(a1, a2, k + 1)) / 3.0)
best / binSz
f_measure(Gap g, Acc a1, Acc a2) =>
float h = g.top - g.bot
float tot = a1.tot + (two ? a2.tot : 0.0)
int n = a1.n + (two ? a2.n : 0)
int lo = two and not na(a2.lo) ? (na(a1.lo) ? a2.lo : math.min(a1.lo, a2.lo)) : a1.lo
int hi = two and not na(a2.hi) ? (na(a1.hi) ? a2.hi : math.max(a1.hi, a2.hi)) : a1.hi
g.why := h < binSz ? 1 : n < 10 or tot <= 0 or na(lo) or na(hi) ? 2 : 0
if g.why == 0
float gapD = f_rangeVol(a1, a2, g.bot, g.top) / h
int nS = math.max(3, int(math.ceil(iSide * h / binSz)))
int kA0 = int(math.floor(g.top / binSz)) + 1
int kB1 = int(math.floor(g.bot / binSz)) - 1
float pA = f_peak(a1, a2, kA0, kA0 + nS - 1)
float pB = f_peak(a1, a2, kB1 - nS + 1, kB1)
float side = math.min(pA, pB)
float avgD = tot / ((hi - lo + 1) * binSz)
g.rLoc := side > 0 ? gapD / side : na
g.rGlo := avgD > 0 ? gapD / avgD : na
bool okL = not na(g.rLoc) and g.rLoc <= iValley
bool okG = not na(g.rGlo) and g.rGlo <= iGlobal
g.meas := true
g.ok := iVal == "Local" ? okL : iVal == "Overall" ? okG : okL and okG
g.ok
int td = time_tradingday
bool newDay = bar_index == 0 or td != td[1]
bool newWk = timeframe.change("W")
bool newMo = timeframe.change("M")
var Acc aD = f_newAcc()
var Acc aPD = f_newAcc()
var Acc aW = f_newAcc()
var Acc aPW = f_newAcc()
var Acc aM = f_newAcc()
var Acc aPM = f_newAcc()
var array<Gap> live = array.new<Gap>()
var array<Gap> ended = array.new<Gap>()
var array<int> st = array.new<int>(6, 0)
f_count(Gap g) =>
if g.meas and not g.counted and g.outcome != 0
g.counted := true
int b = g.ok ? 2 : 4
st.set(b, st.get(b) + 1)
if g.outcome == 1
st.set(b + 1, st.get(b + 1) + 1)
g.counted
string srcTxt = (pKind == 3 ? "month + prior month" : pKind == 2 ? "week + prior week" : pKind == 1 ? "day + prior day" : "day") + ", " + (ltfOk ? "1m bars" : "chart bars")
bool aBull = false
bool aBear = false
bool aTouch = false
if barstate.isconfirmed
if pKind <= 1 and newDay and aD.n > 0
aPD := aD
aD := f_newAcc()
if pKind == 2 and newWk and aW.n > 0
aPW := aW
aW := f_newAcc()
if pKind == 3 and newMo and aM.n > 0
aPM := aM
aM := f_newAcc()
Acc a1 = pKind == 3 ? aM : pKind == 2 ? aW : aD
Acc a2 = pKind == 3 ? aPM : pKind == 2 ? aPW : aPD
f_addBar(a1)
if live.size() > 0
for i = live.size() - 1 to 0
Gap g = live.get(i)
int d = g.dir
float mid = (g.top + g.bot) / 2
bool touchNow = d == 1 ? low <= g.top : high >= g.bot
if not g.touched and touchNow
g.touched := true
if g.ok
aTouch := true
else if g.touched and g.outcome == 0 and (d == 1 ? high >= g.top + iReact * g.atr : low <= g.bot - iReact * g.atr)
g.outcome := 1
f_count(g)
float cx = htfMode ? hC1 : close
bool closeChk = not htfMode or hNew
bool mit = iEnd == "Wick through" ? (d == 1 ? low < g.bot : high > g.top) : closeChk and (iEnd == "Close through" ? (d == 1 ? cx < g.bot : cx > g.top) : (d == 1 ? cx < mid : cx > mid))
bool old = time - g.madeT > ageMs
if mit or old
g.state := mit ? 1 : 2
g.endT := time
if mit and g.outcome == 0
g.touched := true
g.outcome := -1
f_count(g)
live.remove(i)
if g.ok
ended.push(g)
while ended.size() > 250
ended.shift()
float minG = iMinTk * syminfo.mintick
float cH1 = htfMode ? hH1 : high
float cL1 = htfMode ? hL1 : low
float cH3 = htfMode ? hH3 : hi2
float cL3 = htfMode ? hL3 : lo2
bool ready = htfMode ? hNew and not na(cH3) : true
bool bullG = ready and cL1 > cH3 and cL1 - cH3 >= minG
bool bearG = ready and cH1 < cL3 and cL3 - cH1 >= minG
if bullG or bearG
int d = bullG ? 1 : -1
Gap g = Gap.new(dir = d, top = d == 1 ? cL1 : cL3, bot = d == 1 ? cH3 : cH1, t = htfMode ? hT3 : t2, madeT = time, atr = htfMode ? hAtr : atrV, src = srcTxt, tf = tfName)
f_measure(g, a1, a2)
if g.meas
int b = g.ok ? 0 : 1
st.set(b, st.get(b) + 1)
if g.ok
aBull := d == 1
aBear := d == -1
live.push(g)
while live.size() > 200
live.shift()
f_tag(Gap g) =>
float r = iVal == "Overall" ? g.rGlo : g.rLoc
(g.tf != "" ? g.tf + " " : "") + (g.ok ? "LVN" : "fvg") + (iTagPct and g.meas ? " " + f_pct(r) : "")
f_tip(Gap g) =>
string s = (g.tf != "" ? g.tf + " " : "") + (g.dir == 1 ? "Bull" : "Bear") + " FVG " + f_px(g.bot) + " – " + f_px(g.top) + " · " + f_clock(g.madeT)
s += g.ok ? "\nLVN: yes" : g.meas ? "\nLVN: no" : g.why == 1 ? "\nnot measured: gap narrower than one row" : "\nnot measured: profile too short or no volume"
if g.meas
s += "\ngap vs thinner side " + f_pct(g.rLoc) + " · limit " + f_pct(iValley)
s += "\ngap vs profile average " + f_pct(g.rGlo) + " · limit " + f_pct(iGlobal)
s += "\nprofile " + g.src
s += "\n" + (g.state == 0 ? "live" : g.state == 1 ? "ended " + f_clock(g.endT) : "expired " + f_clock(g.endT))
s += g.outcome == 1 ? " · held" : g.outcome == -1 ? " · failed" : g.touched ? " · touched" : " · untouched"
s += "\n" + CR
s
var array<box> dBoxes = array.new<box>()
var array<line> dLines = array.new<line>()
var array<label> dLabels = array.new<label>()
var array<line> vpLines = array.new<line>()
var table info = table.new(f_pos(iInfoPos), 2, 8, bgcolor = color.new(chart.bg_color, 6), frame_width = 0, border_color = color.new(chart.bg_color, 6), border_width = 3)
f_drawGap(Gap g, int x2, bool faded) =>
color c = g.ok ? (g.dir == 1 ? cBull : cBear) : cOther
float mid = (g.top + g.bot) / 2
dBoxes.push(box.new(g.t, g.top, x2, g.bot, xloc = xloc.bar_time, bgcolor = color.new(c, faded ? math.min(98, iTr + 6) : iTr), border_color = color.new(c, 100), border_width = 0))
if iCE and g.ok
dLines.push(line.new(g.t, mid, x2, mid, xloc = xloc.bar_time, color = color.new(cLine, faded ? 60 : 0), style = line.style_dotted, width = 1))
if iTags
dLabels.push(label.new(x2, mid, f_tag(g), xloc = xloc.bar_time, style = label.style_label_left, color = color.new(cLine, 100), textcolor = color.new(cLine, faded ? 55 : 0), size = f_sz(tSz), text_font_family = font.family_monospace, tooltip = f_tip(g)))
x2
f_cell(int r, string k, string v, string tip) =>
table.cell(info, 0, r, k, text_color = color.new(chart.fg_color, 45), text_size = f_sz(tSz), text_halign = text.align_left, text_font_family = font.family_monospace)
table.cell(info, 1, r, v, text_color = chart.fg_color, bgcolor = color.new(#787B86, 82), text_size = f_sz(tSz), text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = tip)
r + 1
f_held(int t, int h) =>
t > 0 ? str.tostring(100.0 * h / t, "#") + "% of " + str.tostring(t) : "no touches yet"
if barstate.islast
for b in dBoxes
box.delete(b)
dBoxes.clear()
for ln in dLines
line.delete(ln)
dLines.clear()
for lb in dLabels
label.delete(lb)
dLabels.clear()
for ln in vpLines
line.delete(ln)
vpLines.clear()
if iVp
Acc p1 = pKind == 3 ? aM : pKind == 2 ? aW : aD
Acc p2 = pKind == 3 ? aPM : pKind == 2 ? aPW : aPD
bool use2 = two and p2.n > 0
int lo = use2 and not na(p2.lo) ? (na(p1.lo) ? p2.lo : math.min(p1.lo, p2.lo)) : p1.lo
int hi = use2 and not na(p2.hi) ? (na(p1.hi) ? p2.hi : math.max(p1.hi, p2.hi)) : p1.hi
int x0 = use2 ? p2.startT : p1.startT
if not na(lo) and not na(hi) and hi > lo and not na(x0)
int nb = hi - lo + 1
int grp = math.max(1, int(math.ceil(nb * 1.0 / iVpRows)))
int rows = int(math.ceil(nb * 1.0 / grp))
array<float> v = array.new<float>(rows, 0.0)
for [k, x] in p1.v
int r = math.max(0, math.min(rows - 1, int(math.floor((k - lo) * 1.0 / grp))))
v.set(r, v.get(r) + x)
if use2
for [k, x] in p2.v
int r = math.max(0, math.min(rows - 1, int(math.floor((k - lo) * 1.0 / grp))))
v.set(r, v.get(r) + x)
float mx = v.max()
float w = math.max(time - x0, 20 * chSec * 1000) * iVpW / 100.0
if mx > 0
for i = 0 to rows - 1
float vv = v.get(i)
if vv > 0
float y = (lo + (i + 0.5) * grp) * binSz
vpLines.push(line.new(x0, y, x0 + int(w * vv / mx), y, xloc = xloc.bar_time, color = cVp, width = 1))
int nUp = 0
int nDn = 0
int nOt = 0
for d in array.from(1, -1)
int nV = 0
int nO = 0
if live.size() > 0
for i = live.size() - 1 to 0
Gap g = live.get(i)
if g.dir == d
if g.ok
if d == 1
nUp += 1
else
nDn += 1
if nV < iLiveN
f_drawGap(g, time, false)
nV += 1
else
nOt += 1
if full and nO < iOtherN
f_drawGap(g, time, true)
nO += 1
int endMax = math.max(iHist ? iHistN : 0, full ? iEndN : 0)
if endMax > 0 and ended.size() > 0
int nE = 0
for i = ended.size() - 1 to 0
Gap g = ended.get(i)
if g.dir == d and g.ok and nE < endMax
f_drawGap(g, g.endT, true)
nE += 1
if iInfo
table.clear(info, 0, 0, 1, 7)
table.cell(info, 0, 0, "True FVG+" + MARK, text_color = chart.fg_color, text_size = f_sz(tSz), text_halign = text.align_left, text_formatting = text.format_bold, text_font_family = font.family_monospace)
int r = 1
int made = st.get(0) + st.get(1)
if not iCompact
r := f_cell(r, "Profile", srcTxt, "the profile gaps are measured against and what it is built from on this bar")
r := f_cell(r, "Live", (htfMode ? tfName + " · " : "") + "LVN ↑" + str.tostring(nUp) + " ↓" + str.tostring(nDn) + " · other " + str.tostring(nOt), "live gaps tracked. the chart draws the newest " + str.tostring(iLiveN) + " LVN gaps per side")
r := f_cell(r, "Made", str.tostring(st.get(0)) + " LVN of " + str.tostring(made) + (made > 0 ? " (" + str.tostring(100.0 * st.get(0) / made, "#") + "%)" : ""), "measured gaps on loaded bars and how many passed the LVN test")
r := f_cell(r, "LVN held", f_held(st.get(2), st.get(3)), "touched LVN gaps that then moved " + str.tostring(iReact) + " ATR away before ending. untouched and expired gaps are not counted")
r := f_cell(r, "Other held", f_held(st.get(4), st.get(5)), "the same count for measured gaps that failed the LVN test. compare the two before trusting the filter")
table.cell(info, 0, r, "free · " + HANDLE + " on X", text_color = color.new(chart.fg_color, 65), text_size = f_sz(tSz), text_halign = text.align_left, text_font_family = font.family_monospace, tooltip = CR)
table.merge_cells(info, 0, r, 1, r)
alertcondition(aBull, "True FVG — Bullish", "Bullish FVG formed in a low volume node on {{ticker}} at {{close}} · " + CR)
alertcondition(aBear, "True FVG — Bearish", "Bearish FVG formed in a low volume node on {{ticker}} at {{close}} · " + CR)
alertcondition(aTouch, "True FVG Touched", "Price traded into a live LVN FVG on {{ticker}} at {{close}} · " + CR)