Risk Simulation Matrix [Monte Carlo]OVERVIEW
This indicator is a multi-dimensional stress-testing tool designed to map the mathematical "fragility" or "robustness" of a trading strategy.
This is NOT a Backtester
While standard backtests provide a static, historical result, the Risk Simulation Matrix constructs a grid of potential outcomes by simultaneously varying Win Rate and Reward-to-Risk (R) parameters. It addresses the fundamental need for sensitivity analysis: understanding how a system performs when its core execution metrics deviate from the ideal. This is not a predictor, but a tool for determining the safety margins of a trading edge within the realm of probability theory.
HOW IT WORKS
To explain the logic through an analogy, imagine telling the Pine Script compiler:
"I have a series of coins where the win probability starts at 10% and increases step-by-step to 60%. For every single one of these coins, I want to test a range of payout scenarios—starting from winning $1.0 per $1 risk and increasing up to $4.0. Now, for every possible intersection (such as the 10% win rate coin paying $1.5, then $2.0, then $2.5, and so on, repeating this for the 15%, 20%, and all other win rates) flip that specific coin 100 times. Repeat each of these unique 100-flip sequences 1000 times. Finally, gather all those results and report the statistical distribution for every single coordinate on this grid."
MECHANICS
The engine combines deterministic randomness with iterative matrix calculations to ensure statistical reliability:
• LCG Implementation (f_random): The script utilizes a Linear Congruential Generator based on Knuth and Park-Miller parameters. This ensures that for a given "Scenario ID" (Seed), the generated trade sequences are identical and repeatable across sessions.
• Sensitivity Grid: The matrix iterates through user-defined ranges for Win Rate and R-multiples. Each cell acts as an independent Monte Carlo engine, processing thousands of simulated trade paths.
• Percentile Interpolation: Rather than relying on simple arithmetic means, the tool uses array.percentile_linear_interpolation. This allows users to view the "Median" (P50) outcome or focus on "Worst-Case" (P0) tails to plan for the most adverse sequences.
• Dynamic Friction: Every trade simulation accounts for compounding equity, randomized stop-loss distances between a defined Min/Max range, and two-way commission fees.
HOW TO USE
• Matrix Dimensions: Define your Min, Max, and Step for both Win Rate and Reward/Risk. To optimize performance, the matrix is limited to 81 cells.
• Setup & Commission: Input your initial capital and the percentage of equity risked per trade (Compounding). Ensure the commission rate reflects your broker’s actual fee structure.
• Stops: Set the range for your Stop-Loss distance (%). This parameter is used to calculate precise position sizing and commission impacts.
• Interpolation (P%): Adjust this value to set the "strictness" of the matrix. 50% represents the median expectation, while 10% reveals how the strategy holds up during significant losing streaks.
OUTPUTS
• Heat Map Table: Cells are four-color-coded based on the Total PnL (%) achieved at the selected percentile.
• Statistical Tooltips: Hovering over any cell reveals a comprehensive performance profile, including Profit Factor, Payoff Ratio, Max Drawdown, Max Loss Streak, and Expectancy in R-multiples.
• Expectancy (Calculated in R): A critical metric provided is the Expectancy per trade, expressed in R-multiples. This reflects the average unit of risk (1R) you can expect to gain or lose per trade, providing a pure measure of the system's mathematical edge regardless of account size.)
For single scenarios and more comprehensive backtest metrics, you can also check out the following indicator: Risk Simulation (single version)
DISCLAIMER
This tool is a mathematical modeling environment and does not account for market liquidity, slippage, or the psychological pressures of live execution. It is designed to test the mathematical viability of a system's parameters; it is not a guarantee of future performance. Users should apply these simulations as a supplementary risk management layer, not as a replacement for comprehensive strategy testing.
Indicador Pine Script®




















