Indicator · Pine Script v6 · On the chart

Martingale Lab

by jayadevrana

Martingale Lab is an educational indicator that tests position sizing models, including flat stake, martingale, and anti-martingale, on simulated trade results from an EMA crossover strategy.

View on GitHub

GitHub stars1on the repository
Pine versionv6current
Lines of code174
Settings8inputs
LicenceMPL-2.0
Last update4 days ago26 Oct 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

174 lines of Pine Script v6 from scripts/martingale-truth.pine.txt, published under MPL-2.0. Keep the author's credit and licence lines when you reuse it.

martingale-truth.pine.txt
//@version=6
indicator("Martingale Lab", overlay = true)

// 1. Settings
fastLen  = input.int(20, "Fast average")
slowLen  = input.int(50, "Slow average")
atrMult  = input.float(2.0, "Stop = ATR x", step = 0.5)
rr       = input.float(2.0, "Target = R x", step = 0.5)
baseRisk = input.float(1.0, "Base risk per trade (%)", step = 0.25) / 100
maxSteps = input.int(6, "How many doublings a rule may take", minval = 1)
sims     = input.int(300, "Alternative orderings to test", minval = 50, maxval = 800)
seed     = input.int(11, "Random seed")

// 2. The strategy. It is ordinary on purpose: the sizing rule is the experiment.
// ta.* calls stay at the top level so they run on every bar.
atr  = ta.atr(14)
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
long = ta.crossover(fast, slow)
plot(fast, "Fast", color.aqua)
plot(slow, "Slow", color.orange)

// 3. Record every trade as an R multiple, keeping the books by hand.
// One open trade at a time: a loser is -1 R, a winner is +rr R, so the
// history is a clean list of R values with no engine assumptions in it.
var float entry = na
var float stop  = na
var float targ  = na
var array<float> rs = array.new<float>()
opened  = false
hitStop = false
hitTarg = false
if na(entry)
    if long and atr > 0
        entry := close
        stop  := close - atrMult * atr
        targ  := close + atrMult * atr * rr
        opened := true
else
    if low <= stop
        array.push(rs, -1.0)
        entry := na
        hitStop := true
    else if high >= targ
        array.push(rs, rr)
        entry := na
        hitTarg := true
plotshape(opened, "Entry", shape.triangleup, location.belowbar, color.teal)
plotshape(hitTarg, "Target", shape.triangledown, location.abovebar, color.green)
plotshape(hitStop, "Stop", shape.xcross, location.abovebar, color.red)

// 4. Random numbers, so we can reorder the same trades (state in a one-slot array)
var array<int> rngBox = array.new_int(1, seed)
nextRand() =>
    st = (1103515245 * array.get(rngBox, 0) + 12345) % 2147483648
    array.set(rngBox, 0, st)
    math.abs(st) / 2147483648.0

// 5. THE EXPERIMENT. One function, three rules, identical trades.
// rule 0 = flat, 1 = martingale (double after a loss),
// 2 = anti-martingale (double after a win).
// Returns where the account finished, its worst drawdown, and whether it was ruined.
replay(src, rule) =>
    eq   = 1.0
    peak = 1.0
    dd   = 0.0
    mult = 1.0
    ruined = false
    for i = 0 to array.size(src) - 1
        r = array.get(src, i)
        stake = math.min(baseRisk * mult, 1.0)
        eq := eq * (1 + stake * r)
        peak := math.max(peak, eq)
        dd := math.max(dd, 1 - eq / peak)
        if eq <= 0.2 and not ruined
            ruined := true
        mult := rule == 0 ? 1.0 :
          rule == 1 ? (r < 0 ? math.min(mult * 2, math.pow(2, maxSteps)) : 1.0) :
          (r > 0 ? math.min(mult * 2, math.pow(2, maxSteps)) : 1.0)
    [eq, dd, ruined]

// 6. A shuffled copy of the trade list (Fisher-Yates)
shuffled(src) =>
    work = array.copy(src)
    for i = array.size(work) - 1 to 1
        j = math.floor(nextRand() * (i + 1))
        tmp = array.get(work, i)
        array.set(work, i, array.get(work, j))
        array.set(work, j, tmp)
    work

// 7. The two tables
var table t = table.new(position.bottom_left, 4, 5, border_width = 1)
head(c, s) =>
    table.cell(t, c, 0, s, text_color = color.white, text_size = size.normal,
      bgcolor = color.new(color.blue, 20))
var table h = table.new(position.middle_right, 3, 5, border_width = 1)
blocks(v, pk) =>
    w = int(math.round(22.0 * v / math.max(pk, 0.0001)))
    s = ""
    // a Pine for-loop with a start above its end counts DOWNWARDS,
    // so an empty bin would print two blocks instead of none
    if w > 0
        for i = 1 to w
            s += "█"
    s

