OPEN-SOURCE SCRIPT
IQR Risk-to-Reward & Dynamic Targets [MantisAlgo]

IQR Risk-to-Reward & Dynamic Targets combines empirical quartile statistics with structural liquidity analysis to identify setups with favorable market-derived risk-to-reward. Instead of assigning arbitrary profit targets such as fixed 2× risk multiples, the indicator divides recent price action into Q1 (25th percentile), Q2 (Median), and Q3 (75th percentile), together with 1.5×IQR statistical outlier boundaries.
These statistical value zones are combined with opposing swing liquidity to calculate the available True Risk-to-Reward Ratio before a setup is displayed. Each qualified setup automatically projects Entry, Structural Stop Loss, Take Profit 1, Take Profit 2, and the resulting True R:R directly on the chart.

🟢 IQR FAIR VALUE STRUCTURE
The Interquartile Range represents the middle 50% of the recent price distribution:
IQR = Q3 − Q1
Q1 = 25th Percentile
Q2 = 50th Percentile (Median)
Q3 = 75th Percentile
When price remains primarily between Q1 and Q3, the market is trading inside its recent statistical value region and tends to rotate around the Median. When price extends beyond an outer quartile or IQR outlier region and subsequently reclaims the Median Wave, the indicator evaluates whether sufficient structural space exists for a move toward opposing liquidity.
This creates two broad market conditions:
• Equilibrium / Mean-Reversion — Price remains within the Q1–Q3 value region and rotates around the Median.
• Expansion / Reclaim — Price extends outside statistical value, then reclaims the Median with sufficient space toward opposing liquidity.
🟢 SIGNALS & TRUE R:R GATE
The core trigger follows a simple sweep-and-reclaim sequence.
For a Long setup, price must first sweep or touch the Lower IQR Band within the recent bars. The signal is then evaluated when price crosses back above the Median Wave and the reclaim candle closes above it.
For a Short setup, price must first sweep or touch the Upper IQR Band. The signal is evaluated when price subsequently crosses back below the Median Wave and the reclaim candle closes below it.
Long: Lower Band Sweep → Median Reclaim → Confirmed Close
Short: Upper Band Sweep → Median Reclaim → Confirmed Close
The reclaim candle becomes the Entry only when volume is sufficiently active, the signal cooldown has been satisfied, and the available True R:R meets or exceeds the selected minimum threshold.
True R:R = Target Distance / Invalidation Distance
Target Distance = |TP2 − Entry|
Invalidation Distance = |Entry − SL|
If True R:R is below the selected threshold, the setup is suppressed. If it meets or exceeds the threshold, the trigger triangle and complete Entry / SL / TP1 / TP2 structure are displayed.
🟢 DYNAMIC TARGET STRUCTURE
Each qualified setup contains four objective levels:
Entry — The exact closing price of the confirmed trigger candle.
Stop Loss — The structural extreme associated with the preceding sweep. For Long setups, the relevant sweep low is used; for Short setups, the relevant sweep high is used. A break beyond this level invalidates the setup structure.
Take Profit 1 — TP1 is based on the opposing quartile boundary: Q3 for Long setups and Q1 for Short setups. It represents the first statistical mean-reversion objective.
Take Profit 2 — TP2 is based on opposing structural liquidity: swing-high liquidity for Long setups and swing-low liquidity for Short setups. Because TP2 is derived from actual chart structure rather than a predetermined fixed multiple, the resulting True R:R changes naturally from setup to setup.

