//+------------------------------------------------------------------+
//|                                              DL Hurst Target.mq4 |
//|                                          Copyright 2015, Pips400 |
//+------------------------------------------------------------------+

/* Update History
1.06 - added option to alert for current or closed bar

*/

#property copyright "Copyright 2015, Pips400, Dragpons Lair Forex"
#property link      "https://www.youtube.com/channel/UC3-o47xzGmPkgb4QFYl8D0A"
#property version   "1.06"
#property  description "Dragons Lair Forex aims to interpret price action"
#property  description "on a day to day basis using classic and timeless "
#property  description "principles to gain an orientation and identify"
#property  description "key areas of interest for optimal trading." 
#property strict

#property indicator_chart_window


enum tfs          {M0=0,M1=1,M5=5,M15=15,M30=30,H1=60,H4=240,D1=1440,W1=10080,MN=43200}; // MTF function kindly provided by Radar - Thank you

/*The indicator uses the TMAcentered indicator to replicate the Hurst principle of cycles to predict entry and exit points in
currency markets. It will plot the most recent swing high (or swing low) together with the TMAcentered crossover point
and Hurst extension zone, depending upon the settings used*/

extern string Welcome_ = "==========   Welcome ===========================================";
extern string Welcome_1 = "The indicator uses the TMAcentered indicator to replicate the Hurst principle of";
extern string Weicome_2 = "cycles to predict entry and exit points in currency markets. It will plot";
extern string Weicome_3 = "the most recent swing high (or swing low) together with the TMAcentered crossover point"; 
extern string Weicome_4 = "and Hurst extension zone, depending upon the settings used."; 
extern string Weicome_5 = "Use hurst extension limits for the percentage of extension to be plotted. e.g. a limit of 75"; 
extern string Weicome_6 = "means extend beyond the crossover point 75% of the distance between the swing point and crossover"; 

extern string _0 = " ";
extern string TMA_centered_name = "TMAcentered";
extern int TMA_slow_value = 10;
extern int TMA_fast_value = 5;
extern bool show_crossover = true;
extern color crossover_colour = Red;
extern tfs    TimeFrame        = 0;//0 means current chart
extern string _01 = "For multiple use of the Hurst Target Indicator specify a unique index number below ";
extern int index = 1;

extern string _1 = " ";
extern bool show_swingpoint = true;
extern color swingpoint_colour = Olive;

extern string _2 = " ";
extern bool show_hurst_extension_zone = true;
extern int hurst_extension_limit1 = 75;
extern int hurst_extension_limit2 = 100;
extern color hurst_extension_colour = Violet;

extern string _3 = " ";
extern bool show_hurst_extension_lines = true;
extern double hurst_extension_line1 = 61.8;
extern double hurst_extension_line2 = 100;
extern double hurst_extension_line3 = 161.8;
extern color extension_lines_colour = Magenta;
extern double line_style = 2;

extern string _4 = " ";
extern bool show_labels = true;
extern int label_font_size = 7;
extern color label_colour = C'160,160,160';

extern string _5 = " ";
extern string Alerts_ = "========== Alerts =========================";
extern bool use_alerts = true;
extern bool use_popup_alert = true;
extern bool use_email_alert = false;
extern bool use_push_notification = false;
extern bool alert_on_current_candle = false;





int ThisBar = 0;
datetime LastUpdate;
datetime startpoint, highpointtime, lowpointtime, highpointtime2, highpointtime3, lowpointtime2, lowpointtime3;
int bars_since_hurstline, alert_candle;
int x = 0, y = 0;
int swing__triangle_pips;
string direction;
double lowpoint, highpoint, SymPoint, lowpoint2, highpoint2;
double hurstextensionline1, hurstextensionline2, hurstextensionline3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   
   deletelabels();
  
   if (Point == 0.00001) SymPoint = 0.0001; //5 digits
   else if (Point == 0.001) SymPoint = 0.01; //3 digits
   else SymPoint = Point; //Normal
   
   switch (Period())
   {
      case 1 : swing__triangle_pips = 3 ; break;
      case 5 : swing__triangle_pips = 5 ; break;
      case 15 : swing__triangle_pips = 7 ; break;
      case 30 : swing__triangle_pips = 10 ; break;
      case 60 : swing__triangle_pips = 15 ; break;
      case 240 : swing__triangle_pips = 20 ; break;
      case 1440 : swing__triangle_pips = 50 ; break;
      case 10080 : swing__triangle_pips = 100 ; break;
      case 43200 : swing__triangle_pips = 200 ; break;
      default : swing__triangle_pips = 10;
   } 
   
   if (alert_on_current_candle) alert_candle = 0; else alert_candle = 1;

   return(INIT_SUCCEEDED);
  }
  
  
//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 

int deinit()
{
   deletelabels();
 
   return(0);
}

