Discussing the article: "Making Custom Indicators for Beginners (Part 1): SuperTrend Indicator"

 

Check out the new article: Making Custom Indicators for Beginners (Part 1): SuperTrend Indicator.

This article builds a robust SuperTrend indicator in MQL5 using ATR-based bands, a ratchet mechanism, and strict series indexing to avoid silent recursion errors and repainting on closed bars. We walk through buffer binding, ATR handle management, seeding, and arrow confirmation logic. A companion EA demonstrates practical integration.

SuperTrend is a representative example of a classic weekend project: two developers can receive identical requirements yet produce very different implementations. The concept is well-established and based on a volatility-band approach (similar to Keltner Channels or Bollinger Bands); however, instead of plotting a static envelope around the price, it is rendered as a single line that switches sides depending on the trend direction. The concept combines the ATR (Average True Range) with a locking mechanism — the ratchet principle. This logic can be implemented in about twenty lines of code.

Repainting can come from a few different causes:

  • the current forming bar changing, which is normal and expected;
  • closed bars changing due to a state-management bug, which is what this article focuses on;
  • genuine future-data leaks, which are a separate and more serious issue.

A subtler, SuperTrend-specific issue is that band values are recursive, so each bar depends on the previous bar's stored state. The current bar's upperband depends on the upper band and the closing price of the previous bar. This means bar i cannot be calculated in isolation; you must know the correct state of bar i−1 (or i+1 in series indexing). Therefore, loop direction and indexing (series arrays vs. standard arrays) must be aligned; otherwise the recursion can silently break in historical data.

Making Custom Indicators for Beginners (Part 1): SuperTrend Indicator

Author: Syed Jawad Hussain Naqvi