//+------------------------------------------------------------------+
//|                             Indicator: koichi17 indicator v1.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 6

#property indicator_type1 DRAW_ARROW
#property indicator_width1 1
#property indicator_color1 0x00FF00
#property indicator_label1 "Close Cross MA"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 1
#property indicator_color2 0x00FF00
#property indicator_label2 "EMA Cross MA"

#property indicator_type3 DRAW_ARROW
#property indicator_width3 1
#property indicator_color3 0x00FF00
#property indicator_label3 "ADX Cross"

#property indicator_type4 DRAW_ARROW
#property indicator_width4 1
#property indicator_color4 0x0000FF
#property indicator_label4 "ADX Cross"

#property indicator_type5 DRAW_ARROW
#property indicator_width5 1
#property indicator_color5 0x0000FF
#property indicator_label5 "Close Cross MA"

#property indicator_type6 DRAW_ARROW
#property indicator_width6 1
#property indicator_color6 0x0000FF
#property indicator_label6 "EMA Cross MA"

//--- indicator buffers
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];

extern int EMA_Length = 8;
extern int MA_Length = 20;
extern int ADX_Length = 14;
datetime time_alert; //used when sending alert
extern bool Audible_Alerts = true;
extern bool Push_Notifications = true;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | koichi17 indicator v1 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | koichi17 indicator v1 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | koichi17 indicator v1 @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(6);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 67);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 241);
   SetIndexBuffer(2, Buffer3);
   SetIndexEmptyValue(2, EMPTY_VALUE);
   SetIndexArrow(2, 241);
   SetIndexBuffer(3, Buffer4);
   SetIndexEmptyValue(3, EMPTY_VALUE);
   SetIndexArrow(3, 242);
   SetIndexBuffer(4, Buffer5);
   SetIndexEmptyValue(4, EMPTY_VALUE);
   SetIndexArrow(4, 242);
   SetIndexBuffer(5, Buffer6);
   SetIndexEmptyValue(5, EMPTY_VALUE);
   SetIndexArrow(5, 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);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   ArraySetAsSeries(Buffer5, true);
   ArraySetAsSeries(Buffer6, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
      ArrayInitialize(Buffer3, 0);
      ArrayInitialize(Buffer4, 0);
      ArrayInitialize(Buffer5, 0);
      ArrayInitialize(Buffer6, 0);
     }
   else
      limit++;
   
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation   
      
      //Indicator Buffer 1
      if(Close[1+i] > iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i)
      && Close[1+i+1] < iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick Close crosses above Moving Average
      )
        {
         Buffer1[i] = Low[1+i]; //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Close Cross MA"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iMA(NULL, PERIOD_CURRENT, EMA_Length, 0, MODE_EMA, PRICE_CLOSE, i) > iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i)
      && iMA(NULL, PERIOD_CURRENT, EMA_Length, 0, MODE_EMA, PRICE_CLOSE, i+1) < iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i+1) //Moving Average crosses above Moving Average
      )
        {
         Buffer2[i] = Low[1+i]; //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "EMA Cross MA"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 3
      if(iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_PLUSDI, i) > iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_MINUSDI, i)
      && iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_PLUSDI, i+1) < iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_MINUSDI, i+1) //Average Directional Movement Index crosses above Average Directional Movement Index
      )
        {
         Buffer3[i] = Low[1+i]; //Set indicator value at Candlestick Low
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "ADX Cross"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer3[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 4
      if(iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_PLUSDI, i) < iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_MINUSDI, i)
      && iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_PLUSDI, i+1) > iADX(NULL, PERIOD_CURRENT, ADX_Length, PRICE_CLOSE, MODE_MINUSDI, i+1) //Average Directional Movement Index crosses below Average Directional Movement Index
      )
        {
         Buffer4[i] = High[1+i]; //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "ADX Cross"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer4[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 5
      if(Close[1+i] < iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i)
      && Close[1+i+1] > iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i+1) //Candlestick Close crosses below Moving Average
      )
        {
         Buffer5[i] = High[1+i]; //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "Close Cross MA"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer5[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 6
      if(iMA(NULL, PERIOD_CURRENT, EMA_Length, 0, MODE_EMA, PRICE_CLOSE, i) < iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i)
      && iMA(NULL, PERIOD_CURRENT, EMA_Length, 0, MODE_EMA, PRICE_CLOSE, i+1) > iMA(NULL, PERIOD_CURRENT, MA_Length, 0, MODE_SMA, PRICE_CLOSE, i+1) //Moving Average crosses below Moving Average
      )
        {
         Buffer6[i] = High[1+i]; //Set indicator value at Candlestick High
         if(i == 0 && Time[0] != time_alert) { myAlert("indicator", "EMA Cross MA"); time_alert = Time[0]; } //Instant alert, only once per bar
        }
      else
        {
         Buffer6[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+