All free tools

Trading metric

Risk of Ruin Calculator

How often does a given risk level breach your loss limit before the run ends? There is no clean formula for this once the payoff ratio is arbitrary, so this simulates thousands of sequences and counts. The seed is fixed, so the same inputs always give the same answer.

Risk of ruin
0.00%
Share of 5,000 simulated runs that lost 50.00% before 200 trades elapsed.
Edge per trade
0.350R
Positive expectancy in units of risk, before costs.
DetailValue
Chance of surviving the run100.00%
Expectancy per trade (in units of risk)0.350
Average end balance of surviving runs4.04x
Trades per simulated run200
Runs simulated5,000

What risk of ruin measures

Risk of ruin is the probability that a sequence of trades loses enough to hit a limit you have defined before the sequence ends. The word ruin sounds absolute, and in practice the threshold is a choice: for some people it means losing everything, and for most it means losing enough that they would stop. Which is why the threshold is an input here rather than fixed at a hundred percent.

The reason this is simulated rather than solved is that the classic closed-form results assume simplifications that do not hold here, such as every win and loss being the same size. Once the payoff ratio can be any value and the stake is a percentage of a balance that moves, the arithmetic stops being tractable and the honest approach is to generate many sequences and count how many breach the limit. That is exactly what this does, with a fixed random seed so the answer is stable rather than shifting each time you look at it.

Each simulated trade risks a fixed percentage of the current balance, which means the stake shrinks as the balance falls. This matters: fixed-fractional staking is self-limiting in a way that fixed-size staking is not, because each successive loss is smaller in absolute terms. It is also why the ruin threshold has to be less than a hundred percent for the question to make sense, since a percentage of a positive balance never quite reaches zero.

Reading the result honestly

The single most important thing to check is the edge, shown alongside the main figure. If the expected result per trade is zero or negative, the risk-of-ruin number is not describing a risk you can manage by sizing. Extend the number of trades and it approaches certainty. Halving the risk per trade roughly doubles how long it takes and does not change where it ends up. Sizing controls the pace of a negative-expectancy outcome, not the outcome.

When the edge is positive, the number is genuinely informative, and the thing it usually reveals is how sharply it responds to the risk per trade. The relationship is not linear. Going from one percent to two percent per trade typically increases the ruin probability by much more than double, because each larger loss leaves a smaller base from which to recover, and the asymmetry compounds. Try it: change only the risk figure and watch.

The inputs deserve scepticism too. A win probability and payoff ratio taken from a backtest are sample statistics, and a sample of thirty or fifty trades estimates them loosely. If the real win rate is five points lower than your sample suggested, the ruin figure changes considerably. Running the calculation two or three times with slightly pessimistic inputs is more informative than running it once with the numbers you hope are right. Our trade expectancy calculator and Kelly criterion calculator work with the same two inputs from different angles.

Finally, the framing. This is a calculator, and choosing a risk level or a ruin threshold is entirely your decision. Nothing here recommends any figure, and a low simulated ruin probability is not a statement that an approach is safe or that any particular outcome is likely.

What are TradingView and Pine Script?

TradingView is one of the most widely used charting and market-analysis platforms, where traders and analysts study price movement across stocks, crypto, forex, and futures on interactive charts. Pine Script is TradingView's own lightweight programming language, created so anyone can build custom tools that run directly on those charts.

People use Pine Script to build four main kinds of tools. Indicators calculate and plot values on the chart, exactly like the calculation above, but recomputed automatically on every bar. Strategies add explicit entry and exit rules and can be backtested against historical data in TradingView's Strategy Tester to see how they would have behaved. Screeners scan many symbols at once for conditions you define. Alerts notify you the moment a condition you specified occurs, so you do not have to watch the screen.

The value is precision and automation. Instead of eyeballing a chart, you describe exactly what you want measured, visualized, or notified about, and TradingView runs it consistently across any market and timeframe. That is why traders, analysts, and developers write Pine Script: it turns a manual charting idea into a repeatable tool. These tools are for tracking, visualizing, and testing market ideas; they do not tell you what to trade, and that decision always remains yours.

Writing that code by hand means learning Pine Script's syntax, its type system, and the exact names of hundreds of built-in functions. It is a real programming language, and small mistakes stop a script from compiling in the Pine Editor.

Turn this into Pine Script

This calculator needs three numbers, and a coded strategy in TradingView reports all of them. The Strategy Tester shows the win rate and the average win and loss directly, and you can also read them in Pine Script through strategy.wintrades, strategy.grossprofit, and their counterparts, as the code above does.

There is a caveat worth carrying across. Those figures come from however many trades the backtest happened to produce, on one symbol over one period. They are a sample, and feeding a sample statistic into a simulation does not make it more certain, it just propagates the uncertainty into a more confident looking output. Treat the result as a sensitivity check on your sizing rather than a measurement. Our guide to how TradingView backtesting works covers what those numbers do and do not represent.

Pine Script v6
//@version=6
strategy("Reading the numbers this needs", overlay = true)

// ... your entry and exit rules go here ...

// The three inputs the simulation wants are all reported by the Strategy
// Tester, and you can read them directly in code as well.
winProb    = strategy.closedtrades > 0 ? strategy.wintrades / strategy.closedtrades * 100 : na
avgWin     = strategy.wintrades  > 0 ? strategy.grossprofit / strategy.wintrades  : na
avgLoss    = strategy.losstrades > 0 ? strategy.grossloss   / strategy.losstrades : na
payoffRatio = na(avgLoss) or avgLoss == 0 ? na : avgWin / avgLoss

plot(winProb,     "Win rate %")
plot(payoffRatio, "Payoff ratio")

// Worth remembering: these are sample statistics from however many trades
// the backtest produced. A payoff ratio from 30 trades is a rough estimate,
// not a property of the strategy.

PineScripter is an AI built specifically for Pine Script. You describe what you want in plain English and it writes TradingView-ready v6 code. Because it is specialized on the Pine Script language and its exact function signatures, it tends to produce code that compiles far more reliably than general-purpose models like ChatGPT, which often invent functions that do not exist in Pine Script.

Related free calculators

From the blog

PineScripter is an AI developer tool that helps you write Pine Script code. It is not a financial advisor and will never offer financial, investment, or trading advice. Everything on this page, including the calculator and the explanations, is provided purely for educational and informational purposes. Any decision about how to interpret an indicator or trade a market is entirely your own. See our full disclaimer for more.