Indicator · Pine Script v5 · On the chart

Monte Carlo (Polyline Traceback) [Kioseff Trading]

by regalouisei· code credited to KioseffTrading

This indicator runs Monte Carlo price-path simulations from recent logarithmic returns and draws the resulting forecast paths as polylines on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code237
Settings9inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago25 Feb 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

237 lines of Pine Script v5 from pinescript/editors_picks/monte-carlo-polyline-traceback-kioseff-trading.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

monte-carlo-polyline-traceback-kioseff-trading.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("Monte Carlo (Polyline Traceback) [Kioseff Trading]", max_lines_count = 500, overlay = true, max_labels_count = 500, max_boxes_count = 500, max_polylines_count = 100)

seed          = input.int      (defval = 0, title = "Set Seed", minval = 0)
distT         = input.string   (defval = "Normal", title = "Type", options = ["Normal", "Bootstrap"])
sims          = input.int      (defval = 1000,    title = "Simulations", minval = 1, step = 100)
forx          = input.int      (defval = 100,     title = "Forecast", maxval = 498, step = 5) + 1
only          = input.bool     (defval = false,  title = "Show Best Case / Worst Case Only")
back          = input.int      (defval = 200,    title = "Historical Data Points", minval = 5, step = 10)
lineS         = input.string   (defval = "Solid", title = "Line Style", options= ["Solid", "Dotted", "Dashed"], group = "Line Style")
showScale     = input.bool     (defval = false, title = "Show Scale")
forceForecast = input.bool     (defval = false, title = "Force Script Load [Cuts Off Some Simulations]")

finS = switch lineS

    "Dotted" => line.style_dotted
    "Solid"  => line.style_solid
    "Dashed" => line.style_dashed

var ret    = array.new_float(), endPoints = array.new_float()

Gauss(z, avg, std) => 
    
    avg + std * z

method boot(array <float> id) => 


    id.get(int(math.random(0, id.size(), seed)))

distribution() => 

    avg    = ret.avg(), std = ret.stdev(false), size = ret.size()
    r1     = math.random(0.01, 0.99, seed)
    r2     = math.random(0.01, 0.99, seed)  
    result = math.sqrt  (-2.0 * math.log(r1)) * math.cos(2.0 * math.pi * r2)

    switch distT

        "Normal"   => Gauss(result, avg, std)
        =>            ret.boot()


method num(float id, val) => 

    id * math.exp(val)

method dete (matrix <float> id, val, i, x, int col, matrix<color> id2, color val2) => 

    if i < id.rows()

        id .set(i, col, val)
        id2.set(i, col, val2)


atr = ta.atr(14)

method mult(float id, up = 1.002, dn = .998) =>
    
    id - atr

if last_bar_index - bar_index <= back  

    ret.push(math.log(close / close[1]))

method iSwitch(matrix <float> id, lastClos, i, x, colData, col, matrix <float> minMax) => 

    switch only

        false => id.dete(lastClos, i, x, x, colData, col) 
        
        =>       minMax.set(0, x, math.min(nz(minMax.get(0, x), 1e8 ), lastClos)), 
                 minMax.set(1, x, math.max(nz(minMax.get(1, x), -1e8), lastClos))


method endTrim (array <box> histBox, array<float> val, array<float> freq, array<label> histLabs) => 

    if freq.size() > 0 and val.size() == freq.size() and histLabs.size() == freq.size() and histBox.size() == freq.size()
        for i = 0 to freq.size() - 1
            switch val.get(i) == 9e9
                true => histBox.get(i).delete(), histLabs.get(i).delete()
                =>      break

        for i = freq.size() - 1 to 0
            switch val.get(i) == 9e9
                true => histBox.get(i).delete(), histLabs.get(i).delete()
                =>      break
// 

method returnCheck(array<float> id, array<string> id2) => 

    if id2.size() > 0 
        for i = 0 to id2.size() - 1
            if not na(str.tonumber(id2.get(i)))
                id.push(str.tonumber(id2.get(i)))

keyValues      = map.new<string, float>()
bestWorst      = matrix.new<float>(forx, 2)
bestWorst.set(0, 0, close)
bestWorst.set(0, 1, close)

loopBreak(start, end) => 

    totalPoints = sims * forx 
    pointsPL    = math.ceil(totalPoints / 100) 
    log.info(str.tostring(pointsPL))
    sum  = close
    
    points = array.new<chart.point>()
    created = false 

    res = 0.


    for i = start to end

        lastClose = sum
        lastCloseArr = array.new<float>()

        col = color.rgb(math.random(0, 255), math.random(0, 255), math.random(0, 255))

        for x = 1 to forx - 1
 
            res       := distribution()
            lastClose := lastClose.num(res)

            lastCloseArr.push(lastClose)
            points.push(chart.point.from_index(bar_index + x - 1, lastClose))

            keyValues.put("Max", math.max(nz(keyValues.get("Max")), lastClose))
            keyValues.put("Min", math.min(nz(keyValues.get("Min"), 20e20), lastClose))

            if only 

                bestWorst.set(x, 0, math.max(nz(bestWorst.get(x, 0)), lastClose))
                bestWorst.set(x, 1, math.min(nz(bestWorst.get(x, 1), 20e20), lastClose))

            else 

                if x == forx - 1

                    endPoints.push(lastClose)

                    for z = lastCloseArr.size() - 1 to 0

                        points.push(chart.point.from_index(bar_index + z, lastCloseArr.get(z)))

                    if points.size() >= (forceForecast ? 1000 : pointsPL)

                        created := true 
                        polyline.new(points, line_color = col, line_style = finS)
                        points.clear()


    if not created and not only
        polyline.new(points, line_color = color.rgb(math.random(0, 255), math.random(0, 255), math.random(0, 255)), line_style = finS)


