//+------------------------------------------------------------------+
//|                                                    FN Signal.mq4 |
//|                                                          Belomor |
//|                                                 belomor@inbox.ru |
//+------------------------------------------------------------------+
#property copyright "Belomor"
#property link      "belomor@inbox.ru"
//----
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Silver
//---- input parameters
extern int       FN=34;
extern double    Deviation=3.0;
extern int       FastEMA=5;
extern int       SlowEMA=13;
extern color     BuyArrowColor=clrLimeGreen;
extern color     SellArrowColor=clrRed;
extern int       ArrowSize=3;
extern bool               alertsOn         = true;
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
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
int    arrow_anch;
double trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   for(int i=0; i<Bars; i++)
    {
  ObjectDelete(0,"FN_Buy_Signal"+(string)i);
  ObjectDelete(0,"FN_Sell_Signal"+(string)i);
       ObjectCreate("FN_Buy_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
       ObjectCreate("FN_Sell_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
    }
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexStyle(2,DRAW_LINE,STYLE_DOT);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   if(FN<2)
      FN=2;
   if(Deviation<0)
      Deviation=1;
   if(FastEMA<1)
      FastEMA=1;
   if(SlowEMA<1)
      SlowEMA=1;
   IndicatorShortName("FNCD ("+FN+","+FastEMA+","+SlowEMA+")");
   SetIndexDrawBegin(0,FN+SlowEMA);
   SetIndexDrawBegin(1,FN+SlowEMA);
   SetIndexLabel(0,"FN Up");
   SetIndexLabel(1,"FN Down");
   SetIndexEmptyValue(0,EMPTY_VALUE);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   IndicatorDigits(4);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double NormalizedX(int F_period, int i)
  {
   double result;
   double A;
   double S;
   double C;
   if(i<Bars-F_period)
     {
      C=Close[i];
      A=iMA(NULL,0,F_period,0,MODE_SMA,PRICE_CLOSE,i);
      S=iStdDev(NULL,0,F_period,MODE_SMA,0,PRICE_CLOSE,i);
      result=(C-A)/S;
     }
   else
      result=0;
   return(result);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double FisherNormalizedX(int F_period, double Dev, int i)
  {
   double result;
   double X;
   if(i<Bars-F_period && Dev>0)
     {
      X=NormalizedX(F_period,i)/Dev;
      if(X>0.99)
         X=0.99;
      if(X<-0.99)
         X=-0.99;
      result=0.5*MathLog((1+X)/(1-X));
     }
   else
      result=0;
   return(result);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   for(int i=0; i<Bars-1; i++)
    {
       if(ObjectFind("FN_Buy_Signal"+(string)i)<0)
       ObjectCreate("FN_Buy_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
       if(ObjectFind("FN_Sell_Signal"+(string)i)<0)
       ObjectCreate("FN_Sell_Signal"+(string)i,OBJ_ARROW,0,0,0,0,0);
    }
   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(i=0; i<limit; i++)
      ExtMapBuffer4[i]=FisherNormalizedX(FN,Deviation,i);
   for(i=0; i<limit; i++)
      ExtMapBuffer3[i]=iMAOnArray(ExtMapBuffer4,Bars,SlowEMA,0,MODE_EMA,i);
   for(i=0; i<limit; i++)
     {
     trend[i] = 0;
     ExtMapBuffer1[i]=EMPTY_VALUE;
     ExtMapBuffer1[i+1]=EMPTY_VALUE;
     ExtMapBuffer2[i]=EMPTY_VALUE;
     ExtMapBuffer2[i+1]=EMPTY_VALUE;
      if(iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i)>ExtMapBuffer3[i])
        {
         ExtMapBuffer1[i]=iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i);
         ExtMapBuffer2[i]=EMPTY_VALUE;
        }
      else
        {
         ExtMapBuffer1[i]=EMPTY_VALUE;
         ExtMapBuffer2[i]=iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i);
        }
         if(iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i)>ExtMapBuffer3[i]&&
            iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i+1)<=ExtMapBuffer3[i+1])
          {
          trend[i]=1;
          SetArrow("FN_Buy_Signal"+(string)i,241,BuyArrowColor,Low[i],Time[i],ANCHOR_TOP,ArrowSize);
          }
         if(iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i)<=ExtMapBuffer3[i]&&
            iMAOnArray(ExtMapBuffer4,Bars,FastEMA,0,MODE_EMA,i+1)>ExtMapBuffer3[i+1])
          {  
          trend[i]=-1;
          SetArrow("FN_Sell_Signal"+(string)i,242,SellArrowColor,High[i],Time[i],ANCHOR_BOTTOM,ArrowSize);
          }
     }
    if (alertsOn)
     {
     int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
     if (trend[whichBar] != trend[whichBar+1])
     if (trend[whichBar] == 1)
           doAlert("up");
     else  doAlert("down");       
     }  
//---- 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() 
 {
   for(int i=0; i<Bars; i++)
    {
  ObjectDelete(0,"FN_Buy_Signal"+(string)i);
  ObjectDelete(0,"FN_Sell_Signal"+(string)i);
    }
  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); 
}
//+------------------------------------------------------------------+
void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];
   
          message = Tf_as_string(PERIOD_CURRENT)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" FN Signal arrow "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" FN Signal arrow ",message);
             if (alertsSound)   PlaySound(soundFile);
      }
}
//+------------------------------------------------------------------+
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
}
//+------------------------------------------------------------------+
