//+------------------------------------------------------------------+
//|                                       Trend or Sideway Gauge.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                               el84r166@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "el84r166@gmail.com"

#property indicator_separate_window
#property indicator_minimum 0.0
#property indicator_maximum 1.0
#property indicator_buffers 3
#property indicator_color1 Crimson
#property indicator_color2 Blue
#property indicator_color3 DarkOrange

extern int Minutes = 0;
extern int MACD_Fast = 1;
extern int MACD_Slow = 21;
extern int MACD_MA = 7;
extern int BarsToCount = 10000;

input int    TriggerCandle 	= 1;
input bool   EnableNativeAlerts = true;
input bool   EnableSoundAlerts  = true;
input bool   EnableEmailAlerts  = true;
input bool   EnablePushAlerts  = true;
input string AlertEmailSubject  = "";
input string AlertText  	= "";
input string SoundFileName	= "alert.wav";
 
datetime LastAlertTime = D'01.01.1970';
int LastAlertDirection = 0;

double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
string TimeFrameStr;
double imacd1;
double imacd2;

int init() {
   if (TimeCurrent() > StrToTime (" 10.10.2095 ")) {
      Alert("Version expired!");
      return;
   }
   SetIndexStyle(0, DRAW_HISTOGRAM, STYLE_SOLID, 4, Red);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 4, RoyalBlue);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 4, Gold);
   SetIndexBuffer(2, ExtMapBuffer3);
   switch (Minutes) {
   case 1:
      TimeFrameStr = "Period_M1";
      break;
   case 5:
      TimeFrameStr = "Period_M5";
      break;
   case 15:
      TimeFrameStr = "Period_M15";
      break;
   case 30:
      TimeFrameStr = "Period_M30";
      break;
   case 60:
      TimeFrameStr = "Period_H1";
      break;
   case 240:
      TimeFrameStr = "Period_H4";
      break;
   case 1440:
      TimeFrameStr = "Period_D1";
      break;
   case 10080:
      TimeFrameStr = "Period_W1";
      break;
   case 43200:
      TimeFrameStr = "Period_MN1";
      break;
   default:
      TimeFrameStr = "Current Timeframe";
      Minutes = 0;
   }
   IndicatorShortName("Trend or Sideway Gauge (" + TimeFrameStr + ")");
   return (0);
}

int deinit() {
   return (0);
}

int start() {
   int counted_bars = IndicatorCounted();
   for (int i = 0; i < BarsToCount; i++) {
          ExtMapBuffer1[i]=0;
          ExtMapBuffer2[i]=0;
          ExtMapBuffer3[i]=0;
      imacd1 = iMACD(NULL, Minutes, MACD_Fast, MACD_Slow, MACD_MA, PRICE_CLOSE, MODE_SIGNAL, i);
      imacd2 = iMACD(NULL, Minutes, MACD_Fast, MACD_Slow, MACD_MA, PRICE_CLOSE, MODE_MAIN, i);
      if (imacd1 < imacd2 && imacd2 > 0.0) ExtMapBuffer2[i] = 1;
      if (imacd1 > imacd2 && imacd2 < 0.0) ExtMapBuffer1[i] = 1;
      if (ExtMapBuffer1[i] == 0.0 && ExtMapBuffer2[i] == 0.0) 
      
      {ExtMapBuffer3[i] = 1;}
   
   if (((TriggerCandle > 0) && (Time[0] > LastAlertTime)) || (TriggerCandle == 0))
{
	string Text;
	// Up-Line
	if (((ExtMapBuffer2[TriggerCandle] > ExtMapBuffer1[TriggerCandle]) && (ExtMapBuffer2[TriggerCandle + 1] <= ExtMapBuffer1[TriggerCandle + 1])) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != 1))))
	{
		Text = AlertText + "Trend or Sideway Gauge: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Up Trend";
		if (EnableNativeAlerts) Alert(Text);
		if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Trend or Sideway Gauge", Text);
		if (EnableSoundAlerts) PlaySound(SoundFileName);
		if (EnablePushAlerts) SendNotification(Text);
		LastAlertTime = Time[0];
		LastAlertDirection = 1;
	}
	// Down-Line
	if (((ExtMapBuffer2[TriggerCandle] < ExtMapBuffer1[TriggerCandle]) && (ExtMapBuffer2[TriggerCandle + 1] >= ExtMapBuffer1[TriggerCandle + 1])) && ((TriggerCandle > 0) || ((TriggerCandle == 0) && (LastAlertDirection != -1))))
	{
		Text = AlertText + "Trend or Sideway Gauge: " + Symbol() + " - " + EnumToString((ENUM_TIMEFRAMES)Period()) + " - Down Trend.";
		if (EnableNativeAlerts) Alert(Text);
		if (EnableEmailAlerts) SendMail(AlertEmailSubject + "Trend or Sideway Gauge", Text);
		if (EnableSoundAlerts) PlaySound(SoundFileName);
		if (EnablePushAlerts) SendNotification(Text);
		LastAlertTime = Time[0];
		LastAlertDirection = -1;
	}
}
      
   }
   return (0);
}