Time Bucket Accumulator
Almost every "does my edge depend on the hour/session/weekday" question ends up implemented the same way: an array of running sums per bucket, a mean formula, a variance formula, and bucket-index arithmetic that has to be re-derived and re-debugged in every script that asks the question. This library packages that pattern once as a reusable accumulator instead of a one-off block of code copied between projects. TB_Init sizes a set of caller-owned parallel arrays (count, sum, sum-of- squares, min, max) to a chosen number of buckets. TB_AddSample feeds one observation into a bucket in O(1), updating all five arrays via the sum/sum-of-squares identity for variance rather than storing every raw observation - the accumulator's memory footprint stays fixed at the bucket count regardless of how many samples are fed in, which matters for a statistic accumulated across years of tick or trade history. TB_Mean, TB_Variance, TB_StdDev, TB_Count, TB_Min and TB_Max read back a bucket's statistics at any point without needing to "finalise" the accumulator first - it can be queried mid-stream. Because the state is caller-owned arrays rather than a hidden global, the same pattern can track several things side by side (P/L, holding time, slippage) just by keeping separate array sets, and an EA can carry one live across OnTick calls for as long as it runs. TB_HourBucket, TB_WeekdayBucket and TB_SessionBucket turn a datetime directly into a bucket index, so the whole "extract the bucket, then accumulate into it" pattern is two library calls. TB_SessionBucket takes the session start hours as a parameter rather than a fixed table, since which server-time hours correspond to which trading session is broker- and timezone-dependent and no single built-in table would be honest about that across brokers. Notes and limitations - Variance is computed from running sums via the E[x^2]-E[x]^2 identity, not Welford's algorithm - simpler and adequate for typical P/L or duration magnitudes, but marginally less numerically stable than Welford on an extremely large sample with very large values. Negative round-off is clamped to 0.0 before the square root. - TB_SessionBucket's session boundaries are whatever hours the caller supplies - there is no built-in Asian/London/New York table, since the correct server-time hours for each session vary by broker and by daylight saving. Set them once for the broker in use. - This is a running accumulator, not a percentile/median tracker - it cannot answer "what's the median P/L in this bucket", only mean, stddev, min, max and count.
Script · MT5 · v1.0 · Free
About Time Bucket Accumulator
Time Bucket Accumulator is a script for MetaTrader 5. It has 1 setting, such as Sample count. MQL source code included.
Files in the download: TimeBucketAccumulator.mq5.mqh, TimeBucketAccumulator.txt, TimeBucketAccumulatorDemo.ex5, TimeBucketAccumulatorDemo.mq5.
Settings
| Setting | Default | What it does |
|---|---|---|
| Sample count | 500 | Synthetic observations to generate |
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: Trading Sessions · MT5 Script · MQL5 Source Code