DALE    ²#  ! ============================================
! System Name:  Zweig Breadth Thrust
! Author:       Steve Hill / AIQ Systems
! Date:         April 2026
! Version:      1.0
!
! Description:
!   Implements the Zweig Breadth Thrust — one of the highest-
!   probability buy signals in market history, developed by
!   Martin Zweig and based on the speed of recovery in NYSE
!   market breadth from an oversold extreme.
!
!   Formula:
!     BreadthRatio = Advancing / (Advancing + Declining)
!     ZBT_EMA      = 10-day EMA of BreadthRatio
!
!   Signal fires when:
!     Condition 1: ZBT_EMA fell below 0.40 (deeply oversold breadth)
!     Condition 2: ZBT_EMA then crosses above 0.615 within 10 days
!     Both conditions must be met within the same 10-day window.
!
!   DATA SOURCE:
!   Uses AIQ TradingExpert Pro's built-in market breadth indicators[Adv Issues]
!   advances and declines — no external data ticker required.
!   The ratio is calculated directly inside the EDS from these
!   built-in daily NYSE advancing and declining issue counts.
!
!   USAGE: Run on daily bars. Signals are very rare — typically
!   only a handful of occurrences across several decades. When the
!   ZweigThrustSignal rule fires, it identifies the early stage of
!   a new bull market with historically exceptional forward returns.
! ============================================


! -------------------------------------------
! SECTION 1: BREADTH RATIO FROM BUILT-IN INDICATORS
! -------------------------------------------
!
! AIQ TradingExpert Pro provides advances and declines as built-in
! daily market breadth indicators. The Zweig ratio is calculated
! directly from these — no pre-built external ticker is needed.
! BreadthRatio ranges from 0.0 (all declining) to 1.0 (all
! advancing). A reading of 0.50 means equal advances and declines.

AdvIssues is [adv issues].
DecIssues is [dec issues].

BreadthRatio is (AdvIssues / (AdvIssues + DecIssues))*100.

! Previous bars — used in direction and crossover logic
BreadthPrev1 is valresult(BreadthRatio, 1).
BreadthPrev2 is valresult(BreadthRatio, 2).


! -------------------------------------------
! SECTION 2: THE 10-DAY EMA
! -------------------------------------------
!
! Zweig specified a 10-day exponential moving average of the daily
! breadth ratio. The EMA smooths out single-day extremes and
! measures the sustained momentum of the breadth recovery.
! expavg() is the correct EDS function for exponential averages.
! There is no weightavg() in EDS — do not use it.

ZBT_EMA is expavg(BreadthRatio, 10).

! Previous EMA values for crossover and direction logic
ZBT_EMAPrev1 is valresult(ZBT_EMA, 1).
ZBT_EMAPrev2 is valresult(ZBT_EMA, 2).


! -------------------------------------------
! SECTION 3: THRESHOLD LEVELS
! -------------------------------------------
!
! The two Zweig thresholds are fixed constants derived from his
! original research on NYSE breadth data going back decades.
! 0.40 marks deeply oversold breadth — panic selling dominates.
! 0.615 marks exceptional breadth recovery — demand has surged.
! The gap between them (21.5 percentage points) is deliberately
! wide, which is why genuine signals are so rare and so reliable.

! Condition 1: EMA is below the oversold threshold (0.40)
OversoldBreadth if ZBT_EMA < 0.40.

! Condition 2: EMA is above the thrust threshold (0.615)
ThrustLevel if ZBT_EMA > 0.615.

! Exact crossover above 0.615 — the day the thrust completes
ThrustCrossover if ZBT_EMA > 0.615 AND ZBT_EMAPrev1 <= 0.615.

! EMA crossing below 0.40 — the start of the oversold window
OversoldCrossover if ZBT_EMA < 0.40 AND ZBT_EMAPrev1 >= 0.40.


! -------------------------------------------
! SECTION 4: THE 10-DAY WINDOW
! -------------------------------------------
!
! The core Zweig rule requires that the move from below 0.40 to
! above 0.615 completes within 10 trading days. countof() checks
! whether OversoldBreadth was true at least once in the last
! 10 bars, confirming the oversold condition is recent enough
! to be within the same thrust window as today's reading.
!
! If OversoldBreadth fired more than 10 bars ago, the window has
! expired and the current recovery does not qualify as a Thrust.

! Was the EMA below 0.40 at least once in the last 10 bars?
OversoldWithin10 if countof(OversoldBreadth, 10) >= 1.

! Was the EMA below 0.40 at least once in the last 5 bars?
! (Tighter window — captures faster, more explosive thrusts)
OversoldWithin5 if countof(OversoldBreadth, 5) >= 1.

! How many of the last 10 bars were in oversold territory?
! A higher count indicates a deeper, more sustained selling panic.
OversoldDays10 is countof(OversoldBreadth, 10).

