Gliding_Channels_[CF]_v2
Download scriptThis script is designed to identify and visualize "Gliding Channels" on a price chart. It automates the process of finding specific structural trends in market movement by identifying key price turning points (extremums) and connecting them to form dynamic boundaries.
Core Logic Overview
The tool works by scanning historical price data to locate significant highs and lows. Unlike standard indicators that might react to every small wiggle, this logic looks for "confirmed" peaks and valleys—points where the price has clearly reached a local extreme and begun to move in the opposite direction.
How the Algorithm Identifies Turning Points
To ensure the channels are based on meaningful market structure rather than "noise," the script applies several filters:
- Confirmation Rule: A point is only considered an extremum if it stands out against at least two preceding candles and two following candles. This ensures that a peak or valley is truly a turning point.
- Group Handling: If multiple candles have nearly identical highs or lows, the script treats them as a single "group." It then selects the most recent candle in that group to anchor the channel line, ensuring the geometry remains clean.
- Distance Constraint: There is an internal check to ensure that these identified points are sufficiently separated, preventing the creation of erratic or overlapping lines in consolidated markets.
Channel Construction Mechanics
Once the key turning points are identified, the script builds the channels using a three-point geometric approach:
- Primary Anchors: It selects the most significant recent high and low.
- The Third Point: It identifies a third extremum to establish the "glide" or angle of the channel. Depending on which points occurred first chronologically, the script automatically adjusts the orientation to ensure the channel follows the logical flow of the trend (upward or downward).
- Parallelism: The logic ensures that the lines forming the boundaries are parallel. This creates a corridor that represents the current momentum of the market.
Dynamic Features and Alerts
- Trend Detection: The script determines whether the channel is currently trending upward or downward based on the relative positions of the identified peaks and valleys.
- Real-Time Monitoring: It continuously calculates the current upper and lower boundaries of the drawn channels.
- Automated Notifications: If the current market price touches or enters the upper boundary, the script can trigger a "Sell" alert; if it touches the lower boundary, it triggers a "Buy" alert.
- Visual Data: On the chart, it provides visual labels for the identified turning points and draws the channels as rays (extending into the future) or segments, depending on the user's settings.
Summary of User Experience
From a visitor's perspective, this tool acts as a structural mapper. It automatically looks back at the market history, finds the most reliable "anchor points" that define the current trend, and draws a parallel corridor. It then watches for the price to hit the edges of that corridor to signal potential trend exhaustion or reversal opportunities.
//+---------------------------------------------------------------------+
//| Gliding Channels [CF].mq4 |
//| Crazy_Fox [UA] |
//| e-mail: crazy_fox@ua.fm |
//| ICQ: 529330 |
//| ÄËß ÍÅÊÎÌÌÅÐ×ÅÑÊÎÃÎ ÈÑÏÎËÜÇÎÂÀÍÈß, ÏÓÁËÈÊÀÖÈß ÐÀÇÐÅØÅÍÀ ÒÎËÜÊÎ Ñ |
//| ÓÊÀÇÀÍÈÅÌ ÏÅÐÂÎÈÑÒÎ×ÍÈÊÀ (http://www.community.finlist.org) |
//| È ÈÌÅÍÈ ÀÂÒÎÐÀ (Crazy_Fox [UA]) |
//| ÐÅÄÀÊÒÈÐÎÂÀÍÈÅ ÈÑÕÎÄÍÎÃÎ ÊÎÄÀ ÐÀÇÐÅØÀÅÒÑß ÒÎËÜÊÎ ÏÐÈ ÓÑËÎÂÈÈ |
//| ÑÎÕÐÀÍÅÍÈß ÄÀÍÍÎÃÎ ÒÅÊÑÒÀ, ÀÄÐÅÑÎÂ È ÑÑÛËÎÊ. ÏÐÎÄÀÆÀ, ÈÍÄÈÊÀÒÎÐÀ, |
//| ÎÒÄÅËÜÍÛÕ ×ÀÑÒÅÉ ÈÑÕÎÄÍÎÃÎ ÊÎÄÀ, ÃÐÀÔÈÊÎÂ, ÈÌ ÏÎÑÒÐÎÅÍÍÛÕ ÇÀÏÐÅÙÅÍÀ |
//| ÀÂÒÎÐ ÍÅ ÍÅÑ¨Ò ÎÒÂÅÒÑÒÂÅÍÍÎÑÒÈ ÇÀ ÂÎÇÌÎÆÍÛÅ ÓÁÛÒÊÈ, ÏÎËÓ×ÅÍÍÛÅ |
//| Â ÐÅÇÓËÜÒÀÒÅ ÐÀÁÎÒÛ ÈÍÄÈÊÀÒÎÐÀ |
//| Ñêîëüçÿùèå êàíàëû Áàðèøïîëüöà |
//| Êàíàëû ñòðîÿòñÿ ïî òðåì ïîñëåäíèì ýêñòðåìóìàì - ëèíèÿ ïî äâóì |
//| íèçàì è ïàðàëëåëüíàÿ ÷åðåç âåðøèíó, èëè íàîáîðîò, ëèíèÿ ïî äâóì |
//| âåðøèíàì è ïàðàëëåëüíàÿ ïî íèçó. Ëèíèè ñòðîÿòñÿ ÏÎ ÌÀÊÑÈÌÀËÜÍÛÌ |
//| (ÌÈÍÈÌÀËÜÍÛÌ) ÇÍÀ×ÅÍÈßÌ, òî åñòü ïî òåíÿì ñâå÷åé. Ýêñòðåìóì |
//| èäåíòèôèöèðóåòñÿ ïðè íå ìåíåå 2 ñâå÷åê äî è 2 ñâå÷åê ïîñëå åãî |
//| ïðîõîæäåíèÿ. Òî åñòü äëÿ ìàêñèìóìà íåîáõîäèìî íå ìåíåå 2 ñâå÷åê |
//| ìåíüøå ìàêñèìàëüíîé ñâå÷è äî, è íå ìåíåå 2 ñâå÷åê ìåíüøå ìàêñèìóìà |
//| - ïîñëå. Ðàññòîÿíèå ìåæäó ñîñåäíèìè ýêñòðåìóìàìè íå îãðàíè÷èâàåòñÿ. |
//| Âñòðå÷àþòñÿ "ãðóïïîâûå ýêñòðåìóìû" - äâå èëè äàæå òðè ñâå÷è ñ |
//| ïðàêòè÷åñêè îäèíàêîâûìè ìàêñèìóìàìè (ìèíèìóìàìè). Èäåíòèôèöèðóåì èõ |
//| êàê îäèí ýêñòðåìóì, ëèíèþ êàíàëà ïðîâîäèì ÷åðåç âåðõ (íèç) ïðàâîé |
//| èç íèõ. |
//+---------------------------------------------------------------------+
#property copyright "Crazy_Fox [UA]"
#property link "http://finlist.ru"
#property indicator_chart_window
//----
extern int Start_Bar = 2;
extern int Line_Width = 2;
extern int Num_of_Channels = 1;
extern int Sensitivity = 0;
extern bool Show_Labels = true;
extern bool Channel_as_Ray = true;
extern bool Show_Alerts = true;
extern color Channel_Color1 = Blue;
extern color Channel_Color2 = LightSkyBlue;
extern color Channel_Color3 = DeepPink;
extern color Channel_Color4 = DodgerBlue;
extern color Channel_Color5 = PaleGreen;
extern color Channel_Color6 = DarkOrange;
extern color Channel_Color7 = DodgerBlue;
extern color Channel_Color8 = Yellow;
//----
double x1 = 0, y1 = 0,
x2 = 0, y2 = 0,
x_l = 0, y_l = 0,
x_f = 0, y_f = 0,
x_m = 0, y_m = 0,
step = 0,
l_border = 0, h_border=0,
Price = 0;
bool main_is_up = true, price_at_border = false;
int CurrentBar = 0,
Extremum[3],
TempExtremum[3],
Colors[8],
f_bar = 0, m_bar = 0, l_bar = 0;
//+------------------------------------------------------------------+
//| init |
//+------------------------------------------------------------------+
int init()
{
Colors[0] = Channel_Color1;
Colors[1] = Channel_Color2;
Colors[2] = Channel_Color3;
Colors[3] = Channel_Color4;
Colors[4] = Channel_Color5;
Colors[5] = Channel_Color6;
Colors[6] = Channel_Color7;
Colors[7] = Channel_Color8;
ArrayInitialize(Extremum, 0);
ArrayInitialize(TempExtremum, 0);
return(0);
}
//+------------------------------------------------------------------+
//| deinit |
//+------------------------------------------------------------------+
int deinit()
{
// Â öèêëå óäàëÿåì âñå îòðèñîâàííûå îáúåêòû
for (int i = 1; i <= Num_of_Channels; i++)
{
ObjectDelete("Channel_"+i);
if(Show_Labels)
{
ObjectDelete("Extremum_1_" + i);
ObjectDelete("Extremum_2_" + i);
ObjectDelete("Extremum_3_" + i);
}
}
Comment(" ");
ArrayInitialize(Extremum, 0);
ArrayInitialize(TempExtremum, 0);
return(0);
}
//+------------------------------------------------------------------+
//| min_ext |
//+------------------------------------------------------------------+
int min_ext(int bar)
{
int find, i, result;
result = 0;
i = bar;
find = 0;
while(find != 1)
{
if((Low[i-2] >= Low[i]) && (Low[i-1] >= Low[i]) && (Low[i] <= Low[i+1]) &&
(Low[i] <= Low[i+2]))
{
// Åñëè i ìåíüøå ëèáî ðàâíî äâóì áëèçñòîÿùèì ñâå÷àì è ñîñåäÿì ýòèõ ñâå÷åé -
// ýòî íàø ìèíèìóì
result=i;
find++;
}
i++;
}
return(result);
}
//+------------------------------------------------------------------+
//| max_ext |
//+------------------------------------------------------------------+
int max_ext(int bar)
{
int find, i, result;
result = 0;
i = bar;
find = 0;
while(find != 1)
{
if((High[i-2] <= High[i]) && (High[i-1] <= High[i]) && (High[i] >= High[i+1]) &&
(High[i] >= High[i+2]))
{
// Åñëè i áîëüøå ëèáî ðàâíî äâóì áëèçñòîÿùèì ñâå÷àì è ñîñåäÿì ýòèõ ñâå÷åé -
// ýòî íàø ìàêñèìóì
result=i;
find++;
}
i++;
}
return(result);
}
//+------------------------------------------------------------------+
//| find_extremums |
//+------------------------------------------------------------------+
void find_extremums(int i)
{
ArrayInitialize(Extremum, 0);
// Èùåì ïåðâûé ìèíèìóì, îïðåäåëÿåì êîîðäèíàòû
Extremum[0] = min_ext(i);
y1=Low[Extremum[0]];
x1=Time[Extremum[0]];
// Èùåì ïåðâûé ìàêñèìóì, îïðåäåëÿåì êîîðäèíàòû
Extremum[1] = max_ext(i);
y2=High[Extremum[1]];
x2=Time[Extremum[1]];
// Åñëè ìèíèìóì âñòðå÷àåòñÿ ðàíüøå ìàêñèìóìà - òðåòüÿ ýêñòðåìóì èùåì âíèçó
if(Extremum[0] < Extremum[1])
{
// Ôóíêöèÿ min_ext âîçâðàùàåò íîìåð ïåðâîãî ìèíèìóìà, ïîèñê íà÷èíàåòñÿ ñ
Extremum[2] = min_ext(Extremum[1]);
y_l = Low[Extremum[2]];
x_l = Time[Extremum[2]];
y_f = y1;
x_f = x1;
y_m = y2;
x_m = x2;
f_bar = Extremum[0];
m_bar = Extremum[1];
l_bar = Extremum[2];
main_is_up = false;
}
if(Extremum[0] > Extremum[1])
{
Extremum[2] = max_ext(Extremum[0]);
y_l = High[Extremum[2]];
x_l = Time[Extremum[2]];
y_f = y2;
x_f = x2;
y_m = y1;
x_m = x1;
f_bar = Extremum[1];
m_bar = Extremum[0];
l_bar = Extremum[2];
main_is_up = true;
}
if(Extremum[0] == Extremum[1])
if(min_ext(Extremum[0] + 1) < max_ext(Extremum[0] + 2))
{
Extremum[2] = min_ext(Extremum[0] + 2);
y_l = Low[Extremum[2]];
x_l = Time[Extremum[2]];
y_f = y1;
x_f = x1;
y_m = y2;
x_m = x2;
f_bar =Extremum[0];
m_bar = Extremum[1];
l_bar = Extremum[2];
main_is_up = false;
}
else
{
Extremum[2] = max_ext(Extremum[0] + 2);
y_l = High[Extremum[2]];
x_l = Time[Extremum[2]];
y_f = y2;
x_f = x2;
y_m = y1;
x_m = x1;
f_bar = Extremum[0];
m_bar = Extremum[1];
l_bar = Extremum[2];
main_is_up = true;
}
}
//+------------------------------------------------------------------+
//| indicator start function |
//+------------------------------------------------------------------+
int start()
{
int n = 1, c = 0, i = 0; // Ñ÷¸ò÷èê êàíàëîâ è ñ÷¸ò÷èê öâåòîâ
if(Start_Bar > 0)
CurrentBar = Start_Bar;
else
CurrentBar = 2; //Òåêóùèé áàð - 2 (äëÿ ïîäòâåðæäåíèÿ ýêñòðåìóìà)
ArrayInitialize(TempExtremum, 0);
find_extremums(CurrentBar);
// Íà ñêîëüêî èçìåíÿåòñÿ öåíà ñ êàæäîé ïîñëåäóþùåé ñâå÷¸é
step = (y_f - y_l) / (l_bar - f_bar);
if(main_is_up)
{
h_border = High[f_bar] + step*f_bar;
l_border = Low [m_bar] + step*m_bar;
}
else
{
l_border = Low [f_bar] + step*f_bar;
h_border = High[m_bar] + step*m_bar;
}
// Åñëè äîñòèãëè âåðõíåé ãðàíèöû êàíàëà
if((Bid >= (h_border - Sensitivity*Point)) && (Bid <= (h_border + Sensitivity*Point)))
{
if(Show_Alerts)
Alert("Âåðõ êàíàëà - ïðîäàæà.");
}
// Åñëè äîñòèãëè íèæíåé ãðàíèöû êàíàëà
if((Ask <= (l_border + Sensitivity*Point)) && (Ask >= (l_border - Sensitivity*Point)))
{
if(Show_Alerts)
Alert("Íèç êàíàëà - ïîêóïêà");
}
deinit();
Comment("Ïàðàìåòðû êàíàëà:\nØèðèíà = ", (h_border - l_border) / Point, "; Íàêëîí = ",
-step / Point, ";\nÂåðõíÿÿ ãðàíèöà = ", DoubleToStr(h_border, Digits),
"; Íèæíÿÿ ãðàíèöà = ", DoubleToStr(l_border, Digits), ";");
//  öèêëå îò 1 äî Num_of_Channels ðàñ÷èòûâàåì ýêñòðåìóìû è ðèñóåì êàíàë ñ íîìåðîì N
while(n <= Num_of_Channels)
{
TempExtremum[0] = Extremum[0];
TempExtremum[1] = Extremum[1];
TempExtremum[2] = Extremum[2];
find_extremums(CurrentBar);
if((TempExtremum[0] == Extremum[0]) && (TempExtremum[1] == Extremum[1]) &&
(TempExtremum[2] == Extremum[2]))
CurrentBar++;
else
{
//SetIndexStyle(0, DRAW_ARROW);
//SetIndexArrow(0, 164);
//SetIndexBuffer(0, ExtMapBuffer);
//SetIndexEmptyValue(0, 0.0);
if(Show_Labels)
{
ObjectCreate("Extremum_1_" + n, OBJ_ARROW, 0, x1, y1);
ObjectSet("Extremum_1_" + n, OBJPROP_COLOR, Colors[c]);
ObjectSet("Extremum_1_" + n, OBJPROP_WIDTH, 1);
ObjectSet("Extremum_1_" + n, OBJPROP_ARROWCODE, SYMBOL_LEFTPRICE);
ObjectSet("Extremum_1_" + n, OBJPROP_STYLE, STYLE_SOLID);
ObjectCreate("Extremum_2_" + n, OBJ_ARROW, 0, x2, y2);
ObjectSet("Extremum_2_" + n, OBJPROP_COLOR, Colors[c]);
ObjectSet("Extremum_2_" + n, OBJPROP_WIDTH, 1);
ObjectSet("Extremum_2_" + n, OBJPROP_ARROWCODE, SYMBOL_LEFTPRICE);
ObjectSet("Extremum_2_" + n, OBJPROP_STYLE, STYLE_SOLID);
ObjectCreate("Extremum_3_" + n, OBJ_ARROW, 0, x_l, y_l);
ObjectSet("Extremum_3_" + n, OBJPROP_COLOR, Colors[c]);
ObjectSet("Extremum_3_" + n, OBJPROP_WIDTH, 1);
ObjectSet("Extremum_3_" + n, OBJPROP_ARROWCODE, SYMBOL_RIGHTPRICE);
ObjectSet("Extremum_3_" + n, OBJPROP_STYLE, STYLE_SOLID);
}
ObjectCreate("Channel_" + n, OBJ_CHANNEL, 0, x_l, y_l, x_f, y_f, x_m, y_m);
ObjectSet("Channel_" + n, OBJPROP_COLOR, Colors[c]);
ObjectSet("Channel_" + n, OBJPROP_WIDTH, Line_Width);
ObjectSet("Channel_" + n, OBJPROP_RAY, Channel_as_Ray);
ObjectSet("Channel_" + n, OBJPROP_STYLE, STYLE_SOLID);
n++;
if(c < 7)
c++;
else
c=0;
}
CurrentBar++;
}
return(0);
}
//+------------------------------------------------------------------+
Comments