Skip to content
All library documents

Adaptive Spread Gates Using Rolling Percentiles

Article MQL5 articles

Summary

This article presents a way to decide whether trading costs are unusually high for an instrument. Instead of comparing every symbol with one fixed spread limit, it tracks each symbol’s recent spread distribution and evaluates the current spread by percentile. A rolling histogram stores observations in bins, while a circular buffer removes aged observations so estimates reflect a fixed recent window. The author describes percentile values and ranks, along with GREEN, YELLOW, RED, and WARMING states and an order-submission gate.

The proposed implementation updates and queries the histogram in time bounded by the number of bins, avoiding repeated sorting of the entire window. The article includes configurable defaults for bin count, window length, and spread range, plus a dashboard and a test against a synthetic distribution. These are implementation examples rather than evidence of improved live trading results. Estimates are approximate because spreads are binned, and thresholds, range, and window length need to suit the instrument and feed; the author also suggests considering multiple horizons or recency weighting.

Key ideas

  • A spread’s cost should be judged against the instrument’s own recent behavior rather than a universal fixed threshold.
  • A rolling histogram estimates spread percentiles while a circular buffer evicts observations outside the window.
  • Histogram queries trade precision for speed by operating on bins instead of sorting every raw observation.
  • A separate warming state indicates that the monitor has not yet collected enough data for reliable estimates.
  • Per-symbol gates can block orders during unusually expensive spread conditions, but their parameters require calibration.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.