//+------------------------------------------------------------------+
//|                                           Indicator: channel.mq4 |
//|                                       Created with EABuilder.com |
//|                                        https://www.eabuilder.com |
//+------------------------------------------------------------------+
#property copyright "Created with EABuilder.com"
#property link      "https://www.eabuilder.com"
#property version   "1.00"
#property description ""

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 5
#property indicator_color1 0xFFFFFF
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 5
#property indicator_color2 0x00FFF7
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern int Tenkan_sen = 27;
extern int Kijun_sen = 78;
extern int Senkou_Span_B = 156;
extern int Candle_Index = 1;
extern int Period1 = 9;
extern int MA_Shift = 0;
extern int Shift = 0;
extern int Candle_Index1 = 0;
datetime time_alert; //used when sending alert
extern bool Send_Email = true;
extern bool Audible_Alerts = true;
extern bool Push_Notifications = true;
double myPoint; //initialized in OnInit

//--- Custom functions ----------------------------------------------- 

double ExampleFunction()
{
   return(High[1] - Low[1]);
}

//--- End of custom functions ----------------------------------------

void myAlert(string type, string message)
  {
   int handle;
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      Print(type+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Audible_Alerts) Alert(type+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Send_Email) SendMail("channel", type+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      handle = FileOpen("channel.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+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
         FileClose(handle);
        }
      if(Push_Notifications) SendNotification(type+" | channel @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 241);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 242);
   //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);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, EMPTY_VALUE);
      ArrayInitialize(Buffer2, EMPTY_VALUE);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(50000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(iMA(NULL, PERIOD_CURRENT, 144, 0, MODE_EMA, PRICE_LOW, i) > iIchimoku(NULL, PERIOD_CURRENT, 27, 78, 156, MODE_SENKOUSPANB, i)
      && iMA(NULL, PERIOD_CURRENT, 144, 0, MODE_EMA, PRICE_LOW, i+1) < iIchimoku(NULL, PERIOD_CURRENT, 27, 78, 156, MODE_SENKOUSPANB, i+1) //Moving Average crosses above Ichimoku Kinko Hyo
      )
        {
         Buffer1[i] = Low[Candle_Index1+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
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iMA(NULL, PERIOD_CURRENT, 144, 0, MODE_EMA, PRICE_HIGH, i) < iIchimoku(NULL, PERIOD_CURRENT, 27, 78, 156, MODE_SENKOUSPANB, i)
      && iMA(NULL, PERIOD_CURRENT, 144, 0, MODE_EMA, PRICE_HIGH, i+1) > iIchimoku(NULL, PERIOD_CURRENT, 27, 78, 156, MODE_SENKOUSPANB, i+1) //Moving Average crosses below Ichimoku Kinko Hyo
      )
        {
         Buffer2[i] = High[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
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+