OPEN-SOURCE SCRIPT
Diupdate

MACD HTF Hardcoded (A/B Presets) + Regimes [CHE]

196
MACD HTF Hardcoded (A/B Presets) + Regimes — Higher-timeframe MACD emulation with acceptance-based regime filter and on-chart diagnostics

Summary

This indicator emulates a higher-timeframe MACD directly on the current chart using two hardcoded preset families and a time-bucket mapping, avoiding cross-timeframe requests. It classifies four MACD regimes and applies an acceptance filter that requires several consecutive bars before a state is considered valid. A small dead-band around zero reduces noise near the axis. An on-chart table reports the active preset, the inferred time bucket, the resolved lengths, and the current regime.

Pine version: v6
Overlay: false
Primary outputs: MACD line, Signal line, Histogram columns, zero line, regime-change alert, info table

Motivation: Why this design?

Cross-timeframe indicators often rely on external timeframe requests, which can introduce repaint paths and added latency. This design provides a deterministic alternative: it maps the current chart’s timeframe to coarse higher-timeframe buckets and uses fixed EMA lengths that approximate those views. The dead-band suppresses flip-flops around zero, and the acceptance counter reduces whipsaw by requiring sustained agreement across bars before acknowledging a regime.

What’s different vs. standard approaches?

Baseline: Classical MACD with user-selected lengths on the same timeframe, or higher-timeframe MACD via cross-timeframe requests.
Architecture differences:

Hardcoded A and B length families with a bucket map derived from the chart timeframe.
No `request.security`; all calculations occur on the current series.
Regime classification from MACD and Histogram sign, gated by an acceptance count and a small zero dead-band.
Diagnostics table for transparency.
Practical effect: The MACD behaves like a slower, higher-timeframe variant without external requests. Regimes switch less often due to the dead-band and acceptance logic, which can improve stability in choppy sessions.

How it works (technical)

The script derives a coarse bucket from the chart timeframe using `timeframe.in_seconds` and maps it to preset-specific EMA lengths. EMAs of the source build MACD and Signal; their difference is the Histogram. Signs of MACD and Histogram define four regimes: strong bull, weak bull, strong bear, and weak bear. A small, user-defined band around zero treats values near the axis as neutral. An acceptance counter checks whether the same regime persisted for a given number of consecutive bars before it is emitted as the filtered regime. A single alert condition fires when the filtered regime changes. The histogram columns change shade based on position relative to zero and whether they are rising or falling. A persistent table object shows preset, bucket tag, resolved lengths, and the filtered regime. No cross-timeframe requests are used, so repaint risk is limited to normal live-bar movement; values stabilize on close.

Parameter Guide

Source — Input series for MACD — Default: Close — Using a smoother source increases stability but adds lag.
Preset — A or B length family — Default: “3,10,16” — Switch to “12,26,9” for the classic family mapped to buckets.
Table Position — Anchor for the info table — Default: Top right — Choose a corner that avoids covering price action.
Table Size — Table text size — Default: Normal — Use small on dense charts, large for presentations.
Dark Mode — Table theme — Default: Enabled — Match your chart background for readability.
Show Table — Toggle diagnostics table — Default: Enabled — Disable for a cleaner pane.
Zero dead-band (epsilon) — Noise gate around zero — Default: Zero — Increase slightly when you see frequent flips near zero.
Acceptance bars (n) — Bars required to confirm a regime — Default: Three — Raise to reduce whipsaw; lower to react faster.

Reading & Interpretation

Histogram columns: Above zero indicates bullish pressure; below zero indicates bearish pressure. Darker shade implies the histogram increased compared with the prior bar; lighter shade implies it decreased.
MACD vs. Signal lines: The spread corresponds to histogram height.
Regimes:

Strong bull: MACD above zero and Histogram above zero.
Weak bull: MACD above zero and Histogram below zero.
Strong bear: MACD below zero and Histogram below zero.
Weak bear: MACD below zero and Histogram above zero.
Table: Inspect active preset, bucket tag, resolved lengths, and the filtered regime number with its description.

Practical Workflows & Combinations

Trend following: Use strong bull to favor long exposure and strong bear to favor short exposure. Use weak states as pullback or transition context. Combine with structure tools such as swing highs and lows or a baseline moving average for confirmation.
Exits and risk: In strong trends, consider exiting partial size on a regime downgrade to a weak state. In choppy sessions, increase the acceptance bars to reduce churn.
Multi-asset / Multi-timeframe: Works on time-based charts across liquid futures, indices, currencies, and large-cap equities. Bucket mapping helps retain a consistent feel when moving from lower to higher timeframes.

Behavior, Constraints & Performance

Repaint/confirmation: No cross-timeframe requests; values can evolve intrabar and settle on close. Alerts follow your TradingView alert timing settings.
Resources: `max_bars_back` is set to five thousand. Very large resolved lengths require sufficient history to seed EMAs; expect a warm-up period on first load or after switching symbols.
Known limits: Dead-band and acceptance can delay recognition at sharp turns. Extremely thin markets or large gaps may still cause brief regime reversals.

Sensible Defaults & Quick Tuning

Start with preset “3,10,16”, dead-band near zero, and acceptance of three bars.

Too many flips near zero: increase the dead-band slightly or raise the acceptance bars.
Too sluggish in clean trends: reduce the acceptance bars by one.
Too sensitive on fast lower timeframes: switch to the “12,26,9” preset family or raise the acceptance bars.
Want less clutter: hide the table and keep the alert.

What this indicator is—and isn’t

This is a visualization and regime layer for MACD using higher-timeframe emulation and stability gates. It is not a complete trading system and does not generate position sizing or risk management. Use it with market structure, execution rules, and protective stops.

Disclaimer

The content provided, including all code and materials, is strictly for educational and informational purposes only. It is not intended as, and should not be interpreted as, financial advice, a recommendation to buy or sell any financial instrument, or an offer of any financial product or service. All strategies, tools, and examples discussed are provided for illustrative purposes to demonstrate coding techniques and the functionality of Pine Script within a trading context.

Any results from strategies or tools provided are hypothetical, and past performance is not indicative of future results. Trading and investing involve high risk, including the potential loss of principal, and may not be suitable for all individuals. Before making any trading decisions, please consult with a qualified financial professional to understand the risks involved.

By using this script, you acknowledge and agree that any trading decisions are made solely at your discretion and risk.

Do not use this indicator on Heikin-Ashi, Renko, Kagi, Point-and-Figure, or Range charts, as these chart types can produce unrealistic results for signal markers and alerts.

Best regards and happy trading

Chervolino

Catatan Rilis
fix the table bug

Pernyataan Penyangkalan

Informasi dan publikasi tidak dimaksudkan untuk menjadi, dan bukan merupakan saran keuangan, investasi, perdagangan, atau rekomendasi lainnya yang diberikan atau didukung oleh TradingView. Baca selengkapnya di Persyaratan Penggunaan.