OPEN-SOURCE SCRIPT
SwingTrades-Long-30Mins

// =====================================================================================
// SwingTrades-Long-30Mins — WHAT THIS INDICATOR DOES AND HOW (read this first)
// =====================================================================================
// THIS IS HALF OF A PAIR
// SwingTrades-Long used to be one script covering 4 categories (MT, 3Q, TR, R). It
// has been split into two independent indicators so each runs on the timeframe that
// actually fits its checks:
// - SwingTrades-Long-Daily (companion script) — Category 1 (MT) + Category 2 (3Q).
// Runs on the DAILY chart. Answers: "is this stock structurally set up to buy,
// and is the trade worth the reward vs. risk?" Updates once per day.
// - SwingTrades-Long-30Mins (THIS script) — Category 3 (TR) + Category 4 (R).
// Runs on the 30-MINUTE chart. Answers: "right now, intraday, is this a safe
// moment to pull the trigger, and how big should the position be?" Updates
// every 30 minutes throughout the session.
// The two scripts are fully independent — each computes and displays its OWN 0-10
// score. There is no shared/combined score across the pair. Run both side by side
// (Daily chart + 30-min chart): use Daily to decide whether to even be watching a
// stock, and this 30-Min script to decide whether right now is the moment to act.
//
// PURPOSE OF THIS SCRIPT
// Scores a stock 0-10 on: (3) Trend Reading — is the stock overextended, and is the
// long bias ("innocent") still intact — and (4) Risk — does the reward-vs-risk still
// hold up live, is there room left to run today, and how big should the position be.
//
// WHERE DATA COMES FROM / DESIGN NOTE
// - Runs on a 30-MINUTE chart. Native 30-min `close` is used everywhere as the
// "current live price" — this is what makes the dashboard refresh in real time
// throughout the session instead of only once at the daily close.
// - Most of the actual THRESHOLDS (5-Day SMA, 20/50-Day SMA trend, ATR, prior-day
// R2 pivot, N-bar daily high for target, daily swing-low for stop) are still
// DAILY concepts, so they are pulled from the Daily timeframe in a single bundled
// request.security() call and reused throughout.
// - VWAP and "minutes since the session open" are computed natively/from real time
// since this script already runs on an intraday chart — no extra security() pull
// needed for those two.
// - The "minutes since open" and live-price checks are only meaningful while the
// market is open and bars are updating in real time; on closed historical bars
// they reflect that bar's snapshot, not true intrabar behavior.
//
// STEP-BY-STEP LOGIC
// STEP A — Inputs, grouped by category (plus a "Daily Reference" group for the
// lengths/lookbacks used to pull Daily-timeframe data).
// STEP B — One bundled request.security() call to the Daily timeframe for every
// daily-scale value this script needs (5/20/50 SMA + slopes, ATR, prior-day
// H/L/C, N-bar high, swing-low, today's open, close N days ago (extension
// check), today's running high/low, day-start timestamp).
// STEP C — Native intraday values: VWAP (resets each session) and "current price"
// (= native 30-min close).
// STEP D — 3. TREND READING (TR) — 5 scored checks + 2 unscored caution flags:
// a. Not overextended vs the Daily 5-SMA (distance measured in daily ATRs),
// also check to make sure price > Daily 5-SMA
// b. Not overextended vs summed daily true range over the last 2-5 days, to
// clarify: if price went too far in the last 2-3 days when compared to
// the summed ATR for those days, then avoid — measured as the NET price
// move over the last `extDays` days against that period's ATR budget
// c. Not too close to / above the prior day's classic R2 pivot
// d. Gap/VWAP OK — no gap today, OR price has returned near session VWAP
// after a gap
// e. Innocent bias — live price > daily 5-SMA and daily 20 & 50 SMA rising
// f. [flag, not scored] Guilty bias — live price < daily 5-SMA and daily
// 20 & 50 SMA both declining. VETOES the entire TR score to 0.
// g. [flag, not scored] First-5-minutes-after-open — separate WATCH
// warning, not folded into the score (temporary, not structural).
// STEP E — 4. RISK (R) — 3 checks + sizing outputs, category score /10:
// a. RvR >= minimum, computed live (daily N-bar high target vs. a stop =
// the wider of a daily structural swing-low and a daily-ATR-multiple
// stop, measured against the current live price)
// b. Room left in today's ATR range (today's running high-low, so far, as
// % of daily ATR)
// c. Adequate stop distance — stop is placed at least N daily ATRs away
// (enforced by construction, shown as an explicit check)
// + Suggested share size, $ position value, and $ stop level, sized off
// account equity and max risk % per trade (informational, not scored)
// STEP F — Overall score: TR and Risk category scores (0-10 each) are weighted
// (wTR/wR, auto-normalized) into one 0-10 score. There is no broader-market
// cap here — that safeguard lives in the companion Daily script.
// STEP G — Plots native VWAP and the pulled Daily 5-SMA.
// STEP H — Draws three configurable horizontal lines (+ optional price labels)
// projecting the current potential trade plan to the right of the chart:
// Entry (live 30-min close), Exit/Target (dTargetHigh from Step B), and
// Stop (stopLevel from Step E). Color, width, style, labels, and how far
// back the visible line segment starts are all inputs.
// STEP I — Builds the dashboard table.
// =====================================================================================
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.
نص برمجي مفتوح المصدر
بروح TradingView الحقيقية، قام مبتكر هذا النص البرمجي بجعله مفتوح المصدر، بحيث يمكن للمتداولين مراجعة وظائفه والتحقق منها. شكرا للمؤلف! بينما يمكنك استخدامه مجانًا، تذكر أن إعادة نشر الكود يخضع لقواعد الموقع الخاصة بنا.
إخلاء المسؤولية
لا يُقصد بالمعلومات والمنشورات أن تكون، أو تشكل، أي نصيحة مالية أو استثمارية أو تجارية أو أنواع أخرى من النصائح أو التوصيات المقدمة أو المعتمدة من TradingView. اقرأ المزيد في شروط الاستخدام.