Navigation:Home > Content >

Heiken_Ashi_-_mtf_separate.mq4

Time: 2018-03-09 | Download file:Heiken_Ashi_-_mtf_separate.mq4

//+------------------------------------------------------------------+
//|                                                      Heiken Ashi |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010,Forex-TSD.com"
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 Yellow
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 DeepSkyBlue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
#property indicator_width4 2
//
extern string TimeFrame       = "Current time frame";
extern bool   Interpolate     = true;
extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;
//
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double trend[];
//
string indicatorFileName;
bool   calculateValue;
bool   returnBars;
int    timeFrame;
//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexBuffer(1, ExtMapBuffer2);
   SetIndexBuffer(2, ExtMapBuffer3);
   SetIndexBuffer(3, ExtMapBuffer4);
   SetIndexBuffer(4, trend);
    //
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
   //
   IndicatorShortName(timeFrameToString(timeFrame)+" Heiken Ashi");
   return(0);
}
//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
            limit = MathMin(Bars-counted_bars,Bars-1);
            if (returnBars) { ExtMapBuffer1[0] = limit+1; return(0); }          
   //   
   if (calculateValue || timeFrame == Period())
   {
      for(i=limit; i >= 0; i--)
      {
         double prOpen  = Open[i];
         double prClose = Close[i];
         double prLow   = Low[i];
         double prHigh  = High[i];  
   //                                       
         double haClose = (prOpen+prHigh+prLow+prClose)/4;
         double haOpen   = (ExtMapBuffer3[i+1]+ExtMapBuffer4[i+1])/2;
         double haHigh   = MathMax(prHigh, MathMax(haOpen,haClose));
         double haLow    = MathMin(prLow,  MathMin(haOpen,haClose));

         if (haOpenExtMapBuffer2[i]) trend[i] = -1;
            if (ExtMapBuffer1[i]=0;i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         ExtMapBuffer1[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",0,y);
         ExtMapBuffer2[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",1,y);
         ExtMapBuffer3[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",2,y);
         ExtMapBuffer4[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",3,y);
         trend[i]         = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",4,y);
    //                                      
            if (y==iBarShift(NULL,timeFrame,Time[i-1]) || !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++)
            {
               ExtMapBuffer1[i+k] = ExtMapBuffer1[i] + (ExtMapBuffer1[i+n]-ExtMapBuffer1[i])*k/n;
               ExtMapBuffer2[i+k] = ExtMapBuffer2[i] + (ExtMapBuffer2[i+n]-ExtMapBuffer2[i])*k/n;
               ExtMapBuffer3[i+k] = ExtMapBuffer3[i] + (ExtMapBuffer3[i+n]-ExtMapBuffer3[i])*k/n;
               ExtMapBuffer4[i+k] = ExtMapBuffer4[i] + (ExtMapBuffer4[i+n]-ExtMapBuffer4[i])*k/n;
            }               
   }
   manageAlerts();   
   //         
   return(0);
}
//+-------------------------------------------------------------------
//|                                                                  
//+-------------------------------------------------------------------
void manageAlerts()
{
   if (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("up");
         else  doAlert("down");       
   }
}
//
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 =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," heiken ashi jurik smoothed trend changed to ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," heiken ashi jurik smoothed "),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 char1 = StringGetChar(s, length);
         if((char1 > 96 && char1 < 123) || (char1 > 223 && char1 < 256))
                     s = StringSetChar(s, length, char1 - 32);
         else if(char1 > -33 && char1 < 0)
                     s = StringSetChar(s, length, char1 + 224);
   }
   return(s);
}

Recommend