Price-Cluster Alert Engine (TradingView + MT5)
A Pine Script v6 indicator and a matching MT5 Expert Advisor that track repeated tests of a price level, draw the clusters and send alerts, with emailed chart screenshots from MT5.
The problem
The client traded gold by watching price test the same level again and again. He sketched the rules on chart screenshots and wanted the patterns detected, counted and drawn automatically, with an alert the moment a cluster became active or price touched a key line. He also wanted the same alerts from MetaTrader 5, by email with a picture of the chart.
What I built
- Cluster detection, two generations: the first used Williams fractals. The second drops fractals for a running extreme that locks once price moves a set distance away. After that, each new level needs a full round trip (touch the offset line, then come back to touch the anchor).
- Many sequences at once: each cluster has its own ID and state and is stored as a Pine user-defined type in an array.
- Asymmetric reset gates: different reset distances on the breakout side and the round-trip side, so a cluster isn’t cancelled by its own round trips.
- Counting separate from drawing: each sequence stores event timestamps and is drawn retroactively only once it activates. A “current cluster only” mode hides older clusters but keeps counting and alerting on them.
- Batched alerts: activation, touch and reset events are collected per bar and sent together, so two sequences firing on the same bar are both reported.
- MT5 Expert Advisor with the same logic:
- touches checked intrabar on every tick, levels counted on closed bars only
- history rebuilt on load, missed bars caught up oldest first after a disconnect
- a timer so drawing works even without ticks
- Email alerts from MT5: a hand-written SMTP client over STARTTLS that sends MIME multipart email with the chart screenshot attached.
Details
- Timestamp-based drawing: all Pine drawing uses bar time instead of bar index. This avoids TradingView’s limit on drawing objects too far from the current bar on long-lived clusters.
- Draw first, notify second: the EA queues touches, redraws the chart and only then takes the screenshot, so the attached image always shows the dot that fired.
- Time zones: broker server time is converted to the client’s time zone for display. The conversion is recalculated on each call, so a broker daylight-saving change doesn’t shift the clock.
- Scale guard: the EA warns when the symbol’s price is too low for the dollar-based inputs to make sense.