// 8. Run all three rules on the real order, then on hundreds of reorderings
if barstate.islast and array.size(rs) > 20
    n = array.size(rs)
    // the longest losing streak in the real history: this is what kills a martingale
    streak = 0
    longest = 0
    for i = 0 to n - 1
        streak := array.get(rs, i) < 0 ? streak + 1 : 0
        longest := math.max(longest, streak)
    ruin = array.new_float(3, 0.0)
    for s = 0 to sims - 1
        order = shuffled(rs)
        for rule = 0 to 2
            [_e, _d, wiped] = replay(order, rule)
            if wiped
                array.set(ruin, rule, array.get(ruin, rule) + 1)

    // 9. The comparison table: same trades, three rules
    head(0, "SAME " + str.tostring(n) + " TRADES")
    head(1, "ends at")
    head(2, "worst drawdown")
    head(3, "ruined in " + str.tostring(sims) + " orders")
    names = array.from("flat size", "martingale", "anti-martingale")
    for rule = 0 to 2
        [e, d, _w] = replay(rs, rule)
        pctRuin = 100.0 * array.get(ruin, rule) / sims
        table.cell(t, 0, rule + 1, array.get(names, rule), text_color = color.white,
          bgcolor = color.new(color.gray, 20), text_halign = text.align_left,
          text_size = size.normal)
        table.cell(t, 1, rule + 1, str.tostring(e, "#.00") + "x",
          text_color = color.white, text_size = size.normal,
          bgcolor = e < 1 ? color.new(color.red, 25) : color.new(color.teal, 25))
        table.cell(t, 2, rule + 1, str.tostring(100 * d, "#.0") + "%",
          text_color = color.white, text_size = size.normal,
          bgcolor = d > 0.5 ? color.new(color.red, 25) : color.new(color.gray, 25))
        table.cell(t, 3, rule + 1, str.tostring(pctRuin, "#.0") + "%",
          text_color = color.white, text_size = size.normal,
          bgcolor = pctRuin > 0 ? color.new(color.red, 20) :
            color.new(color.green, 30))
    table.cell(t, 0, 4, "longest losing streak", text_color = color.white,
      bgcolor = color.new(color.gray, 20), text_halign = text.align_left)
    table.cell(t, 1, 4, str.tostring(longest) + " in a row",
      text_color = color.white,
      bgcolor = color.new(color.orange, 25))
    table.cell(t, 2, 4, "a martingale needs", text_color = color.white,
      bgcolor = color.new(color.gray, 20))
    table.cell(t, 3, 4, str.tostring(math.pow(2, longest - 1), "#") + "x stake",
      text_color = color.white, bgcolor = color.new(color.red, 20))

    // 10. Risk of ruin, drawn as a bar so the difference is impossible to miss
    table.cell(h, 0, 0, "rule", text_color = color.white, text_size = size.small,
      bgcolor = color.new(color.blue, 20))
    table.cell(h, 1, 0, "RISK OF RUIN", text_color = color.white,
      text_size = size.small,
      bgcolor = color.new(color.blue, 20))
    table.cell(h, 2, 0, "", bgcolor = color.new(color.blue, 20))
    for rule = 0 to 2
        p = 100.0 * array.get(ruin, rule) / sims
        table.cell(h, 0, rule + 1, array.get(names, rule), text_color = color.white,
          bgcolor = color.new(color.gray, 30), text_size = size.tiny,
          text_halign = text.align_left)
        table.cell(h, 1, rule + 1, blocks(p, 100), text_color = color.red,
          bgcolor = color.new(color.black, 0), text_size = size.tiny,
          text_halign = text.align_left)
        table.cell(h, 2, rule + 1, str.tostring(p, "#.0") + "%",
          text_color = color.white,
          bgcolor = color.new(color.black, 0), text_size = size.tiny)

Overview

Martingale Lab logs trade returns in R-multiples using ATR-based stop losses and take profit targets, then runs Monte Carlo simulations by shuffling trade sequences. It plots moving averages and entry or exit shapes on the price chart while displaying summary tables for final equity, max drawdown, and ruin risk.

Details

Published by

jayadevrana on GitHub

Draws

On the price chart

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.

Topics

algorithmic-tradingbacktestingema-crossoverpine-scriptpine-script-v6pinescriptrsitechnical-analysistrading-automationtrading-indicators

Settings

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

SettingTypeDefaultVariable
Fast averageint20fastLen
Slow averageint50slowLen
Stop = ATR xfloat2.0atrMult
Target = R xfloat2.0rr
Base risk per trade (%)float1.0baseRisk
How many doublings a rule may takeint6maxSteps
Alternative orderings to testint300sims
Random seedint11seed
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

This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup
257 stars

This indicator plots a 200-period weighted moving average (WMA) on the main chart and monitors price deviations across multiple timeframes. It retrieves daily, 15-minute, 5-minute, and 2-minute price data to track distance from the 200 WMA relative to key sess

Indicator v5 Moving averagesSessions & time Repaints
Updated 7 weeks ago · None stated GitHub repository of Donald SPX - Options Trading
228 stars

TradersPost Trading Checklist is an overlay indicator that evaluates a suite of popular technical conditions for long and short trade setups. It checks session-anchored VWAP, fast and slow EMA alignments and crosses, 200 SMA, RSI overbought or oversold conditi

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of TradersPost Trading Checklist

8zz反指標v2

by hansai-art
121 stars

The 8zz Contrarian Indicator plots reverse sentiment trading signals directly on the price chart based on tracked retail post activity. It generates bullish signals during retail panic and bearish signals during market euphoria. The indicator features adjustab

Indicator v6 Moving averagesOscillators & momentum
Updated 3 months ago · None stated GitHub repository of 8zz反指標v2
92 stars

This indicator plots five moving averages of a chosen source on the price chart, defaulting to EMA lengths 10, 20, 30, 40 and 50. The user can switch the average type to SMA, WMA or HMA and recolor each line. Wide translucent glows sit under the lines, and sof

Indicator v5 TrendMoving averages
Updated 2 months ago · Custom licence GitHub repository of Moon Dev Rainbow MA Ribbon 10-50

Price vs MA

by be-thomas
65 stars

This indicator builds a table of simple moving averages and places it on the price chart. It calculates a user-chosen number of SMAs whose periods start at a base length and increase by a fixed step. On the last bar it lists each period and its SMA value, colo

Indicator v4 Moving averagesScreeners & dashboards
Updated 6 days ago · GPL-3.0 GitHub repository of Price vs MA

More Moving averages scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

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

Compare brokers