Monte Carlo simulation indicator

Monte Carlo simulation indicator

1) What this simulator does (and when to use it)

This indicator projects multiple possible future price paths from the latest bar, using the asset’s historical return statistics. It’s designed to answer: “Where could price plausibly go over the next N candles, given recent volatility and drift?”
It’s not a full trade backtester. Instead, it’s a path simulator: it draws NbSimu independent scenarios forward by CandleForward candles and returns the running maximum and minimum reached across all simulated paths.


2) How the engine works (high level)

  • At each bar, the script computes log-like simple returns over NbCandle bars:
    v = Close / Close[NbCandle] − 1.

  • Over a lookback of length p, it estimates:

    • Mean = Average[p](v) (drift)

    • StandardDev = STD[p](v) (volatility)

  • On the last bar only (IsLastBarUpdate), it generates NbSimu scenarios. Each scenario evolves in steps of NbCandle bars:

    1. Draw a uniform random x ∈ (0,1) with high Precision.

    2. Transform x into a normally distributed shock (NDRV) via a rational transform (a fast inverse-CDF approximation).

    3. Scale and shift: NDRV = Mean + NDRV * StandardDev.

    4. Update price multiplicatively: DestPrice = OriginPrice * (1 + NDRV).

    5. Draw a colored segment from the previous point to the new point.

  • While simulating, the script tracks the highest (mx) and lowest (mini) prices touched across all paths and returns them so you can plot bands if desired.


3) Parameters you can tune

Set these as indicator variables (they appear in the indicator’s settings):

  • NbSimu (integer): number of simulated paths to draw. More paths = smoother distribution insight, heavier workload.

  • CandleForward (integer): horizon length (in candles) for each scenario.

  • per (integer): lookback size used for drift/vol estimates.

    • If per = 0 or per > BarIndex, the script automatically uses all available history up to the last bar.

  • NbCandle (integer): step size for return sampling.

    • 1 means simulate each candle; 5 means jump in 5-bar blocks (useful to smooth noise).

Advanced internal:

  • Precision controls the granularity of the uniform RNG → smoother normal transform. The default 100000 is already fine.


4) How to set it up in ProRealTime (step-by-step)

  1. Create a new indicator in ProBuilder and paste the full code.

  2. Keep the code as-is; parameters are meant to be changed via the indicator settings.

  3. Apply the indicator to your chart.

  4. Adjust:

    • Start with NbSimu = 25–50, CandleForward = 50, per = 0 (full history), NbCandle = 1.

    • If it feels noisy, try NbCandle = 3–5.

    • For regime sensitivity (e.g., recent volatility only), set per to a fixed window like 250.


5) Reading the on-chart output

  • Colored path segments: each scenario is drawn in a random color, stepping forward from the last bar. These are possible (not predictions).

  • mx and mini (returned series): the running upper and lower envelopes reached by any scenario across all steps.

    • You can plot them as lines to visualize the aggregate uncertainty band.

  • Because simulation runs on the last bar, paths emanate from “now” and do not clutter historical sections.


6) Practical tips for realistic simulations

  • Match your sampling to your trading lens:

    • Intraday scaling? Keep NbCandle = 1.

    • Swing lens? Use NbCandle = 5–20 to reflect multi-bar steps.

  • Window length (per) matters:

    • per = 0 (full history) blends regimes and can under/overstate current risk.

    • A rolling window (e.g., 100–250 bars) adapts better to current volatility.

  • Horizon vs variance: increasing CandleForward compounds volatility; expect a wider fan even with the same StandardDev.

  • Drift realism: the mean return is typically small; most dispersion comes from StandardDev. If your asset trends strongly, a shorter per may capture it.

  • Reproducibility: the internal RNG is not seeded; each refresh will produce new paths. Save snapshots if you need to keep a particular simulation.


7) Performance notes & troubleshooting

  • Drawing load: many paths × long horizons = heavy rendering. If it lags:

    • Lower NbSimu, shorten CandleForward, or increase NbCandle.

  • No lines appear? Ensure you’re on the last bar and the indicator is set to update on the live bar (the simulation runs inside IsLastBarUpdate).

  • Flat bands: happens if StandardDev is ~0 (quiet market or too small per). Increase per or wait for more variability.

  • Spikes/overshoots: normal shocks are unbounded; in high volatility regimes, outliers are expected by construction.


8) Code placement reminder (what you may edit safely)

  • Adjust only the parameters at the top: NbSimu, CandleForward, per, NbCandle (and optionally Precision).

  • The simulation core (normal transform, loops, drawing, mx/mini tracking) should be left untouched to avoid breaking the math and rendering.

Share this

Risk disclosure:

No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.

ProRealTime ITF files and other attachments : How to import ITF files into ProRealTime platform?

PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials

avatar
Register or
Related users ' posts
Ciccarelli Franco Scusa e cosa devo cambiare? Grazie
Sofitech Bonjour Ivan, comment pourrais tu modifier ce code pour qu'un histogramme indiquant la créat...
geroniman Merci Ivan, cet indicateur est excellent. Comme les cours s'arretent souvent sur les 50% d'u...

Top