//+------------------------------------------------------------------+
//|                                                  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=0;
extern int       ChannelAlert=40;
extern int       SlopeAlert=25;
extern bool      ChannelTalk=false;
int CurrentBar=0;
int alertTag;
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 firsttime=0;
int MAPERIOD;
double ChannelWidth15;
double ChannelWidth60;
double ChannelWidth240;
// couleur15_Wide SpringGreen;
// couleur60_Wide="Lime";
// couleur240_Wide="LimeGreen";
// couleur15_Narrow LightSalmon;
// couleur60_Narrow="Red";
// couleur240_Narrow="OrangeRed";
//+------------------------------------------------------------------+
//| 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("TLM15_1");
	ObjectDelete("TLM15_2");
	ObjectDelete("MIDL15");
	ObjectDelete("TLM60_1");
	ObjectDelete("TLM60_2");
	ObjectDelete("MIDL60");
	ObjectDelete("TLM240_1");
	ObjectDelete("TLM240_2");
	ObjectDelete("MIDL240");
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  	DelObj();

   int    counted_bars=IndicatorCounted();
   string symbolTxt;
   string WhatTxt;
   
if (StringSubstr(Symbol(),0,6)=="EURUSD") { symbolTxt = "Euro Dollar"; }
if (StringSubstr(Symbol(),0,6)=="EURJPY") { symbolTxt = "Euro Yen "; }
if (StringSubstr(Symbol(),0,6)=="EURAUD") { symbolTxt = "Euro Aussie "; }
if (StringSubstr(Symbol(),0,6)=="EURCAD") { symbolTxt = "Euro Canadian"; }
if (StringSubstr(Symbol(),0,6)=="EURCHF") { symbolTxt = "Euro Swiss"; }
if (StringSubstr(Symbol(),0,6)=="GBPUSD") { symbolTxt = "Cable Dollar"; }
if (StringSubstr(Symbol(),0,6)=="GBPJPY") { symbolTxt = "Cable Yen "; }
if (StringSubstr(Symbol(),0,6)=="GBPCHF") { symbolTxt = "Cable Swiss"; }
if (StringSubstr(Symbol(),0,6)=="AUDUSD") { symbolTxt = "Aussie"; }
if (StringSubstr(Symbol(),0,6)=="USDCHF") { symbolTxt = "Swiss Dollar"; }
if (StringSubstr(Symbol(),0,6)=="USDCAD") { symbolTxt = "Canada"; }
if (StringSubstr(Symbol(),0,6)=="USDJPY") { symbolTxt = "Yen Dollar"; } 
if (StringSubstr(Symbol(),0,6)=="CHFJPY") { symbolTxt = "Swiss Yen "; }
if (StringSubstr(Symbol(),0,6)=="GOLD")   { symbolTxt = "Gold"; }

   MAPERIOD=15;
