本页源码来自 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/
// © AhmedAminTC
// © Ahmed Amin, CFTe®, CETA®
// Exit Map & Rank Regime · v1.0
// -----------------------------------------------------------------------------
// Market-agnostic. Works on any symbol / universe the chart's data plan covers.
//
// 1) EXIT MAP zero-drift Monte Carlo (ATR-normalised moves, block
// bootstrap, High/Low touch, same-bar tie = stop, fixed seed)
// -> P05/P50/P95 cone, P(target first), P(stop first),
// expectancy in R ± standard error.
// 2) RANK REGIME ranks the chart symbol inside a universe by its relative
// return vs the universe's equal-weight basket, AND measures
// on this chart's own history whether that rank has reversed
// (reversal regime), persisted (momentum regime) or done
// nothing. The zone label follows the measured regime —
// it is never assumed.
// 3) SELF-TESTS band coverage (empirical proxy of the P05–P95 band) and
// rank IC (Spearman) with an overlap-adjusted t-stat.
// 4) DATA GATE bar moves above a plausible limit are treated as
// unadjusted corporate actions and excluded.
//
// Descriptive tool, not a buy/sell signal. Probabilities are model outputs
// from historical data and do not guarantee future results.
// -----------------------------------------------------------------------------
//@version=6
indicator("Exit Map & Rank Regime", "EMRR", overlay = true, max_lines_count = 150, max_labels_count = 20, dynamic_requests = true)
// ============================================================================
// INPUTS
// ============================================================================
grpMC = "1 · Exit map (Monte Carlo)"
hzn = input.int(10, "Horizon (bars)", minval = 2, maxval = 40, group = grpMC, tooltip = "Bars ahead to simulate.\n\nعدد الشموع المستقبلية التي تتم محاكاتها.")
sims = input.int(2000, "Simulations", minval = 200, maxval = 2500, step = 100, group = grpMC, tooltip = "Number of simulated paths. Horizon × Simulations must stay ≤ 100,000.\n\nعدد المسارات المُحاكاة. حاصل ضرب الأفق في عدد المسارات يجب ألا يتجاوز 100,000.")
lookbk = input.int(250, "Sample size (bars)", minval = 100, maxval = 1000, group = grpMC, tooltip = "Recent bars whose ATR-normalised moves feed the simulation.\n\nعدد الشموع الأخيرة التي تُستخدم حركاتها (مطبّعة بالـ ATR) في المحاكاة.")
blockL = input.int(5, "Bootstrap block length", minval = 1, maxval = 20, group = grpMC, tooltip = "Consecutive bars drawn together to preserve volatility clustering.\n\nعدد الشموع المتتالية التي تُسحب معاً للحفاظ على تجمّع التذبذب.")
atrLen = input.int(14, "ATR length", minval = 2, group = grpMC)
stopM = input.float(1.5, "Stop (× ATR)", minval = 0.25, step = 0.25, group = grpMC, tooltip = "Stop distance below the last close, in ATR.\n\nمسافة الوقف أسفل آخر إغلاق، بمضاعفات الـ ATR.")
tgtM = input.float(3.0, "Target (× ATR)", minval = 0.25, step = 0.25, group = grpMC, tooltip = "Target distance above the last close, in ATR.\n\nمسافة الهدف أعلى آخر إغلاق، بمضاعفات الـ ATR.")
showDrift = input.bool(true, "Also show with-drift expectancy (comparison only)", group = grpMC, tooltip = "Re-runs the model keeping the historical trend. Shown only to reveal how much of the result comes from past trend.\n\nيعيد تشغيل النموذج مع الإبقاء على الاتجاه التاريخي، لإظهار مقدار ما يأتي من اتجاه الماضي فقط.")
seedIn = input.int(42, "Random seed", group = grpMC, tooltip = "Fixed seed keeps results stable between ticks.\n\nالبذرة الثابتة تمنع تغيّر النتائج مع كل تحديث سعري.")
grpU = "2 · Universe & rank regime"
preset = input.string("Auto (chart exchange)", "Universe preset", options = ["Auto (chart exchange)", "EGX (33)", "Saudi Tadawul (25)", "US sectors (11 SPDR)", "US mega caps (30)", "Custom list"], group = grpU, tooltip = "Auto: EGX → EGX list, TADAWUL → Tadawul list, US exchanges → SPDR sectors, anything else → Custom list.\n\nتلقائي: EGX ← قائمة البورصة المصرية، TADAWUL ← قائمة تداول، البورصات الأمريكية ← قطاعات SPDR، وغير ذلك ← القائمة المخصصة.")
customList = input.text_area("NASDAQ:AAPL,NASDAQ:MSFT,NASDAQ:NVDA,NASDAQ:AMZN,NASDAQ:GOOGL,NASDAQ:META,NASDAQ:AVGO,NASDAQ:TSLA", "Custom list (EXCHANGE:TICKER, comma-separated, max 34)", group = grpU, tooltip = "Use symbols from the same market/calendar as the chart.\n\nاستخدم رموزاً من نفس سوق الشارت ونفس أيام التداول.")
rsLen = input.int(21, "Rank lookback (bars)", minval = 5, maxval = 252, group = grpU, tooltip = "Window for relative return vs the equal-weight basket.\n\nنافذة حساب العائد النسبي مقابل السلة متساوية الأوزان.")
fwdLen = input.int(20, "Self-test forward horizon (bars)", minval = 5, maxval = 120, group = grpU, tooltip = "Horizon over which the rank is tested against the following relative return.\n\nالأفق الذي يُختبر عليه الترتيب مقابل العائد النسبي اللاحق.")
testStep = input.int(5, "Self-test sampling step (bars)", minval = 1, maxval = 20, group = grpU)
zonePct = input.float(20.0, "Zone size (% of universe)", minval = 5.0, maxval = 40.0, step = 5.0, group = grpU)
minIC = input.float(0.03, "Min |IC| to call a regime", minval = 0.0, step = 0.01, group = grpU, tooltip = "Minimum absolute rank correlation before a regime is declared.\n\nالحد الأدنى لمعامل ارتباط الترتيب قبل إعلان النظام.")
minT = input.float(2.0, "Min |t| (overlap-adjusted)", minval = 1.0, step = 0.5, group = grpU, tooltip = "Minimum statistical significance after adjusting for overlapping windows.\n\nالحد الأدنى للدلالة الإحصائية بعد تصحيح تداخل النوافذ.")
grpC = "3 · Context (Custom list only)"
ctx1In = input.symbol("TVC:DXY", "Context 1", group = grpC)
ctx2In = input.symbol("TVC:US10Y", "Context 2", group = grpC)
grpG = "4 · Data gate"
gateIn = input.float(0.0, "Max plausible daily move % (0 = auto)", minval = 0.0, maxval = 100.0, group = grpG, tooltip = "Larger moves are treated as unadjusted corporate actions and excluded. Auto: EGX 20%, Tadawul 10.5%, others 50%, scaled by √days on higher timeframes.\n\nالحركات الأكبر تُعامل كإجراءات شركات غير معدّلة وتُستبعد. تلقائي: البورصة المصرية 20%، تداول 10.5%، غيرها 50%، وتُضبط بالجذر التربيعي لعدد الأيام على الأطر الأعلى.")
grpV = "5 · Display"
lang = input.string("English", "Language / اللغة", options = ["English", "العربية"], group = grpV)
tblPos = input.string("top_right", "Table position", options = ["top_right", "top_left", "middle_right", "bottom_right", "bottom_left"], group = grpV)
txtSize = input.string("small", "Text size", options = ["tiny", "small", "normal"], group = grpV)
showCone = input.bool(true, "Draw P05 / P50 / P95 cone", group = grpV)
showLvls = input.bool(true, "Draw stop / target lines", group = grpV)
cBand = input.color(color.new(#9aa0a6, 0), "Band", group = grpV)
cMid = input.color(color.new(#2962ff, 0), "Median", group = grpV)
cTgt = input.color(color.new(#089981, 0), "Target", group = grpV)
cStp = input.color(color.new(#f23645, 0), "Stop", group = grpV)
// ============================================================================
// PRESETS
// ============================================================================
string LIST_EGX = "EGX:COMI,EGX:TMGH,EGX:HRHO,EGX:EAST,EGX:ETEL,EGX:SWDY,EGX:ABUK,EGX:FWRY,EGX:EFIH,EGX:ORAS,EGX:AMOC,EGX:MFPC,EGX:PHDC,EGX:OCDI,EGX:CIEB,EGX:HELI,EGX:JUFO,EGX:EMFD,EGX:ADIB,EGX:ISPH,EGX:CLHO,EGX:EFID,EGX:ALCN,EGX:GBCO,EGX:BTFH,EGX:ORWE,EGX:EGAL,EGX:SKPC,EGX:MASR,EGX:RAYA,EGX:CCAP,EGX:EGCH,EGX:SUGR"
string LIST_KSA = "TADAWUL:2222,TADAWUL:1120,TADAWUL:1180,TADAWUL:2010,TADAWUL:7010,TADAWUL:1150,TADAWUL:1211,TADAWUL:1010,TADAWUL:2082,TADAWUL:1060,TADAWUL:2280,TADAWUL:1140,TADAWUL:1080,TADAWUL:5110,TADAWUL:7020,TADAWUL:4013,TADAWUL:2020,TADAWUL:1050,TADAWUL:4190,TADAWUL:2050,TADAWUL:4280,TADAWUL:2310,TADAWUL:4030,TADAWUL:8210,TADAWUL:4002"
string LIST_SEC = "AMEX:XLK,AMEX:XLF,AMEX:XLE,AMEX:XLV,AMEX:XLY,AMEX:XLP,AMEX:XLI,AMEX:XLB,AMEX:XLU,AMEX:XLRE,AMEX:XLC"
string LIST_MEGA = "NASDAQ:AAPL,NASDAQ:MSFT,NASDAQ:NVDA,NASDAQ:AMZN,NASDAQ:GOOGL,NASDAQ:META,NASDAQ:AVGO,NASDAQ:TSLA,NASDAQ:COST,NASDAQ:NFLX,NASDAQ:PEP,NASDAQ:AMD,NYSE:BRK.B,NYSE:JPM,NYSE:V,NYSE:LLY,NYSE:UNH,NYSE:XOM,NYSE:MA,NYSE:JNJ,NYSE:PG,NYSE:HD,NYSE:WMT,NYSE:ABBV,NYSE:BAC,NYSE:KO,NYSE:ORCL,NYSE:CVX,NYSE:MRK,NYSE:CRM"
bool usEx = syminfo.prefix == "NASDAQ" or syminfo.prefix == "NYSE" or syminfo.prefix == "AMEX" or syminfo.prefix == "BATS" or syminfo.prefix == "CBOE" or syminfo.prefix == "SP"
string pre = preset != "Auto (chart exchange)" ? preset : syminfo.prefix == "EGX" ? "EGX (33)" : syminfo.prefix == "TADAWUL" ? "Saudi Tadawul (25)" : usEx ? "US sectors (11 SPDR)" : "Custom list"
string listStr = switch pre
"EGX (33)" => LIST_EGX
"Saudi Tadawul (25)" => LIST_KSA
"US sectors (11 SPDR)" => LIST_SEC
"US mega caps (30)" => LIST_MEGA
=> customList
string ctx1 = switch pre
"EGX (33)" => "EGX:EGX30"
"Saudi Tadawul (25)" => "TADAWUL:TASI"
"Custom list" => ctx1In
=> "SP:SPX"
string ctx2 = switch pre
"EGX (33)" => "FX_IDC:USDEGP"
"Saudi Tadawul (25)" => "TVC:UKOIL"
"US sectors (11 SPDR)" => "TVC:US10Y"
"US mega caps (30)" => "TVC:DXY"
=> ctx2In
float gateBase = gateIn > 0 ? gateIn : pre == "EGX (33)" ? 20.0 : pre == "Saudi Tadawul (25)" ? 10.5 : syminfo.prefix == "EGX" ? 20.0 : syminfo.prefix == "TADAWUL" ? 10.5 : 50.0
float tfDays = math.max(1.0, timeframe.in_seconds() / 86400.0)
float gate = math.min(100.0, gateBase * (timeframe.isintraday ? 1.0 : math.sqrt(tfDays)))
var array<string> syms = str.split(str.replace_all(listStr, " ", ""), ",")
int nU = math.min(array.size(syms), 34) // 34 + 2 context = 36 requests (limit 40)
int gateWin = math.max(63, fwdLen + rsLen) + 1
// ============================================================================
// HELPERS
// ============================================================================
bool isAr = lang == "العربية"
tx(string en, string a) => isAr ? a : en
f_pos() =>
switch tblPos
"top_left" => position.top_left
"middle_right" => position.middle_right
"bottom_right" => position.bottom_right
"bottom_left" => position.bottom_left
=> position.top_right
f_size() =>
switch txtSize
"tiny" => size.tiny
"normal" => size.normal
=> size.small
f_pct(float v) => na(v) ? "—" : str.tostring(v, "+#.0;-#.0") + "%"
f_p(float v) => na(v) ? "—" : str.tostring(v * 100, "#.0") + "%"
// average-rank transform (ties share the mean rank)
f_rank(array<float> x) =>
int m = array.size(x)
array<float> r = array.new<float>(m, 0.0)
for i = 0 to m - 1
float xi = array.get(x, i)
float below = 0.0
float ties = 0.0
for j = 0 to m - 1
float xj = array.get(x, j)
if xj < xi
below += 1
else if xj == xi and j != i
ties += 1
array.set(r, i, below + ties / 2 + 1)
r
f_spearman(array<float> x, array<float> y) =>
array<float> rx = f_rank(x)
array<float> ry = f_rank(y)
float sx = array.stdev(rx)
float sy = array.stdev(ry)
sx > 0 and sy > 0 ? array.covariance(rx, ry) / (sx * sy) : na
f_demean(array<float> x) =>
float m = array.avg(x)
array<float> out = array.new<float>()
for v in x
array.push(out, v - m)
out
// ============================================================================
// DATA GATE + NORMALISED SAMPLE (confirmed bars only)
// ============================================================================
float atrV = ta.atr(atrLen)
float pc = close[1]
float a1 = atrV[1]
float barMove = na(pc) or pc == 0 ? na : math.abs(close / pc - 1) * 100
bool corp = not na(barMove) and barMove > gate
var array<float> sC = array.new<float>()
var array<float> sH = array.new<float>()
var array<float> sL = array.new<float>()
var array<float> hSums = array.new<float>()
var int corpIdx = na
var int corpTime = na
if barstate.isconfirmed and not na(a1) and a1 > 0
if corp
corpIdx := bar_index
corpTime := time
else
array.push(sC, (close - pc) / a1)
array.push(sH, (high - pc) / a1)
array.push(sL, (low - pc) / a1)
if array.size(sC) > lookbk
array.shift(sC)
array.shift(sH)
array.shift(sL)
int sz = array.size(sC)
if sz >= hzn
float hs = 0.0
for k = sz - hzn to sz - 1
hs += array.get(sC, k)
array.push(hSums, hs)
if array.size(hSums) > lookbk
array.shift(hSums)
bool corpRecent = not na(corpIdx) and bar_index - corpIdx <= lookbk
// ============================================================================
// SELF-TEST A · band coverage (empirical proxy of the MC P05–P95 band)
// ============================================================================
var array<int> qIdx = array.new<int>()
var array<float> qLo = array.new<float>()
var array<float> qHi = array.new<float>()
var int covHit = 0
var int covN = 0
if barstate.isconfirmed
while array.size(qIdx) > 0 and bar_index - array.get(qIdx, 0) >= hzn
float lo0 = array.shift(qLo)
float hi0 = array.shift(qHi)
array.shift(qIdx)
covN += 1
covHit += close >= lo0 and close <= hi0 ? 1 : 0
if bar_index % testStep == 0 and array.size(hSums) >= 100 and atrV > 0
float drift = hzn * array.avg(sC)
array.push(qIdx, bar_index)
array.push(qLo, close + (array.percentile_linear_interpolation(hSums, 5) - drift) * atrV)
array.push(qHi, close + (array.percentile_linear_interpolation(hSums, 95) - drift) * atrV)
// ============================================================================
// UNIVERSE · current rank inputs + SELF-TEST B (rank IC)
// ============================================================================
array<string> uName = array.new<string>()
array<float> uSig = array.new<float>()
array<float> u63 = array.new<float>()
array<float> lagS = array.new<float>()
array<float> fwdR = array.new<float>()
int excl = 0
if nU > 0
for i = 0 to nU - 1
string s = array.get(syms, i)
[sig, lagSig, fwd, r63, mx] = request.security(s, timeframe.period, [math.log(close / close[rsLen]), math.log(close[fwdLen] / close[fwdLen + rsLen]), math.log(close / close[fwdLen]), math.log(close / close[63]), ta.highest(math.abs(close / close[1] - 1) * 100, gateWin)], ignore_invalid_symbol = true)
bool bad = not na(mx) and mx > gate
if bad
excl += 1
else
if not na(sig) and not na(r63)
array.push(uName, s)
array.push(uSig, sig)
array.push(u63, r63)
if not na(lagSig) and not na(fwd)
array.push(lagS, lagSig)
array.push(fwdR, fwd)
var float icS = 0.0
var float icS2 = 0.0
var int icN = 0
if barstate.isconfirmed and bar_index % testStep == 0 and array.size(lagS) >= 8
float ic = f_spearman(f_demean(lagS), f_demean(fwdR))
if not na(ic)
icS += ic
icS2 += ic * ic
icN += 1
float icMean = icN > 0 ? icS / icN : na
float icSd = icN > 1 ? math.sqrt(math.max(0.0, icS2 / icN - icMean * icMean)) : na
float tAdj = icN > 1 and icSd > 0 ? icMean / (icSd / math.sqrt(icN)) / math.sqrt(math.max(1.0, fwdLen / float(testStep))) : na
int regime = icN < 30 or na(tAdj) ? 9 : icMean <= -minIC and tAdj <= -minT ? -1 : icMean >= minIC and tAdj >= minT ? 1 : 0 // -1 reversal · 1 momentum · 0 none · 9 insufficient
[c1Now, c1Then] = request.security(ctx1, timeframe.period, [close, close[rsLen]], ignore_invalid_symbol = true)
[c2Now, c2Then] = request.security(ctx2, timeframe.period, [close, close[rsLen]], ignore_invalid_symbol = true)
float ownSig = math.log(close / close[rsLen])
float own63 = math.log(close / close[63])
bool ownBad = ta.highest(nz(barMove), gateWin) > gate
// ============================================================================
// MONTE CARLO ENGINE
// ============================================================================
f_mc(bool demean, float p0, float atrNow) =>
int n = array.size(sC)
float mu = demean ? array.avg(sC) : 0.0
float tgt = p0 + tgtM * atrNow
float stp = p0 - stopM * atrNow
float rUnit = stopM * atrNow
int maxStart = math.max(1, n - blockL)
matrix<float> paths = matrix.new<float>(hzn, sims, na)
int nT = 0
int nS = 0
float sumR = 0.0
float sumR2 = 0.0
for s = 0 to sims - 1
float price = p0
int start = 0
int state = 0
for k = 0 to hzn - 1
if k % blockL == 0
start := int(math.floor(math.random(0, maxStart, seedIn)))
int j = math.min(start + k % blockL, n - 1)
float prev = price
price := prev + (array.get(sC, j) - mu) * atrNow
if state == 0
float lo = prev + (array.get(sL, j) - mu) * atrNow
float hi = prev + (array.get(sH, j) - mu) * atrNow
if lo <= stp
state := -1
else if hi >= tgt
state := 1
matrix.set(paths, k, s, price)
float r = state == 1 ? tgtM / stopM : state == -1 ? -1.0 : (price - p0) / rUnit
nT += state == 1 ? 1 : 0
nS += state == -1 ? 1 : 0
sumR += r
sumR2 += r * r
float m = sumR / sims
float sd = math.sqrt(math.max(0.0, sumR2 / sims - m * m))
[paths, nT / float(sims), nS / float(sims), m, sd / math.sqrt(sims), tgt, stp]
// ============================================================================
// OUTPUT
// ============================================================================
var array<line> drawn = array.new<line>()
var array<label> labs = array.new<label>()
var table tb = table.new(f_pos(), 2, 26, bgcolor = color.new(#131722, 0), border_color = color.new(#2a2e39, 0), border_width = 1, frame_color = color.new(#2a2e39, 0), frame_width = 1)
f_row(int r, string k, string v, color vc) =>
table.cell(tb, 0, r, k, text_color = color.new(#b2b5be, 0), text_size = f_size(), text_halign = text.align_left)
table.cell(tb, 1, r, v, text_color = vc, text_size = f_size(), text_halign = text.align_right)
f_head(int r, string t) =>
table.cell(tb, 0, r, t, text_color = color.white, bgcolor = color.new(#2a2e39, 0), text_size = f_size(), text_halign = text.align_left)
table.merge_cells(tb, 0, r, 1, r)
if barstate.islast
table.clear(tb, 0, 0, 1, 25)
for l in drawn
line.delete(l)
array.clear(drawn)
for lb in labs
label.delete(lb)
array.clear(labs)
int row = 0
table.cell(tb, 0, row, tx("Exit Map & Rank Regime", "خريطة الخروج ونظام الترتيب"), text_color = color.white, bgcolor = color.new(#1e3a8a, 0), text_size = f_size())
table.merge_cells(tb, 0, row, 1, row)
row += 1
if array.size(sC) < 100 or na(atrV) or atrV <= 0
f_row(row, tx("Data", "البيانات"), tx("Need ≥ 100 clean bars", "يلزم 100 شمعة نظيفة على الأقل"), color.orange)
else
// ---------------------------------------------------- 1) EXIT MAP
[paths, pT, pS, eR, se, tgt, stp] = f_mc(true, close, atrV)
array<float> termRow = matrix.row(paths, hzn - 1)
float p05 = array.percentile_linear_interpolation(termRow, 5)
float p50 = array.percentile_linear_interpolation(termRow, 50)
float p95 = array.percentile_linear_interpolation(termRow, 95)
if showCone
float prev05 = close
float prev50 = close
float prev95 = close
for k = 0 to hzn - 1
array<float> rowK = matrix.row(paths, k)
float q05 = array.percentile_linear_interpolation(rowK, 5)
float q50 = array.percentile_linear_interpolation(rowK, 50)
float q95 = array.percentile_linear_interpolation(rowK, 95)
array.push(drawn, line.new(bar_index + k, prev05, bar_index + k + 1, q05, color = cBand, width = 1))
array.push(drawn, line.new(bar_index + k, prev50, bar_index + k + 1, q50, color = cMid, width = 2))
array.push(drawn, line.new(bar_index + k, prev95, bar_index + k + 1, q95, color = cBand, width = 1))
prev05 := q05
prev50 := q50
prev95 := q95
array.push(labs, label.new(bar_index + hzn + 1, p95, "P95 " + str.tostring(p95, format.mintick), style = label.style_label_left, color = color.new(cBand, 70), textcolor = color.white, size = size.small))
array.push(labs, label.new(bar_index + hzn + 1, p50, "P50 " + str.tostring(p50, format.mintick), style = label.style_label_left, color = color.new(cMid, 60), textcolor = color.white, size = size.small))
array.push(labs, label.new(bar_index + hzn + 1, p05, "P05 " + str.tostring(p05, format.mintick), style = label.style_label_left, color = color.new(cBand, 70), textcolor = color.white, size = size.small))
if showLvls
array.push(drawn, line.new(bar_index, tgt, bar_index + hzn, tgt, color = cTgt, style = line.style_dashed))
array.push(drawn, line.new(bar_index, stp, bar_index + hzn, stp, color = cStp, style = line.style_dashed))
f_head(row, tx("Exit map · ", "خريطة الخروج · ") + str.tostring(hzn) + tx(" bars, zero-drift", " شمعة، دون الاتجاه التاريخي"))
row += 1
f_row(row, tx("Expected range P05 → P95", "النطاق المتوقع P05 ← P95"), f_pct((p05 / close - 1) * 100) + " → " + f_pct((p95 / close - 1) * 100), color.white)
row += 1
f_row(row, tx("Stop / Target", "الوقف / الهدف"), str.tostring(stp, format.mintick) + " / " + str.tostring(tgt, format.mintick), color.white)
row += 1
f_row(row, tx("P(Target first)", "احتمال لمس الهدف أولاً"), f_p(pT), cTgt)
row += 1
f_row(row, tx("P(Stop first)", "احتمال لمس الوقف أولاً"), f_p(pS), cStp)
row += 1
f_row(row, tx("Unresolved at horizon", "لم يُحسم في نهاية الأفق"), f_p(1 - pT - pS), color.new(#b2b5be, 0))
row += 1
bool noEdge = math.abs(eR) < 2 * se
color eCol = noEdge ? color.new(#b2b5be, 0) : eR > 0 ? cTgt : cStp
f_row(row, tx("Expectancy", "القيمة المتوقعة"), str.tostring(eR, "+0.00;-0.00") + "R ± " + str.tostring(se, "0.00"), eCol)
row += 1
f_row(row, tx("Verdict", "الحكم"), noEdge ? tx("≈ 0 · R:R alone is not an edge", "≈ صفر · نسبة العائد للمخاطرة وحدها ليست ميزة") : eR > 0 ? tx("Positive", "إيجابية") : tx("Negative", "سلبية"), eCol)
row += 1
if showDrift
[_p, _t, _s, eRd, _se, _g, _x] = f_mc(false, close, atrV)
f_row(row, tx("With past drift (not an edge)", "مع الاتجاه التاريخي (ليس ميزة)"), str.tostring(eRd, "+0.00;-0.00") + "R", color.new(#787b86, 0))
row += 1
float cov = covN > 0 ? covHit / float(covN) : na
bool covOk = not na(cov) and cov >= 0.85 and cov <= 0.95
f_row(row, tx("Band self-test (target 90%)", "اختبار النطاق (المستهدف 90%)"), na(cov) ? tx("building…", "جارٍ البناء…") : f_p(cov) + " · n=" + str.tostring(covN), na(cov) ? color.new(#787b86, 0) : covOk ? cTgt : color.orange)
row += 1
// ---------------------------------------------------- 2) RANK REGIME
int cnt = array.size(uSig)
f_head(row, tx("Rank regime · ", "نظام الترتيب · ") + str.tostring(rsLen) + tx(" bars", " شمعة"))
row += 1
string regTxt = regime == -1 ? tx("Reversal (leaders fade)", "ارتداد (القادة يتراجعون)") : regime == 1 ? tx("Momentum (leaders persist)", "زخم (القادة يستمرون)") : regime == 0 ? tx("No reliable signal", "لا توجد إشارة موثوقة") : tx("Insufficient history", "تاريخ غير كافٍ")
string icTxt = na(icMean) ? "—" : "IC " + str.tostring(icMean, "+0.00;-0.00") + " · t " + str.tostring(tAdj, "+0.0;-0.0") + " · n=" + str.tostring(icN)
f_row(row, tx("Measured on this market", "مقيس على هذا السوق"), regTxt, regime == -1 or regime == 1 ? color.white : color.new(#787b86, 0))
row += 1
f_row(row, tx("Self-test", "الاختبار الذاتي"), icTxt, color.new(#787b86, 0))
row += 1
if cnt < 8 or ownBad or na(ownSig)
f_row(row, tx("Status", "الحالة"), ownBad ? tx("Excluded: corporate action suspected", "مستبعد: إجراء شركة محتمل") : tx("Universe data insufficient", "بيانات العينة غير كافية"), color.orange)
row += 1
else
float ew = array.avg(uSig)
float ew63 = array.avg(u63)
float ownRel = ownSig - ew
int above = 0
int others = 0
for i = 0 to cnt - 1
string nm = array.get(uName, i)
bool isSelf = nm == syminfo.tickerid or nm == syminfo.prefix + ":" + syminfo.ticker
if not isSelf
others += 1
above += array.get(uSig, i) - ew > ownRel ? 1 : 0
int nAll = others + 1
int rank = above + 1
int zone = math.max(1, int(math.ceil(nAll * zonePct / 100)))
bool inTop = rank <= zone
bool inBot = rank > nAll - zone
string zTxt = regime == -1 ? (inTop ? tx("EXTENSION WARNING", "تحذير تمدد") : inBot ? tx("Relative rebound candidate", "مرشح لارتداد نسبي") : tx("Neutral", "محايد")) : regime == 1 ? (inTop ? tx("Leadership (momentum)", "قيادة (زخم)") : inBot ? tx("Laggard (momentum)", "متأخر (زخم)") : tx("Neutral", "محايد")) : tx("Rank only · no regime", "ترتيب فقط · بدون نظام")
color zCol = regime == -1 ? (inTop ? color.orange : inBot ? color.new(#26a69a, 0) : color.new(#b2b5be, 0)) : regime == 1 ? (inTop ? color.new(#26a69a, 0) : inBot ? color.orange : color.new(#b2b5be, 0)) : color.new(#787b86, 0)
f_row(row, tx("Relative vs EW basket", "الأداء مقابل السلة"), f_pct((math.exp(ownRel) - 1) * 100), color.white)
row += 1
f_row(row, tx("Rank in universe", "الترتيب في العينة"), str.tostring(rank) + " / " + str.tostring(nAll), color.white)
row += 1
f_row(row, tx("Zone", "المنطقة"), zTxt, zCol)
row += 1
f_row(row, tx("63-bar vs basket (info)", "أداء 63 شمعة (للعلم)"), f_pct((math.exp(own63 - ew63) - 1) * 100), color.new(#787b86, 0))
row += 1
if excl > 0
f_row(row, tx("Excluded by data gate", "مستبعد ببوابة البيانات"), str.tostring(excl), color.new(#787b86, 0))
row += 1
// ---------------------------------------------------- 3) CONTEXT
f_head(row, tx("Context · ", "السياق · ") + str.tostring(rsLen) + tx(" bars", " شمعة"))
row += 1
f_row(row, ctx1, f_pct(na(c1Then) or c1Then == 0 ? na : (c1Now / c1Then - 1) * 100), color.white)
row += 1
f_row(row, ctx2, f_pct(na(c2Then) or c2Then == 0 ? na : (c2Now / c2Then - 1) * 100), color.white)
row += 1
// ---------------------------------------------------- 4) DATA GATE
if corpRecent
f_row(row, tx("Data gate", "بوابة البيانات"), tx("Jump > ", "قفزة > ") + str.tostring(gate, "#") + "% · " + str.format_time(corpTime, "yyyy-MM-dd") + tx(" excluded", " مستبعدة"), color.orange)
row += 1
table.cell(tb, 0, row, tx("Model output from history · not a recommendation", "مخرجات نموذج من بيانات تاريخية · ليست توصية"), text_color = color.new(#787b86, 0), text_size = size.tiny)
table.merge_cells(tb, 0, row, 1, row)
row += 1
table.cell(tb, 0, row, "Ahmed Amin, CFTe®, CETA®", text_color = color.new(#787b86, 0), text_size = size.tiny)
table.merge_cells(tb, 0, row, 1, row)