An EA can set spread and tick-gap thresholds by percentile without storing every tick. CPSquare implements the P-Square algorithm, and two order-independent classes serve as alternatives: CCountQuantile returns exact percentiles of rounded spreads, and CLogQuantile keeps a chosen relative error within a set range. Unit tests, mutation runs, and a Python consistency check cover the code. On EURUSD and XAUUSD ticks in time order, the P-Square estimates of high percentiles stayed far from the exact values.
An EA often needs a percentile of a value observed on every tick. A spread filter can block entries when the spread is above its own 95th percentile. A feed monitor can raise an alarm when no tick arrives for longer than the 99.9th percentile of normal gaps between ticks. Computing the exact percentile requires storing all values. For one month of EURUSD ticks on MetaQuotes-Demo, that is 5,443,254 values per series, about 43 MB of doubles, and a sort each time the threshold must be updated.
The P-Square algorithm of Raj Jain and Imrich Chlamtac (Communications of the ACM, 1985) estimates a percentile without storing the values. The result is an approximation and can differ from the exact percentile of a full sort. The algorithm keeps five markers on the distribution and moves them with a parabolic formula as values arrive. Nothing else is stored. My plan for this article was a spread gate built on P-Square.
I implemented the algorithm as the CPSquare class and validated it in two ways. The class reproduced every marker position of the worked example in the paper. On generated data in random order, the median rank error after one million values was below 0.01 percentage points for four continuous distributions. Then I gave it real ticks in the order they arrived. For the 90th percentile of the time between EURUSD ticks, the exact answer was 801 ms and P-Square returned 4,346.63 ms. For the 95th percentile of the XAUUSD spread, the exact answer was 52 points and P-Square returned 5,645.75 points. Shuffled into a random order, the same ticks gave 821.82 ms and 52.90 points.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Check out the new article: Streaming Percentiles in MQL5: Testing the P-Square Algorithm on Real Tick Data.
An EA often needs a percentile of a value observed on every tick. A spread filter can block entries when the spread is above its own 95th percentile. A feed monitor can raise an alarm when no tick arrives for longer than the 99.9th percentile of normal gaps between ticks. Computing the exact percentile requires storing all values. For one month of EURUSD ticks on MetaQuotes-Demo, that is 5,443,254 values per series, about 43 MB of doubles, and a sort each time the threshold must be updated.
The P-Square algorithm of Raj Jain and Imrich Chlamtac (Communications of the ACM, 1985) estimates a percentile without storing the values. The result is an approximation and can differ from the exact percentile of a full sort. The algorithm keeps five markers on the distribution and moves them with a parabolic formula as values arrive. Nothing else is stored. My plan for this article was a spread gate built on P-Square.
I implemented the algorithm as the CPSquare class and validated it in two ways. The class reproduced every marker position of the worked example in the paper. On generated data in random order, the median rank error after one million values was below 0.01 percentage points for four continuous distributions. Then I gave it real ticks in the order they arrived. For the 90th percentile of the time between EURUSD ticks, the exact answer was 801 ms and P-Square returned 4,346.63 ms. For the 95th percentile of the XAUUSD spread, the exact answer was 52 points and P-Square returned 5,645.75 points. Shuffled into a random order, the same ticks gave 821.82 ms and 52.90 points.
Author: Ali Akbar