K-#MTF_shi_channel_1[1].1

Download script
Author: Alex_Bugalter
K-#MTF_shi_channel_1[1].1

This script is designed as a visual technical analysis tool for the MetaTrader 4 platform. It automatically identifies and maps out price channels based on specific market structures, helping traders visualize trends and potential reversal zones.

Core Logic and Functionality

1. Structural Identification (Fractals) The core logic begins by scanning historical price data to find "fractals"—specific points where the market has reached a local high or low relative to the bars surrounding it. The script automatically adjusts the sensitivity of these scans based on the timeframe you are currently viewing (e.g., shorter timeframes use smaller look-back windows, while larger timeframes use broader ones).

2. Channel Construction Once two significant structural points (fractals) are identified, the script calculates a constant "Step" or slope between them. It uses this slope to project a linear channel onto your chart. This provides a visual representation of the current market's trajectory:

  • Upper Boundary: Represents the ceiling of the price movement.
  • Lower Boundary: Represents the floor of the price movement.
  • Median Line: A dotted center line that helps identify the "equilibrium" price of the current trend.

3. Dynamic Trend Analysis The script distinguishes between different types of market behavior:

  • Trend Identification: It determines whether the channel is moving upward or downward based on the calculated slope.
  • Volatility Filtering (Flat Market Detection): The tool includes a built-in mechanism to detect "flat" or sideways markets. It compares the width of the price channel against a volatility indicator (ATR). If the price movement is too narrow relative to recent history, it recognizes the market as being in a consolidation phase rather than a clear trend.

4. Automated Labeling and Signals One of the primary features for the user is the automated labeling on the chart. Based on the interaction between the current price and the calculated boundaries, the script applies text labels to the lines:

  • Buy/Sell Labels: These appear during specific market conditions to indicate potential entry zones or trend shifts.
  • Take Labels: These are used to identify target areas based on the relationship between the trend direction and the channel boundaries.

Visual Output

The script provides a clean, real-time visual experience by drawing three distinct objects on your chart:

  1. Two solid lines representing the outer bounds of the price channel.
  2. A dotted line representing the median path of the trend.
  3. Dynamic text labels that change in real-time to provide immediate "Buy," "Sell," or "Take" prompts based on the mathematical logic described above.
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each barSeries array that contains open time of each bar
Indicators Used
Indicator of the average true range
Miscellaneous
Implements a curve of type %1
10 Views
0 Downloads
0 Favorites
K-#MTF_shi_channel_1[1].1
//+------------------------------------------------------------------+ 
//| SHI_Channel.mq4                                                  | 
//|Alex_Bugalter                                                     |
//+------------------------------------------------------------------+ 
#property copyright "Alex_Bugalter" 
#property link "Alex_Bugalter@treide.ru"  

#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_color1 Red 

double ExtMapBuffer1[]; 
//---- input parameters 
extern int TimeFrame=60;
extern int AllBars=240; 
extern int BarsForFract=0; 
extern double flatCriteria=0.25;
extern color collor=Lime;

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() 
{ 
//---- 
   DelObj();
//---- 
return(0); 
} 