//----  
  
//+------------------------------------------------------------------+
//| 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[])
  {
//---
   

  if (iBars(NULL, TimeFrame) != ThisBar)
  {
  ThisBar = iBars(NULL, TimeFrame);
  deletelabels();
  tma_centred_value (); //get crossover points
  
  if (direction == "above") 
  {
   lowpoint = iLow (NULL,TimeFrame,iLowest(NULL,TimeFrame,MODE_LOW,y,x)); // get price at lowest point of swing
   lowpointtime = iTime(NULL,TimeFrame,iLowest(NULL,TimeFrame,MODE_LOW,y,x)); // get time of swing low
   lowpointtime2 = lowpointtime - Period()*60;
   lowpointtime3 = lowpointtime + Period()*60;
   lowpoint2 = lowpoint - SymPoint*swing__triangle_pips;
   if (show_swingpoint)
   {
   ObjectCreate (0,StringConcatenate("swinglow",index),OBJ_TRIANGLE,0,lowpointtime,lowpoint,lowpointtime2,lowpoint2,lowpointtime3,lowpoint2);
   ObjectSet (StringConcatenate("swinglow",index),OBJPROP_COLOR,swingpoint_colour);
   }
  } 
  
  
  
  if (direction == "below") 
  { 
   highpoint = iHigh (NULL,TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH,y,x)); // get price at highest point of swing
   highpointtime = iTime(NULL,TimeFrame,iHighest(NULL,TimeFrame,MODE_HIGH,y,x)); // get time of swing high
   highpointtime2 = highpointtime - Period()*60;
   highpointtime3 = highpointtime + Period()*60;
   highpoint2 = highpoint + SymPoint*swing__triangle_pips;
   if (show_swingpoint)
   {
   ObjectCreate (0,StringConcatenate("swinghigh",index),OBJ_TRIANGLE,0,highpointtime,highpoint,highpointtime2,highpoint2,highpointtime3,highpoint2);
   ObjectSet (StringConcatenate("swinghigh",index),OBJPROP_COLOR,swingpoint_colour);
   }

  }

   //Calculation of crossover value
  double a,b,c,d;
   
   a = iCustom (NULL,TimeFrame,"TMAcentered",TMA_fast_value,0,x-1);
   b = iCustom (NULL,TimeFrame,"TMAcentered",TMA_slow_value,0,x-1);
   c = iCustom (NULL,TimeFrame,"TMAcentered",TMA_fast_value,0,x);
   d = iCustom (NULL,TimeFrame,"TMAcentered",TMA_slow_value,0,x);
   
   double crossovervalue =    NormalizeDouble (((((a+b)/2) + ((c+d)/2))/2), Digits); // get value at crossover
   datetime crossovertime = iTime(NULL,TimeFrame,x);
   datetime crossovertime2 = iTime(NULL,TimeFrame,x+y);   
   
   if (show_crossover)
   {
   ObjectCreate (StringConcatenate("crossoverline",index),OBJ_TREND,0,crossovertime2,crossovervalue,crossovertime,crossovervalue); //create crossover line
   ObjectSet (StringConcatenate("crossoverline",index),OBJPROP_RAY,FALSE);
   ObjectSet (StringConcatenate("crossoverline",index),OBJPROP_COLOR,crossover_colour);
   }
   // end calculation of crossover value
   

   if (show_hurst_extension_zone)
   {
   if (direction == "below")
   {
   double hurstextension1 = crossovervalue - MathAbs(crossovervalue - highpoint)*hurst_extension_limit2/100;
   double hurstextension2 = crossovervalue - MathAbs(crossovervalue - highpoint)*hurst_extension_limit1/100;
   ObjectCreate (0,StringConcatenate("hurstextensionzone",index),OBJ_RECTANGLE,0,crossovertime,hurstextension1,TimeCurrent(),hurstextension2);
   ObjectSet (StringConcatenate("hurstextensionzone",index),OBJPROP_COLOR,hurst_extension_colour);  
   
   }
     
   if (direction == "above")
   {
   double hurstextension1 = crossovervalue + MathAbs(crossovervalue - lowpoint)*hurst_extension_limit2/100;
   double hurstextension2 = crossovervalue + MathAbs(crossovervalue - lowpoint)*hurst_extension_limit1/100;
   ObjectCreate (0,StringConcatenate("hurstextensionzone",index),OBJ_RECTANGLE,0,crossovertime,hurstextension1,TimeCurrent(),hurstextension2);  
   ObjectSet (StringConcatenate("hurstextensionzone",index),OBJPROP_COLOR,hurst_extension_colour);  

   }
   }

   
   if (show_hurst_extension_lines)
   {
      if (direction == "below")
      {
         hurstextensionline1 = crossovervalue - MathAbs(crossovervalue - highpoint)*hurst_extension_line1/100;
         hurstextensionline2 = crossovervalue - MathAbs(crossovervalue - highpoint)*hurst_extension_line2/100;
         hurstextensionline3 = crossovervalue - MathAbs(crossovervalue - highpoint)*hurst_extension_line3/100;
      }
      else
      {
         hurstextensionline1 = crossovervalue + MathAbs(crossovervalue - lowpoint)*hurst_extension_line1/100;
         hurstextensionline2 = crossovervalue + MathAbs(crossovervalue - lowpoint)*hurst_extension_line2/100;
         hurstextensionline3 = crossovervalue + MathAbs(crossovervalue - lowpoint)*hurst_extension_line3/100;
      }
          
      create_hurst_level (StringConcatenate("hurstextline1_",index),crossovertime2,hurstextensionline1);
      create_hurst_level (StringConcatenate("hurstextline2_",index),crossovertime2,hurstextensionline2);
      create_hurst_level (StringConcatenate("hurstextline3_",index),crossovertime2,hurstextensionline3);
            
      if (show_labels)
      {
         create_text_level(StringConcatenate("level1_text_",index), hurstextensionline1, hurst_extension_line1 );
         create_text_level(StringConcatenate("level2_text_",index), hurstextensionline2, hurst_extension_line2 );
         create_text_level(StringConcatenate("level3_text_",index), hurstextensionline3, hurst_extension_line3 );
      }
   
   }  
  
  if (use_alerts && crossover_alert ()) alerts ();
  
   
  }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


