//+------------------------------------------------------------------+
//|                                  Simple_Signal_Arrows_Alerts.mq4 |
//|                                     Copyright 2022, BestTraderEv |
//|                         https://www.forexfactory.com/besttraderev|
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, BestTraderEv"
#property link      "https://www.forexfactory.com/besttraderev"
#property version   "1.00"
#property strict
#property indicator_chart_window

enum enScenario{
WithCandleDirection,
CounterCandleDirection,
};
//---- input parameters
extern enScenario         WhichDirection = CounterCandleDirection;
extern color              BuyArrowColor=clrLimeGreen;
extern color              SellArrowColor=clrRed;
extern int                ArrowSize=3;
extern bool               alertsOn         = false;
extern bool               alertsOnCurrent  = false;
extern bool               alertsMessage    = true;
extern bool               alertsSound      = false;
extern bool               alertsNotify     = false;
extern bool               alertsEmail      = false;
extern string             soundFile        = "alert2.wav";
//---- buffers
int    arrow_anch;
string OBJ_Prefix = "SSAA_Arrow_";
string message;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   objectsDelete(OBJ_Prefix);
   for(int i=0; i<Bars-1; i++)
    {
       ObjectCreate(OBJ_Prefix+"Buy_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
       ObjectCreate(OBJ_Prefix+"Sell_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {
    
   int whichBar = 1; 
   string sWhichBar = " on first closed Bar";
   if(alertsOnCurrent) 
    {
    whichBar = 0;
    sWhichBar = " on current Bar";
    }
   
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(int i=1; i<limit; i++)
     {
     if(WhichDirection==CounterCandleDirection)
      {
         if(Close[i]<Low[i+1])
          {
          SetArrow(OBJ_Prefix+"Buy_Signal"+(string)i,241,BuyArrowColor,Low[i],Time[i],ANCHOR_TOP,ArrowSize);
          if(alertsOn&&i==whichBar)
           {
           message=_Symbol+Tf_as_string(PERIOD_CURRENT)+" Simple Signal Buy Arrow @ "+TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES)+sWhichBar;
           AlertOnce(message,whichBar,PERIOD_CURRENT,soundFile);
           }
          }
         if(Close[i]>High[i+1])
          {  
          SetArrow(OBJ_Prefix+"Sell_Signal"+(string)i,242,SellArrowColor,High[i],Time[i],ANCHOR_BOTTOM,ArrowSize);
          if(alertsOn&&i==whichBar)
           {
           message=_Symbol+Tf_as_string(PERIOD_CURRENT)+" Simple Signal Sell Arrow @ "+TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES)+sWhichBar;
           AlertOnce(message,whichBar,PERIOD_CURRENT,soundFile);
           }
          }
      } 
     if(WhichDirection==WithCandleDirection)
      {
         if(Close[i]>High[i+1])
          {
          SetArrow(OBJ_Prefix+"Buy_Signal"+(string)i,241,BuyArrowColor,Low[i],Time[i],ANCHOR_TOP,ArrowSize);
          if(alertsOn&&i==whichBar)
           {
           message=_Symbol+Tf_as_string(PERIOD_CURRENT)+" Simple Signal Buy Arrow @ "+TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES)+sWhichBar;
           AlertOnce(message,whichBar,PERIOD_CURRENT,soundFile);
           }
          }
         if(Close[i]<Low[i+1])
          {  
          SetArrow(OBJ_Prefix+"Sell_Signal"+(string)i,242,SellArrowColor,High[i],Time[i],ANCHOR_BOTTOM,ArrowSize);
          if(alertsOn&&i==whichBar)
           {
           message=_Symbol+Tf_as_string(PERIOD_CURRENT)+" Simple Signal Sell Arrow @ "+TimeToString(TimeLocal(),TIME_DATE|TIME_MINUTES)+sWhichBar;
           AlertOnce(message,whichBar,PERIOD_CURRENT,soundFile);
           }
          }
      }   
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+
void SetArrow(string name,int code,color c,double p,datetime t,int a,int w)
   {
       ObjectSet(name,OBJPROP_ARROWCODE,code);
       ObjectSet(name,OBJPROP_COLOR,c);        
       ObjectSet(name,OBJPROP_PRICE1,p); 
       ObjectSet(name,OBJPROP_TIME1,t);
       ObjectSet(name,OBJPROP_SELECTABLE,false);  
       ObjectSet(name,OBJPROP_ANCHOR,a);   
       ObjectSet(name,OBJPROP_WIDTH,w); 
   }
//+------------------------------------------------------------------+
int deinit() 
 {
  objectsDelete(OBJ_Prefix);
  return(0);
 }
//+------------------------------------------------------------------+
bool IsNewBar()
{ 
  static datetime Trend_Candle_prevTime1 = -1;
  
  if(Trend_Candle_prevTime1 != Time[6])
  { 
   Trend_Candle_prevTime1 = Time[6]; 
       
   return(true);  
  } 

  return(false); 
}
//+------------------------------------------------------------------+
int AlertOnce(string alert_msg, int ref, int tf, string sound)
{  
int i = 0;


  static int LastAlert[1000];
   
  while (i < 1000)
{
 if (ref == i)
  {
           if( LastAlert[i] == 0 || LastAlert[i] < iBars(NULL,tf) )
        {
           if(alertsMessage)         Alert(alert_msg);
           if(alertsSound)           PlaySound(sound);
           if(alertsEmail)           SendMail("FN Signal Alert",alert_msg);
           if(alertsNotify)          SendNotification(alert_msg);
           LastAlert[i] = iBars(NULL,tf);
           return (1);
        }
      break;
 }
   
 i = i + 1;
  }
 return(0);  
}
//+------------------------------------------------------------------+
string   Tf_as_string(ENUM_TIMEFRAMES period){
   if(period == PERIOD_CURRENT)  period   = (ENUM_TIMEFRAMES) _Period;
   string   period_xxx  = EnumToString(period);                // PERIOD_XXX
   return StringSubstr(period_xxx, 7);                         // XXX
}
//+------------------------------------------------------------------+
void objectsDelete(string name)
{
   int objTotal = ObjectsTotal();
   for(int i=objTotal-1; i>=0; i--)  
  {
      string objName = ObjectName(i);
      if(StringFind(objName,name,0) == 0)
       ObjectDelete(0,objName);
   }
}
//+------------------------------------------------------------------+
