Trend Magic2
Download scriptThe TrendMagic indicator is designed to help traders visualize market momentum and identify the structural boundaries of a price trend. Instead of cluttering your chart with every minor fluctuation, this tool focuses on defining "support" and "resistance" levels dynamically based on price action and volatility.
Core Methodology
The logic operates by analyzing two primary components: momentum (measured via the Commodity Channel Index) and market volatility (measured via the Average True Range). By combining these, the script determines whether the market is in an uptrend or a downtrend and draws corresponding lines to signal potential entry or exit points.
How it Works
The script follows a systematic three-step logic:
1. Trend Detection The tool constantly monitors momentum shifts. When the momentum crosses from negative to positive, the indicator recognizes a potential shift into an upward trend. Conversely, when momentum drops from positive to negative, it identifies a shift into a downward trend.
2. Dynamic Boundary Calculation Once a trend is established, the script calculates specific price "floors" and "ceilings":
- In an Uptrend: It calculates a support line based on the recent price lows adjusted by a volatility offset. This creates a "trailing" safety net that sits below the current price.
- In a Downtrend: It calculates a resistance line based on recent price highs adjusted by a volatility offset, creating a "ceiling" above the current price.
3. The Trailing Mechanism (Ratchet Effect) One of the most important features of this logic is its "ratchet" behavior. To ensure the lines only move in your favor—and never provide a false signal by moving backward—the script applies a specific rule:
- In an Uptrend, the support line can only move higher as price climbs; it will never drop lower than its previous level.
- In a Downtrend, the resistance line can only move lower as price falls; it will never rise higher than its previous level.
Summary for Traders
This indicator acts as a dynamic trend-following guide. By filtering out "market noise" and focusing on the structural highs and lows, it provides a clear visual of the current trend's strength. It effectively tells you where the trend "floor" is during a rally and where the "ceiling" is during a sell-off, helping you stay on the right side of the market movement while providing a logical point to manage risk.
//+------------------------------------------------------------------+
//| TrendMagic.mq4 |
//| Tidied up by TudorGirl 28 May 2009 |
//| AnneTudor@ymail.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_width1 2
#property indicator_color2 Red
#property indicator_width2 2
//+------------------------------------------------------------------+
extern int CCI = 50;
extern int ATR = 5;
//+------------------------------------------------------------------+
double bufferUp[];
double bufferDn[];
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(0, bufferUp);
SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2);
SetIndexBuffer(1, bufferDn);
return (0);
}
//+------------------------------------------------------------------+
int deinit()
{
return (0);
}
//+------------------------------------------------------------------+
int start()
{
double thisCCI;
double lastCCI;
int counted_bars = IndicatorCounted();
if (counted_bars < 0) return (-1);
if (counted_bars > 0) counted_bars--;
int limit = Bars - counted_bars;
for (int shift = limit; shift >= 0; shift--)
{
thisCCI = iCCI(NULL, 0, CCI, PRICE_TYPICAL, shift);
lastCCI = iCCI(NULL, 0, CCI, PRICE_TYPICAL, shift + 1);
if (thisCCI >= 0 && lastCCI < 0) bufferUp[shift + 1] = bufferDn[shift + 1];
if (thisCCI <= 0 && lastCCI > 0) bufferDn[shift + 1] = bufferUp[shift + 1];
if (thisCCI >= 0)
{
bufferUp[shift] = Low[shift] - iATR(NULL, 0, ATR, shift);
if (bufferUp[shift] < bufferUp[shift + 1])
bufferUp[shift] = bufferUp[shift + 1];
}
else
{
if (thisCCI <= 0)
{
bufferDn[shift] = High[shift] + iATR(NULL, 0, ATR, shift);
if (bufferDn[shift] > bufferDn[shift + 1])
bufferDn[shift] = bufferDn[shift + 1];
}
}
}
return (0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
Comments