! How many of the last 10 bars were above 0.615?
ThrustDays10 is countof(ThrustLevel, 10).


! -------------------------------------------
! SECTION 5: THE ZWEIG BREADTH THRUST SIGNAL
! -------------------------------------------
!
! The primary signal: the EMA crosses above 0.615 today AND was
! below 0.40 within the preceding 10 trading days. This captures
! the exact day the thrust completes — the actionable entry bar.
!
! ZweigThrustSignal   — classic Zweig rule, strict crossover day
! ZweigThrustActive   — EMA is above 0.615 and oversold within 10d
!                       (true for multiple bars if EMA stays above)
! ZweigThrustFast     — same as classic but oversold within 5 days
!                       (captures explosive panic-to-surge moves)

ZweigThrustSignal if ThrustCrossover AND OversoldWithin10.

ZweigThrustActive if ThrustLevel AND OversoldWithin10.

ZweigThrustFast if ThrustCrossover AND OversoldWithin5.


! -------------------------------------------
! SECTION 6: BREADTH MOMENTUM GAUGES
! -------------------------------------------
!
! Supporting numeric indicators to track the state and speed of
! the breadth recovery. Display these as chart lines alongside
! the ZBT_EMA to visualise where the ratio is in its cycle.

! Direction of the EMA — is breadth improving or deteriorating?
ZBT_EMA_Rising  if ZBT_EMA > ZBT_EMAPrev1.
ZBT_EMA_Falling if ZBT_EMA < ZBT_EMAPrev1.

! Rate of change of the EMA over 5 days — speed of recovery
ZBT_Momentum5 is ZBT_EMA - valresult(ZBT_EMA, 5).

! Rate of change over 10 days — full window momentum
ZBT_Momentum10 is ZBT_EMA - valresult(ZBT_EMA, 10).

! Gap above the thrust line (positive = how far above 0.615)
ZBT_ThrustGap is ZBT_EMA - 0.615.

! Gap below the oversold line (negative = how far below 0.40)
ZBT_OversoldGap is ZBT_EMA - 0.40.

! Breadth regime: above 0.55 = bullish, below 0.45 = bearish
BreadthBullish if ZBT_EMA > 0.55.
BreadthNeutral  if ZBT_EMA >= 0.45 AND ZBT_EMA <= 0.55.
BreadthBearish  if ZBT_EMA < 0.45.

! Extreme breadth readings — intermediate warnings
BreadthExtremeBull if ZBT_EMA > 0.70.
BreadthExtremeBear if ZBT_EMA < 0.30.


! -------------------------------------------
! SECTION 7: FOLLOW-THROUGH CONFIRMATION
! -------------------------------------------
!
! After a Thrust signal fires, these rules track whether the
! breadth momentum is being sustained in subsequent sessions.
! A genuine Thrust should be followed by continued strong breadth.
! If ZBT_EMA immediately reverses below 0.50, treat the signal
! with caution — it may be a failed thrust.

! Was a Thrust signal confirmed within the last 20 trading days?
ThrustRecentlyFired if countof(ZweigThrustSignal, 20) >= 1.

! Is breadth still bullish following the thrust?
ThrustFollowThrough if ThrustRecentlyFired AND BreadthBullish.

! Did breadth retreat below 0.50 after the thrust? (caution flag)
ThrustFailed if ThrustRecentlyFired AND ZBT_EMA < 0.50.


! -------------------------------------------
! SECTION 8: MASTER SCREENING RULES
! -------------------------------------------
!
! Apply these screens to your NYSE breadth ratio ticker ($ZBTRAT
! or equivalent) on daily bars in Expert Design Studio.
!
! ZweigThrustSignal is the primary screen — run this every day.
! It fires only on the exact crossover bar, so daily screening
! will catch it immediately when it occurs.
!
! ThrustFollowThrough is useful in the days after a signal to
! confirm that breadth is sustaining the recovery.
!
! BreadthExtremeBear is an early watch alert — when this fires,
! monitor closely for an OversoldCrossover that could start the
! clock on a potential Thrust setup.

! PRIMARY: The classic Zweig Breadth Thrust buy signal
PrimaryZweigThrust if ZweigThrustSignal.

! FAST VERSION: Oversold within 5 days — most explosive setups
FastZweigThrust if ZweigThrustFast.

! FOLLOW-THROUGH: Breadth still positive after a recent signal
ZBT_FollowThrough if ThrustFollowThrough.

! WATCH ALERT: Breadth entering oversold — potential setup forming
ZBT_SetupWatch if OversoldBreadth.

! CAUTION FLAG: Post-thrust breadth deteriorating
ZBT_ThrustAtRisk if ThrustFailed.

! BEAR REGIME: Breadth has been consistently negative
BreadthBearRegime if BreadthBearish AND ZBT_EMA_Falling.   Rule Libraryÿÿ  	 CCodeView2/    ÿÿÿÿ            00:00