PROTECTED SOURCE SCRIPT

QuantTrader MTF Momentum — QUIET 4 (TG-clean) - mylivingedge

16
What this indicator is

**QuantTrader MTF Momentum — QUIET** is a *signal gate* that only prints a BUY/SELL when the market meets a checklist of quality conditions. It’s designed for low-noise, rules-based entries on intraday FX/crypto, with built-in higher-timeframe confirmation, volatility/quality guards, and an optional Telegram push.

---

## The core idea (at a glance)

A bar can trigger a **BUY** (or **SELL**) only when:

1. **Session**: you’re within enabled trading sessions (Asian/London/NY) if that option is on.
2. **Trend**: price is on the correct side of a baseline (EMA/HMA/Supertrend) **and** the higher-timeframe EMA slope points the same way.
3. **Momentum score**: at least *N out of 3* momentum checks agree (RSI, MACD, Stoch).
4. **Market quality**: ATR regime is “normal,” ADX (if enabled) is strong enough, chop is low enough, and the candle body isn’t tiny vs ATR.
5. **Breakout filter**: (optional) price breaks the prior Donchian high/low.

If any requirement fails, the signal is blocked.

---

## What’s plotted

* **BUY/SELL markers** on qualifying bars.
* **Risk overlay** (optional): stop line and 1R/2R/3R targets computed from ATR.
* **Session shading** (optional): lightly tinted background for Asian/London/NY.

---

## Presets & Quiet Mode

* **Presets** (Forex Fast, Gold Momentum, BTC Intraday, + London variants) nudge thresholds so you don’t have to retune everything per market.
* **Quiet Mode (ON)** = stricter: requires full trend, momentum, quality and (optionally) Donchian breakout.
* **Quiet Mode (OFF)** = looser / earlier: requires trend + 1 momentum vote + ATR regime only.

---

## The building blocks (signals & guards)

### 1) Trend & HTF confirmation

* **Baseline**: choose **EMA**, **HMA**, or **Supertrend**. Trend is *up* when `close > baseline`, *down* when `close < baseline`.
* **HTF EMA slope** (e.g., 60-min on a 5-min chart):

* Up-trend OK if slope > +0.02% and price sits above the HTF EMA.
* Down-trend OK if slope < −0.02% and price below the HTF EMA.

### 2) Momentum score (0–3)

Votes for **UP**:

* **RSI**: RSI ≥ buy level and *rising* over the last 3 bars.
* **MACD**: MACD line crossed **above** signal & histogram > 0.
* **Stochastic**: %K crossed **above** %D with both below oversold.

Votes for **DOWN** are the mirrors (RSI <= level and *falling*, MACD cross **below**, Stoch cross **below** with both above overbought).

You choose how many of these 3 must agree (default 2). Presets can adjust that automatically.

### 3) Volatility / Quality guards

* **ATR regime**: current ATR must sit between `ATR_Min × median(ATR)` and `ATR_Max × median(ATR)` to avoid dead/trending extremes.
* **ADX gate** (optional): requires ADX ≥ threshold (computed via `ta.dmi` for robustness).
* **Choppiness**: simple chop proxy must be ≤ your max.
* **Body vs ATR**: candle body must be at least *X × ATR* to avoid micro-bars.

### 4) Breakout filter (optional)

* **Donchian**: BUY requires close > prior Donchian high; SELL requires close < prior Donchian low.

---

## Risk-to-reward overlay

When a signal prints, the indicator:

* Drops an **ATR-based stop** at `StopMultiple × ATR`.
* Projects **1R/2R/3R** targets using that same stop distance.
You can turn these lines on/off and change R multiples.

The script also keeps a tiny in-memory **position state** (side/stop/TPs) to detect when price hits TP1/TP2/TP3 or the stop on later bars (used for Telegram notifications below).

---

## Telegram integration (optional)

If you enable it:

* On each BUY/SELL, the script calls `alert()` with a JSON payload containing `chat_id` and rich text (symbol, TF, preset, mode, score, session, components).
* On each **TP1/TP2/TP3** or **STOP** hit, it sends a similar alert with the level (and R when applicable).

**How to wire it in TradingView:**

1. Create an alert with **Condition = Any alert() function call**.
2. Turn on **Webhook URL** and paste:
`api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage`
3. Leave the message field empty (the script supplies JSON).
4. Test with a small text symbol to ensure the bot can post to your **CHAT_ID**.

*(Tip: Your bot must have permission to message that chat. If you get “message text is empty,” it usually means the alert has no payload—ensure you used “Any alert() function call” and webhook is enabled.)*

---

## What I fixed/optimized in the code

* **No more “inconsistent calculation” warnings**:
All `ta.crossover/ta.crossunder/ta.rising/ta.falling` are computed **unconditionally** first, stored as booleans, then referenced in conditions. That removes TradingView’s short-circuit warning while preserving behavior.
* **Typed Telegram helper**:
`build_tg_risk(string,string,float,float)` is typed and when no R applies we pass `float(na)` (explicit type) so Pine doesn’t complain.
* **Safer TP/SL detection**:
“Safe cross” helpers substitute sentinel levels when a TP/SL is `na`, so the cross check just returns false without extra guards.
* **MTF slope calculation** uses `request.security` with lookahead off; no repainting beyond normal bar close behavior.

---

## Key inputs (quick map)

**Trend**

* Baseline: EMA / HMA / Supertrend (+ lengths / factor).
**HTF Confirm**
* Use HTF EMA slope? TF (e.g., 60), Length (e.g., 200).
**Momentum**
* RSI len / buy/sell levels; MACD fast/slow/signal; Stoch len/K/D/OB/OS; enable/disable each.
**Volatility/Noise**
* ATR len, **ATR min/max × median(ATR)**, ADX length/smoothing/threshold, Choppiness length & max, Min body × ATR.
**Breakout**
* Require Donchian breakout? Donchian length.
**Quiet Mode**
* On = strict checklist; Off = trend + ≥1 momentum + ATR only.
**Risk Overlay**
* Stop ATR multiple; 1R/2R/3R multiples.
**Telegram**
* Enable, Chat ID (and set webhook URL in alert).

---

## How to use it (recommended workflow)

1. **Pick a preset**:

* *Balanced/Strict 5m London* → EUR/USD during London on 5-minute.
* *Forex Fast* → general FX intraday.
* *BTC Intraday* → looser RSI bounds, breakout off by default.

2. **Keep Quiet Mode ON** for cleaner signals.

3. **Choose Baseline** (EMA 200 is a good default).

4. **Confirm** you’re within your enabled session (or turn that guard off).

5. **Wait for a marker**. On a buy marker, the risk overlay shows the stop and 1R/2R/3R—use that to plan your trade.

6. **Set alerts** (buy/sell or “Any alert() function call”) if you want push/Telegram.

---

## Good to know

* The indicator **does not enter/exit trades**—it only signals and optionally pushes to Telegram.
* It’s **bar-close** logic by design (reduces whipsaw).
* HTF confirmation uses **non-repainting** higher-TF values with `lookahead_off`.
* You can tighten/loosen behavior quickly via **Quiet Mode** and **Min Momentum votes**.

---

## Where to tweak first if you want fewer/more signals

* **Min Momentum Signals** (1–3).
* **ATR Min/Max** window → widen to allow more conditions.
* **ADX threshold** → lower for more trades; raise for stronger trends only.
* **Body × ATR** → raise to skip tiny bars; lower if you want earlier entries.
* **Require Donchian Breakout** → off = earlier, on = stronger continuation.

---

Clause de non-responsabilité

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