Loading…

Circular Statistics

Averaging clock times as plain numbers is wrong the moment the sample straddles midnight. Two trades opened at 23:00 and 01:00 are two hours apart, and their true average time is obviously midnight - but averaged as the plain numbers 23 and 1, the arithmetic mean is 12, i.e. noon, on the exact opposite side of the clock from the correct answer. Every "average time of day my winning trades open" or "average hour my stop gets hit" statistic computed with a plain mean is silently wrong for any sample clustered near midnight - and for many session-based strategies, that is not a rare edge case, it is the busiest hour of the day. Circular statistics treats each time-of-day as an angle on a clock face (24 hours mapped to 360 degrees / 2*pi radians) and averages the sine and cosine components separately before converting the result back to a clock hour - the standard, correct way to average angular data, used identically for compass headings, day-of-year seasonality and phase measurements, not something specific to trading. MeanResultantLength and CircularVariance describe how CONCENTRATED the sample is around its mean direction (a mean resultant length near 1 means every observation clusters tightly; near 0 means the observations are spread so evenly around the clock that they cancel out) - the circular analogue of a standard deviation, but on a bounded, directly comparable 0 to 1 scale. EstimateKappa converts that same resultant length into the von Mises distribution's concentration parameter, using the standard closed- form approximation. RayleighTestStatistic gives a quick non-uniformity test statistic (z = n * R^2); MQL4 has no incomplete-gamma/chi-square library function to convert that into an exact p-value, so the statistic itself is returned rather than a manufactured p-value - the common rough rule of thumb is z > 3 for roughly p < 0.05 on moderate sample sizes. Notes and limitations - MQL4 has no MathArctan2 (two-argument arctangent) built in - only the single-argument MathArctan. The internal CsArcTan2 helper reproduces the standard atan2 quadrant logic by hand from MathArctan plus the sign of each component, so CircularMeanRadians resolves the correct quadrant rather than only the correct ratio. - EstimateKappa uses the standard closed-form approximation (split by range of R for numerical stability), not the exact modified Bessel function inversion - accurate enough for a concentration READING, not intended as an input to a downstream maximum-likelihood von Mises fit. - RayleighTestStatistic returns the test statistic itself, not a p-value - MQL4 has no chi-square/incomplete-gamma function to convert it exactly. Treat the common z > 3 rule of thumb as approximate, not a precise significance threshold. - This library measures TIME-OF-DAY (a 24-hour cycle) specifically via the Cs*ToRadians helpers, but the core CircularMeanRadians/ MeanResultantLength/EstimateKappa/RayleighTestStatistic functions work on any angle in radians - day-of-week-as-a-7-day-cycle or day-of-year-as-a- 365-day-cycle data can be mapped to radians the same way and passed in directly.

Script · MT4 · v1.0 · Free

About Circular Statistics

Circular Statistics is a script for MetaTrader 4. MQL source code included.

Files in the download: CircularStatistics.mqh, CircularStatistics.txt, CircularStatisticsDemo.mq4.

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