Trade Survival Analysis Kit
Averaging closed-trade holding times to answer "how long do our trades typically last" quietly excludes every trade that has not closed yet - and that exclusion is not random. A trade still open right now will, once it eventually closes, very likely have a LONGER holding time than the trades that have already closed, simply because it has had more time to run. Naive averaging systematically biases the estimate toward short durations, worse the more open trades exist. Kaplan-Meier survival analysis, the standard tool from medical statistics for exactly this "some subjects haven't had the event yet" problem, treats an open trade as CENSORED at its current age: it correctly counts toward the number of trades still "at risk" at every duration up to now, without being wrongly counted as having closed. The resulting curve is S(t), the probability a trade is still open past holding time t, built as a step function that only moves at durations where a real close (not a censoring) happened. This header also reports the discrete hazard rate at each step - the conditional probability of closing right at that duration, given survival to it - which is where "when do stop-outs actually strike" is read directly, and Greenwood's formula for the variance of the curve, giving a proper confidence band instead of presenting the curve as if it were known exactly. Notes and limitations - Assumes censoring is non-informative - that a trade being open right now says nothing by itself about how long it will ultimately last beyond what its age already implies. A strategy that force-closes trades early under certain conditions (e.g. news filter) violates this assumption for those trades specifically. - The median holding time is reported as unavailable (-1) rather than guessed when the survival curve never drops to 0.5 within the observed data - this happens when a large fraction of trades are still open, and is the correct Kaplan-Meier answer, not a bug. - Greenwood's confidence band is a normal approximation; it can be unreliable at the extreme ends of the curve where very few trades remain at risk. Treat wide or asymmetric-looking bands there with caution. - This header analyses HOLDING TIME only. It draws no conclusion about profitability - a trade surviving longer is not itself good or bad.
Script · MT5 · v1.0 · Free
About Trade Survival Analysis Kit
Trade Survival Analysis Kit is a script for MetaTrader 5. It has 4 settings, such as Trade count, Mean hold hours and Censor after. MQL source code included.
Files in the download: TradeSurvivalAnalysis.mq5.mqh, TradeSurvivalAnalysis.txt, TradeSurvivalAnalysisDemo.ex5, TradeSurvivalAnalysisDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Trade count | 40 | Number of synthetic trades |
| Mean hold hours | 6.0 | Mean holding time used to generate the sample |
| Censor after | 9.0 | Trades still "open" beyond this age are censored |
| Seed | 5 | Random seed for the synthetic generator |
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