Execute Linesw-1(Alert)_001
Download scriptThis custom indicator is designed to analyze market trends by calculating the momentum and direction of price movements. It operates in a separate window below your main chart, providing visual cues to help identify shifts between upward and downward trends.
How the Logic Works
1. Data Processing and Smoothing The script begins by looking at price data over a specific period (which you can customize). To filter out minor market "noise" and focus on the broader movement, it uses a weighted moving average calculation. Specifically, it calculates a mathematical relationship between two different averages: one based on half of your chosen period and one based on the full period. By subtracting one from the other, the script identifies the underlying "vector" or velocity of the price change.
2. Trend Mapping Once this raw movement data is gathered, the script performs a second layer of smoothing. It applies a moving average to the calculated vectors. This creates a smoothed trend line (the "Map Buffer"). The goal of this step is to ensure that the indicator only reacts to significant shifts in momentum rather than every small price fluctuation.
3. Trend Identification and Visuals The core logic then compares each new point on this smoothed map to the previous one:
- Upward Trend: If the current value is higher than the previous value, the script identifies an "Uptrend." This is represented visually on your screen in Blue.
- Downward Trend: If the current value is lower than the previous value, it identifies a "Downtrend," which is displayed in Red.
4. Dynamic Switching and Alerts The indicator is designed to be reactive. When the logic detects a crossover—where the momentum switches from positive to negative (or vice versa)—it performs three actions simultaneously:
- It clears the previous trend line.
- It begins drawing the new trend line in the corresponding color.
- It triggers an automated "Alert" notification to notify you immediately of the shift in market direction.
Summary for the User
Essentially, this tool acts as a momentum tracker. It takes raw price history, smooths out the "jitter," and tells you which way the market is currently leaning. When the line turns Blue, the underlying momentum is pushing upward; when it turns Red, the momentum is shifting downward.
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int period=40;
int method=3; // MODE_SMA
int price=0; // PRICE_CLOSE
//---- buffers
double Uptrend[];
double Dntrend[];
double ExtMapBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(3);
SetIndexBuffer(0, Uptrend);
//ArraySetAsSeries(Uptrend, true);
SetIndexBuffer(1, Dntrend);
//ArraySetAsSeries(Dntrend, true);
SetIndexBuffer(2, ExtMapBuffer);
ArraySetAsSeries(ExtMapBuffer, true);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
IndicatorShortName("Execute Line("+period+")");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
// ???? ????? ?????? ??????
return(0);
}
//+------------------------------------------------------------------+
//| ?????????? ??????? |
//+------------------------------------------------------------------+
double WMA(int x, int p)
{
return(iMA(NULL, 0, p, 0, method, price, x));
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();
bool direction =0;
if(counted_bars < 0)
return(-1);
int x = 0;
int p = MathSqrt(period);
int e = Bars - counted_bars + period + 1;
double vect[], trend[];
if(e > Bars)
e = Bars;
ArrayResize(vect, e);
ArraySetAsSeries(vect, true);
ArrayResize(trend, e);
ArraySetAsSeries(trend, true);
for(x = 0; x < e; x++)
{
vect[x] = 2*WMA(x, period/2) - WMA(x, period);
// Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ", WMA(x, period));
}
for(x = 0; x < e-period; x++)
ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);
for(x = e-period; x >= 0; x--)
{
trend[x] = trend[x+1];
if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
if (trend[x]>0)
{ Uptrend[x] = ExtMapBuffer[x];
if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
Dntrend[x] = EMPTY_VALUE;
if (direction == -1) Alert("UP");
direction = 1;
}
else
if (trend[x]<0)
{
Dntrend[x] = ExtMapBuffer[x];
if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
Uptrend[x] = EMPTY_VALUE;
if (direction == 1)Alert("UP");
direction = -1;
}
//Print( " trend=",trend[x]);
}
return(0);
}
//+------------------------------------------------------------------+
Comments