void DelObj() 
{ 

ObjectDelete("TL1"+TimeFrame+BarsForFract); 
ObjectDelete("TL2"+TimeFrame+BarsForFract); 
ObjectDelete("MIDL"+TimeFrame+BarsForFract); 
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function | 
//+------------------------------------------------------------------+ 
int start() 
   { 
 
   int    i,limit,y=0;
   int counted_bars=IndicatorCounted();  
 
      limit=Bars-counted_bars;
 
   int X = (Ask-P1);
   int X2 = (Bid-P2);
   //---- 
   

   
    if ((AllBars==0) || (Bars<AllBars)) AB=Bars; else AB=AllBars; //AB-êîëè÷åñòâî îáñ÷èòûâàåìûõ áàðîâ 
//    string TimeFrame;
 
   if (BarsForFract>0) 
      BFF=BarsForFract; 
   else 
      { 
      switch (TimeFrame) 
      { 
      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=y-1; B2=y-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(),TimeFrame,MODE_LOW,BFF*2+1,CurrentBar-BFF))) 
         { 
         if(UpDown==0) 
            {
            UpDown=-1; B1=CurrentBar; P1=iLow(Symbol(),TimeFrame,B1); 
            } 
         else 
            { 
            B2=CurrentBar; P2=iLow(Symbol(),TimeFrame,B2);
            } 
         } 
      if((UpDown>-1) && (CurrentBar==Highest(Symbol(),TimeFrame,MODE_HIGH,BFF*2+1,CurrentBar-BFF)))
         { 
         if(UpDown==0) 
            { 
            UpDown=1; B1=CurrentBar; P1=iHigh(Symbol(),TimeFrame,B1); 
            } 
         else 
            { 
            B2=CurrentBar; P2=iHigh(Symbol(),TimeFrame,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=iLow(Symbol(),TimeFrame,2)-2*Step; 
      for(i=3;i<=B2;i++) 
         { 
         if(iLow(Symbol(),TimeFrame,i)<PP+Step*i) 
            { 
            PP=iLow(Symbol(),TimeFrame,i)-i*Step; 
            } 
         } 
      if(iLow(Symbol(),TimeFrame,0)<PP) 
         {
         ishift=0; 
         iprice=PP;
         } 
      if(iLow(Symbol(),TimeFrame,1)<PP+Step) 
         {
         ishift=1; 
         iprice=PP+Step;
         } 
      if(iHigh(Symbol(),TimeFrame,0)>P1) 
         {
         ishift=0; 
         iprice=P1;
         } 
      if(iHigh(Symbol(),TimeFrame,1)>P1+Step) 
         {
         ishift=1; 
         iprice=P1+Step;
         } 
      } 
   else 
      { 
      PP=iHigh(Symbol(),TimeFrame,2)-2*Step; 
      for(i=3;i<=B2;i++) 
         { 
         if(iHigh(Symbol(),TimeFrame,i)>PP+Step*i) 
            { 
            PP=iHigh(Symbol(),TimeFrame,i)-i*Step;
            } 
         } 
      if(iLow(Symbol(),TimeFrame,0)<P1) 
         {
         ishift=0; 
         iprice=P1;
         } 
      if(iLow(Symbol(),TimeFrame,1)<P1+Step) 
         {
         ishift=1; 
         iprice=P1+Step;
         } 
      if(iHigh(Symbol(),TimeFrame,0)>PP) 
         {
         ishift=0; 
         iprice=PP;
         } 
      if(iHigh(Symbol(),TimeFrame,1)>PP+Step) 
         {
         ishift=1; 
         iprice=PP+Step;
         } 
      } 
      
   //Òåïåðü ïåðåñòàâèì êîíå÷íóþ öåíó è áàð íà ÀÂ, ÷òîáû ëèíèè êàíàëà ðèñîâàëèñü ïîäëèííåå 
   P2=P1+AB*Step; 
    T1=Time[iBarShift(Symbol(),0,iTime(Symbol(),TimeFrame,B1))]; 
    T2=Time[iBarShift(Symbol(),0,iTime(Symbol(),TimeFrame,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(Symbol(),TimeFrame,40,0)*flatCriteria) flat=0; else flat=1;
   
   if (ObjectFind("TL1"+TimeFrame+BarsForFract)==-1)   
      {
      ObjectCreate("TL1"+TimeFrame+BarsForFract,OBJ_TREND,0,T2,PP+Step*AB,T1,PP); 
      ObjectSet("TL1"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor); 
		ObjectSet("TL1"+TimeFrame+BarsForFract,OBJPROP_WIDTH,2); 
      }
   else
      {
      ObjectSet("TL1"+TimeFrame+BarsForFract, OBJPROP_TIME1,T2);       
      ObjectSet("TL1"+TimeFrame+BarsForFract, OBJPROP_PRICE1,PP+Step*AB);       
      ObjectSet("TL1"+TimeFrame+BarsForFract, OBJPROP_TIME2,T1);       
      ObjectSet("TL1"+TimeFrame+BarsForFract, OBJPROP_PRICE2,PP); 
      ObjectSet("TL1"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor); 
		ObjectSet("TL1"+TimeFrame+BarsForFract,OBJPROP_WIDTH,2);       
      }   
/*
   ObjectSet("TL1",OBJPROP_COLOR,ForestGreen); 
   ObjectSet("TL1",OBJPROP_WIDTH,2); 
   ObjectSet("TL1",OBJPROP_STYLE,STYLE_SOLID);
*/
   if (ObjectFind("TL2"+TimeFrame+BarsForFract)==-1) 
      {
      ObjectCreate("TL2"+TimeFrame+BarsForFract,OBJ_TREND,0,T2,P2,T1,P1); 
      ObjectSet("TL2"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor); 
		ObjectSet("TL2"+TimeFrame+BarsForFract,OBJPROP_WIDTH,2); 
      }
   else
      {
      ObjectSet("TL2"+TimeFrame+BarsForFract, OBJPROP_TIME1,T2);       
      ObjectSet("TL2"+TimeFrame+BarsForFract, OBJPROP_PRICE1,P2);       
      ObjectSet("TL2"+TimeFrame+BarsForFract, OBJPROP_TIME2,T1);       
      ObjectSet("TL2"+TimeFrame+BarsForFract, OBJPROP_PRICE2,P1);   
     	ObjectSet("TL2"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor); 
		ObjectSet("TL2"+TimeFrame+BarsForFract,OBJPROP_WIDTH,2);     
      }   
/*
   ObjectSet("TL2",OBJPROP_COLOR,ForestGreen); 
   ObjectSet("TL2",OBJPROP_WIDTH,2); 
   ObjectSet("TL2",OBJPROP_STYLE,STYLE_SOLID); 
*/
   if (ObjectFind("MIDL"+TimeFrame+BarsForFract)==-1)  
      {
      ObjectCreate("MIDL"+TimeFrame+BarsForFract,OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2); 
     	ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor); 
		ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_WIDTH,1); 
		ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_STYLE,STYLE_DOT); 
      }
   else
      {
      ObjectSet("MIDL"+TimeFrame+BarsForFract, OBJPROP_TIME1,T2);       
      ObjectSet("MIDL"+TimeFrame+BarsForFract, OBJPROP_PRICE1,(P2+PP+Step*AB)/2);       
      ObjectSet("MIDL"+TimeFrame+BarsForFract, OBJPROP_TIME2,T1);       
      ObjectSet("MIDL"+TimeFrame+BarsForFract, OBJPROP_PRICE2,(P1+PP)/2);    
      ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_WIDTH,1); 
		ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_STYLE,STYLE_DOT); 
		ObjectSet("MIDL"+TimeFrame+BarsForFract,OBJPROP_COLOR,collor);   
      }   
