//https://forex-station.com/viewtopic.php?f=579496&p=1295461442#p1295461442
//https://www.mql5.com/en/forum/175037/page1214
//------------------------------------------------------------------
//
//------------------------------------------------------------------

#property indicator_separate_window
#property indicator_height  120
#property indicator_buffers 6
#property indicator_color1  clrDimGray
#property indicator_color2  clrWhite
#property indicator_color3  clrLime
#property indicator_color4  clrLime
#property indicator_color5  clrRed
#property indicator_color6  clrRed
#property indicator_minimum -1.05
#property indicator_maximum  1.05
#property indicator_levelcolor   clrDarkSlateGray
#property strict

//
enum enTimeFrames
{
   tf_cu  = PERIOD_CURRENT, // Current time frame
   tf_m1  = PERIOD_M1,      // 1 minute
   tf_m5  = PERIOD_M5,      // 5 minutes
   tf_m15 = PERIOD_M15,     // 15 minutes
   tf_m30 = PERIOD_M30,     // 30 minutes
   tf_h1  = PERIOD_H1,      // 1 hour
   tf_h4  = PERIOD_H4,      // 4 hours
   tf_d1  = PERIOD_D1,      // Daily
   tf_w1  = PERIOD_W1,      // Weekly
   tf_mn1 = PERIOD_MN1,     // Monthly
   tf_n1  = -1,             // First higher time frame
   tf_n2  = -2,             // Second higher time frame
   tf_n3  = -3              // Third higher time frame
};
//
//
//
//

extern enTimeFrames       TimeFrame   = tf_m15;           // Time frame to use
extern int                WPR_Period  = 89;               // Wpr Period
extern ENUM_APPLIED_PRICE WPR_Price   = 0;                // Wpr Price
extern int                MA_Period   = 9;                // MaPeriod
extern ENUM_MA_METHOD     MA_Method   = MODE_LWMA;        // Ma Method
extern double             levelOb     = 0.9;              // overbought level
extern double             levelOs     = -0.9;             // oversold level 
extern double             Sensitivity = 10;               // Sensitivity for inverse fisher transform
extern int                LineWidth   = 3;                // Lines width
extern bool               Interpolate = true;             // Interpolating when using multi time frame mode 
extern bool            AlertsOn            = false;          // Turn alerts on?
extern bool            AlertsOnCurrent     = false;          // Alerts on still opened bar?
extern bool            AlertsMessage       = true;           // Alerts should display message?
extern bool            AlertsSound         = true;           // Alerts should play a sound?
extern bool            AlertsNotify        = false;          // Alerts should send a notification?
extern bool            AlertsEmail         = false;          // Alerts should send an email?
extern string          AlertsSoundFile     = "alert2.wav";   // Sound file

input bool            arrowsVisible             = false;              // Arrows visible true/false?
input string          arrowsIdentifier          = "WPR1";             // Unique ID for arrows
input bool            arrowsOnNewest            = false;
input double          arrowsUpperGap            = 0.5;                // Upper arrow gap
input double          arrowsLowerGap            = 0.5;                // Lower arrow gap
input color           arrowsUpColor             = clrAqua;            // Up arrow color
input color           arrowsDnColor             = clrMagenta;         // Down arrow color
input int             arrowsUpCode              = 124;                // Up arrow code
input int             arrowsDnCode              = 124;                // Down arrow code
input int             arrowsUpSize              = 2;                  // Up arrow size
input int             arrowsDnSize              = 2;                  // Down arrow size
//
//
//
//
//

