Loading…

Sharpe And Sortino Ratios

Sharpe ratio divides mean excess return by the standard deviation of ALL returns - upside surprises and downside surprises contribute equally to the denominator. That is a strange way to measure risk for a trading strategy, where nobody complains about a streak of unusually large winners. A strategy whose volatility comes mostly from bigger-than-usual gains scores a worse Sharpe ratio than one whose volatility comes from bigger-than-usual losses, even when the second one is obviously the riskier strategy to actually run. Sortino ratio fixes this by replacing the denominator with downside deviation - dispersion measured only over returns that fall below a target (commonly zero, or a risk-free rate). Periods at or above the target are excluded from the calculation entirely rather than clamped to zero and included; clamping would dilute the downside dispersion with a run of values that never actually happened, understating the risk. The included demo builds two series with the same mean and a near-identical Sharpe ratio, one skewed to the upside and one to the downside, and shows Sortino telling them apart when Sharpe cannot. Calmar ratio is also included, taking an already-computed annualised return and maximum drawdown percentage rather than recomputing drawdown itself - drawdown analysis belongs to the companion DrawdownCalc library in this same catalogue, and duplicating that logic here would only let the two drift out of sync over time. A parameter that matters more than it looks: every rate in this header (risk-free rate, target return) is expressed PER PERIOD, matching the return series, never as an annual rate. Passing an annual rate directly - a common mistake - silently subtracts far too much from every period and produces a ratio that is wrong but still looks like a plausible number, which is worse than an obvious error because nothing flags it. Self-contained: no dependencies beyond the standard library, no DLLs. Small mean/stddev helpers are duplicated locally rather than pulled in from EquityStats.mqh, because every project in this catalogue is a separate CodeBase submission and must stand alone. Every function operates purely on caller-supplied double arrays - no broker, symbol or account call anywhere in this header, on either platform. Notes and limitations - All rate parameters (risk-free rate, Sortino target) are PER PERIOD, not annual. Convert before calling - see the demo's InpRiskFreeAnnualPct handling for the correct conversion. - SharpeRatio and SortinoRatio both return 0.0 when the relevant denominator (stddev, downside deviation) is zero or the input array is empty. A 0.0 result on a real series with no volatility at all is a degenerate case, not a meaningful "market neutral" reading - check the input length first. - CalmarRatio does not compute drawdown itself. Feed it a value from DrawdownCalc.mqh (this catalogue) or your own drawdown measurement. - Ratios computed on a short return series are noisy. A handful of samples is not enough to trust any of these numbers as a stable estimate. - No thread or state concerns: every function is pure and operates only on the arrays passed in, on either platform.

Script · MT4 · v1.0 · Free

About Sharpe And Sortino Ratios

Sharpe And Sortino Ratios is a script for MetaTrader 4. It has 2 settings, such as Risk free annual pct and Periods per year. MQL source code included.

Files in the download: RiskRatios.mqh, RiskRatios.txt, RiskRatiosDemo.mq4.

Settings

SettingDefaultWhat it does
Risk free annual pct4.0Annual risk-free rate, percent (converted to per-period internally)
Periods per year252.0Samples per year implied by the series (252 = daily)

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria