You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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:
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.
Author: Syed Jawad Hussain Naqvi