double Value[];
double iFish[];
double iFishua[];
double iFishub[];
double iFishda[];
double iFishdb[];
double state[];
double shadow[];
string indicatorFileName;
bool   returnBars;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init() 
{ 
   IndicatorBuffers(8);
   SetIndexBuffer(0,shadow);    SetIndexStyle(0,EMPTY,EMPTY,LineWidth+4);
   SetIndexBuffer(1,iFish);     SetIndexStyle(1,EMPTY,EMPTY,LineWidth);
   SetIndexBuffer(2,iFishua);   SetIndexStyle(2,EMPTY,EMPTY,LineWidth);
   SetIndexBuffer(3,iFishub);   SetIndexStyle(3,EMPTY,EMPTY,LineWidth); 
   SetIndexBuffer(4,iFishda);   SetIndexStyle(4,EMPTY,EMPTY,LineWidth);
   SetIndexBuffer(5,iFishdb);   SetIndexStyle(5,EMPTY,EMPTY,LineWidth);
   SetIndexBuffer(6,Value);
   SetIndexBuffer(7,state);
   SetLevelValue(0, levelOs);
   SetLevelValue(1, levelOb);
   SetLevelValue(2, 0);
        indicatorFileName = WindowExpertName();
        returnBars        = TimeFrame==-99;
      TimeFrame         = (enTimeFrames)timeFrameValue(TimeFrame);   
   IndicatorShortName(timeFrameToString(TimeFrame)+" ift of wpr ("+(string)WPR_Period+","+(string)MA_Period+","+(string)Sensitivity+")" );
return(0); 
}  
int deinit() 
{ 
 deleteArrows();

return(0); } 


//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start() 
{ 
   int i,counted_bars = IndicatorCounted(); 
      if (counted_bars < 0) return(-1); 
      if (counted_bars > 0) counted_bars--;  
         int limit=MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { shadow[0] = limit+1; return(0); }

   //
   //
   //
   //
   //
      
   if (TimeFrame==Period())
   {
      if (state[limit] ==  1) CleanPoint(limit,iFishua,iFishub);
      if (state[limit] == -1) CleanPoint(limit,iFishda,iFishdb);
      for (i= limit; i >= 0; i--) Value[i] = (1/Sensitivity)*(iWPR(NULL,0,WPR_Period,i)+50.0);    
      for (i= limit; i >= 0; i--)    
      {
         double MA = iMAOnArray(Value,0,MA_Period,0,MA_Method,i); 
         iFish[i]  = (MathExp(2.0*MA)-1.0)/(MathExp(2.0*MA)+1.0);
         iFishua[i] = EMPTY_VALUE;
         iFishub[i] = EMPTY_VALUE;
         iFishda[i] = EMPTY_VALUE;
         iFishdb[i] = EMPTY_VALUE;
         if (i<Bars-1)
         {
           shadow[i]  = iFish[i];
           state[i]   = 0;
           if (iFish[i]>levelOb) state[i] =  1;
           if (iFish[i]<levelOs) state[i] = -1;
           if (state[i] ==  1) PlotPoint(i,iFishua,iFishub,iFish);
           if (state[i] == -1) PlotPoint(i,iFishda,iFishdb,iFish);
         }
              if (arrowsVisible)
        {
            string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);            
            if (i<(Bars-1) && state[i] != state[i+1])
            {
              if (state[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,arrowsUpSize,false);
              if (state[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode,arrowsDnSize, true);  
      }
       }
         if (AlertsOn)
      {
         int whichBar = 1; if (AlertsOnCurrent) whichBar = 0; 
         if (state[whichBar] != state[whichBar+1])
         {
            if (state[whichBar] == 1)                          doAlert(" up");
            if (state[whichBar] ==-1)                          doAlert(" down");       
              
         }         
      }              
     }
     return(0); 
    }
    
    //
   //
   //
   //
   //
   
   limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));;
   if (state[limit] ==  1) CleanPoint(limit,iFishua,iFishub);
   if (state[limit] == -1) CleanPoint(limit,iFishda,iFishdb);
   for (i=limit;i>=0; i--)
   {
      int y = iBarShift(NULL,TimeFrame,Time[i]);
        iFish[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,WPR_Period,WPR_Price,MA_Period,MA_Method,levelOb,levelOs,Sensitivity,LineWidth,Interpolate,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsSound,AlertsNotify,AlertsEmail,AlertsSoundFile,arrowsVisible,arrowsIdentifier,arrowsOnNewest,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,1,y);
        state[i]   = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,WPR_Period,WPR_Price,MA_Period,MA_Method,levelOb,levelOs,Sensitivity,LineWidth,Interpolate,AlertsOn,AlertsOnCurrent,AlertsMessage,AlertsSound,AlertsNotify,AlertsEmail,AlertsSoundFile,arrowsVisible,arrowsIdentifier,arrowsOnNewest,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,arrowsUpSize,arrowsDnSize,7,y);
        shadow[i]  = iFish[i];
        iFishua[i] = EMPTY_VALUE;
        iFishub[i] = EMPTY_VALUE;
        iFishda[i] = EMPTY_VALUE;
        iFishdb[i] = EMPTY_VALUE;
        
          //
          //
          //
          //
          //
            
          if (!Interpolate || (i>0 &&y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;

          //
          //
          //
          //
          //

          int n,s; datetime time = iTime(NULL,TimeFrame,y);
             for(n = 1; i+n<Bars && Time[i+n] >= time; n++) continue;
             for(s = 1; i+n<Bars && i+s<Bars && s<n; s++)
             {
                iFish[i+s]  = iFish[i] + (iFish[i+n] - iFish[i]) * s/n;
                shadow[i+s] = iFish[i+s];
  	          }   
   }
   for (i=limit;i>=0;i--)
   {
      if (state[i] ==  1) PlotPoint(i,iFishua,iFishub,iFish);
      if (state[i] == -1) PlotPoint(i,iFishda,iFishdb,iFish);
   }
return(0);
} 

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   if (i>=Bars-3) return;
   if ((second[i]  != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE))
        second[i+1] = EMPTY_VALUE;
   else
      if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE))
          first[i+1] = EMPTY_VALUE;
}

void PlotPoint(int i,double& first[],double& second[],double& from[])
{
   if (i>=Bars-2) return;
   if (first[i+1] == EMPTY_VALUE)
      if (first[i+2] == EMPTY_VALUE) 
            { first[i]  = from[i];  first[i+1]  = from[i+1]; second[i] = EMPTY_VALUE; }
      else  { second[i] =  from[i]; second[i+1] = from[i+1]; first[i]  = EMPTY_VALUE; }
   else     { first[i]  = from[i];                           second[i] = EMPTY_VALUE; }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}
int timeFrameValue(int _tf)
{
   int add  = (_tf>=0) ? 0 : MathAbs(_tf);
   if (add != 0) _tf = _Period;
   int size = ArraySize(iTfTable); 
      int i =0; for (;i<size; i++) if (iTfTable[i]==_tf) break;
                                   if (i==size) return(_Period);
                                                return(iTfTable[(int)MathMin(i+add,size-1)]);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//
void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   
      if (previousAlert != doWhat || previousTime != Time[0]) 
      {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          string message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" ift WPR : "+doWhat;
             if (AlertsMessage) Alert(message);
             if (AlertsNotify)  SendNotification(message);
             if (AlertsEmail)   SendMail(_Symbol+" ift WPR ",message);
             if (AlertsSound)   PlaySound(AlertsSoundFile);
      }
}



void drawArrow(int i,color theColor,int theCode, int theSize, bool up)
{
   string name = arrowsIdentifier+":"+(string)Time[i];
   double gap  = iATR(NULL,0,20,i);   
   
      //
      //
      //
      //
      //

      datetime time = Time[i]; if (arrowsOnNewest) time += _Period*60-1;      
      ObjectCreate(name,OBJ_ARROW,0,Time[i],0);
         ObjectSet(name,OBJPROP_ARROWCODE,theCode);
         ObjectSet(name,OBJPROP_WIDTH,    theSize);
         ObjectSet(name,OBJPROP_COLOR,    theColor);
         if (up)
               ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
         else  ObjectSet(name,OBJPROP_PRICE1,Low[i]  - arrowsLowerGap * gap);
}

void deleteArrows()
{
   string lookFor       = arrowsIdentifier+":";
   int    lookForLength = StringLen(lookFor);
   for (int i=ObjectsTotal()-1; i>=0; i--)
   {
      string objectName = ObjectName(i);
         if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
   }
}