本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Mr Oatto US Stock Hunter", shorttitle="OattoUSHunter", overlay=true)
// ============================================================
// EMA TREND ANALYZER — วิเคราะห์ symbol ปัจจุบันที่เปิดบนกราฟ (run trend / swing trade)
// ปรับสำหรับตลาดหุ้น US: EMA 10/25/75/200 + Market overview เป็น Nasdaq100 / S&P500
// ทุก metric ของ current symbol (EMA/Trend/ATR/Extension/Swing/Trend Age/RS vs Nasdaq100)
// ถูกบังคับให้คำนวณบน DAILY เสมอ ผ่าน request.security(syminfo.tickerid, "D", ...)
// ไม่ว่าจะเปิดกราฟ timeframe ไหนก็ตาม (กัน RS/Trend Age เปลี่ยนค่าเวลาสลับ timeframe)
// + Trade-ready status + Buy-on-dip EMA levels + Fundamentals (PE/Profit/Grow)
// + Weekly trend filter (multi-timeframe alignment, Daily vs Weekly)
// + Relative Strength vs Nasdaq100 (leader/laggard, เทียบ window เดียวกันทั้งคู่บน Daily)
// + Chase-risk filter: extension from EMA (ATR/%) + swing-leg counter (Elliott-wave-ish proxy)
// -> เมื่อราคาวิ่งไกลจากแนวรับ EMA ไปแล้ว หรือขึ้นมาหลาย leg แล้ว จะเปลี่ยนคำแนะนำ
// จาก "พร้อมรันเทรนด์" เป็น "รอ Pullback" แทน เพื่อไม่ให้ไล่ซื้อยอด
// + ATR-based Stop/Target + R:R
// + Trend age / overextension warning
// + Alerts (trend flip, cut warning, overextended, chase-risk, pullback entry)
// + Market trend overview (Nasdaq100 / S&P500)
// 3 ตาราง แยกกันคนละมุมจอ (Trend Panel / Edge Panel / Market Panel) เพื่อไม่ให้ทับกัน
// การใช้งาน: เปิด US stock ไหนก็ได้ (timeframe ไหนก็ได้) → indicator วิเคราะห์บน Daily ให้เสมอ
// ============================================================
// ============================================================
// INPUTS
// ============================================================
grpMarket = "Market Trend (Nasdaq100 / S&P500)"
showMarketTable = input.bool(true, "Show Nasdaq100/S&P500 trend table", group=grpMarket)
marketTablePosStr = input.string("top_right", "Market table position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpMarket)
grpEMA = "EMA / Trend Settings (Daily - fixed)"
emaFast = input.int(10, "EMA Fast (Entry 1)", group=grpEMA)
emaMed = input.int(25, "EMA Medium (Entry 2)", group=grpEMA)
emaSlow = input.int(75, "EMA Slow (Entry 3)", group=grpEMA)
emaFilter = input.int(75, "EMA Trade Filter", group=grpEMA)
emaMajor = input.int(200, "EMA Major Trend", group=grpEMA)
cutDays = input.int(5, "Consecutive days below EMA-Filter -> cut warning", group=grpEMA)
grpChart = "Chart Display (current symbol)"
showChartEMAs = input.bool(true, "Show EMA lines on current chart", group=grpChart)
emaSetStr = input.string("all", "EMA set to display", options=["all", "entry", "ribbon", "major"], group=grpChart)
showTrendPanel = input.bool(true, "Show trend panel (bottom-right)", group=grpChart)
trendPanelPosStr = input.string("bottom_right", "Trend panel position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpChart)
showTradeReady = input.bool(true, "Show trade-ready status in panel", group=grpChart)
showFundColumns = input.bool(true, "Show fundamentals (PE / Profit / Grow) in trend panel", group=grpChart)
panelTextSizeStr = input.string("small", "Table text size", options=["tiny", "small", "normal"], group=grpChart)
grpEdge = "Trend Edge (Weekly / Relative Strength / ATR Risk)"
showEdgeMetrics = input.bool(true, "Show edge metrics panel", group=grpEdge)
edgePanelPosStr = input.string("bottom_left", "Edge panel position", options=["top_right", "top_left", "bottom_right", "bottom_left"], group=grpEdge)
useWeeklyFilter = input.bool(true, "Use Weekly EMA trend as MTF filter", group=grpEdge)
rsLookback = input.int(60, "Relative Strength lookback (Daily bars)", minval=5, group=grpEdge)
atrLen = input.int(14, "ATR length (Daily)", minval=1, group=grpEdge)
atrMultSL = input.float(1.5, "ATR multiplier for Stop Loss", minval=0.1, step=0.1, group=grpEdge)
rrRatio = input.float(2.0, "Reward:Risk ratio for Target", minval=0.5, step=0.5, group=grpEdge)
overextendedBars = input.int(60, "Overextended warning after N Daily bars in same trend regime", minval=5, group=grpEdge)
grpChase = "Chase Risk (avoid buying an extended move)"
extensionMethod = input.string("ATR", "Extension measured by", options=["ATR", "Percent"], group=grpChase)
extensionATRMult = input.float(3.0, "Extended if price > N x ATR from EMA Slow", minval=0.5, step=0.5, group=grpChase)
extensionPct = input.float(15.0, "Extended if price > N% from EMA Slow", minval=1, step=1, group=grpChase)
pivotLeftBars = input.int(5, "Swing pivot: left bars (Daily)", minval=1, group=grpChase)
pivotRightBars = input.int(5, "Swing pivot: right bars (Daily)", minval=1, group=grpChase)
waveCautionCount = input.int(3, "Caution if swing legs >= N since trend start", minval=1, group=grpChase)
showSwingMarkers = input.bool(false, "Plot swing high/low markers on chart (approx.)", group=grpChase)
// ============================================================
// HELPERS
// ============================================================
f_pos(p) =>
switch p
"top_left" => position.top_left
"bottom_right" => position.bottom_right
"bottom_left" => position.bottom_left
=> position.top_right
f_textSize(s) =>
switch s
"tiny" => size.tiny
"normal" => size.normal
=> size.small
f_trendColor(t) =>
t == "Super Bullish" ? color.new(color.lime, 0) : t == "Bullish" ? color.new(color.green, 0) : t == "Sideway Up" ? color.new(color.olive, 0) : t == "Sideway Down" ? color.new(color.orange, 0) : t == "Downtrend" ? color.new(color.red, 0) : t == "Super Bear" ? color.new(color.maroon, 0) : color.new(color.gray, 0)
f_classifyTrend(c, e1, e2, e3, e4, e5) =>
bullStack = e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5
bearStack = e1 < e2 and e2 < e3 and e3 < e4 and e4 < e5
c > e5 and bullStack ? "Uptrend" : c < e5 and bearStack ? "Downtrend" : "Sideway"
f_classifyTrendDetailed(c, e1, e2, e3, e4, e5) =>
superBull = c > e1 and e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5
bull = e1 > e2 and e2 > e3 and e3 > e4 and e4 > e5
partialBull = e1 > e2 and e2 > e3 and e3 > e4
partialBear = e1 < e2 and e2 < e3 and e3 < e4
aboveEMA200 = c > e5
belowEMA89 = c < e4
if superBull
"Super Bullish"
else if bull
"Bullish"
else if aboveEMA200 and partialBull
"Sideway Up"
else if belowEMA89 and aboveEMA200 and not partialBear
"Sideway Down"
else if c < e5
"Super Bear"
else if belowEMA89
"Downtrend"
else
"Sideway"
f_isBullishRegime(t) =>
t == "Super Bullish" or t == "Bullish" or t == "Sideway Up"
f_isBearishRegime(t) =>
t == "Downtrend" or t == "Super Bear" or t == "Sideway Down"
f_fmtBool(v) =>
na(v) ? "N/A" : v > 0 ? "Yes" : "No"
f_fmtGrow(v) =>
na(v) or na(v[252]) ? "N/A" : v > v[252] ? "Yes" : "No"
f_fmtNum(v) =>
na(v) ? "N/A" : str.tostring(v, "#.##")
f_fmtPct(v) =>
na(v) ? "N/A" : (v >= 0 ? "+" : "") + str.tostring(v, "#.##") + "%"
// PE = ราคาปัจจุบัน / EPS(TTM)
f_pe(px, eps) =>
float res = na
if not na(eps) and eps > 0
res := px / eps
res
// ทุกอย่างของ current symbol คำนวณภายในฟังก์ชันนี้บน context ของ Daily bar จริง
// (เรียกผ่าน request.security เท่านั้น ห้ามเรียกตรงๆ นอก security เพราะ close/high/low
// ข้างในนี้คือของ Daily ไม่ใช่ของ timeframe กราฟที่เปิดอยู่)
f_dailyMetrics() =>
c = close
h = high
l = low
e1 = ta.ema(c, emaFast)
e2 = ta.ema(c, emaMed)
e3 = ta.ema(c, emaSlow)
e4 = ta.ema(c, emaFilter)
e5 = ta.ema(c, emaMajor)
trend = f_classifyTrendDetailed(c, e1, e2, e3, e4, e5)
regime = f_isBullishRegime(trend) ? 1 : f_isBearishRegime(trend) ? -1 : 0
ageBars = ta.barssince(regime != regime[1])
barsSinceAboveFilter = ta.barssince(c >= e4)
atrV = ta.atr(atrLen)
ph = ta.pivothigh(h, pivotLeftBars, pivotRightBars)
pl = ta.pivotlow(l, pivotLeftBars, pivotRightBars)
closeLookback = c[rsLookback]
var int swingCnt = 0
if regime != regime[1]
swingCnt := 0
if regime == 1 and not na(ph)
swingCnt := swingCnt + 1
if regime == -1 and not na(pl)
swingCnt := swingCnt + 1
[c, h, l, e1, e2, e3, e4, e5, atrV, ageBars, barsSinceAboveFilter, ph, pl, closeLookback, bar_index, swingCnt]
// ============================================================
// MARKET TREND: Nasdaq100 / S&P500 (Daily EMA ribbon)
// ============================================================
[ndxC, ndxE1, ndxE2, ndxE3, ndxE4, ndxE5] = request.security("TVC:NDX", "D", [close, ta.ema(close, emaFast), ta.ema(close, emaMed), ta.ema(close, emaSlow), ta.ema(close, emaFilter), ta.ema(close, emaMajor)], lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
[spxC, spxE1, spxE2, spxE3, spxE4, spxE5] = request.security("TVC:SPX", "D", [close, ta.ema(close, emaFast), ta.ema(close, emaMed), ta.ema(close, emaSlow), ta.ema(close, emaFilter), ta.ema(close, emaMajor)], lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
ndxTrend = f_classifyTrendDetailed(ndxC, ndxE1, ndxE2, ndxE3, ndxE4, ndxE5)
spxTrend = f_classifyTrendDetailed(spxC, spxE1, spxE2, spxE3, spxE4, spxE5)
// ============================================================
// WEEKLY TREND FILTER (multi-timeframe alignment)
// ============================================================
[wC, wE1, wE2, wE3, wE4, wE5] = request.security(syminfo.tickerid, "W", [close, ta.ema(close, emaFast), ta.ema(close, emaMed), ta.ema(close, emaSlow), ta.ema(close, emaFilter), ta.ema(close, emaMajor)], lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
weeklyTrend = f_classifyTrendDetailed(wC, wE1, wE2, wE3, wE4, wE5)
// ============================================================
// CURRENT SYMBOL (บังคับ Daily เสมอ) — EMA + trend panel + trade-ready + fundamentals
// ============================================================
[dC, dH, dL, dE1, dE2, dE3, dE4, dE5, dATR, dAgeBars, dBarsSinceAboveFilter, dPH, dPL, dCloseLookback, dBarIndex, dSwingCnt] =
request.security(syminfo.tickerid, "D", f_dailyMetrics(), lookahead=barmerge.lookahead_off, ignore_invalid_symbol=true)
curTrend = f_classifyTrendDetailed(dC, dE1, dE2, dE3, dE4, dE5)
curBelow89 = dC < dE4
curBelow200 = dC < dE5
curCutD = na(dBarsSinceAboveFilter) ? dBarIndex + 1 : int(dBarsSinceAboveFilter)
curCutW = curCutD >= cutDays
regimeState = f_isBullishRegime(curTrend) ? 1 : f_isBearishRegime(curTrend) ? -1 : 0
// Multi-timeframe alignment (Daily trend confirmed by Weekly trend)
mtfAligned = (f_isBullishRegime(curTrend) and f_isBullishRegime(weeklyTrend)) or (f_isBearishRegime(curTrend) and f_isBearishRegime(weeklyTrend))
mtfLabel = not useWeeklyFilter ? "—" : mtfAligned ? "✅ Aligned" : "❌ Not Aligned"
mtfColor = not useWeeklyFilter ? color.gray : mtfAligned ? color.green : color.gray
// Relative Strength vs Nasdaq100 — ทั้งสองฝั่งเทียบ "N Daily bars ย้อนหลัง" เท่ากันเสมอ
stockChangePct = (dC - dCloseLookback) / dCloseLookback * 100
ndxChangePct = (ndxC - ndxC[rsLookback]) / ndxC[rsLookback] * 100
rsValue = stockChangePct - ndxChangePct
rsColor = na(rsValue) ? color.gray : rsValue > 0 ? color.green : color.red
// ATR-based Stop Loss / Target / R:R (Daily ATR, for long-bias setups)
atrVal = dATR
atrStop = dC - atrMultSL * atrVal
atrTarget = dC + (atrMultSL * rrRatio) * atrVal
// Trend age / overextension (Daily bars since last regime change)
trendAgeBars = dAgeBars
isOverextended = regimeState != 0 and trendAgeBars >= overextendedBars
trendAgeLabel = str.tostring(trendAgeBars) + (isOverextended ? " ⚠️" : "")
trendAgeColor = isOverextended ? color.orange : color.white
// --- Chase-risk filter #1: extension from EMA Slow (ATR or %), Daily ---
extAmount = f_isBullishRegime(curTrend) ? (dC - dE3) : f_isBearishRegime(curTrend) ? (dE3 - dC) : na
extATR = (not na(extAmount) and atrVal > 0) ? extAmount / atrVal : na
extPct = (not na(extAmount) and dE3 != 0) ? extAmount / dE3 * 100 : na
isExtended = extensionMethod == "ATR" ? (not na(extATR) and extATR > extensionATRMult) : (not na(extPct) and extPct > extensionPct)
extLabel = extensionMethod == "ATR" ? (na(extATR) ? "N/A" : f_fmtNum(extATR) + "x ATR") : (na(extPct) ? "N/A" : f_fmtNum(extPct) + "%")
extColor = isExtended ? color.orange : color.white
// --- Chase-risk filter #2: swing-leg counter (Daily, Elliott-wave-ish proxy, NOT true EW) ---
swingCount = dSwingCnt
isLateStageWave = swingCount >= waveCautionCount
swingColor = isLateStageWave ? color.orange : color.white
// marker ตำแหน่งเป็นค่าประมาณ (Daily pivot วาดทับบนแกนเวลาของกราฟที่เปิดอยู่)
plotshape(showSwingMarkers and not na(dPH), title="Swing High", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.tiny, offset=-pivotRightBars)
plotshape(showSwingMarkers and not na(dPL), title="Swing Low", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny, offset=-pivotRightBars)
// Combined "don't chase" flag
tooFarToChase = isExtended or isOverextended or isLateStageWave
// Fundamentals for current symbol (ไม่ขึ้นกับ timeframe อยู่แล้ว)
curEPS = showFundColumns ? request.financial(syminfo.tickerid, "EARNINGS_PER_SHARE_BASIC", "TTM", ignore_invalid_symbol=true) : na
curNI = showFundColumns ? request.financial(syminfo.tickerid, "NET_INCOME", "FY", ignore_invalid_symbol=true) : na
curPE = f_pe(dC, curEPS)
curProf = curNI > 0
curGrow = curNI > curNI[252]
// --- EMA lines for CHART DISPLAY ONLY: วิ่งตาม timeframe ของกราฟที่เปิดอยู่ ---
// (ต่างจาก dE1-dE5 ที่ใช้คำนวณ Trend/Trade Status/Buy-on-dip/Edge metrics ซึ่ง fix เป็น Daily เสมอ)
chartE1 = ta.ema(close, emaFast)
chartE2 = ta.ema(close, emaMed)
chartE3 = ta.ema(close, emaSlow)
chartE4 = ta.ema(close, emaFilter)
chartE5 = ta.ema(close, emaMajor)
showE1 = showChartEMAs and (emaSetStr == "all" or emaSetStr == "entry")
showE2 = showChartEMAs and (emaSetStr == "all" or emaSetStr == "entry")
showE3 = showChartEMAs and (emaSetStr == "all" or emaSetStr == "entry")
showE4 = showChartEMAs and (emaSetStr == "all" or emaSetStr == "ribbon")
showE5 = showChartEMAs and (emaSetStr == "all" or emaSetStr == "major")
plot(showE1 ? chartE1 : na, title="EMA Fast", color=color.new(color.yellow, 0),linewidth=1)
plot(showE2 ? chartE2 : na, title="EMA Medium", color=color.new(color.orange, 0),linewidth=1)
plot(showE3 ? chartE3 : na, title="EMA Slow", color=color.new(color.aqua, 0), linewidth=1)
plot(showE4 ? chartE4 : na, title="EMA Filter", color=color.new(color.aqua, 0), linewidth=2)
plot(showE5 ? chartE5 : na, title="EMA Major", color=color.new(color.white, 0), linewidth=2)
// --- Trade-ready logic ---
// Price < EMA200 -> Don't trade (too risky)
// Cut warning (consecutive Daily bars below EMA-Filter) -> should cut first
// Bullish regime + price < EMA-Filter -> Understandard Trade (long risky)
// Bullish regime + extended (ATR/% from EMA) or late-stage swing legs -> wait for Pullback instead of chasing
// Bullish regime + price above EMA-Filter, not extended -> ready long
// Bearish regime + price < EMA-Filter + extended/late-stage -> wait for Pullback (short)
// Bearish regime + price < EMA-Filter, not extended -> ready short
// Else -> wait for signal
f_readyStatus(trend, below89, below200, cutW, chase) =>
bull = trend == "Super Bullish" or trend == "Bullish" or trend == "Sideway Up"
bear = trend == "Downtrend" or trend == "Super Bear" or trend == "Sideway Down"
if below200
"❌ อย่าเล่น (Price < EMA Major)"
else if cutW
"⚠️ ควรคัตไปก่อน"
else if bull and below89
"⚠️ Understandard Trade"
else if bull and chase
"🔸 รอ Pullback เข้า EMA (วิ่งไกลแล้ว)"
else if bull
"✅ พร้อมรันเทรนด์ (Long)"
else if bear and below89 and chase
"🔸 รอ Pullback เข้า EMA (วิ่งไกลแล้ว)"
else if bear and below89
"✅ พร้อมรันเทรนด์ (Short)"
else
"⏳ รอจังหวะ"
curStatus = f_readyStatus(curTrend, curBelow89, curBelow200, curCutW, tooFarToChase)
curStatusColor = curStatus == "✅ พร้อมรันเทรนด์ (Long)" ? color.green : curStatus == "✅ พร้อมรันเทรนด์ (Short)" ? color.red : curStatus == "❌ อย่าเล่น (Price < EMA Major)" ? color.maroon : curStatus == "⚠️ ควรคัตไปก่อน" ? color.orange : curStatus == "⚠️ Understandard Trade" ? color.orange : curStatus == "🔸 รอ Pullback เข้า EMA (วิ่งไกลแล้ว)" ? color.olive : color.gray
textSz = f_textSize(panelTextSizeStr)
// ============================================================
// TABLE 1: TREND PANEL (core trend/status/EMA levels/fundamentals)
// ============================================================
trendRows = showFundColumns ? 11 : 8
var table trendTbl = na
if showTrendPanel and barstate.isfirst
trendTbl := table.new(f_pos(trendPanelPosStr), 2, trendRows, bgcolor=color.new(color.black, 70), border_width=1, border_color=color.gray)
if showTrendPanel and barstate.islast and not na(trendTbl)
table.cell(trendTbl, 0, 0, "Symbol", text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(trendTbl, 1, 0, syminfo.ticker, text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(trendTbl, 0, 1, "Trend (D)", text_color=color.white, text_size=textSz)
table.cell(trendTbl, 1, 1, curTrend, text_color=f_trendColor(curTrend), text_size=textSz)
if showTradeReady
table.cell(trendTbl, 0, 2, "Trade Status", text_color=color.white, text_size=textSz)
table.cell(trendTbl, 1, 2, curStatus, text_color=curStatusColor, text_size=textSz)
table.cell(trendTbl, 0, 3, "BUY on Dip at:", text_color=color.white, bgcolor=color.new(color.orange, 60), text_size=textSz)
table.cell(trendTbl, 1, 3, "Price", text_color=color.white, bgcolor=color.new(color.orange, 60), text_size=textSz)
table.cell(trendTbl, 0, 4, "EMA Fast", text_color=color.yellow, text_size=textSz)
table.cell(trendTbl, 1, 4, f_fmtNum(dE1), text_color=color.yellow, text_size=textSz)
table.cell(trendTbl, 0, 5, "EMA Medium", text_color=color.orange, text_size=textSz)
table.cell(trendTbl, 1, 5, f_fmtNum(dE2), text_color=color.orange, text_size=textSz)
table.cell(trendTbl, 0, 6, "EMA Slow", text_color=color.aqua, text_size=textSz)
table.cell(trendTbl, 1, 6, f_fmtNum(dE3), text_color=color.aqua, text_size=textSz)
table.cell(trendTbl, 0, 7, "EMA Filter", text_color=color.aqua, text_size=textSz)
table.cell(trendTbl, 1, 7, f_fmtNum(dE4), text_color=color.aqua, text_size=textSz)
if showFundColumns
table.cell(trendTbl, 0, 8, "PE", text_color=color.white, text_size=textSz)
table.cell(trendTbl, 1, 8, f_fmtNum(curPE), text_color=color.white, text_size=textSz)
table.cell(trendTbl, 0, 9, "Profit", text_color=color.white, text_size=textSz)
table.cell(trendTbl, 1, 9, f_fmtBool(curNI), text_color=curProf ? color.green : color.red, text_size=textSz)
table.cell(trendTbl, 0, 10, "Grow", text_color=color.white, text_size=textSz)
table.cell(trendTbl, 1, 10, f_fmtGrow(curNI), text_color=curGrow ? color.green : color.red, text_size=textSz)
// ============================================================
// TABLE 2: EDGE PANEL (Weekly / RS / Extension / Swing / ATR risk / Trend age)
// แยกออกจาก Trend Panel และวางคนละมุมจอ เพื่อไม่ให้ทับกัน
// ============================================================
var table edgeTbl = na
if showEdgeMetrics and barstate.isfirst
edgeTbl := table.new(f_pos(edgePanelPosStr), 2, 10, bgcolor=color.new(color.black, 70), border_width=1, border_color=color.gray)
if showEdgeMetrics and barstate.islast and not na(edgeTbl)
table.cell(edgeTbl, 0, 0, "Edge Metrics (D)", text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(edgeTbl, 1, 0, syminfo.ticker, text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(edgeTbl, 0, 1, "Weekly Trend", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 1, weeklyTrend, text_color=f_trendColor(weeklyTrend), text_size=textSz)
table.cell(edgeTbl, 0, 2, "MTF Align", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 2, mtfLabel, text_color=mtfColor, text_size=textSz)
table.cell(edgeTbl, 0, 3, "RS vs Nasdaq100", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 3, f_fmtPct(rsValue), text_color=rsColor, text_size=textSz)
table.cell(edgeTbl, 0, 4, "Extension", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 4, extLabel, text_color=extColor, text_size=textSz)
table.cell(edgeTbl, 0, 5, "Swing Legs", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 5, str.tostring(swingCount), text_color=swingColor, text_size=textSz)
table.cell(edgeTbl, 0, 6, "ATR Stop", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 6, f_fmtNum(atrStop), text_color=color.red, text_size=textSz)
table.cell(edgeTbl, 0, 7, "ATR Target", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 7, f_fmtNum(atrTarget), text_color=color.green, text_size=textSz)
table.cell(edgeTbl, 0, 8, "R:R", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 8, "1:" + str.tostring(rrRatio, "#.#"), text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 0, 9, "Trend Age (D bars)", text_color=color.white, text_size=textSz)
table.cell(edgeTbl, 1, 9, trendAgeLabel, text_color=trendAgeColor, text_size=textSz)
// ============================================================
// TABLE 3: MARKET TREND (Nasdaq100 / S&P500)
// ============================================================
var table marketTbl = na
if showMarketTable and barstate.isfirst
marketTbl := table.new(f_pos(marketTablePosStr), 2, 3, bgcolor=color.new(color.black, 70), border_width=1, border_color=color.gray)
if showMarketTable and barstate.islast and not na(marketTbl)
table.cell(marketTbl, 0, 0, "Index", text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(marketTbl, 1, 0, "Trend", text_color=color.white, bgcolor=color.new(color.blue, 60), text_size=textSz)
table.cell(marketTbl, 0, 1, "Nasdaq100", text_color=color.white, text_size=textSz)
table.cell(marketTbl, 1, 1, ndxTrend, text_color=f_trendColor(ndxTrend), text_size=textSz)
table.cell(marketTbl, 0, 2, "S&P500", text_color=color.white, text_size=textSz)
table.cell(marketTbl, 1, 2, spxTrend, text_color=f_trendColor(spxTrend), text_size=textSz)
// ============================================================
// ALERTS
// ============================================================
bullishFlip = f_isBullishRegime(curTrend) and not f_isBullishRegime(curTrend[1])
bearishFlip = f_isBearishRegime(curTrend) and not f_isBearishRegime(curTrend[1])
cutWarningFlip = curCutW and not curCutW[1]
overextendedFlip = isOverextended and not isOverextended[1]
chaseRiskFlip = tooFarToChase and not tooFarToChase[1]
pullbackEntry = f_isBullishRegime(curTrend) and not curBelow89 and low <= dE1 and high >= dE1
alertcondition(bullishFlip, title="Trend Turned Bullish", message="{{ticker}} Daily trend turned Bullish")
alertcondition(bearishFlip, title="Trend Turned Bearish", message="{{ticker}} Daily trend turned Bearish")
alertcondition(cutWarningFlip, title="Cut Warning", message="{{ticker}} closed below Daily EMA Filter for too long - consider cutting")
alertcondition(overextendedFlip, title="Trend Overextended", message="{{ticker}} Daily trend has run long - watch for pullback/reversal")
alertcondition(chaseRiskFlip, title="Chase Risk - Wait for Pullback", message="{{ticker}} already extended or late-stage swing - avoid chasing, wait for pullback to EMA")
alertcondition(pullbackEntry, title="Pullback Entry Signal", message="{{ticker}} pulled back to Daily EMA Fast within an uptrend - possible entry")