SHI_Channel_Mod
Download scriptThis tool is designed to automatically identify and project a dynamic price channel on your trading chart based on historical price behavior. Rather than relying on standard moving averages or fixed percentages, it uses geometric logic to find "Fractals"—significant peak and valley points in the market.
How the Logic Works
1. Identifying Key Structural Points The script first scans a specific range of historical data (the number of bars is customizable) to find significant high and low points. It looks for "Fractals," which are price points where the high or low of a specific candle is higher/lower than all its immediate neighbors. By finding two of these distinct points—one peak and one valley—the script establishes the foundation for the channel.
2. Calculating the Channel Slope Once it identifies these two structural points, the script calculates the "Step" or the rate of change between them. It determines how much the price moved over what distance. This calculation allows the tool to project a linear trajectory (a slope) forward in time. This resulting line represents the expected path of the market based on the most recent significant structure.
3. Dynamic Trend Projection The tool automatically draws three visual components on your chart:
- The Upper Boundary: A trend line representing the upper limit of the price movement.
- The Lower Boundary: A trend line representing the lower limit.
- The Median Line: A dotted center line that represents the equilibrium point of the channel.
The script is intelligent enough to adjust these lines based on whether the market structure is currently in an upward or downward trend, ensuring the boundaries remain relevant to current price action.
4. Intelligent Price Violations (Alerts) Beyond just drawing lines, the script monitors live price action against this calculated channel. It features a logic system to notify you when specific events occur:
- Breakout Alerts: It monitors if the current price closes on the opposite side of the median line or the boundary lines, signaling a potential shift in momentum or a trend breakout.
- Directional Change Alerts: The script tracks the overall orientation of the channel. If the trend logic flips from "Up" to "Down" (or vice versa), it can trigger an alert to notify you of a major structural shift in market sentiment.
Summary for the User
In simple terms, this script acts as a "Trend Path Mapper." It looks back at how the market behaved recently, finds the most significant highs and lows, and draws a channel that shows where the price is likely headed. If the price breaks out of these boundaries or the trend switches directions, the script provides real-time alerts to keep you informed of potential trading opportunities.
//+------------------------------------------------------------------+
//| SHI_Channel.mq4 |
//| Copyright © 2004, Shurka & Kevin |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, Shurka & Kevin"
#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Pink
double ExtMapBuffer1[];
//---- input parameters
extern int AllBars=240;
extern int BarsForFract=13;
extern int PipsAlertMode = 1;
extern int DirectionAlertMode = 1;
int CurrentBar=0;
double Step=0;
int B1=-1,B2=-1;
int UpDown=0;
double P1=0,P2=0,PP=0;
int i=0,AB=300,BFF=0;
int ishift=0;
double iprice=0;
datetime T1,T2;
extern color clr=Red;
double alertBar;
double last;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,164);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexEmptyValue(0,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
void DelObj()
{
ObjectDelete("2TL1");
ObjectDelete("2TL2");
ObjectDelete("2MIDL");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
if ((AllBars==0) || (Bars<AllBars)) AB=Bars; else AB=AllBars; //AB-êîëè÷åñòâî îáñ÷èòûâàåìûõ áàðîâ
if (BarsForFract>0)
BFF=BarsForFract;
else
{
switch (Period())
{
case 1: BFF=12; break;
case 5: BFF=48; break;
case 15: BFF=24; break;
case 30: BFF=24; break;
case 60: BFF=12; break;
case 240: BFF=15; break;
case 1440: BFF=10; break;
case 10080: BFF=6; break;
default: DelObj(); return(-1); break;
}
}
CurrentBar=2; //ñ÷èòàåì ñ òðåòüåãî áàðà, ÷òîáû ôðàêòàë "çàêðåïèëñÿ
B1=-1; B2=-1; UpDown=0;
while(((B1==-1) || (B2==-1)) && (CurrentBar<AB))
{
//UpDown=1 çíà÷èò ïåðâûé ôðàêòàë íàéäåí ñâåðõó, UpDown=-1 çíà÷èò ïåðâûé ôðàêòàë
//íàéäåí ñíèçó, UpDown=0 çíà÷èò ôðàêòàë åù¸ íå íàéäåí.
//Â1 è Â2 - íîìåðà áàðîâ ñ ôðàêòàëàìè, ÷åðåç íèõ ñòðîèì îïîðíóþ ëèíèþ.
//Ð1 è Ð2 - ñîîòâåòñòâåííî öåíû ÷åðåç êîòîðûå áóäåì ëèíèþ ïðîâîäèòü
if((UpDown<1) && (CurrentBar==Lowest(Symbol(),Period(),MODE_LOW,BFF*2+1,CurrentBar-BFF)))
{
if(UpDown==0) { UpDown=-1; B1=CurrentBar; P1=Low[B1]; }
else { B2=CurrentBar; P2=Low[B2];}
}
if((UpDown>-1) && (CurrentBar==Highest(Symbol(),Period(),MODE_HIGH,BFF*2+1,CurrentBar-BFF)))
{
if(UpDown==0) { UpDown=1; B1=CurrentBar; P1=High[B1]; }
else { B2=CurrentBar; P2=High[B2]; }
}
CurrentBar++;
}
if((B1==-1) || (B2==-1)) {DelObj(); return(-1);} // Çíà÷èò íå íàøëè ôðàêòàëîâ ñðåäè 300 áàðîâ 8-)
Step=(P2-P1)/(B2-B1);//Âû÷èñëèëè øàã, åñëè îí ïîëîæèòåëüíûé, òî êàíàë íèñõîäÿùèé
P1=P1-B1*Step; B1=0;//ïåðåñòàâëÿåì öåíó è ïåðâûé áàð ê íóëþ
//À òåïåðü îïîðíóþ òî÷êó ïðîòèâîïîëîæíîé ëèíèè êàíàëà.
ishift=0; iprice=0;
if(UpDown==1)
{
PP=Low[2]-2*Step;
for(i=3;i<=B2;i++)
{
if(Low[i]<PP+Step*i) { PP=Low[i]-i*Step; }
}
if(Low[0]<PP) {ishift=0; iprice=PP;}
if(Low[1]<PP+Step) {ishift=1; iprice=PP+Step;}
if(High[0]>P1) {ishift=0; iprice=P1;}
if(High[1]>P1+Step) {ishift=1; iprice=P1+Step;}
}
else
{
PP=High[2]-2*Step;
for(i=3;i<=B2;i++)
{
if(High[i]>PP+Step*i) { PP=High[i]-i*Step;}
}
if(Low[0]<P1) {ishift=0; iprice=P1;}
if(Low[1]<P1+Step) {ishift=1; iprice=P1+Step;}
if(High[0]>PP) {ishift=0; iprice=PP;}
if(High[1]>PP+Step) {ishift=1; iprice=PP+Step;}
}
//Òåïåðü ïåðåñòàâèì êîíå÷íóþ öåíó è áàð íà ÀÂ, ÷òîáû ëèíèè êàíàëà ðèñîâàëèñü ïîäëèííåå
P2=P1+AB*Step;
T1=Time[B1]; T2=Time[AB];
//Åñëè íå áûëî ïåðåñå÷åíèÿ êàíàëà, òî 0, èíà÷å ñòàâèì ïñèñó.
if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
DelObj();
// Alerts Start
if(last == 0 && P1 < P2) last=1;
if(last == 0 && P1 > P2) last = 2;
if (PipsAlertMode > 0 && Close[1]>=PP && Close[0]<PP && Bars>alertBar) {Alert("SHI Channel Pip Breakout Down on ",Symbol()," ",Period());alertBar = Bars;}
if (PipsAlertMode > 0 && Close[1]<=PP && Close[0]>PP && Bars>alertBar) {Alert("SHI Channel Pip Breakout Up on ",Symbol()," ",Period());alertBar = Bars;}
if(DirectionAlertMode > 0 && last == 1 && P1 < P2 && Bars>alertBar) {Alert("SHI_Channel Alert changed Direction to Down on ",Symbol()," ",Period());alertBar = Bars;last=2;}
if(DirectionAlertMode > 0 && last == 2 && P1 > P2 && Bars>alertBar) {Alert("SHI_Channel Alert changed Direction to Up on ",Symbol()," ",Period());alertBar = Bars;last=1;}
// Alerts Done
ObjectCreate("2TL1",OBJ_TREND,0,T2,PP+Step*AB,T1,PP);
ObjectSet("2TL1",OBJPROP_COLOR,clr);
ObjectSet("2TL1",OBJPROP_WIDTH,2);
ObjectSet("2TL1",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("2TL2",OBJ_TREND,0,T2,P2,T1,P1);
ObjectSet("2TL2",OBJPROP_COLOR,clr);
ObjectSet("2TL2",OBJPROP_WIDTH,2);
ObjectSet("2TL2",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("2MIDL",OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2);
ObjectSet("2MIDL",OBJPROP_COLOR,clr);
ObjectSet("2MIDL",OBJPROP_WIDTH,1);
ObjectSet("2MIDL",OBJPROP_STYLE,STYLE_DOT);
//----
return(0);
}
//+------------------------------------------------------------------+
Comments