Loading…

Roll Effective Spread Estimator

The quoted spread on the Market Watch window is a snapshot of right now. Roll's 1984 estimator answers a different question: what spread would explain the bid-ask bounce visible in a series of past price changes, without needing any quote or spread history at all. The intuition is that a price bouncing between bid and ask induces negative first-order serial covariance in consecutive price changes - a rise is more likely to be followed by a fall (and vice versa) purely from the bounce, even with zero underlying drift. The size of that negative covariance implies a spread. This script pulls a run of Close prices from the chart's own history, computes the sequence of price changes dP, and its lag-1 sample covariance. When that covariance is negative - which it usually is on liquid, range-bound intraday data - the spread is 2 * sqrt(-covariance), reported in price units, points and pips alongside the current live quoted spread for comparison. When the covariance comes out non-negative - common on strongly trending samples, or windows too short to isolate the bounce - Roll's formula has no real square root. Most amateur re-implementations either crash, return NaN silently, or (worse) take the absolute value and report a fabricated number anyway. This script instead prints a clear explanation and reports no spread figure at all for that run, which is the honest behaviour Roll's own model requires. Notes and limitations - Roll's estimator assumes a stationary, no-drift price process bouncing between bid and ask. A trending sample violates that assumption and commonly produces a non-negative covariance, in which case this script correctly reports no result rather than a number that would be meaningless under the model. - The result is an implied spread from price statistics, not a measured one - it can differ materially from the broker's actual quoted spread, particularly around news or during thin liquidity when the bounce pattern breaks down. - Uses Close only, matching Roll's original formulation. It does not use intrabar High/Low, so it cannot detect within-bar spread widening. - Needs the chart to already hold enough history for the requested InpBars; if it does not, the script says so and asks for more history or a smaller InpBars rather than silently truncating the sample.

Script · MT5 · v1.0 · Free

About Roll Effective Spread Estimator

Roll Effective Spread Estimator is a script for MetaTrader 5. It can alert you with a pop-up, sound, email or push message. It has 3 settings, such as Timeframe, Bars and Export csv. MQL source code included.

Files in the download: RollEffectiveSpreadEstimator.ex5, RollEffectiveSpreadEstimator.mq5, RollEffectiveSpreadEstimator.txt.

Settings

SettingDefaultWhat it does
TimeframePERIOD_M1Timeframe whose Close series is sampled
Bars500Number of price changes to use (needs Bars+1 closes)
Export csvtrueWrite the underlying dP series to CSV

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
  3. Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
  4. Open the .mq5 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: Alerts · MT5 Script · MQL5 Source Code

MQL4 & MQL5 scripts · PolyMaria