OPEN-SOURCE SCRIPT

CQ_(L)_Reverse MACD Overlay

229
================================================================================
CQ_(L)_REVERSE MACD OVERLAY
User Manual
================================================================================

OVERVIEW
--------------------------------------------------------------------------------
This indicator draws a full MACD system (MACD line, EMA signal, SMA signal,
and histograms) directly on the price chart instead of in a separate pane,
and adds "Reverse MACD" math that back-solves for the exact price level at
which the MACD line would turn, cross its signal line, or cross zero. In
other words, instead of just showing where MACD is now, it can tell you
what price needs to happen next for MACD's behavior to change.

It combines two lineages of prior work: the reverse-MACD price-solving
formulas (adapted from The_Caretaker) and a price-scaled overlay/dashboard
rendering engine (also adapted from The_Caretaker's dashboard work, with an
overlay rendering approach adapted from dgtrd).


SECTION 1 — MACD PROPERTIES
--------------------------------------------------------------------------------
Price Source (default: close)
Fast Period (default: 12)
Slow Period (default: 26)
EMA Signal Length (default: 9) - length of the EMA-based signal line
SMA Signal Length (default: 9) - length of the SMA-based signal line

Both an EMA-based and an SMA-based signal line are calculated simultaneously;
each can be shown or hidden independently (see Sections 3 & 4).


SECTION 2 — MACD LINE PLOT SETTINGS
--------------------------------------------------------------------------------
Show MACD Line - toggle the main MACD line on/off
Color Type - "Single" (one fixed color) or "Dual" (color changes
based on whether MACD is rising or falling bar to bar)
Single Color - color used when Color Type = Single
Dual Colors - separate Rising / Falling colors when Color Type = Dual
Line Width - 1-4


SECTION 3 — EMA SIGNAL LINE SETTINGS
--------------------------------------------------------------------------------
Same structure as the MACD line settings, applied to the EMA signal line:
Show EMA Signal, Color Type (Single/Dual), Single Color, Dual Rising/Falling
colors, Line Width. Shown by default.


SECTION 4 — SMA SIGNAL LINE SETTINGS
--------------------------------------------------------------------------------
Same structure again, applied to the SMA signal line. Hidden by default —
enable "Show SMA Signal" if you want to compare an SMA-based signal against
the EMA-based one.


SECTION 5 — EMA HISTOGRAM SETTINGS
--------------------------------------------------------------------------------
Show EMA Histogram - toggle histogram (MACD minus EMA signal)
Histogram Scaling Factor (0-10) - multiplies histogram height for visibility
Upper Rising / Upper Falling colors - used when histogram is >= 0
Lower Rising / Lower Falling colors - used when histogram is < 0
("Rising/Falling" is determined bar-to-bar on the histogram value itself)


SECTION 6 — SMA HISTOGRAM SETTINGS
--------------------------------------------------------------------------------
Same structure as the EMA histogram, but built from MACD minus the SMA
signal line. Hidden by default.

Note: if both EMA and SMA histograms are off, the "active histogram" used
internally falls back to raw MACD; if EMA histogram is on it takes
priority over SMA for any calculation that needs a single active series.


SECTION 7 — OVERLAY LAYOUT
--------------------------------------------------------------------------------
Highlight MACD/Signal Area
Fills the area between the MACD line and whichever signal line is
active (EMA takes priority if both are on). Fill color reflects
whether MACD is currently above (bullish, blue) or below (bearish,
orange) that signal line.

Display MACD/Signal Crosses
Shows small up/down triangle labels on the price chart itself
whenever MACD crosses over/under its active signal line.

Display Length (bars) (default 50, range 10-250)
How many recent bars are included in the overlay panel.

Placement (Top / Bottom, default Bottom)
Anchors the overlay panel to the highest or lowest visible price
over the Display Length window.

Height (default 7, internally inverted: 12 - input)
Controls how tall the MACD/signal/histogram panel appears. Lower
input values here produce a taller panel; higher input values
produce a shorter, more compressed one.

Vertical Position Adjustment (default 5, i.e. 0.5 internally)
Pushes the panel's baseline further from price based on the recent
high/low range, giving more or less separation from candles.


SECTION 8 — DASHBOARD
--------------------------------------------------------------------------------
Display Reverse MACD Dashboard - toggles an on-chart table
Position / Size - standard table placement and text size
(use Tiny on mobile, Small on web)
Text Color - table text color
BG MACD Falling / BG MACD Rising - background color used throughout the
table, switching based on whether MACD
is currently rising or falling