//---- 
	if ((AllBars==0) || (iBars(Symbol(),MAPERIOD)<AllBars)) AB=iBars(Symbol(),MAPERIOD); else AB=AllBars; 
	if (BarsForFract>0) 
		BFF=BarsForFract; 
	else
      BFF=24;

	CurrentBar=2; 
	B1=-1; B2=-1; UpDown=0;
	while(((B1==-1) || (B2==-1)) && (CurrentBar<AB))
	{
	//double iHigh( string symbol, int timeframe, int shift) 
   //double iLow( string symbol, int timeframe, int shift) 

		if((UpDown<1) && (CurrentBar==Lowest(Symbol(),MAPERIOD,MODE_LOW,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=-1; B1=CurrentBar; P1=iLow(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iLow(Symbol(),MAPERIOD,B2);}
		}
		if((UpDown>-1) && (CurrentBar==Highest(Symbol(),MAPERIOD,MODE_HIGH,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=1; B1=CurrentBar; P1=iHigh(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iHigh(Symbol(),MAPERIOD,B2); }
		}
		CurrentBar++;
	}
	if((B1==-1) || (B2==-1)) {return(-1);} 
	Step=(P2-P1)/(B2-B1);
	P1=P1-B1*Step; B1=0;
	ishift=0; iprice=0;
	if(UpDown==1)
	{ 
		PP=iLow(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iLow(Symbol(),MAPERIOD,i)<PP+Step*i) { PP=iLow(Symbol(),MAPERIOD,i)-i*Step; }
		}
		if(iLow(Symbol(),MAPERIOD,0)<PP) {ishift=0; iprice=PP;}
		if(iLow(Symbol(),MAPERIOD,1)<PP+Step) {ishift=1; iprice=PP+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>P1) {ishift=0; iprice=P1;}
		if(iHigh(Symbol(),MAPERIOD,1)>P1+Step) {ishift=1; iprice=P1+Step;}
	} 
	else
	{ 
		PP=iHigh(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iHigh(Symbol(),MAPERIOD,i)>PP+Step*i) { PP=iHigh(Symbol(),MAPERIOD,i)-i*Step;}
		}
		if(iLow(Symbol(),MAPERIOD,0)<P1) {ishift=0; iprice=P1;}
		if(iLow(Symbol(),MAPERIOD,1)<P1+Step) {ishift=1; iprice=P1+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>PP) {ishift=0; iprice=PP;}
		if(iHigh(Symbol(),MAPERIOD,1)>PP+Step) {ishift=1; iprice=PP+Step;}
	}
	P2=P1+AB*Step;
	T1=iTime(Symbol(),MAPERIOD,B1); T2=iTime(Symbol(),MAPERIOD,AB);

	if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
	
	double ChannelWidth15=MathAbs(PP - P1)/Point;
	double SlopeAngle=MathFloor((-Step/Point)*100);
	ObjectCreate("TLM15_1",OBJ_TREND,0,T2,PP+Step*AB,T1,PP);
	// Channel is Green if width is greater than ChannelAlert (Default 50), otherwise it is couleur15_Narrow
      if(ChannelWidth15 >= ChannelAlert) {ObjectSet("TLM15_1",OBJPROP_COLOR,SpringGreen);}
	      else {ObjectSet("TLM15_1",OBJPROP_COLOR,LightSalmon); }
		ObjectSet("TLM15_1",OBJPROP_WIDTH,2); 
		ObjectSet("TLM15_1",OBJPROP_STYLE,STYLE_SOLID); 
	ObjectCreate("TLM15_2",OBJ_TREND,0,T2,P2,T1,P1); 	
		if(ChannelWidth15 >= ChannelAlert) {ObjectSet("TLM15_2",OBJPROP_COLOR,SpringGreen); }
   	  else{ObjectSet("TLM15_2",OBJPROP_COLOR,LightSalmon);}
      ObjectSet("TLM15_2",OBJPROP_WIDTH,2); 
		ObjectSet("TLM15_2",OBJPROP_STYLE,STYLE_SOLID); 
	   ObjectCreate("MIDL15",OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2);
	 // Mid line is Green if Slope is greater than SlopeAlert (Default 25), otherwise it is SpringGreen
		if(ChannelWidth15 >= ChannelAlert && MathAbs(SlopeAngle) >= SlopeAlert) {ObjectSet("MIDL15",OBJPROP_COLOR,SpringGreen); }
		   else{ObjectSet("MIDL15",OBJPROP_COLOR,LightSalmon);}
		ObjectSet("MIDL15",OBJPROP_WIDTH,1); 
		ObjectSet("MIDL15",OBJPROP_STYLE,STYLE_DOT); 

//---- PERIOD H1
   MAPERIOD=60;
//---- 
	if ((AllBars==0) || (iBars(Symbol(),MAPERIOD)<AllBars)) AB=iBars(Symbol(),MAPERIOD); else AB=AllBars; 
	if (BarsForFract>0) 
		BFF=BarsForFract; 
	else
      BFF=12; 

	CurrentBar=2; 
	B1=-1; B2=-1; UpDown=0;
	while(((B1==-1) || (B2==-1)) && (CurrentBar<AB))
	{
	//double iHigh( string symbol, int timeframe, int shift) 
   //double iLow( string symbol, int timeframe, int shift) 

		if((UpDown<1) && (CurrentBar==Lowest(Symbol(),MAPERIOD,MODE_LOW,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=-1; B1=CurrentBar; P1=iLow(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iLow(Symbol(),MAPERIOD,B2);}
		}
		if((UpDown>-1) && (CurrentBar==Highest(Symbol(),MAPERIOD,MODE_HIGH,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=1; B1=CurrentBar; P1=iHigh(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iHigh(Symbol(),MAPERIOD,B2); }
		}
		CurrentBar++;
	}
	if((B1==-1) || (B2==-1)) {return(-1);} 
	Step=(P2-P1)/(B2-B1);
	P1=P1-B1*Step; B1=0;
	ishift=0; iprice=0;
	if(UpDown==1)
	{ 
		PP=iLow(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iLow(Symbol(),MAPERIOD,i)<PP+Step*i) { PP=iLow(Symbol(),MAPERIOD,i)-i*Step; }
		}
		if(iLow(Symbol(),MAPERIOD,0)<PP) {ishift=0; iprice=PP;}
		if(iLow(Symbol(),MAPERIOD,1)<PP+Step) {ishift=1; iprice=PP+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>P1) {ishift=0; iprice=P1;}
		if(iHigh(Symbol(),MAPERIOD,1)>P1+Step) {ishift=1; iprice=P1+Step;}
	} 
	else
	{ 
		PP=iHigh(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iHigh(Symbol(),MAPERIOD,i)>PP+Step*i) { PP=iHigh(Symbol(),MAPERIOD,i)-i*Step;}
		}
		if(iLow(Symbol(),MAPERIOD,0)<P1) {ishift=0; iprice=P1;}
		if(iLow(Symbol(),MAPERIOD,1)<P1+Step) {ishift=1; iprice=P1+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>PP) {ishift=0; iprice=PP;}
		if(iHigh(Symbol(),MAPERIOD,1)>PP+Step) {ishift=1; iprice=PP+Step;}
	}
	P2=P1+AB*Step;
	T1=iTime(Symbol(),MAPERIOD,B1); T2=iTime(Symbol(),MAPERIOD,AB);

	if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
	
	ChannelWidth60=MathAbs(PP - P1)/Point;
	SlopeAngle=MathFloor((-Step/Point)*100);
	ObjectCreate("TLM60_1",OBJ_TREND,0,T2,PP+Step*AB,T1,PP);
	// Channel is Green if width is greater than ChannelAlert (Default 50), otherwise it is couleur60_Wide
      if(ChannelWidth60 >= ChannelAlert) {ObjectSet("TLM60_1",OBJPROP_COLOR,Lime);}
	      else {ObjectSet("TLM60_1",OBJPROP_COLOR,Red); }
		ObjectSet("TLM60_1",OBJPROP_WIDTH,2); 
		ObjectSet("TLM60_1",OBJPROP_STYLE,STYLE_SOLID); 
	ObjectCreate("TLM60_2",OBJ_TREND,0,T2,P2,T1,P1); 	
		if(ChannelWidth60 >= ChannelAlert) {ObjectSet("TLM60_2",OBJPROP_COLOR,Lime); }
   	  else{ObjectSet("TLM60_2",OBJPROP_COLOR,Red);}
      ObjectSet("TLM60_2",OBJPROP_WIDTH,2); 
		ObjectSet("TLM60_2",OBJPROP_STYLE,STYLE_SOLID); 
	   ObjectCreate("MIDL60",OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2);
	 // Mid line is Green if Slope is greater than SlopeAlert (Default 25), otherwise it is Lime
		if(ChannelWidth60 >= ChannelAlert && MathAbs(SlopeAngle) >= SlopeAlert) {ObjectSet("MIDL60",OBJPROP_COLOR,Lime); }
		   else{ObjectSet("MIDL60",OBJPROP_COLOR,Red);}
		ObjectSet("MIDL60",OBJPROP_WIDTH,1); 
		ObjectSet("MIDL60",OBJPROP_STYLE,STYLE_DOT); 

//---- PERIOD H4
   MAPERIOD=240;
//---- 
	if ((AllBars==0) || (iBars(Symbol(),MAPERIOD)<AllBars)) AB=iBars(Symbol(),MAPERIOD); else AB=AllBars; 
	if (BarsForFract>0) 
		BFF=BarsForFract; 
	else
      BFF=15; 
	CurrentBar=2; 
	B1=-1; B2=-1; UpDown=0;
	while(((B1==-1) || (B2==-1)) && (CurrentBar<AB))
	{
	//double iHigh( string symbol, int timeframe, int shift) 
   //double iLow( string symbol, int timeframe, int shift) 

		if((UpDown<1) && (CurrentBar==Lowest(Symbol(),MAPERIOD,MODE_LOW,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=-1; B1=CurrentBar; P1=iLow(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iLow(Symbol(),MAPERIOD,B2);}
		}
		if((UpDown>-1) && (CurrentBar==Highest(Symbol(),MAPERIOD,MODE_HIGH,BFF*2+1,CurrentBar-BFF))) 
		{
			if(UpDown==0) { UpDown=1; B1=CurrentBar; P1=iHigh(Symbol(),MAPERIOD,B1); }
			else { B2=CurrentBar; P2=iHigh(Symbol(),MAPERIOD,B2); }
		}
		CurrentBar++;
	}
	if((B1==-1) || (B2==-1)) {return(-1);} 
	Step=(P2-P1)/(B2-B1);
	P1=P1-B1*Step; B1=0;
	ishift=0; iprice=0;
	if(UpDown==1)
	{ 
		PP=iLow(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iLow(Symbol(),MAPERIOD,i)<PP+Step*i) { PP=iLow(Symbol(),MAPERIOD,i)-i*Step; }
		}
		if(iLow(Symbol(),MAPERIOD,0)<PP) {ishift=0; iprice=PP;}
		if(iLow(Symbol(),MAPERIOD,1)<PP+Step) {ishift=1; iprice=PP+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>P1) {ishift=0; iprice=P1;}
		if(iHigh(Symbol(),MAPERIOD,1)>P1+Step) {ishift=1; iprice=P1+Step;}
	} 
	else
	{ 
		PP=iHigh(Symbol(),MAPERIOD,2)-2*Step;
		for(i=3;i<=B2;i++) 
		{
			if(iHigh(Symbol(),MAPERIOD,i)>PP+Step*i) { PP=iHigh(Symbol(),MAPERIOD,i)-i*Step;}
		}
		if(iLow(Symbol(),MAPERIOD,0)<P1) {ishift=0; iprice=P1;}
		if(iLow(Symbol(),MAPERIOD,1)<P1+Step) {ishift=1; iprice=P1+Step;}
		if(iHigh(Symbol(),MAPERIOD,0)>PP) {ishift=0; iprice=PP;}
		if(iHigh(Symbol(),MAPERIOD,1)>PP+Step) {ishift=1; iprice=PP+Step;}
	}
	P2=P1+AB*Step;
	T1=iTime(Symbol(),MAPERIOD,B1); T2=iTime(Symbol(),MAPERIOD,AB);

	if(iprice!=0) ExtMapBuffer1[ishift]=iprice;
	
	ChannelWidth240=MathAbs(PP - P1)/Point;
	SlopeAngle=MathFloor((-Step/Point)*100);
	ObjectCreate("TLM240_1",OBJ_TREND,0,T2,PP+Step*AB,T1,PP);
	// Channel is Green if width is greater than ChannelAlert (Default 50), otherwise it is couleur240
      if(ChannelWidth240 >= ChannelAlert) {ObjectSet("TLM240_1",OBJPROP_COLOR,LimeGreen);}
	      else {ObjectSet("TLM240_1",OBJPROP_COLOR,LimeGreen); }
		ObjectSet("TLM240_1",OBJPROP_WIDTH,2); 
		ObjectSet("TLM240_1",OBJPROP_STYLE,STYLE_SOLID); 
	ObjectCreate("TLM240_2",OBJ_TREND,0,T2,P2,T1,P1); 	
		if(ChannelWidth240 >= ChannelAlert) {ObjectSet("TLM240_2",OBJPROP_COLOR,LimeGreen); }
   	  else{ObjectSet("TLM240_2",OBJPROP_COLOR,OrangeRed);}
      ObjectSet("TLM240_2",OBJPROP_WIDTH,2); 
		ObjectSet("TLM240_2",OBJPROP_STYLE,STYLE_SOLID); 
	   ObjectCreate("MIDL240",OBJ_TREND,0,T2,(P2+PP+Step*AB)/2,T1,(P1+PP)/2);
	 // Mid line is Green if Slope is greater than SlopeAlert (Default 25), otherwise it is couleur240_wide
		if(ChannelWidth240 >= ChannelAlert && MathAbs(SlopeAngle) >= SlopeAlert) {ObjectSet("MIDL240",OBJPROP_COLOR,LimeGreen); }
		   else{ObjectSet("MIDL240",OBJPROP_COLOR,OrangeRed);}
		ObjectSet("MIDL240",OBJPROP_WIDTH,1); 
		ObjectSet("MIDL240",OBJPROP_STYLE,STYLE_DOT); 
//----
   return(0);
  }
//+------------------------------------------------------------------+