/*
   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"+TimeFrame+BarsForFract,"Sell"); 
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Buy"); 
         }
      else
         {
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Sell"); 
         ObjectSetText("TL1"+TimeFrame+BarsForFract,"Buy"); 
         }   
      }
   else
      {
      //Print("flat=0");
      if((PP+Step*AB>P2)&&(trend==-1))
         {
         ObjectSetText("TL1"+TimeFrame+BarsForFract,"Sell"); 
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Take"); 
         }   
      if((PP+Step*AB>P2)&&(trend==1))
         {
         ObjectSetText("TL1"+TimeFrame+BarsForFract,"Take"); 
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Buy"); 
         }
      if((PP+Step*AB<P2)&&(trend==-1))
         {
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Sell"); 
         ObjectSetText("TL1"+TimeFrame+BarsForFract,"Take"); 
         }   
      if((PP+Step*AB<P2)&&(trend==1))
         {
         ObjectSetText("TL2"+TimeFrame+BarsForFract,"Take"); 
         ObjectSetText("TL1"+TimeFrame+BarsForFract,"Buy"); 
         }
      }   
 //}  //---- 
 //  Comment("Âåðõ êàíàëà = ",PP,"\n","Ñåðåäèíà = ",(P1+PP)/2,"\n","Íèç êàíàëà = ",P1,"\n","Øèðèíà = ",(PP-P1),"\n","Êàñàíèå Âåðõà = ",-(PP-Bid)," ïèïñîâ","\n","Êàñàíèå Íèçà = ",(P1-Ask)," ïèïñîâ"); 
   return(0); 
   } 
//+------------------------------------------------------------------+

Comments