void tma_centred_value () {
   x = 0;
   y = 0;
  

   if ( iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,0) < iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,0) )
   
   {
   direction = "below";
   while ( iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,x) < iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,x) )
   {
      x++;      
   }
   }
   
   else
   {
   direction = "above";
   while ( iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,x) > iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,x) )
   {
      x++;      
   }
   }
   
   if (direction == "below")
   
   {
   while ( iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,x+y) > iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,x+y) )
   {
      y++;
   }
   }
   
   if (direction == "above")
   
   {
   while ( iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,x+y) < iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,x+y) )
   {
      y++;
   }
   }

}

void deletelabels() {
   // when removing the indicator delete all objects
   ObjectDelete(StringConcatenate("crossoverline",index));  
   ObjectDelete(StringConcatenate("swinghigh",index));
   ObjectDelete(StringConcatenate("swinglow",index));
   ObjectDelete(StringConcatenate("hurstextensionzone",index));
   ObjectDelete(StringConcatenate("hurstextline1_",index));
   ObjectDelete(StringConcatenate("hurstextline2_",index));
   ObjectDelete(StringConcatenate("hurstextline3_",index));
   ObjectDelete(StringConcatenate("level1_text_",index));
   ObjectDelete(StringConcatenate("level2_text_",index));
   ObjectDelete(StringConcatenate("level3_text_",index));

   }

void create_hurst_level(string levelname, datetime starttime, double postion) {
   
   ObjectCreate (levelname,OBJ_TREND,0,starttime,postion,TimeCurrent(),postion); //create first extension line
   ObjectSet (levelname,OBJPROP_RAY,FALSE);
   ObjectSet (levelname,OBJPROP_COLOR,extension_lines_colour);
   ObjectSet (levelname, OBJPROP_STYLE,line_style);

}

void create_text_level(string textname, double position, double text ) {

   ObjectCreate(textname,OBJ_TEXT,0,TimeCurrent()+Period()*60*5,position);
   ObjectSetInteger(0,textname,OBJPROP_ANCHOR,ANCHOR_RIGHT);
   ObjectSetInteger(0,textname,OBJPROP_FONTSIZE,label_font_size);
   ObjectSetInteger(0,textname,OBJPROP_COLOR,label_colour);
   ObjectSetString(0,textname,OBJPROP_TEXT,StringConcatenate(text));

}

bool crossover_alert () {

    if ( 
         (iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,0+alert_candle) < iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,0+alert_candle) &&
         iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,1+alert_candle) > iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,1+alert_candle))   ||
         
         (iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,0+alert_candle) > iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,0+alert_candle) &&
         iCustom (NULL,TimeFrame,TMA_centered_name,TMA_fast_value,0,1+alert_candle) < iCustom (NULL,TimeFrame,TMA_centered_name,TMA_slow_value,0,1+alert_candle)) 
   
       )
       
       return true;
       return false;

}

void alerts (){   
     if (use_popup_alert)  Alert(Symbol() + " Hurst TMAcentered Crossover");
     if (use_email_alert) SendMail(Symbol() + " Hurst TMAcentered Crossover", Symbol() + " Hurst TMAcentered Crossover" ); 
     if (use_push_notification) SendNotification(Symbol() + " Hurst TMAcentered Crossover"); 
}