Advertisement IC Markets

A custom spread indicator that paints the background when too wide

Optimize MetaTrader 4, MetaTrader 5, cTrader, and TradingView for speed. Discuss Level II Market Depth (DOM), custom hotkeys, and volume indicators.
Post Reply
FXS London Scalper
Posts: 1149
Joined: Sun Oct 18, 2026 12:25 pm

A custom spread indicator that paints the background when too wide

Post by FXS London Scalper »

A visual cue on the chart helps avoid entering when spread is wide. An indicator that changes the chart background colour when spread exceeds a threshold is simple and effective.

Logic:
- On each tick, calculate spread.
- If spread > threshold, set chart background to a warning colour.
- If spread < threshold, restore normal background.

MQL5 snippet:

Code: Select all

void OnTick() {
   double spr = (SymbolInfoDouble(_Symbol,SYMBOL_ASK) - SymbolInfoDouble(_Symbol,SYMBOL_BID)) / _Point;
   color bg = (spr > MaxSpreadPts) ? clrMaroon : clrBlack;
   ChartSetInteger(0, CHART_COLOR_BACKGROUND, bg);
}
(For an indicator, use OnCalculate instead of OnTick.)

Refinements:
- Different thresholds by hour.
- Gradual colour change (yellow, orange, red).
- Display current spread as a label.

This kind of visual cue works because it's impossible to miss, unlike a number in the corner.

Does anyone use something similar? Share your version.
FXS Official Team | London Session Desk
Recommended broker for automated trading & scalping IC Markets
Amanhasnoname
Posts: 2539
Joined: Fri Oct 30, 2026 9:45 am

Re: A custom spread indicator that paints the background when too wide

Post by Amanhasnoname »

A background colour change is a smart choice because it hits peripheral vision, which a small number in the corner never does. One thing worth fixing in the snippet: ChartSetInteger only takes effect on the next redraw, so add ChartRedraw() after setting the colour, otherwise the change can lag a few ticks on quiet symbols. Also store the original background colour in OnInit and restore it in OnDeinit, or you end up with a maroon chart after removing the indicator.

On thresholds, a fixed point value tends to break down across symbols and brokers. It is worth testing a threshold based on the median spread for that hour over the last few weeks, then flagging anything above roughly 1.5 to 2 times that median. That adapts to rollover and news without you editing inputs.

A common pitfall is flicker when spread hovers right at the limit. A small buffer helps, for example switching to warning above the threshold but only back to normal once it drops 10 to 20 percent below it. Logging each warning with a timestamp is useful too, since it shows which hours really cost you.
Post Reply