//+------------------------------------------------------------------+
//|                                       Indicator: ++H4arrow++.mq4 |
//|                                                                  |
//|                                                 BetyX            |
//+------------------------------------------------------------------+
#property copyright "Betyx"
#property link      "betyx2@gmail.com"
#property version   "1.00"
#property description "1 a 2 špice"

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_chart_window
#property indicator_buffers 2

#property indicator_type1 DRAW_ARROW
#property indicator_width1 2
#property indicator_color1 0xFF1500
#property indicator_label1 "Buy"

#property indicator_type2 DRAW_ARROW
#property indicator_width2 2
#property indicator_color2 0xFF00F2
#property indicator_label2 "Sell"

//--- indicator buffers
double Buffer1[];
double Buffer2[];

extern double arrow = 20;
extern bool ZobudMaALLERT = true;
extern int PowerArrow = 2;
extern int Shift = 0;
extern int PowerWilko = 4;
extern int Repaint_0_or_NO_1 = 0;
extern int Repaint_1_or_NO_2 = 1;
datetime time_alert; //used when sending alert
extern bool Send_Email = true;
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+" | ++H4arrow++ @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
   else if(type == "indicator")
     {
      if(Audible_Alerts) Alert(type+" | ++H4arrow++ @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Send_Email) SendMail("++H4arrow++", type+" | ++H4arrow++ @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
      if(Push_Notifications) SendNotification(type+" | ++H4arrow++ @ "+Symbol()+","+IntegerToString(Period())+" | "+message);
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {   
   IndicatorBuffers(2);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, EMPTY_VALUE);
   SetIndexArrow(0, 181);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, EMPTY_VALUE);
   SetIndexArrow(1, 181);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   Audible_Alerts = ZobudMaALLERT; //initialize to input
   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++;
   
   int j;
   
   double IndicatorData1[];
   int IndicatorData1_count = 2*PowerArrow;
   if(IndicatorData1_count < MathMax(limit, 50)) IndicatorData1_count = MathMax(limit, 50);
   ArrayResize(IndicatorData1, IndicatorData1_count);
   ArraySetAsSeries(IndicatorData1, true);
   for(j = IndicatorData1_count-1; j >= 0; j--)
     {
      IndicatorData1[j] = iWPR(NULL, PERIOD_CURRENT, PowerWilko, j); //IndicatorData1 = William's Percent Range
     }
   
   //--- 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   
      
      int barshift_H4 = iBarShift(Symbol(), PERIOD_H4, Time[i]);
      if(barshift_H4 < 0) continue;
      
      //Indicator Buffer 1
      if(iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_0_or_NO_1+barshift_H4) > iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_1_or_NO_2+barshift_H4)
      && iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_0_or_NO_1+barshift_H4+1) < iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_1_or_NO_2+barshift_H4+1) //Moving Average crosses above Moving Average
      )
        {
         Buffer1[i] = Low[i] - arrow * myPoint; //Set indicator value at Candlestick Low - fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Buy"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer1[i] = EMPTY_VALUE;
        }
      //Indicator Buffer 2
      if(iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_0_or_NO_1+barshift_H4) < iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_1_or_NO_2+barshift_H4)
      && iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_0_or_NO_1+barshift_H4+1) > iMAOnArray(IndicatorData1, 0, PowerArrow, Shift, MODE_EMA, Repaint_1_or_NO_2+barshift_H4+1) //Moving Average crosses below Moving Average
      )
        {
         Buffer2[i] = High[i] + arrow * myPoint; //Set indicator value at Candlestick High + fixed value
         if(i == 1 && Time[1] != time_alert) myAlert("indicator", "Sell"); //Alert on next bar open
         time_alert = Time[1];
        }
      else
        {
         Buffer2[i] = EMPTY_VALUE;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+