本页源码来自 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/
// © Sattam
//@version=6
indicator("IFVG 4-Step Model | Sattam", shorttitle = "IFVG 4-Step", overlay = true, max_boxes_count = 500, max_lines_count = 500, max_labels_count = 500, max_bars_back = 2000)
// ════════════════════════════════════════════════════════════════════════════
// The method in four steps
// 1) Higher-timeframe bias (respected vs disrespected FVGs) + draw on liquidity
// 2) A valid key level in the bias direction: unmitigated FVG, ITL/ITH inside
// a gap, CISD, rejection block
// 3) Confirmation: the highest-timeframe inversion FVG inside the manipulation
// leg that hit the key level
// 4) Execution: entry on the inversion close, stop at the leg extreme,
// breakeven at the opposite leg extreme, target = nearest liquidity (1R..3R),
// New York entry window and daily limits
// Every higher-timeframe value is read from closed bars only (no repaint).
// ════════════════════════════════════════════════════════════════════════════
// ───────────────────────────── Inputs ─────────────────────────────
G_B = "① Bias & DOL · التحيّز وهدف السيولة"
iBD = input.bool(true, "Daily · يومي", group = G_B)
iB4 = input.bool(true, "4H · أربع ساعات", group = G_B)
iB1 = input.bool(true, "1H · ساعة", group = G_B)
iB15 = input.bool(false, "15m · ربع ساعة", group = G_B)
iBMode = input.string("Majority · الأغلبية", "Bias rule · قاعدة التحيّز", options = ["Majority · الأغلبية", "All agree · الإجماع"], group = G_B,
tooltip = "Each timeframe is bullish after it respects a bullish FVG or closes through a bearish FVG, and bearish for the opposite.\nكل فريم يصير صاعداً إذا احترم فجوة صاعدة أو أغلق خلف فجوة هابطة، والعكس للهابط.")
iDol = input.bool(true, "Show draw on liquidity · إظهار هدف السيولة", group = G_B)
iDolTf = input.timeframe("60", "DOL timeframe · فريم هدف السيولة", group = G_B)
iDolStr = input.int(3, "DOL swing strength · قوة القمة/القاع", minval = 1, maxval = 5, group = G_B)
G_K = "② Key levels · المستويات المفتاحية"
iK1 = input.bool(true, "Timeframe 1 · الفريم 1", inline = "k1", group = G_K)
iK1tf = input.timeframe("5", "", inline = "k1", group = G_K)
iK2 = input.bool(true, "Timeframe 2 · الفريم 2", inline = "k2", group = G_K)
iK2tf = input.timeframe("15", "", inline = "k2", group = G_K)
iK3 = input.bool(true, "Timeframe 3 · الفريم 3", inline = "k3", group = G_K)
iK3tf = input.timeframe("60", "", inline = "k3", group = G_K)
iKF = input.bool(true, "Unmitigated FVG · فجوة غير ممسوسة", group = G_K)
iKI = input.bool(true, "ITL / ITH inside the gap · القاع/القمة الوسيطة داخل الفجوة", group = G_K)
iKC = input.bool(true, "CISD · تغيّر حالة التسليم", group = G_K)
iKR = input.bool(true, "Rejection block · كتلة الرفض", group = G_K)
iRbW = input.float(0.25, "Rejection wick ≥ × candle range · الذيل ≥ × مدى الشمعة", minval = 0.1, maxval = 0.9, step = 0.05, group = G_K)
iMinG = input.int(0, "Minimum gap size (ticks) · أقل حجم للفجوة (تك)", minval = 0, group = G_K)
iOnly = input.bool(true, "Draw only bias-direction levels · ارسم مستويات اتجاه التحيّز فقط", group = G_K)
iMaxD = input.int(4, "Nearest live levels shown per timeframe · أقرب المستويات الحية المعروضة لكل فريم", minval = 1, maxval = 20, group = G_K)
iDist = input.float(1.0, "Show levels within × daily ATR of price · اعرض المستويات ضمن × ATR اليومي من السعر", minval = 0.1, maxval = 10, step = 0.1, group = G_K)
G_C = "③ IFVG confirmation · التأكيد بالـ IFVG"
iL30 = input.bool(false, "30s (needs seconds data) · 30 ثانية (تحتاج بيانات الثواني)", group = G_C)
iL1 = input.bool(true, "IFVG 1m", group = G_C)
iL2 = input.bool(true, "IFVG 2m", group = G_C)
iL3 = input.bool(true, "IFVG 3m", group = G_C)
iL4 = input.bool(true, "IFVG 4m", group = G_C)
iL5 = input.bool(true, "IFVG 5m", group = G_C)
iLegStr = input.int(5, "Manipulation leg swing strength (bars) · قوة قمة/قاع ساق التلاعب (شموع)", minval = 2, maxval = 20, group = G_C)
iExp = input.int(60, "Setup expiry (bars) · انتهاء الإعداد (شموع)", minval = 5, maxval = 500, group = G_C)
G_E = "④ Execution & risk · التنفيذ وإدارة المخاطرة"
iWS = input.int(930, "Entry window start (New York, HHMM) · بداية نافذة الدخول (نيويورك)", minval = 0, maxval = 2359, group = G_E)
iWE = input.int(1100, "Entry window end (New York, HHMM) · نهاية نافذة الدخول (نيويورك)", minval = 0, maxval = 2359, group = G_E)
iTgtTf = input.timeframe("5", "Target liquidity timeframe · فريم سيولة الهدف", group = G_E)
iTgtStr = input.int(3, "Target swing strength · قوة قمة/قاع الهدف", minval = 1, maxval = 10, group = G_E)
iMinRR = input.float(1.0, "Minimum R:R · أقل عائد للمخاطرة", minval = 0.5, maxval = 10, step = 0.1, group = G_E)
iMaxRR = input.float(3.0, "Maximum R:R · أعلى عائد للمخاطرة", minval = 0.5, maxval = 20, step = 0.1, group = G_E)
iBE = input.bool(true, "Breakeven at the leg extreme · التعادل عند طرف الساق", group = G_E)
iMaxT = input.int(2, "Max trades per day · أقصى صفقات في اليوم", minval = 1, maxval = 10, group = G_E)
iMaxL = input.int(2, "Stop after losses · توقّف بعد خسائر", minval = 1, maxval = 10, group = G_E)
iStopWin = input.bool(true, "Stop after a win · توقّف بعد ربح", group = G_E)
iMaxRisk = input.float(0, "Max stop size in points (0 = off) · أقصى وقف بالنقاط (0 = بلا حد)", minval = 0, group = G_E)
iBig = input.string("Limit at the IFVG · أمر معلّق عند الـ IFVG", "When the stop is too big · إذا كان الوقف أكبر من الحد", options = ["Limit at the IFVG · أمر معلّق عند الـ IFVG", "Skip · تخطَّ الصفقة"], group = G_E)
iTgtGap = input.bool(true, "Unfilled gaps are targets too · الفجوات غير المملوءة أهداف أيضاً", group = G_E)
iEod = input.int(1600, "Close an open trade at (New York, HHMM) · إغلاق الصفقة المفتوحة عند (نيويورك)", minval = 0, maxval = 2359, group = G_E)
G_S = "⑤ Style · الشكل"
iSmt = input.bool(true, "Show SMT · إظهار SMT", group = G_S)
iSmtSym = input.symbol("", "SMT symbol (empty = auto) · رمز المقارنة (فارغ = تلقائي)", group = G_S)
iPos = input.bool(true, "Show position (target / stop) · إظهار المركز (الهدف / الوقف)", group = G_S)
iTbl = input.bool(true, "Show panel · إظهار اللوحة", group = G_S)
iLang = input.string("English", "Panel language · لغة اللوحة", options = ["English", "العربية"], group = G_S)
iHist = input.int(30, "Trade history rows (0 = off) · عدد صفقات السجل (0 = إيقاف)", minval = 0, maxval = 60, group = G_S)
iHistPos = input.string("Bottom left · أسفل اليسار", "Trade history position · مكان سجل الصفقات", options = ["Top right · أعلى اليمين", "Bottom right · أسفل اليمين", "Top left · أعلى اليسار", "Bottom left · أسفل اليسار"], group = G_S)
iTblPos = input.string("Top right · أعلى اليمين", "Panel position · مكان اللوحة", options = ["Top right · أعلى اليمين", "Bottom right · أسفل اليمين", "Top left · أعلى اليسار", "Bottom left · أسفل اليسار"], group = G_S)
cFvgB = input.color(color.rgb(120, 120, 110, 80), "Bullish FVG · فجوة صاعدة", group = G_S)
cFvgS = input.color(color.rgb(138, 100, 106, 80), "Bearish FVG · فجوة هابطة", group = G_S)
cIfvg = input.color(color.rgb(103, 105, 114, 70), "IFVG / RB", group = G_S)
iPosBars = input.int(20, "Trade box width ahead of price (bars) · امتداد صندوق الصفقة أمام السعر (شموع)", minval = 3, maxval = 200, group = G_S)
cPosTp = input.color(#26A69A, "Trade box · TP · صندوق الصفقة · الهدف", group = G_S)
cPosSl = input.color(#F23645, "Trade box · SL · صندوق الصفقة · الوقف", group = G_S)
cPosEn = input.color(#2962FF, "Trade box · entry · صندوق الصفقة · الدخول", group = G_S)
// ───────────────────────────── Constants ─────────────────────────────
TZ = "America/New_York"
cLine = color.new(chart.fg_color, 15)
C_BOXTXT = color.new(chart.fg_color, 45)
C_RBTXT = color.rgb(130, 100, 96)
C_CE = color.new(chart.fg_color, 40)
C_POS = color.rgb(62, 104, 128)
LBG = chart.bg_color
CH_SEC = timeframe.in_seconds()
EXT = 20 * CH_SEC * 1000
EXEC_OK = CH_SEC <= 60
MIN_SZ = iMinG * syminfo.mintick
// ───────────────────────────── Types ─────────────────────────────
type Feed
string tf
string tag
bool ok
int sec
array<float> o
array<float> h
array<float> l
array<float> c
array<int> t
int n = 0
type Gap
int dir
float top
float bot
int t1
int tm
int t3
int born
bool alive = true
bool fresh = true
bool inv = false
float itl = na
int itlT = na
box bx = na
line il = na
label ilb = na
type Lvl
int dir
int kind
float lvl
float ext
int t
int born
bool alive = true
bool used = false
line ln = na
label lb = na
box bx = na
line ce = na
type KL
Feed f
int idx
array<Gap> gaps
array<Lvl> lv
int lastDir = 0
bool dA = false
float dO = na
float dL = na
int dT = na
bool dTouch = false
bool uA = false
float uO = na
float uH = na
int uT = na
bool uTouch = false
type LT
Feed f
array<Gap> gaps
type Piv
int dir
float p
int t
bool swept = false
bool wasDol = false
line ln = na
label lb = na
type Tr
int t
int dir
string tag
float e
float s
float tp
float rr
int code = 9
float r = na
type St
int bias = 0
int sDir = 0
float sAnc = na
int sAncT = na
float sExt = na
int sExtBi = na
string sInfo = ""
Gap sG = na
Lvl sV = na
int pDir = 0
float pPx = na
float pStop = na
float pBE = na
int pBeT = na
int pBi = na
int pSec = na
string pTag = ""
Gap pG = na
int tDay = na
int tDir = 0
float tE = na
float tS = na
float tT = na
float tBE = na
float tRisk = na
float tRR = na
int tEt = na
bool tBeDone = false
box bT = na
box bS = na
label lT = na
label lS = na
label lM = na
line lBE = na
label lBEl = na
box bI = na
line lE = na
line l1 = na
line l2 = na
label lb1 = na
label lb2 = na
int dKey = na
int dTrades = 0
int dLoss = 0
bool dWin = false
int nT = 0
int nW = 0
int nL = 0
int nBE = 0
float sumR = 0.0
string lastHit = ""
string lastTrade = ""
float dHit = na
float dEnt = na
float dEp = na
float dSl = na
float dTp = na
float dEx = na
float dXp = na
// ───────────────────────────── Helpers ─────────────────────────────
f_tag(string tf) =>
int s = timeframe.in_seconds(tf)
s < 60 ? str.tostring(s) + "s" : s < 3600 ? str.tostring(s / 60) + "m" : s < 86400 ? str.tostring(s / 3600) + "h" : s < 604800 ? "D" : "W"
f_valid(string tf, bool on) => on and timeframe.in_seconds(tf) >= CH_SEC
f_req(string tf, bool on) => f_valid(tf, on) ? tf : ""
f_feed(string tf, bool on) =>
Feed.new(tf, f_tag(tf), f_valid(tf, on), timeframe.in_seconds(tf), array.new<float>(), array.new<float>(), array.new<float>(), array.new<float>(), array.new<int>(), 0)
method push(Feed f, float o, float h, float l, float c, int t) =>
f.o.unshift(o)
f.h.unshift(h)
f.l.unshift(l)
f.c.unshift(c)
f.t.unshift(t)
if f.o.size() > 24
f.o.pop()
f.h.pop()
f.l.pop()
f.c.pop()
f.t.pop()
f.n := f.n + 1
f_rel(int t) => not na(t) and (na(t[1]) or t != t[1])
f_hhmm(int t) => hour(t, TZ) * 100 + minute(t, TZ)
f_inWin(int t) =>
int m = f_hhmm(t)
m > iWS and m <= iWE
f_px(float x) => str.tostring(x, format.mintick)
// keep a bounded number of finished drawings
var array<box> keepB = array.new<box>()
var array<line> keepL = array.new<line>()
var array<label> keepT = array.new<label>()
f_keepB(box b) =>
if not na(b)
keepB.push(b)
if keepB.size() > 220
box.delete(keepB.shift())
f_keepL(line l) =>
if not na(l)
keepL.push(l)
if keepL.size() > 220
line.delete(keepL.shift())
f_keepT(label t) =>
if not na(t)
keepT.push(t)
if keepT.size() > 260
label.delete(keepT.shift())
// a line with its name written in the middle (the label hides the line under the text)
f_mline(int x1, float y, int x2, string txt, string lstyle) =>
line ln = line.new(x1, y, x2, y, xloc.bar_time, color = cLine, width = 1, style = lstyle)
label lb = label.new(int((x1 + x2) / 2), y, txt, xloc.bar_time, color = LBG, textcolor = cLine, style = label.style_label_center, size = size.small)
[ln, lb]
f_mend(line ln, label lb, int x2) =>
if not na(ln)
line.set_x2(ln, x2)
if not na(lb)
label.set_x(lb, int((line.get_x1(ln) + x2) / 2))
// ───────────────────────────── Step 1: bias (computed on each HTF's own history) ─────────────────────────────
f_biasCore(float minSz) =>
var array<int> gD = array.new<int>()
var array<float> gT = array.new<float>()
var array<float> gB = array.new<float>()
var array<int> gN = array.new<int>()
var int st = 0
bool bu = false
bool be = false
int n = gD.size()
if n > 0
for i = n - 1 to 0
int d = gD.get(i)
bool rm = false
if d == 1
if close < gB.get(i)
be := true
rm := true
else if low <= gT.get(i) and close > gT.get(i)
bu := true
else
if close > gT.get(i)
bu := true
rm := true
else if high >= gB.get(i) and close < gB.get(i)
be := true
if not rm and bar_index - gN.get(i) > 300
rm := true
if rm
gD.remove(i)
gT.remove(i)
gB.remove(i)
gN.remove(i)
if bu and be
st := close > open ? 1 : close < open ? -1 : st
else if bu
st := 1
else if be
st := -1
if bar_index >= 2
if low > high[2] and low - high[2] >= minSz
gD.push(1)
gT.push(low)
gB.push(high[2])
gN.push(bar_index)
if high < low[2] and low[2] - high >= minSz
gD.push(-1)
gT.push(low[2])
gB.push(high)
gN.push(bar_index)
while gD.size() > 60
gD.shift()
gT.shift()
gB.shift()
gN.shift()
st
f_biasPrev(float minSz) =>
int s = f_biasCore(minSz)
s[1]
bVD = f_valid("D", iBD)
bV4 = f_valid("240", iB4)
bV1 = f_valid("60", iB1)
bV15 = f_valid("15", iB15)
sD = request.security(syminfo.tickerid, f_req("D", iBD), f_biasPrev(MIN_SZ), lookahead = barmerge.lookahead_on)
s4 = request.security(syminfo.tickerid, f_req("240", iB4), f_biasPrev(MIN_SZ), lookahead = barmerge.lookahead_on)
s1 = request.security(syminfo.tickerid, f_req("60", iB1), f_biasPrev(MIN_SZ), lookahead = barmerge.lookahead_on)
s15 = request.security(syminfo.tickerid, f_req("15", iB15), f_biasPrev(MIN_SZ), lookahead = barmerge.lookahead_on)
// ───────────────────────────── HTF closed-bar feeds ─────────────────────────────
f_sec(string tf) => request.security(syminfo.tickerid, tf, [open[1], high[1], low[1], close[1], time[1]], lookahead = barmerge.lookahead_on)
[dO_, dH_, dL_, dC_, dT_] = f_sec(f_req(iDolTf, iDol))
[k1o, k1h, k1l, k1c, k1t] = f_sec(f_req(iK1tf, iK1))
[k2o, k2h, k2l, k2c, k2t] = f_sec(f_req(iK2tf, iK2))
[k3o, k3h, k3l, k3c, k3t] = f_sec(f_req(iK3tf, iK3))
[tgo, tgh, tgl, tgc, tgt] = f_sec(f_req(iTgtTf, EXEC_OK))
[a5o, a5h, a5l, a5c, a5t] = f_sec(f_req("5", iL5 and EXEC_OK))
[a4o, a4h, a4l, a4c, a4t] = f_sec(f_req("4", iL4 and EXEC_OK))
[a3o, a3h, a3l, a3c, a3t] = f_sec(f_req("3", iL3 and EXEC_OK))
[a2o, a2h, a2l, a2c, a2t] = f_sec(f_req("2", iL2 and EXEC_OK))
[a1o, a1h, a1l, a1c, a1t] = f_sec(f_req("1", iL1 and EXEC_OK))
[a0o, a0h, a0l, a0c, a0t] = f_sec(f_req("30S", iL30 and EXEC_OK))
rDol = f_rel(dT_)
rK1 = f_rel(k1t)
rK2 = f_rel(k2t)
rK3 = f_rel(k3t)
rTg = f_rel(tgt)
rA5 = f_rel(a5t)
rA4 = f_rel(a4t)
rA3 = f_rel(a3t)
rA2 = f_rel(a2t)
rA1 = f_rel(a1t)
rA0 = f_rel(a0t)
dAtr = request.security(syminfo.tickerid, "D", ta.atr(14)[1], lookahead = barmerge.lookahead_on)
// SMT comparison symbol
string autoCmp = syminfo.root == "NQ" ? "ES1!" : syminfo.root == "MNQ" ? "MES1!" : syminfo.root == "ES" ? "NQ1!" : syminfo.root == "MES" ? "MNQ1!" : "ES1!"
string cmpSym = not iSmt ? syminfo.tickerid : iSmtSym != "" ? iSmtSym : syminfo.type == "futures" ? syminfo.prefix + ":" + autoCmp : syminfo.tickerid
float cmpLow = request.security(cmpSym, timeframe.period, low, ignore_invalid_symbol = true)
float cmpHigh = request.security(cmpSym, timeframe.period, high, ignore_invalid_symbol = true)
// ───────────────────────────── State ─────────────────────────────
var St st = St.new()
var array<Tr> hist = array.new<Tr>()
var Feed dolF = f_feed(iDolTf, iDol)
var Feed tgtF = f_feed(iTgtTf, EXEC_OK)
var array<Piv> dolP = array.new<Piv>()
var array<Piv> tgtP = array.new<Piv>()
var KL kl1 = KL.new(f_feed(iK1tf, iK1), 1, array.new<Gap>(), array.new<Lvl>())
var KL kl2 = KL.new(f_feed(iK2tf, iK2), 2, array.new<Gap>(), array.new<Lvl>())
var KL kl3 = KL.new(f_feed(iK3tf, iK3), 3, array.new<Gap>(), array.new<Lvl>())
var array<KL> kls = array.from(kl1, kl2, kl3)
var LT lt5 = LT.new(f_feed("5", iL5 and EXEC_OK), array.new<Gap>())
var LT lt4 = LT.new(f_feed("4", iL4 and EXEC_OK), array.new<Gap>())
var LT lt3 = LT.new(f_feed("3", iL3 and EXEC_OK), array.new<Gap>())
var LT lt2 = LT.new(f_feed("2", iL2 and EXEC_OK), array.new<Gap>())
var LT lt1 = LT.new(f_feed("1", iL1 and EXEC_OK), array.new<Gap>())
var LT lt0 = LT.new(f_feed("30S", iL30 and EXEC_OK), array.new<Gap>())
var array<LT> lts = array.from(lt5, lt4, lt3, lt2, lt1, lt0)
// ───────────────────────────── Day reset ─────────────────────────────
int dk = year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
if na(st.dKey) or dk != st.dKey
st.dKey := dk
st.dTrades := 0
st.dLoss := 0
st.dWin := false
st.dHit := na
st.dEnt := na
st.dEp := na
st.dSl := na
st.dTp := na
st.dEx := na
st.dXp := na
// ───────────────────────────── 1) Overall bias ─────────────────────────────
int nB = 0
int pB = 0
int qB = 0
if bVD
nB += 1
pB += nz(sD) == 1 ? 1 : 0
qB += nz(sD) == -1 ? 1 : 0
if bV4
nB += 1
pB += nz(s4) == 1 ? 1 : 0
qB += nz(s4) == -1 ? 1 : 0
if bV1
nB += 1
pB += nz(s1) == 1 ? 1 : 0
qB += nz(s1) == -1 ? 1 : 0
if bV15
nB += 1
pB += nz(s15) == 1 ? 1 : 0
qB += nz(s15) == -1 ? 1 : 0
bool allRule = str.startswith(iBMode, "All")
st.bias := nB == 0 ? 0 : allRule ? (pB == nB ? 1 : qB == nB ? -1 : 0) : (pB > qB and pB * 2 >= nB ? 1 : qB > pB and qB * 2 >= nB ? -1 : 0)
// ───────────────────────────── Pivots on a feed ─────────────────────────────
f_feedPivots(Feed f, int s, array<Piv> P) =>
if f.n >= 2 * s + 1
float ph = f.h.get(s)
float pl = f.l.get(s)
bool isH = true
bool isL = true
for j = 0 to 2 * s
if j != s
if f.h.get(j) >= ph
isH := false
if f.l.get(j) <= pl
isL := false
if isH
P.push(Piv.new(1, ph, f.t.get(s)))
if isL
P.push(Piv.new(-1, pl, f.t.get(s)))
while P.size() > 80
Piv old = P.shift()
line.delete(old.ln)
label.delete(old.lb)
if dolF.ok and rDol
dolF.push(dO_, dH_, dL_, dC_, dT_)
f_feedPivots(dolF, iDolStr, dolP)
// ───────────────────────────── 2) Key levels ─────────────────────────────
f_touch(KL k, int dir, float h, float l) =>
bool r = false
if k.gaps.size() > 0
for g in k.gaps
if g.alive and g.dir == dir and l <= g.top and h >= g.bot
r := true
break
r
f_tipIn(KL k, int dir, float tip) =>
bool r = false
if k.gaps.size() > 0
for g in k.gaps
if g.alive and g.dir == dir and tip <= g.top and tip >= g.bot
r := true
break
r
f_show(int dir) => not iOnly or dir == st.bias
f_endGap(Gap g) =>
box.delete(g.bx)
line.delete(g.il)
label.delete(g.ilb)
g.bx := na
g.il := na
g.ilb := na
g.itl := na
f_endLvl(Lvl v) =>
box.delete(v.bx)
line.delete(v.ce)
line.delete(v.ln)
label.delete(v.lb)
v.bx := na
v.ce := na
v.ln := na
v.lb := na
f_newLvl(KL k, int dir, int kind, float lvl, float ext, int t) =>
Lvl v = Lvl.new(dir, kind, lvl, ext, t, k.f.n)
k.lv.push(v)
while k.lv.size() > 40
Lvl ov = k.lv.shift()
box.delete(ov.bx)
line.delete(ov.ce)
line.delete(ov.ln)
label.delete(ov.lb)
f_klRelease(KL k, float o, float h, float l, float c, int t) =>
Feed f = k.f
f.push(o, h, l, c, t)
// (a) invalidations on the closed bar
if k.gaps.size() > 0
for g in k.gaps
if g.alive
bool dead = (g.dir == 1 ? c < g.bot : c > g.top) or f.n - g.born > 300
if dead
g.alive := false
f_endGap(g)
if k.lv.size() > 0
for v in k.lv
if v.alive
bool dead = (v.dir == 1 ? c < v.ext : c > v.ext) or f.n - v.born > 300
if dead
v.alive := false
f_endLvl(v)
// (b) CISD: body close through the open of the last down/up-close series that traded into a gap
if iKC
if c > o and k.dA and k.dTouch and c > k.dO
f_newLvl(k, 1, 3, k.dO, k.dL, k.dT)
k.dA := false
if c < o and k.uA and k.uTouch and c < k.uO
f_newLvl(k, -1, 3, k.uO, k.uH, k.uT)
k.uA := false
if c < o
if k.lastDir == -1 and k.dA
k.dL := math.min(k.dL, l)
k.dTouch := k.dTouch or f_touch(k, 1, h, l)
else
k.dA := true
k.dO := o
k.dL := l
k.dT := t
k.dTouch := f_touch(k, 1, h, l)
if c > o
if k.lastDir == 1 and k.uA
k.uH := math.max(k.uH, h)
k.uTouch := k.uTouch or f_touch(k, -1, h, l)
else
k.uA := true
k.uO := o
k.uH := h
k.uT := t
k.uTouch := f_touch(k, -1, h, l)
k.lastDir := c > o ? 1 : c < o ? -1 : 0
// (c) rejection block: a long wick whose tip traded into a gap
if iKR and h > l
float bodyLo = math.min(o, c)
float bodyHi = math.max(o, c)
if bodyLo - l >= iRbW * (h - l) and f_tipIn(k, 1, l)
f_newLvl(k, 1, 4, bodyLo, l, t)
if h - bodyHi >= iRbW * (h - l) and f_tipIn(k, -1, h)
f_newLvl(k, -1, 4, bodyHi, h, t)
// (d) new fair value gap
if f.n >= 3
float h2 = f.h.get(2)
float l2 = f.l.get(2)
for d = 0 to 1
int dir = d == 0 ? 1 : -1
bool isG = dir == 1 ? (l > h2 and l - h2 >= MIN_SZ) : (h < l2 and l2 - h >= MIN_SZ)
if isG
k.gaps.push(Gap.new(dir, dir == 1 ? l : l2, dir == 1 ? h2 : h, f.t.get(2), f.t.get(1), t, f.n))
while k.gaps.size() > 40
Gap og = k.gaps.shift()
box.delete(og.bx)
line.delete(og.il)
label.delete(og.ilb)
// (e) intermediate low / high inside a live gap (swing strength 2)
if iKI and f.n >= 5
float pl = f.l.get(2)
float ph = f.h.get(2)
int pt = f.t.get(2)
bool isL = pl < f.l.get(0) and pl < f.l.get(1) and pl < f.l.get(3) and pl < f.l.get(4)
bool isH = ph > f.h.get(0) and ph > f.h.get(1) and ph > f.h.get(3) and ph > f.h.get(4)
int n = k.gaps.size()
if n > 0 and (isL or isH)
bool doneL = not isL
bool doneH = not isH
for i = n - 1 to 0
Gap g = k.gaps.get(i)
if g.alive and g.t3 < pt
if not doneL and g.dir == 1 and pl >= g.bot and pl <= g.top
line.delete(g.il)
label.delete(g.ilb)
g.itl := pl
g.itlT := pt
g.il := na
g.ilb := na
doneL := true
if not doneH and g.dir == -1 and ph >= g.bot and ph <= g.top
line.delete(g.il)
label.delete(g.ilb)
g.itl := ph
g.itlT := pt
g.il := na
g.ilb := na
doneH := true
if doneL and doneH
break
if kl1.f.ok and rK1
f_klRelease(kl1, k1o, k1h, k1l, k1c, k1t)
if kl2.f.ok and rK2
f_klRelease(kl2, k2o, k2h, k2l, k2c, k2t)
if kl3.f.ok and rK3
f_klRelease(kl3, k3o, k3h, k3l, k3c, k3t)
if tgtF.ok and rTg
tgtF.push(tgo, tgh, tgl, tgc, tgt)
f_feedPivots(tgtF, iTgtStr, tgtP)
// ───────────────────────────── 3) IFVG confirmation & 4) entry ─────────────────────────────
f_inLeg(Gap g) => g.t3 >= st.sAncT and (st.sDir == 1 ? g.top <= st.sAnc : g.bot >= st.sAnc)
f_noHigher(int sec) =>
bool r = true
for x in lts
if x.f.ok and x.f.sec > sec and x.gaps.size() > 0
for g in x.gaps
if not g.inv and g.dir == -st.sDir and f_inLeg(g)
r := false
break
if not r
break
r
// nearest liquidity in the trade direction (swings, and unfilled gaps), kept between min and max R
f_target(int dir, float px, float risk) =>
float tg = na
if tgtP.size() > 0
for p in tgtP
if not p.swept and p.dir == dir and dir * (p.p - px) > 0
if na(tg) or dir * (p.p - tg) < 0
tg := p.p
if iTgtGap
for k in kls
if k.f.ok and k.gaps.size() > 0
for g in k.gaps
if g.alive and g.fresh
float e = dir == 1 ? g.bot : g.top
if dir * (e - px) > 0 and (na(tg) or dir * (e - tg) < 0)
tg := e
float rr = na(tg) ? na : dir * (tg - px) / risk
if na(rr) or rr < iMinRR
tg := px + dir * iMinRR * risk
else if rr > iMaxRR
tg := px + dir * iMaxRR * risk
tg
f_dayOk() => st.dTrades < iMaxT and st.dLoss < iMaxL and not (iStopWin and st.dWin)
f_open(int dir, float px, float stop, float beLvl, int beT, Gap g, string tag, int sec) =>
float risk = dir * (px - stop)
float tg = f_target(dir, px, risk)
st.tDir := dir
st.tE := px
st.tS := stop
st.tT := tg
st.tBE := beLvl
st.tRisk := risk
st.tEt := time
st.tDay := year(time, TZ) * 10000 + month(time, TZ) * 100 + dayofmonth(time, TZ)
st.tBeDone := false
st.dTrades := st.dTrades + 1
st.nT := st.nT + 1
st.dEnt := dir * sec / 60.0
st.dEp := px
st.dSl := stop
st.dTp := tg
st.tRR := math.abs(tg - px) / risk
hist.push(Tr.new(time, dir, tag, px, stop, tg, st.tRR))
if hist.size() > 60
hist.shift()
st.lastTrade := (dir == 1 ? "Long " : "Short ") + tag + " IFVG @ " + f_px(px) + " · R:R " + str.tostring(st.tRR, "0.00")
// the trade box (same shape as the SK System box): TP / SL boxes, entry line, 1R and 2R steps, prices on the right edge.
// It moves ahead of price on every bar until the target, the stop or the end of day.
int x2 = time + iPosBars * CH_SEC * 1000
if iPos
float sp = math.abs(px - stop)
st.bI := box.new(g.tm, g.top, x2, g.bot, xloc = xloc.bar_time, bgcolor = cIfvg, border_color = color(na), text = tag + " IFVG", text_color = C_BOXTXT, text_size = size.small)
st.bT := box.new(time, math.max(px, tg), x2, math.min(px, tg), xloc = xloc.bar_time, border_color = cPosTp, bgcolor = color.new(cPosTp, 78))
st.bS := box.new(time, math.max(px, stop), x2, math.min(px, stop), xloc = xloc.bar_time, border_color = cPosSl, bgcolor = color.new(cPosSl, 78))
st.lE := line.new(time, px, x2, px, xloc.bar_time, color = cPosEn, width = 2)
st.lM := label.new(x2, px, (dir == 1 ? "LONG " : "SHORT ") + f_px(px) + " · R:R " + str.tostring(st.tRR, "0.00"), xloc.bar_time, style = label.style_label_left, color = cPosEn, textcolor = color.white, size = size.small)
st.lT := label.new(x2, tg, "TP: " + f_px(tg) + " (" + str.tostring(st.tRR, "0.00") + "R)", xloc.bar_time, style = label.style_label_left, color = cPosTp, textcolor = color.white, size = size.small)
st.lS := label.new(x2, stop, "SL: " + f_px(stop) + " (" + f_px(sp) + ")", xloc.bar_time, style = label.style_label_left, color = cPosSl, textcolor = color.white, size = size.small)
st.l1 := na
st.l2 := na
st.lb1 := na
st.lb2 := na
float r1 = px + dir * risk
float r2 = px + dir * 2 * risk
if dir * (tg - r1) > syminfo.mintick / 2
st.l1 := line.new(time, r1, x2, r1, xloc.bar_time, color = cPosTp, style = line.style_dashed)
st.lb1 := label.new(x2, r1, "1R: " + f_px(r1), xloc.bar_time, style = label.style_label_left, color = color.new(cPosTp, 30), textcolor = color.white, size = size.tiny)
if dir * (tg - r2) > syminfo.mintick / 2
st.l2 := line.new(time, r2, x2, r2, xloc.bar_time, color = cPosTp, style = line.style_dashed)
st.lb2 := label.new(x2, r2, "2R: " + f_px(r2), xloc.bar_time, style = label.style_label_left, color = color.new(cPosTp, 30), textcolor = color.white, size = size.tiny)
st.lBE := na
st.lBEl := na
if iBE
[ln, lb] = f_mline(beT, beLvl, x2, "BE", line.style_solid)
st.lBE := ln
st.lBEl := lb
f_keepL(ln)
f_keepT(lb)
f_keepB(st.bI)
f_keepB(st.bT)
f_keepB(st.bS)
f_keepL(st.lE)
f_keepL(st.l1)
f_keepL(st.l2)
f_keepT(st.lT)
f_keepT(st.lS)
f_keepT(st.lM)
f_keepT(st.lb1)
f_keepT(st.lb2)
alert((dir == 1 ? "LONG " : "SHORT ") + syminfo.ticker + " · " + tag + " IFVG · entry " + f_px(px) + " · stop " + f_px(stop) + " · target " + f_px(tg), alert.freq_once_per_bar)
// the highest-timeframe inversion of the leg: enter at its close, or rest a limit at the gap when the stop is too big.
// Either way the leg's confirmation is spent, so the setup ends here.
f_enter(int dir, float px, Gap g, string tag, int sec) =>
float stop = st.sExt
float risk = dir * (px - stop)
if risk > 0 and f_dayOk() and st.tDir == 0 and st.pDir == 0 and f_inWin(time)
if iMaxRisk == 0 or risk <= iMaxRisk
f_open(dir, px, stop, st.sAnc, st.sAncT, g, tag, sec)
else if str.startswith(iBig, "Limit")
float lp = dir == 1 ? g.top : g.bot
float lr = dir * (lp - stop)
if lr > 0 and lr <= iMaxRisk
st.pDir := dir
st.pPx := lp
st.pStop := stop
st.pBE := st.sAnc
st.pBeT := st.sAncT
st.pBi := bar_index
st.pSec := sec
st.pTag := tag
st.pG := g
st.sDir := 0
f_ltRelease(LT x, float o, float h, float l, float c, int t) =>
Feed f = x.f
f.push(o, h, l, c, t)
if x.gaps.size() > 0
for g in x.gaps
if not g.inv
bool inv = g.dir == -1 ? c > g.top : c < g.bot
if inv
g.inv := true
if st.sDir == -g.dir and f_inLeg(g) and f_noHigher(f.sec)
f_enter(st.sDir, c, g, f.tag, f.sec)
if f.n >= 3
float h2 = f.h.get(2)
float l2 = f.l.get(2)
if l > h2 and l - h2 >= MIN_SZ
x.gaps.push(Gap.new(1, l, h2, f.t.get(2), f.t.get(1), t, f.n))
if h < l2 and l2 - h >= MIN_SZ
x.gaps.push(Gap.new(-1, l2, h, f.t.get(2), f.t.get(1), t, f.n))
while x.gaps.size() > 40
x.gaps.shift()
if lt5.f.ok and rA5
f_ltRelease(lt5, a5o, a5h, a5l, a5c, a5t)
if lt4.f.ok and rA4
f_ltRelease(lt4, a4o, a4h, a4l, a4c, a4t)
if lt3.f.ok and rA3
f_ltRelease(lt3, a3o, a3h, a3l, a3c, a3t)
if lt2.f.ok and rA2
f_ltRelease(lt2, a2o, a2h, a2l, a2c, a2t)
if lt1.f.ok and rA1
f_ltRelease(lt1, a1o, a1h, a1l, a1c, a1t)
if lt0.f.ok and rA0
f_ltRelease(lt0, a0o, a0h, a0l, a0c, a0t)
// ───────────────────────────── Trade management ─────────────────────────────
f_posDraw(int x2) =>
box.set_right(st.bT, x2)
box.set_right(st.bS, x2)
box.set_right(st.bI, x2)
line.set_x2(st.lE, x2)
line.set_x2(st.l1, x2)
line.set_x2(st.l2, x2)
label.set_x(st.lM, x2)
label.set_x(st.lT, x2)
label.set_x(st.lS, x2)
label.set_x(st.lb1, x2)
label.set_x(st.lb2, x2)
f_mend(st.lBE, st.lBEl, x2)
// code: 1 target, -1 stop, 0 breakeven, 2 end-of-day exit (counted by its sign)
f_closeTrade(float xp, int code) =>
float r = st.tDir * (xp - st.tE) / st.tRisk
st.dEx := code == 0 ? 0.5 : float(code)
st.dXp := xp
if code == 1 or (code == 2 and r > 0)
st.nW := st.nW + 1
st.dWin := true
else if code == -1 or (code == 2 and r < 0)
st.nL := st.nL + 1
st.dLoss := st.dLoss + 1
else
st.nBE := st.nBE + 1
st.sumR := st.sumR + r
if hist.size() > 0
Tr last = hist.last()
last.code := code
last.r := r
if iPos
f_posDraw(math.max(time, st.tEt + CH_SEC * 1000))
label.set_text(st.lM, (st.tDir == 1 ? "LONG " : "SHORT ") + (code == 0 ? "BE" : str.tostring(r, "+0.00;-0.00") + "R") + (code == 2 ? " (time)" : "") + " · R:R " + str.tostring(st.tRR, "0.00"))
label.set_color(st.lM, code == 1 or (code == 2 and r > 0) ? cPosTp : code == -1 or (code == 2 and r < 0) ? cPosSl : cPosEn)
alert("EXIT " + syminfo.ticker + " · " + (code == 1 ? "target" : code == -1 ? "stop" : code == 2 ? "end of day" : "breakeven") + " @ " + f_px(xp), alert.freq_once_per_bar)
st.tDir := 0
// a resting limit order: filled when price trades back to it inside the window, cancelled otherwise
if st.pDir != 0
int pd = st.pDir
if not f_inWin(time) or bar_index - st.pBi > iExp or not f_dayOk() or (pd == 1 ? low <= st.pStop : high >= st.pStop) and not (pd == 1 ? low <= st.pPx : high >= st.pPx)
st.pDir := 0
else if pd == 1 ? low <= st.pPx : high >= st.pPx
st.pDir := 0
f_open(pd, st.pPx, st.pStop, st.pBE, st.pBeT, st.pG, st.pTag, st.pSec)
// intraday only: an open trade is closed at the end-of-day time, or on a new day
if st.tDir != 0 and (f_hhmm(time) >= iEod or dk != st.tDay)
f_closeTrade(open, 2)
if st.tDir != 0
int td = st.tDir
bool hitS = td == 1 ? low <= st.tS : high >= st.tS
bool hitT = td == 1 ? high >= st.tT : low <= st.tT
if hitS
f_closeTrade(st.tS, st.tS == st.tE ? 0 : -1)
else if hitT
f_closeTrade(st.tT, 1)
else if iBE and not st.tBeDone and (td == 1 ? high >= st.tBE : low <= st.tBE)
st.tS := st.tE
st.tBeDone := true
if iPos
label.set_text(st.lS, "SL: " + f_px(st.tE - st.tDir * st.tRisk) + " → BE " + f_px(st.tE))
// the open trade box keeps moving ahead of price
if st.tDir != 0 and iPos
f_posDraw(time + iPosBars * CH_SEC * 1000)
// ───────────────────────────── Chart swings (manipulation leg & SMT) ─────────────────────────────
var float phP = na
var int phBi = na
var float plP = na
var int plBi = na
int L = iLegStr
bool newPH = false
bool newPL = false
if bar_index >= 2 * L
bool isH = true
bool isL = true
for j = 0 to 2 * L
if j != L
if high[j] >= high[L]
isH := false
if low[j] <= low[L]
isL := false
if isH
phP := high[L]
phBi := bar_index - L
newPH := true
if isL
plP := low[L]
plBi := bar_index - L
newPL := true
// ───────────────────────────── Key-level hits on the chart bar ─────────────────────────────
f_onHit(int dir, int kind, KL k, Gap g, Lvl v) =>
string nm = kind == 1 ? "FVG" : kind == 2 ? (dir == 1 ? "ITL" : "ITH") : kind == 3 ? "CISD" : "RB"
if na(st.dHit)
st.dHit := dir * (kind * 10 + k.idx)
st.lastHit := (dir == 1 ? "▲ " : "▼ ") + k.f.tag + " " + nm + " @ " + str.format_time(time, "HH:mm", TZ)
alert("KEY LEVEL " + syminfo.ticker + " · " + (dir == 1 ? "bullish " : "bearish ") + k.f.tag + " " + nm, alert.freq_once_per_bar)
if st.sDir == -dir
st.sDir := 0
if st.sDir == 0 and EXEC_OK
int pb = dir == 1 ? phBi : plBi
if not na(pb) and bar_index - pb <= 1500
float a = dir == 1 ? high : low
for j = 0 to bar_index - pb
a := dir == 1 ? math.max(a, high[j]) : math.min(a, low[j])
st.sDir := dir
st.sAnc := a
st.sAncT := time[bar_index - pb]
st.sExt := dir == 1 ? low : high
st.sExtBi := bar_index
st.sInfo := k.f.tag + " " + nm
st.sG := g
st.sV := v
f_hits(KL k) =>
if k.f.ok
if k.gaps.size() > 0
for g in k.gaps
if g.alive
if g.fresh and (g.dir == 1 ? low <= g.top : high >= g.bot)
g.fresh := false
if iKF and st.bias == g.dir
f_onHit(g.dir, 1, k, g, na)
if not na(g.itl) and (g.dir == 1 ? low < g.itl : high > g.itl)
line.delete(g.il)
label.delete(g.ilb)
g.il := na
g.ilb := na
g.itl := na
if iKI and st.bias == g.dir
f_onHit(g.dir, 2, k, g, na)
if k.lv.size() > 0
for v in k.lv
if v.alive and not v.used and (v.dir == 1 ? low <= v.lvl : high >= v.lvl)
v.used := true
if st.bias == v.dir
f_onHit(v.dir, v.kind, k, na, v)
f_hits(kl1)
f_hits(kl2)
f_hits(kl3)
// ───────────────────────────── Setup maintenance ─────────────────────────────
// the setup lives only while its key level is valid (a close through the level kills it)
bool klDead = (not na(st.sG) and not st.sG.alive) or (not na(st.sV) and not st.sV.alive)
if st.sDir != 0 and klDead
st.sDir := 0
if st.sDir == 1
if low < st.sExt
st.sExt := low
st.sExtBi := bar_index
if bar_index - st.sExtBi > iExp or st.bias != 1 or close > st.sAnc
st.sDir := 0
else if st.sDir == -1
if high > st.sExt
st.sExt := high
st.sExtBi := bar_index
if bar_index - st.sExtBi > iExp or st.bias != -1 or close < st.sAnc
st.sDir := 0
// ───────────────────────────── Liquidity sweeps & draw on liquidity ─────────────────────────────
f_sweep(array<Piv> P) =>
if P.size() > 0
for p in P
if not p.swept and (p.dir == 1 ? high > p.p : low < p.p)
p.swept := true
f_mend(p.ln, p.lb, time)
f_sweep(dolP)
f_sweep(tgtP)
var Piv curDol = na
float dolPx = na
if iDol and st.bias != 0 and dolP.size() > 0
Piv best = na
for p in dolP
if not p.swept and p.dir == st.bias and st.bias * (p.p - close) > 0
if na(best) or st.bias * (p.p - best.p) < 0
best := p
if not na(best)
dolPx := best.p
if not na(curDol) and (curDol.t != best.t or curDol.p != best.p) and not curDol.swept
line.delete(curDol.ln)
label.delete(curDol.lb)
curDol.ln := na
curDol.lb := na
curDol.wasDol := false
curDol := best
if not best.wasDol
best.wasDol := true
[ln, lb] = f_mline(best.t, best.p, time + EXT, best.dir == 1 ? "BSL" : "SSL", line.style_solid)
best.ln := ln
best.lb := lb
// ───────────────────────────── SMT (display) ─────────────────────────────
var float smtPL = na
var float smtCL = na
var int smtTL = na
var float smtPH = na
var float smtCH = na
var int smtTH = na
float cmpLo = ta.lowest(cmpLow, 2 * L + 1)
float cmpHi = ta.highest(cmpHigh, 2 * L + 1)
if iSmt and cmpSym != syminfo.tickerid
if newPL and not na(cmpLo)
if not na(smtPL) and low[L] > smtPL and cmpLo < smtCL and (not EXEC_OK or st.sDir == 1)
[ln, lb] = f_mline(smtTL, smtPL, time[L], "SMT", line.style_dashed)
f_keepL(ln)
f_keepT(lb)
line.set_y2(ln, low[L])
label.set_y(lb, (smtPL + low[L]) / 2)
smtPL := low[L]
smtCL := cmpLo
smtTL := time[L]
if newPH and not na(cmpHi)
if not na(smtPH) and high[L] < smtPH and cmpHi > smtCH and (not EXEC_OK or st.sDir == -1)
[ln, lb] = f_mline(smtTH, smtPH, time[L], "SMT", line.style_dashed)
f_keepL(ln)
f_keepT(lb)
line.set_y2(ln, high[L])
label.set_y(lb, (smtPH + high[L]) / 2)
smtPH := high[L]
smtCH := cmpHi
smtTH := time[L]
// ───────────────────────────── Extend live drawings on the last bar ─────────────────────────────
f_gDist(Gap g) => math.abs((g.top + g.bot) / 2 - close)
f_vDist(Lvl v) => math.abs(v.lvl - close)
// live levels are drawn on the last bar only: bias direction, nearest to price, within the daily ATR band
f_drawKL(KL k, int x2, float lim) =>
if k.gaps.size() > 0
for g in k.gaps
if g.alive
int closer = 0
for q in k.gaps
if q.alive and q.dir == g.dir and f_gDist(q) < f_gDist(g)
closer += 1
bool ok = iKF and f_show(g.dir) and closer < iMaxD and f_gDist(g) <= lim
if ok
if na(g.bx)
g.bx := box.new(g.tm, g.top, x2, g.bot, xloc = xloc.bar_time, bgcolor = g.dir == 1 ? cFvgB : cFvgS, border_color = color(na),
text = k.f.tag + " FVG", text_color = C_BOXTXT, text_size = size.small)
else
box.set_right(g.bx, x2)
else
box.delete(g.bx)
g.bx := na
int closerI = 0
if not na(g.itl)
for q in k.gaps
if q.alive and q.dir == g.dir and not na(q.itl) and math.abs(q.itl - close) < math.abs(g.itl - close)
closerI += 1
bool okI = iKI and not na(g.itl) and f_show(g.dir) and math.abs(g.itl - close) <= lim and closerI < iMaxD
if okI
if na(g.il)
[ln, lb] = f_mline(g.itlT, g.itl, x2, k.f.tag + (g.dir == 1 ? " ITL" : " ITH"), line.style_solid)
g.il := ln
g.ilb := lb
else
f_mend(g.il, g.ilb, x2)
else
line.delete(g.il)
label.delete(g.ilb)
g.il := na
g.ilb := na
if k.lv.size() > 0
for v in k.lv
if v.alive
int closer = 0
for q in k.lv
if q.alive and q.dir == v.dir and q.kind == v.kind and f_vDist(q) < f_vDist(v)
closer += 1
bool ok = f_show(v.dir) and closer < iMaxD and f_vDist(v) <= lim
if ok
if v.kind == 3
if na(v.ln)
[ln, lb] = f_mline(v.t, v.lvl, x2, k.f.tag + " CISD", line.style_solid)
v.ln := ln
v.lb := lb
else
f_mend(v.ln, v.lb, x2)
else
if na(v.bx)
v.bx := box.new(v.t, math.max(v.lvl, v.ext), x2, math.min(v.lvl, v.ext), xloc = xloc.bar_time, bgcolor = cIfvg, border_color = color(na),
text = (v.dir == 1 ? "+RB " : "-RB ") + k.f.tag, text_color = C_RBTXT, text_size = size.small, text_halign = text.align_right, text_valign = text.align_center)
float ce = (v.lvl + v.ext) / 2
v.ce := line.new(v.t, ce, x2, ce, xloc.bar_time, color = C_CE, style = line.style_dashed, width = 1)
else
box.set_right(v.bx, x2)
line.set_x2(v.ce, x2)
else
box.delete(v.bx)
line.delete(v.ce)
line.delete(v.ln)
label.delete(v.lb)
v.bx := na
v.ce := na
v.ln := na
v.lb := na
true
if barstate.islast
int x2 = time + EXT
float lim = na(dAtr) ? 1e10 : iDist * dAtr
f_drawKL(kl1, x2, lim)
f_drawKL(kl2, x2, lim)
f_drawKL(kl3, x2, lim)
if dolP.size() > 0
for p in dolP
if p.wasDol and not p.swept
f_mend(p.ln, p.lb, x2)
// ───────────────────────────── Panel ─────────────────────────────
f_t(string en, string ar) => iLang == "English" ? en : ar
f_bs(int s) => s == 1 ? f_t("Bullish ▲", "صاعد ▲") : s == -1 ? f_t("Bearish ▼", "هابط ▼") : f_t("Neutral", "محايد")
f_ar(int s) => nz(s) == 1 ? "▲" : nz(s) == -1 ? "▼" : "·"
f_pos() =>
iTblPos == "Top right · أعلى اليمين" ? position.top_right : iTblPos == "Bottom right · أسفل اليمين" ? position.bottom_right : iTblPos == "Top left · أعلى اليسار" ? position.top_left : position.bottom_left
var table tb = table.new(f_pos(), 2, 12, bgcolor = chart.bg_color, border_color = color.new(chart.fg_color, 70), border_width = 1, frame_color = cLine, frame_width = 1)
f_row(int r, string a, string b, color bc) =>
table.cell(tb, 0, r, a, text_color = cLine, text_size = size.small, text_halign = text.align_left, bgcolor = bc)
table.cell(tb, 1, r, b, text_color = cLine, text_size = size.small, text_halign = text.align_right, bgcolor = bc)
if iTbl and barstate.islast
table.cell(tb, 0, 0, "IFVG 4-Step", text_color = color.white, bgcolor = C_POS, text_size = size.small)
table.cell(tb, 1, 0, syminfo.ticker + " · " + f_tag(timeframe.period), text_color = color.white, bgcolor = C_POS, text_size = size.small)
string bt = (bVD ? "D " + f_ar(sD) + " " : "") + (bV4 ? "4H " + f_ar(s4) + " " : "") + (bV1 ? "1H " + f_ar(s1) + " " : "") + (bV15 ? "15m " + f_ar(s15) : "")
f_row(1, f_t("① Timeframes", "① الفريمات"), bt, na)
f_row(2, f_t("Bias", "التحيّز"), f_bs(st.bias), st.bias == 1 ? color.new(color.teal, 75) : st.bias == -1 ? color.new(color.red, 75) : na)
f_row(3, f_t("Draw on liquidity", "هدف السيولة"), na(dolPx) ? "—" : (st.bias == 1 ? "BSL " : "SSL ") + f_px(dolPx), na)
f_row(4, f_t("② Last key level", "② آخر مستوى"), st.lastHit == "" ? "—" : st.lastHit, na)
string sTxt = not EXEC_OK ? f_t("needs a ≤1m chart", "يحتاج شارت دقيقة أو أقل") : st.sDir == 0 ? "—" : f_t("waiting IFVG · ", "بانتظار IFVG · ") + st.sInfo
f_row(5, f_t("③ Setup", "③ الإعداد"), sTxt, na)
string tTxt = st.tDir == 0 ? (st.lastTrade == "" ? "—" : st.lastTrade) : (st.tDir == 1 ? f_t("Long open", "شراء مفتوح") : f_t("Short open", "بيع مفتوح")) + " · SL " + f_px(st.tS) + " · TP " + f_px(st.tT) + " · R:R " + str.tostring(st.tRR, "0.00")
f_row(6, f_t("④ Trade", "④ الصفقة"), tTxt, na)
bool done = st.dTrades >= iMaxT or st.dLoss >= iMaxL or (iStopWin and st.dWin)
f_row(7, f_t("Today", "اليوم"), str.tostring(st.dTrades) + f_t(" trades", " صفقات") + (done ? f_t(" · done", " · انتهى") : ""), na)
f_row(8, f_t("Window (NY)", "النافذة (نيويورك)"), str.format("{0,number,0000}–{1,number,0000}", iWS, iWE) + (f_inWin(time) ? f_t(" · open", " · مفتوحة") : f_t(" · closed", " · مغلقة")), na)
f_row(9, f_t("Results", "النتائج"), str.tostring(st.nW) + "W / " + str.tostring(st.nL) + "L / " + str.tostring(st.nBE) + "BE", na)
f_row(10, f_t("Win rate · Net", "نسبة الفوز · الصافي"), (st.nW + st.nL > 0 ? str.tostring(100.0 * st.nW / (st.nW + st.nL), "#.#") + "%" : "—") + " · " + str.tostring(st.sumR, "+#.##;-#.##") + "R", na)
// ───────────────────────────── Trade history ─────────────────────────────
f_posOf(string p) =>
p == "Top right · أعلى اليمين" ? position.top_right : p == "Bottom right · أسفل اليمين" ? position.bottom_right : p == "Top left · أعلى اليسار" ? position.top_left : position.bottom_left
var table th = table.new(f_posOf(iHistPos), 9, 62, bgcolor = chart.bg_color, border_color = color.new(chart.fg_color, 70), border_width = 1, frame_color = cLine, frame_width = 1)
f_hc(int c, int r, string txt, color tc, color bc) =>
table.cell(th, c, r, txt, text_color = tc, bgcolor = bc, text_size = size.tiny, text_halign = c == 0 ? text.align_left : text.align_right)
if iHist > 0 and barstate.islast
table.clear(th, 0, 0, 8, 61)
array<string> hd = array.from(f_t("Date (NY)", "التاريخ (نيويورك)"), f_t("Side", "الاتجاه"), "IFVG", f_t("Entry", "الدخول"), f_t("Stop", "الوقف"), f_t("Target", "الهدف"), "R:R", f_t("Result", "النتيجة"), "R")
for c = 0 to 8
f_hc(c, 0, hd.get(c), color.white, C_POS)
int n = math.min(iHist, hist.size())
float tot = 0.0
int w = 0
int lo = 0
if n > 0
for i = 0 to n - 1
Tr x = hist.get(hist.size() - 1 - i)
color rc = x.code == 9 ? cPosEn : x.r > 0 ? cPosTp : x.r < 0 ? cPosSl : cLine
string res = x.code == 9 ? f_t("open", "مفتوحة") : x.code == 1 ? f_t("target", "هدف") : x.code == -1 ? f_t("stop", "وقف") : x.code == 0 ? "BE" : f_t("time", "وقت")
f_hc(0, i + 1, str.format_time(x.t, "yyyy-MM-dd HH:mm", TZ), cLine, na)
f_hc(1, i + 1, x.dir == 1 ? f_t("Long", "شراء") : f_t("Short", "بيع"), x.dir == 1 ? cPosTp : cPosSl, na)
f_hc(2, i + 1, x.tag, cLine, na)
f_hc(3, i + 1, f_px(x.e), cLine, na)
f_hc(4, i + 1, f_px(x.s), cLine, na)
f_hc(5, i + 1, f_px(x.tp), cLine, na)
f_hc(6, i + 1, str.tostring(x.rr, "0.00"), cLine, na)
f_hc(7, i + 1, res, rc, na)
f_hc(8, i + 1, na(x.r) ? "—" : str.tostring(x.r, "+0.00;-0.00"), rc, na)
if not na(x.r)
tot += x.r
w += x.r > 0 ? 1 : 0
lo += x.r < 0 ? 1 : 0
f_hc(0, n + 1, f_t("Last ", "آخر ") + str.tostring(n), color.white, C_POS)
f_hc(1, n + 1, str.tostring(w) + "W / " + str.tostring(lo) + "L", color.white, C_POS)
for c = 2 to 6
f_hc(c, n + 1, "", color.white, C_POS)
f_hc(7, n + 1, w + lo > 0 ? str.tostring(100.0 * w / (w + lo), "#.#") + "%" : "—", color.white, C_POS)
f_hc(8, n + 1, str.tostring(tot, "+0.00;-0.00"), color.white, C_POS)
// ───────────────────────────── Data for verification (hidden) ─────────────────────────────
plot(bar_index, "bi", display = display.none)
plot(st.bias, "bias", display = display.none)
plot(sD, "bD", display = display.none)
plot(s4, "b4", display = display.none)
plot(s1, "b1", display = display.none)
plot(st.dHit, "hit", display = display.none)
plot(st.dEnt, "ent", display = display.none)
plot(st.dEp, "ep", display = display.none)
plot(st.dSl, "sl", display = display.none)
plot(st.dTp, "tp", display = display.none)
plot(st.dEx, "ex", display = display.none)
plot(st.dXp, "xp", display = display.none)
plot(st.sDir, "sdir", display = display.none)
plot(st.sDir != 0 ? st.sExt : na, "sext", display = display.none)
plot(st.sDir != 0 ? st.sAnc : na, "sanc", display = display.none)
plot(dolPx, "dol", display = display.none)