//+------------------------------------------------------------------+
//|                                          Gann high-low activator |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1  DeepSkyBlue
#property indicator_color2  PaleVioletRed
#property indicator_color3  PaleVioletRed
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    MaPeriod        = 6;
extern int    MaMethod        = 2;
extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
extern bool   Interpolate     = true;

//
//
//
//
//

double gup[];
double gdna[];
double gdnb[];
double trend[];

//
//
//
//
//

int    timeFrame;
string indicatorFileName;
bool   returnBars;
bool   calculateValue;

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(4);
   SetIndexBuffer(0,gup);
   SetIndexBuffer(1,gdna);
   SetIndexBuffer(2,gdnb);
   SetIndexBuffer(3,trend);
   
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
      
      //
      //
      //
      //
      //
      
   IndicatorShortName(timeFrameToString(timeFrame)+" Gann high/low activator - HeikenAshi");
         
   return(0);
}

//+------------------------------------------------------------------
//|                                                                  
//+------------------------------------------------------------------
//
//
//
//
//

double work[][4];
#define haClose 0
#define haOpen  1
#define haHigh  2
#define haLow   3

//
//
//
//
//

int start()
{
   int i,r,limit,counted_bars=IndicatorCounted();

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=MathMin(Bars-counted_bars,Bars-1);
           if (returnBars) { gup[0] = limit+1; return(0); }
           if (ArrayRange(work,0)!=Bars) ArrayResize(work,Bars);

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame == Period())
   {
      if (!calculateValue && trend[limit]==-1) CleanPoint(limit,gdna,gdnb);
      for(i=limit, r=Bars-i-1;i>=0;i--,r++)
      {
         if (r==0)
         {
            work[r][haOpen]  = (Open[i]+Close[i])/2.0;
            work[r][haClose] = (Open[i]+Close[i]+High[i]+Low[i])/4.0;
            work[r][haHigh]  = High[i];
            work[r][haLow]   = Low[i];
            continue;
         }
         
         //
         //
         //
         //
         //

            double maOpen  = iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_OPEN ,i);
            double maClose = iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_CLOSE,i);
            double maLow   = iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_LOW  ,i);
            double maHigh  = iMA(NULL,0,MaPeriod,0,MaMethod,PRICE_HIGH ,i);

            //
            //
            //
            //
            //
               
               work[r][haOpen]  = (work[r-1][haOpen]+work[r-1][haClose])/2.0;
               work[r][haClose] = (maOpen+maHigh+maLow+maClose)/4.0;
               work[r][haHigh]  = MathMax(maHigh,MathMax(work[r][haOpen],work[r][haClose]));
               work[r][haLow]   = MathMin(maLow ,MathMin(work[r][haOpen],work[r][haClose]));

            //
            //
            //
            //

               gdna[i]  = EMPTY_VALUE;
               gdnb[i]  = EMPTY_VALUE;
               trend[i] = trend[i+1];
                  if(Close[i]>work[r-1][haHigh]) trend[i] =  1;
                  if(Close[i]<work[r-1][haLow] ) trend[i] = -1;
         
                  if(trend[i] == -1)
                        gup[i] = work[r-1][haHigh];
                  else  gup[i] = work[r-1][haLow];
                  if (!calculateValue && trend[i]==-1) PlotPoint(i,gdna,gdnb,gup);
      }

      //
      //
      //
      //
      //
      
      manageAlerts();
      return(0);
   }      

   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   if (trend[limit]==-1) CleanPoint(limit,gdna,gdnb);
   for(i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         gdna[i]  = EMPTY_VALUE;
         gdnb[i]  = EMPTY_VALUE;
         gup[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",MaPeriod,MaMethod,0,y);
         trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",MaPeriod,MaMethod,3,y);
            
         //
         //
         //
         //
         //
      
         if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
         if (!Interpolate) continue;

         //
         //
         //
         //
         //

         datetime time = iTime(NULL,timeFrame,y);
            for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
            for(int k = 1; k < n; k++)
               gup[i+k] = gup[i] + (gup[i+n]-gup[i])*k/n;
   }
   for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,gdna,gdnb,gup);

   //
   //
   //
   //
   //
   
   manageAlerts();
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void manageAlerts()
{
   if (!calculateValue && alertsOn)
   {
      if (alertsOnCurrent)
           int whichBar = 0;
      else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar] == 1) doAlert(whichBar,"up");
         if (trend[whichBar] ==-1) doAlert(whichBar,"down");
      }         
   }
}   

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Gann HL activator trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(),"Gann HL "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   tfs = stringUpperCase(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

//
//
//
//
//

string stringUpperCase(string str)
{
   string   s = str;

   for (int length=StringLen(str)-1; length>=0; length--)
   {
      int char = StringGetChar(s, length);
         if((char > 96 && char < 123) || (char > 223 && char < 256))
                     s = StringSetChar(s, length, char - 32);
         else if(char > -33 && char < 0)
                     s = StringSetChar(s, length, char + 224);
   }
   return(s);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void CleanPoint(int i,double& first[],double& second[])
{
   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 (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;
      }
}