Table layout (columns: REVERSE MACD / ACTUAL / PROJECTION / LEVEL / DIF.):
Row 1 - Tendency: whether MACD is now rising or falling, the price
level ("Reverse ▲/▼") where that would flip, and the
dollar difference from current price.
Row 2 - EMA Signal: bars since the last EMA signal cross (with
direction arrow), the price level needed for MACD
to cross the EMA signal the *other* way, and the
dollar difference. Shows "OFF" if EMA signal is
disabled.
Row 3 - SMA Signal: same as Row 2, but for the SMA signal line. Shows
"OFF" if SMA signal is disabled.
Row 4 - Zero Cross: bars since MACD last crossed zero, the price level
needed for the next zero cross, and the dollar
difference.
Row 5 - MACD Now: current MACD, EMA signal, and EMA histogram values
(raw numbers, 4 decimal places).

Inline Label on Overlay (separate from the table)
Show Inline Label on Overlay - places a compact text label directly
next to the price-scaled MACD line
on the overlay panel, showing:
tendency (rising/falling), reverse
level + $ difference, EMA/SMA cross
levels + bars-ago, and the zero-cross
level + bars-ago.
Label Bar Offset (1-10) - how many bars to the right of the
last drawn bar the label sits.
Label Text Size - tiny/small/normal/large.


SECTION 9 — REVERSE MACD MATH (what the numbers mean)
--------------------------------------------------------------------------------
These are closed-form solutions (not simulations) for the exact price that
would need to print on the current bar to produce a specific MACD outcome:

Reverse Level (macd_eq)
The price at which the MACD line's slope would flip — i.e. the point
where MACD stops rising and starts falling, or vice versa.

Zero Cross Level (macd_level at 0)
The price at which MACD would cross the zero line.

EMA Cross Level (macd_cross_ema)
The price at which MACD would cross its EMA signal line.

SMA Cross Level (macd_cross_sma)
The price at which MACD would cross its SMA signal line.

Each of these is recalculated every bar using the prior bar's EMA/SMA
values algebraically solved for the current bar's price, so they update in
real time as price moves intrabar.


SECTION 10 — SIGNALS & ALERTS
--------------------------------------------------------------------------------
Long Cross / Short Cross shapes
Small up/down labels plotted below/above bars when MACD crosses over
or under whichever signal line is active (EMA preferred, otherwise
SMA). Controlled by "Display MACD/Signal Crosses."

Four built-in alerts (set these up in TradingView's Alert dialog):
Long : Early Warning - fires the moment MACD crosses over signal
Long : Trading Opportunity - fires one bar after, once the cross
has held (confirmation)
Short : Early Warning - mirror of the long early warning
Short : Trading Opportunity - mirror of the long confirmation

All four alert messages include exchange, ticker, price, and time.


SECTION 11 — HOW THE OVERLAY IS DRAWN (for reference)
--------------------------------------------------------------------------------
On the last bar, all previously drawn lines, histogram boxes, and fills are
deleted and redrawn across the "Display Length" window:
1. A baseline price is anchored to the highest (Top placement) or lowest
(Bottom placement) price over that window, adjusted outward using the
Vertical Position Adjustment and the recent price range.
2. Each bar's MACD, EMA signal, and SMA signal values are scaled relative
to the largest recent absolute MACD value ("oscHighest") and the
Height setting, then converted into a price offset from the baseline.
3. Line segments are drawn bar-to-bar for each enabled series (MACD, EMA
signal, SMA signal), plus histogram boxes for each enabled histogram.
4. If "Highlight MACD/Signal Area" is on, a colored fill is drawn between
the MACD line and the active signal line for each bar pair.
5. The inline label (if enabled) is placed a few bars to the right of the
last drawn bar, at the price-scaled Y position of the current MACD
value.


SECTION 12 — PRACTICAL NOTES & LIMITS
--------------------------------------------------------------------------------
- Declared limits: max_lines_count = 500, max_boxes_count = 250,
max_bars_back = 500. The drawing loop stops adding new lines once 501
are queued, as a safety guard.
- "Display Length" is capped at 250 bars; larger values increase how many
lines/boxes get drawn per refresh (relevant if you also enable both
histograms and the highlight fill at once).
- The Dashboard and the Inline Label are independent features — you can
run either, both, or neither without affecting the overlay lines
themselves.
- EMA signal takes priority over SMA signal for cross detection, active
histogram fallback, and the "Highlight MACD/Signal Area" fill whenever
both are enabled at the same time.
- The Reverse MACD levels (Reverse, Zero Cross, EMA Cross, SMA Cross) are
price *projections* based on current EMA/SMA state — they describe what
price would need to do, not a prediction that it will.

================================================================================
End of manual — CQ_(L)_Reverse MACD Overlay
================================================================================

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.