//+------------------------------------------------------------------+
//|                                     Indicator: CCI NLS Alarm.mq4 |
//+------------------------------------------------------------------+
#property copyright "Created by Gandhi0815"
#property link      ""
#property version   "1.01"
#property description "Gandhi0815"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 4

#property indicator_type1 DRAW_ARROW
#property indicator_width1 4
#property indicator_color1 0x00FF80
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 4
#property indicator_color2 0x0000FF
#property indicator_label2 "Sell"

#property indicator_type3 DRAW_ARROW
#property indicator_width3 4
#property indicator_color3 0x00FF80
#property indicator_label3 "Exit long"

#property indicator_type4 DRAW_ARROW
#property indicator_width4 4
#property indicator_color4 0x0000FF
#property indicator_label4 "Exit short"

//--- indicator buffers
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];

datetime time_alert; //used when sending alert
extern bool Send_Email = true;
extern bool Audible_Alerts = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   int handle;
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
      if(Audible_Alerts) Alert(type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
      if(Send_Email) SendMail("CCI NLS Alarm", type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
      handle = FileOpen("CCI NLS Alarm.txt", FILE_TXT|FILE_READ|FILE_WRITE|FILE_SHARE_READ|FILE_SHARE_WRITE, ';');
      if(handle != INVALID_HANDLE)
        {
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
         FileClose(handle);
        }
      SendNotification(type+" | CCI NLS Alarm @ "+Symbol()+","+Period()+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(4);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexArrow(1, 242);
   SetIndexBuffer(2, Buffer3);
   SetIndexEmptyValue(2, 0);
   SetIndexArrow(2, 242);
   SetIndexBuffer(3, Buffer4);
   SetIndexEmptyValue(3, 0);
   SetIndexArrow(3, 241);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime& time[],
                const double& open[],
                const double& high[],
                const double& low[],
                const double& close[],
                const long& tick_volume[],
                const long& volume[],
                const int& spread[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
      ArrayInitialize(Buffer3, 0);
      ArrayInitialize(Buffer4, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(500-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      //Indicator Buffer 1
      if(iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i) > 100 //Commodity Channel Index > fixed value
      && iCCI(NULL, PERIOD_H4, 20, PRICE_TYPICAL, i) > 100 //Commodity Channel Index > fixed value
      && iCCI(NULL, PERIOD_H1, 20, PRICE_TYPICAL, i) > 100 //Commodity Channel Index > fixed value
      )
        {
         Buffer1[i] = iLow(NULL, PERIOD_D1, i); //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Buy"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      //Indicator Buffer 2
      if(iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i) < -100 //Commodity Channel Index < fixed value
      && iCCI(NULL, PERIOD_H4, 20, PRICE_TYPICAL, i) < -100 //Commodity Channel Index < fixed value
      && iCCI(NULL, PERIOD_H1, 20, PRICE_TYPICAL, i) < -100 //Commodity Channel Index < fixed value
      )
        {
         Buffer2[i] = iHigh(NULL, PERIOD_D1, i); //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Sell"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      //Indicator Buffer 3
      if(iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i) < 100
      && iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i+1) > 100 //Commodity Channel Index crosses below fixed value
      )
        {
         Buffer3[i] = iHigh(NULL, PERIOD_D1, i); //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Exit long"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      //Indicator Buffer 4
      if(iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i) > -100
      && iCCI(NULL, PERIOD_D1, 20, PRICE_TYPICAL, i+1) < -100 //Commodity Channel Index crosses above fixed value
      )
        {
         Buffer4[i] = iLow(NULL, PERIOD_D1, i); //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Exit short"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+