Power And Sample Size Calculator
A trader who back-tests two rule variants on forty closed trades each and picks the one with the higher average P/L is usually running an under-powered comparison without knowing it - the size of edge they are chasing might genuinely need several hundred trades per side before it is distinguishable from ordinary sampling noise at any reasonable confidence level. This library runs the actual power/sample-size arithmetic instead of leaving that as a gut feeling. PSS_SampleSizeForMeanDifference implements the standard two-sample z-test power formula, n = 2*(z_alpha + z_beta)^2*sigma^2/delta^2, answering "how many trades per group do I need" given an estimated per-trade standard deviation, the minimum edge worth detecting, a significance level and a desired power. PSS_SampleSizeForProportions runs the equivalent pooled- variance formula for comparing two win rates instead of two means. PSS_PowerForSampleSize runs the same relationship in reverse - given a FIXED sample size already collected (or the only size realistically available), what power did that comparison actually have. This is the more commonly useful direction in practice: a trader rarely gets to choose their trade count freely, so knowing "my 40-trade test could only reliably detect an edge above X" is what actually calibrates how much weight the result deserves. PSS_MinimumDetectableEffect answers the same question directly in the observation's own units, rather than as a probability. The underlying inverse-normal quantile (needed to convert a significance level or a desired power into a z-score) is Peter Acklam's published rational approximation, accurate to about 1.15e-9 - the standard closed- form algorithm for this exact problem, not an iterative solve. Notes and limitations - All formulas assume two independent samples with a roughly known or estimated standard deviation, using the two-sample z-test power formula (the standard, honestly-stated approximation) rather than a t-test/ unequal-variance formulation, which needs numerical distribution machinery MQL4 has no library support for. This is a very close approximation once each group has more than about 30 observations, and a conservative planning tool below that. - Sample-size functions return 0 on invalid input (non-positive sigma or delta, alpha/power outside (0,1), identical proportions) rather than a meaningless number. - PSS_SampleSizeForProportions requires 0 < p1, p2 < 1 - it is not defined for a 0% or 100% baseline rate. - These are PLANNING and DIAGNOSTIC tools, not a hypothesis test in themselves - they say how many observations a comparison needs or how much power a comparison had, not whether a specific observed result is significant.
Script · MT5 · v1.0 · Free
About Power And Sample Size Calculator
Power And Sample Size Calculator is a script for MetaTrader 5. It has 4 settings, such as Sigma, Delta and Alpha. MQL source code included.
Files in the download: PowerSampleSizeCalculator.mq5.mqh, PowerSampleSizeCalculator.txt, PowerSampleSizeCalculatorDemo.ex5, PowerSampleSizeCalculatorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sigma | 30.0 | Estimated per-trade P/L standard deviation |
| Delta | 5.0 | Minimum edge worth detecting between the two variants |
| Alpha | 0.05 | Significance level (two-sided) |
| Power | 0.80 | Desired statistical power |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code