shi_channel4(for trade channel)
Download scriptThis script is designed as a technical analysis tool for the MetaTrader trading platform. It automatically identifies and draws a price channel on your chart to help visualize market trends, specifically focusing on identifying structural boundaries such as "support" and "resistance" levels.
Core Logic Overview
The indicator operates by scanning historical price data to find significant turning points in the market. Instead of just looking at every price movement, it uses a logic based on "fractals"—which are essentially peaks and valleys in price action.
1. Identification of Key Points The script looks back over a specific number of bars (which can be adjusted by the user) to find two critical points:
- A significant high point (peak).
- A significant low point (valley).
Once these two points are identified, the script uses them as anchors to construct a linear channel.
2. Trend and Channel Calculation By connecting these two anchor points, the script calculates the "slope" of the market.
- If the price is moving upward from the low to the high, it identifies an ascending trend.
- If the price is moving downward from the high to the low, it identifies a descending trend.
The script then projects this channel forward into the future based on your settings, allowing you to see where the current market trajectory is headed.
3. Dynamic Price Tracking The logic isn't static; it continuously monitors the "live" price as it moves. It calculates the distance between the current price and the boundaries of the identified channel. A key feature of this script is its ability to detect "breaches." If the price moves outside of the expected path of the channel, the script identifies these interactions.
4. Visual Indicators and Labels To make the data easy for a human trader to read, the script provides several visual cues:
- Trend Lines: It draws physical lines on the chart to represent the channel boundaries.
- Automated Labels: The script dynamically places text labels (such as "Buy," "Sell," or "Take") based on two factors:
- The current direction of the trend.
- Whether the market is currently in a "flat" (sideways) state or a trending state.
- Flat Market Detection: The script includes a specific logic to determine if the market is moving sideways. It compares the width of the channel against the average volatility of the asset. If the movement is too small relative to normal volatility, it marks the market as "flat."
Summary for the User
In plain terms, this tool acts like an automated trend-line drawer. It looks at past highs and lows to draw a path for the current price. It then watches the price live; if the price stays within the lines, it shows you the trend. If the price interacts with those lines or moves into a sideways "flat" zone, it updates the text labels on your screen to suggest whether the market is leaning toward a bullish or bearish bias.
//+------------------------------------------------------------------+
//| 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 Red
double ExtMapBuffer1[];
//---- input parameters
extern int AllBars=240;
extern int BarsForFract=2;
extern double flatCriteria=0.25;
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;
int trend,flat;
//+------------------------------------------------------------------+
//| 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("TL1");
ObjectDelete("TL2");
ObjectDelete("MIDL");
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int X = (Ask-P1);
int X2 = (Bid-P2);
//----
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;
X = P1;
}
}
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 áàðîâ
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];
//Print("P1=",P1," P2=",P2);
//Åñëè íå áûëî ïåðåñå÷åíèÿ êàíàëà, òî 0, èíà÷å ñòàâèì ïñèñó.
if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
//DelObj();
if (Step>0) trend=-1; else trend=1;
if (MathAbs(Step*20)>=2*iATR(NULL,0,40,0)*flatCriteria) flat=0; else flat=1;
if (ObjectFind("TL1")==-1)
{
ObjectCreate("TL1",OBJ_TREND,0,T2,PP+Step*AB,T1,PP);
}
else
{
ObjectSet("TL1", OBJPROP_TIME1,T2);
ObjectSet("TL1", OBJPROP_PRICE1,PP+Step*AB);
ObjectSet("TL1", OBJPROP_TIME2,T1);
ObjectSet("TL1", OBJPROP_PRICE2,PP);
}
/*
ObjectSet("TL1",OBJPROP_COLOR,ForestGreen);
ObjectSet("TL1",OBJPROP_WIDTH,2);
ObjectSet("TL1",OBJPROP_STYLE,STYLE_SOLID);
*/
if (ObjectFind("TL2")==-1)
{
ObjectCreate("TL2",OBJ_TREND,0,T2,P2,T1,P1);
}
else
{
ObjectSet("TL2", OBJPROP_TIME1,T2);
ObjectSet("TL2", OBJPROP_PRICE1,P2);
ObjectSet("TL2", OBJPROP_TIME2,T1);
ObjectSet("TL2", OBJPROP_PRICE2,P1);
}
/*
ObjectSet("TL2",OBJPROP_COLOR,ForestGreen);
ObjectSet("TL2",OBJPROP_WIDTH,2);
ObjectSet("TL2",OBJPROP_STYLE,STYLE_SOLID);
*/
/* if (ObjectFind("MIDL")==-1)
{
ObjectCreate("MIDL",OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2);
}
else
{
ObjectSet("MIDL", OBJPROP_TIME1,T2);
ObjectSet("MIDL", OBJPROP_PRICE1,(P2+PP+Step*AB)/2);
ObjectSet("MIDL", OBJPROP_TIME2,T1);
ObjectSet("MIDL", OBJPROP_PRICE2,(P1+PP)/2);
}
*/
/*
ObjectSet("MIDL",OBJPROP_COLOR,ForestGreen);
ObjectSet("MIDL",OBJPROP_WIDTH,1);
ObjectSet("MIDL",OBJPROP_STYLE,STYLE_DOT);
*/
if (flat==1)
{
//Print("flat=1");
if(PP+Step*AB>P2)
{
ObjectSetText("TL1","Sell");
ObjectSetText("TL2","Buy");
}
else
{
ObjectSetText("TL2","Sell");
ObjectSetText("TL1","Buy");
}
}
else
{
//Print("flat=0");
if((PP+Step*AB>P2)&&(trend==-1))
{
ObjectSetText("TL1","Sell");
ObjectSetText("TL2","Take");
}
if((PP+Step*AB>P2)&&(trend==1))
{
ObjectSetText("TL1","Take");
ObjectSetText("TL2","Buy");
}
if((PP+Step*AB<P2)&&(trend==-1))
{
ObjectSetText("TL2","Sell");
ObjectSetText("TL1","Take");
}
if((PP+Step*AB<P2)&&(trend==1))
{
ObjectSetText("TL2","Take");
ObjectSetText("TL1","Buy");
}
}
//----
// Comment("Âåðõ êàíàëà = ",PP,"\n","Ñåðåäèíà = ",(P1+PP)/2,"\n","Íèç êàíàëà = ",P1,"\n","Øèðèíà = ",(PP-P1),"\n","Êàñàíèå Âåðõà = ",-(PP-Bid)," ïèïñîâ","\n","Êàñàíèå Íèçà = ",(P1-Ask)," ïèïñîâ");
// return(0);
}
//+------------------------------------------------------------------+
Comments