🟢 IQR OUTLIER FENCES
The indicator also calculates traditional Box Plot outlier boundaries:
Lower Fence = Q1 − (1.5 × IQR)
Upper Fence = Q3 + (1.5 × IQR)
These boundaries identify price observations that are unusually extended relative to the recent rolling distribution and provide additional context for statistical sweeps.
🟢 DYNAMIC MEDIAN WAVE
The central Median Wave is based on Q2 and smoothed using a two-pole SuperSmoother-style digital filter. Its purpose is to reduce short-term noise while remaining responsive to directional changes. The wave changes visual state according to its directional slope and acts as the primary reclaim reference for potential setups.
🟢 STATISTICAL BOX-PLOT PIVOTS
Confirmed structural swing pivots can display compact Box Plot brackets directly on the chart. Each bracket visualizes the local Q1 Lower Quartile, Q2 Median, Q3 Upper Quartile, and IQR outlier boundaries, making it easier to compare structural turning points with the surrounding statistical distribution.
🟢 STATISTICAL HUD
The optional top-right HUD provides a compact summary of the current statistical and structural state, allowing users to review the active distribution, directional context, and setup information without manually inspecting every plotted level.
🟢 SETTINGS
Quartile Sample Window — 34: Controls the rolling observation window used to calculate Q1, Q2, and Q3.
Outlier Whisker Multiplier — 1.5: Controls the IQR multiplier used to define the statistical outlier fences.
Structural Liquidity Lookback — 20: Controls the search window used to identify opposing swing liquidity for TP2.
Minimum True R:R Ratio — 2.0: Defines the minimum required target distance relative to structural risk. Higher values produce fewer but more selective setups.
Signal Cooldown — 6: Controls the minimum separation between consecutive signals.
Visual Settings: Show IQR Fair Value Cloud, Show Statistical Box-Plot Pins, Show Signal Trigger Triangles, Show Target Projection Rays, Max Historical Setups to Display, and Show Statistical HUD Dashboard.
🟢 INTERPRETATION
The indicator combines three elements: Statistical Location, Structural Invalidation, and Available Target Space. It does not assume that every statistical extreme will reverse or that every liquidity target will be reached.
Instead, it evaluates whether a confirmed statistical reclaim has enough remaining structural space relative to its invalidation risk to satisfy the selected True R:R requirement. The goal is to make signal timing, structural risk, and available market space directly visible on the chart rather than attaching arbitrary target multiples after a signal appears.
🟢 DISCLAIMER
This indicator is designed for technical analysis, quantitative research, and educational purposes only. It does not constitute financial advice. Historical statistical relationships do not guarantee future outcomes. Always apply appropriate risk management and position sizing.
These statistical value zones are combined with opposing swing liquidity to calculate the available True Risk-to-Reward Ratio before a setup is displayed. Each qualified setup automatically projects Entry, Structural Stop Loss, Take Profit 1, Take Profit 2, and the resulting True R:R directly on the chart.
🟢 IQR FAIR VALUE STRUCTURE
The Interquartile Range represents the middle 50% of the recent price distribution:
IQR = Q3 − Q1
Q1 = 25th Percentile
Q2 = 50th Percentile (Median)
Q3 = 75th Percentile
When price remains primarily between Q1 and Q3, the market is trading inside its recent statistical value region and tends to rotate around the Median. When price extends beyond an outer quartile or IQR outlier region and subsequently reclaims the Median Wave, the indicator evaluates whether sufficient structural space exists for a move toward opposing liquidity.
This creates two broad market conditions:
• Equilibrium / Mean-Reversion — Price remains within the Q1–Q3 value region and rotates around the Median.
• Expansion / Reclaim — Price extends outside statistical value, then reclaims the Median with sufficient space toward opposing liquidity.
🟢 SIGNALS & TRUE R:R GATE
The core trigger follows a simple sweep-and-reclaim sequence.
For a Long setup, price must first sweep or touch the Lower IQR Band within the recent bars. The signal is then evaluated when price crosses back above the Median Wave and the reclaim candle closes above it.
For a Short setup, price must first sweep or touch the Upper IQR Band. The signal is evaluated when price subsequently crosses back below the Median Wave and the reclaim candle closes below it.
Long: Lower Band Sweep → Median Reclaim → Confirmed Close
Short: Upper Band Sweep → Median Reclaim → Confirmed Close
The reclaim candle becomes the Entry only when volume is sufficiently active, the signal cooldown has been satisfied, and the available True R:R meets or exceeds the selected minimum threshold.
True R:R = Target Distance / Invalidation Distance
Target Distance = |TP2 − Entry|
Invalidation Distance = |Entry − SL|
If True R:R is below the selected threshold, the setup is suppressed. If it meets or exceeds the threshold, the trigger triangle and complete Entry / SL / TP1 / TP2 structure are displayed.
🟢 DYNAMIC TARGET STRUCTURE
Each qualified setup contains four objective levels:
Entry — The exact closing price of the confirmed trigger candle.
Stop Loss — The structural extreme associated with the preceding sweep. For Long setups, the relevant sweep low is used; for Short setups, the relevant sweep high is used. A break beyond this level invalidates the setup structure.
Take Profit 1 — TP1 is based on the opposing quartile boundary: Q3 for Long setups and Q1 for Short setups. It represents the first statistical mean-reversion objective.
Take Profit 2 — TP2 is based on opposing structural liquidity: swing-high liquidity for Long setups and swing-low liquidity for Short setups. Because TP2 is derived from actual chart structure rather than a predetermined fixed multiple, the resulting True R:R changes naturally from setup to setup.
🟢 IQR OUTLIER FENCES
The indicator also calculates traditional Box Plot outlier boundaries:
Lower Fence = Q1 − (1.5 × IQR)
Upper Fence = Q3 + (1.5 × IQR)
These boundaries identify price observations that are unusually extended relative to the recent rolling distribution and provide additional context for statistical sweeps.
🟢 DYNAMIC MEDIAN WAVE
The central Median Wave is based on Q2 and smoothed using a two-pole SuperSmoother-style digital filter. Its purpose is to reduce short-term noise while remaining responsive to directional changes. The wave changes visual state according to its directional slope and acts as the primary reclaim reference for potential setups.
🟢 STATISTICAL BOX-PLOT PIVOTS
Confirmed structural swing pivots can display compact Box Plot brackets directly on the chart. Each bracket visualizes the local Q1 Lower Quartile, Q2 Median, Q3 Upper Quartile, and IQR outlier boundaries, making it easier to compare structural turning points with the surrounding statistical distribution.
🟢 STATISTICAL HUD
The optional top-right HUD provides a compact summary of the current statistical and structural state, allowing users to review the active distribution, directional context, and setup information without manually inspecting every plotted level.
🟢 SETTINGS
Quartile Sample Window — 34: Controls the rolling observation window used to calculate Q1, Q2, and Q3.
Outlier Whisker Multiplier — 1.5: Controls the IQR multiplier used to define the statistical outlier fences.
Structural Liquidity Lookback — 20: Controls the search window used to identify opposing swing liquidity for TP2.
Minimum True R:R Ratio — 2.0: Defines the minimum required target distance relative to structural risk. Higher values produce fewer but more selective setups.
Signal Cooldown — 6: Controls the minimum separation between consecutive signals.
Visual Settings: Show IQR Fair Value Cloud, Show Statistical Box-Plot Pins, Show Signal Trigger Triangles, Show Target Projection Rays, Max Historical Setups to Display, and Show Statistical HUD Dashboard.
🟢 INTERPRETATION
The indicator combines three elements: Statistical Location, Structural Invalidation, and Available Target Space. It does not assume that every statistical extreme will reverse or that every liquidity target will be reached.
Instead, it evaluates whether a confirmed statistical reclaim has enough remaining structural space relative to its invalidation risk to satisfy the selected True R:R requirement. The goal is to make signal timing, structural risk, and available market space directly visible on the chart rather than attaching arbitrary target multiples after a signal appears.
🟢 DISCLAIMER
This indicator is designed for technical analysis, quantitative research, and educational purposes only. It does not constitute financial advice. Historical statistical relationships do not guarantee future outcomes. Always apply appropriate risk management and position sizing.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.