Trendlines are one of the most reliable tools in technical analysis. They highlight support, resistance, and potential breakout levels. But manually drawing and updating them can be repetitive and error-prone.
To solve this, I created an Auto Trend Lines Indicator for MetaTrader 4 (MT4) and MetaTrader 5 (MT5). It automatically detects swing highs and lows, then draws and updates trendlines dynamically as new candles appear.
You can check out the full project here: Auto-Trend-Lines-Indicator-Metatrader
How It Works
The indicator scans historical and real-time price data to detect swing highs and swing lows. Then it connects these points to generate valid trendlines.
Here’s a simplified core snippet from the code:
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
// Loop through candles to detect highs and lows
for(int i = rates_total - 100; i > 1; i--)
{
if(high[i] > high[i+1] && high[i] > high[i-1])
DrawTrendLine("High_" + IntegerToString(i), time[i], high[i], clrRed);
if(low[i] < low[i+1] && low[i] < low[i-1])
DrawTrendLine("Low_" + IntegerToString(i), time[i], low[i], clrBlue);
}
return(rates_total);
}
Why this is important
The loop checks recent candles (rates_total - 100) for local highs and lows.
If a candle’s high is greater than both its neighbors → it’s a swing high.
If a candle’s low is lower than both its neighbors → it’s a swing low.
The indicator then draws dynamic trendlines (DrawTrendLine()) at these levels.
This way, the chart always reflects updated market structure without manual effort.
Get the Code
You can explore and download the full source code here:
GitHub Repository
If you prefer a packaged download instead of browsing GitHub, you can also find it on my resource site: tradingfinder.com
Top comments (0)