Late at night, you open the trade history again. After all those days of work, one large loss holds your attention. Before closing the screen, you want to look once more at the limits you chose.
The calculator runs in your browser, free, with zero network requests: https://zero-trace-ship-kit.zeus-lab-global.workers.dev/quant/?utm_source=devto
Take an account with rules such as "lose no more than 5% in a day, no more than 10% in total, done when profit reaches 8%". With the same win rate and payoff ratio, changing only the risk per trade reorders which rule you hit first. This post implements a small Monte Carlo that counts it in plain JavaScript, then writes the same model again in Python and uses agreement between the two as the check.
Everything here is arithmetic on numbers you enter. It is not a recommendation to trade, not a forecast and not investment advice, and it names no firm or product.
The model
- A trade is
+payoff × riskwith probabilityp, otherwise−risk. Trades are independent. - Risk is a constant share of the start balance. The balance starts at 100.
-
ntrades a day forDdays. - Rules are checked after every trade:
- maximum loss: balance at or below
100 × (1 − M)(from the peak when trailing) - daily loss limit: fall from the day's opening balance of at least
opening × d - profit target: balance at or above
100 + T
- maximum loss: balance at or below
- The first one to happen stops the path. The balance stays at its value from then on, and the maximum drawdown is measured up to that moment. The balance cannot go below 0.
- A trade that reaches the daily limit and the maximum loss together is counted separately as "both".
Real trading has variable outcomes, streaks, fees and slippage. The model has none of them.
Implementation (JavaScript, no dependencies)
The generator is the seedable mulberry32. Math.random() cannot be seeded, so a result could not be reproduced.
function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;
let t=Math.imul(a^a>>>15,1|a);t=t+Math.imul(t^t>>>7,61|t)^t;
return((t^t>>>14)>>>0)/4294967296}}
function simulate(p){
const rnd=mulberry32(p.seed),win=p.win/100,gain=p.risk*p.rr,loss=p.risk;
let pass=0,bDaily=0,bMax=0,bBoth=0;
for(let r=0;r<p.runs;r++){
let bal=100,peak=100,outcome=0;
for(let d=0;d<p.days&&outcome===0;d++){
const dayStart=bal;
for(let t=0;t<p.tpd&&outcome===0;t++){
bal+=rnd()<win?gain:-loss;if(bal<0)bal=0;
if(bal>peak)peak=bal;
const hitMax=bal<=(p.trail?peak:100)*(1-p.maxl/100);
const hitDaily=dayStart-bal>=dayStart*p.daily/100;
if(hitMax&&hitDaily)outcome=4; // same trade reaches both
else if(hitMax)outcome=3;
else if(hitDaily)outcome=2;
else if(bal>=100+p.target)outcome=1;
}
} // the path stops at the first event
if(outcome===1)pass++;else if(outcome===2)bDaily++;else if(outcome===3)bMax++;else if(outcome===4)bBoth++;
}
return{pass:pass/p.runs*100,daily:bDaily/p.runs*100,max:bMax/p.runs*100,both:bBoth/p.runs*100};
}
Results (calculated values)
Win rate 45%, payoff 1.5, 3 trades a day, 30 days, daily limit 5%, maximum loss 10% from the start balance, target 8%, 5,000 paths, seed 1. Only the risk per trade changes.
| Risk per trade | Target first | Daily limit first | Maximum loss first | Both at once | None | Median max drawdown |
|---|---|---|---|---|---|---|
| 0.5% | 46.8% | 0.0% | 1.3% | 0.0% | 51.9% | 3.9% |
| 1% | 76.9% | 0.0% | 12.8% | 0.0% | 10.3% | 5.3% |
| 2% | 52.1% | 46.9% | 0.2% | 0.8% | 0.0% | 5.8% |
| 3% | 46.3% | 52.9% | 0.1% | 0.7% | 0.0% | 5.9% |
The maximum drawdown is measured up to the moment the path stops. A "0.0%" in the table means it did not occur in 5,000 paths, not that it cannot happen.
- At 0.5% the rules are almost never touched, but half the paths do not reach the target in 30 days either.
- At 2%, "daily limit first" jumps from 0.0% to 46.9%. The reason is plain: three losses in a day are
3 × 2% = 6%, past the 5% limit. At 1%, three losses are 3%, so in this model the daily limit cannot be reached at all. - So under these inputs the shape of the result is set by whether trades per day × risk per trade crosses the daily limit.
Across seeds 1, 2 and 3 the "target first" share at 1% risk was 76.9%, 76.4% and 75.5%. With 5,000 paths the sampling error of a share q is √(q(1−q)/5000), about ±0.6 points at 77% (one standard error, from the formula). That covers the random draw only, not errors in the assumptions.
With a trailing maximum loss, "maximum loss first" at 1% risk goes from 12.8% to 20.4%.
The check: write the model twice
A simulation produces plausible numbers even when it is wrong. So the same model is written independently in Python and compared with the JavaScript result. mulberry32 is 32-bit integer arithmetic; in Python that is done with masks.
M = 0xFFFFFFFF
def mulberry32(seed):
a = seed & M
def rnd():
nonlocal a
a = (a + 0x6D2B79F5) & M
t = ((a ^ (a >> 15)) * (1 | a)) & M
t = ((t + (((t ^ (t >> 7)) * (61 | t)) & M)) & M) ^ t
return ((t ^ (t >> 14)) & M) / 4294967296
return rnd
With the same random sequence and the same order of floating-point operations, the two must agree to the last bit. The page writes its results as JSON when opened with ?selftest=1; headless Chrome opens it and a script compares.
quant model: AGREE — 5 cases · largest difference 0.00e+00
Five cases of 5,000 paths each, largest difference zero (Python 3.14, Chrome, an Apple Silicon Mac, one run). A difference stops the release. This catches slips such as swapping the order of the rule checks on one side.
There is also a boundary test. With a 20% win rate, a payoff of 0.3, 5% risk and 120 days, nearly every path is disqualified within days. An implementation that keeps computing after disqualification piles up fixed-size losses and reports a maximum drawdown far above 100% (our first version had this mistake). With the stop in place, the largest maximum drawdown in the sample under these inputs is 13.95%, inside "maximum loss 10% + one 5% risk". The check stops the release if that bound is exceeded. It does not catch a misunderstanding shared by both implementations.
Use and limits
- Calculator: https://zero-trace-ship-kit.zeus-lab-global.workers.dev/quant/?utm_source=devto — move a slider and it recomputes. Nothing you enter is sent anywhere; the page's CSP forbids network requests.
- Account rules differ between providers and change. Enter the numbers from the current official rules.
- Win rate and payoff are assumed numbers.
- This counts how often the loss limits are reached under the conditions and rules you enter. It does not forecast sudden market moves and does not reproduce any firm's actual rule evaluation.
- A tool that does the same counting on your own trade-history CSV, resampling whole days, is in the paid kit described on the same page.
Advertisement (affiliate link): if you sign up through this link, ZEUS LAB may receive compensation. It leads to TradingView's Black Friday information page; check the link for availability, eligible plans, prices and conditions. You do not need any subscription to use this calculator. TradingView
Once more: this is arithmetic and says nothing about what or how to trade. Trading can lose all of the money you commit.
Disclosure: this article was drafted with AI assistance (Claude) and checked against the measured results and the published files. Product links lead to our own paid kits.
Top comments (0)