if barstate.islastconfirmedhistory 

    div = math.floor(sims / 20) 

    loopBreak(0, div)
    loopBreak(div + 1, div * 2)
    loopBreak(div * 2 + 1, div * 3)
    loopBreak(div * 3 + 1, div * 4)
    loopBreak(div * 4 + 1, div * 5)
    loopBreak(div * 5 + 1, div * 6)
    loopBreak(div * 6 + 1, div * 7)
    loopBreak(div * 7 + 1, div * 8)
    loopBreak(div * 8 + 1, div * 9)
    loopBreak(div * 9 + 1, div * 10)
    loopBreak(div * 10 + 1, div * 11)
    loopBreak(div * 11 + 1, div * 12)
    loopBreak(div * 12 + 1, div * 13)
    loopBreak(div * 13 + 1, div * 14)
    loopBreak(div * 14 + 1, div * 15)
    loopBreak(div * 15 + 1, div * 16)
    loopBreak(div * 16 + 1, div * 17)
    loopBreak(div * 17 + 1, div * 18)
    loopBreak(div * 18 + 1, div * 19)
    loopBreak(div * 19 + 1, sims)

    if only 

        bestWorstPoints = matrix.new<chart.point>(0, 2)
        
        for i = 0 to bestWorst.rows() - 1

            bestWorstPoints.add_row(

                 bestWorstPoints.rows(), 

                     array.from( chart.point.from_index(bar_index + i, bestWorst.get(i, 0)), 
                                 chart.point.from_index(bar_index + i, bestWorst.get(i, 1))
                     )
             )

        polyline.new(bestWorstPoints.col(0), line_color = #74ffbc, line_style = finS)
        polyline.new(bestWorstPoints.col(1), line_color = color.rgb(255, 116, 116), line_style = finS)

    if showScale

        getMin = keyValues.get("Min") - atr

        divScale = (keyValues.get("Max") - getMin) / 10

        line.new(bar_index + forx + 5, keyValues.get("Max"), bar_index + forx + 5, getMin, color = chart.fg_color)
        line.new(bar_index - 1, getMin, bar_index + forx + 5,  getMin, color = chart.fg_color)

        for i = 0 to 10

            calc = getMin + (divScale * i)

            label.new(bar_index + forx + 5, calc, str.tostring(calc, format.mintick), 
                         textcolor = chart.fg_color, 
                         color     = #00000000, 
                         size      = size.small, 
                         style     = label.style_label_left
                         )


        for i = 0 to forx - 2
            label.new(bar_index + i, getMin, str.tostring(i), textcolor = chart.fg_color, 
                          color = #00000000, 
                          style = label.style_label_up, 
                          size = size.auto
                          )

    tablePerformance = table.new(position.bottom_right, 99, 99,  bgcolor =  #20222C, border_color = #363843, 
              frame_color = #363843, border_width = 1, frame_width = 1)

    tablePerformance.cell(0, 0, str.tostring(sims) + " Simulations", text_color = color.white)
    tablePerformance.cell(1, 0, str.tostring(forx - 1) + " Bar Forecast", text_color = color.white)

Overview

Monte Carlo (Polyline Traceback) [Kioseff Trading] samples either a normal distribution or a bootstrap of stored returns, controlled by a seed, simulation count, lookback, and forecast length. Users can show every path or only the best and worst cases, and can change line style or display a scale. A load option shortens some simulations so the script can finish.

Details

Published by

regalouisei on GitHub

Code credit

KioseffTrading, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Set Seedint0seed
TypestringNormaldistT
Simulationsint1000sims
Forecastint100forx
Show Best Case / Worst Case Onlyboolfalseonly
Historical Data Pointsint200back
Line StylestringSolidlineS
Show ScaleboolfalseshowScale
Force Script Load [Cuts Off Some Simulations]boolfalseforceForecast
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

Chandelier Exit

by everget
948 stars

This indicator plots an ATR-based Chandelier Exit trailing stop on the price chart. It tracks a long stop under recent highs and a short stop above recent lows, then switches direction when price crosses the opposite stop. Buy and sell labels mark those flips.

Indicator v6 Volatility & bandsTrend Repaints
Updated 10 days ago · GPL-3.0 GitHub repository of Chandelier Exit

ICT Liquidity Void Fill

by ArunKBhaskar
575 stars

This overlay indicator marks ICT liquidity void fills on the price chart and can show long and short signals. It places a Fibonacci level inside a displacement candle, defaulting to the midpoint, and draws lines in a chosen style. Optional filters limit the fi

Indicator v6 SMC / ICT / price actionSupport & resistance
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Liquidity Void Fill

This indicator plots trade entry and exit signals on the price chart by combining Bollinger Bands with optional filter conditions. It calculates standard Bollinger Bands along with RSI, Aroon, and moving average filters. Reversal signals trigger when price bre

Indicator v4 Volatility & bandsOscillators & momentum
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner IND [Alorse]
318 stars

This indicator calculates an ATR trailing stop line derived from the highest high minus an average true range multiple over a specified lookback period. It plots the trailing stop line directly on the price chart and colors price bars based on whether the clos

Indicator v5 TrendVolatility & bands
Updated 2 months ago · MIT GitHub repository of ATR Trailing Stoploss

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon

Algo AVT

by ricardosantos79
261 stars

This indicator calculates an outlier-filtered moving average based on statistical filtering concepts. It stores close prices in a rolling array over a user-defined length, calculates the mean and standard deviation, and excludes values falling within a specifi

Indicator v4 Moving averagesVolatility & bands
Updated 4 years ago · None stated GitHub repository of Algo AVT

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers