Navigation´╝ÜHome > Content >

Nrp_SuperTrend_Histo_mtf+alerts_(1).mq4

Time: 2010-03-07 | Download file:Nrp_SuperTrend_Histo_mtf+alerts_(1).mq4

//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"
//------------------------------------------------------------------

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  clrRed
#property indicator_color2  clrDodgerBlue
#property indicator_width1  2
#property indicator_width2  2
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//
//
//

extern string TimeFrame       = "Current time frame";
extern int    Nbr_Periods     = 10;
extern double Multiplier      = 2.50;
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double UpH[];
double DnH[];
double Up[];
double Dn[];
double Direction[];

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

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
      SetIndexBuffer(0,UpH); SetIndexStyle(0,DRAW_HISTOGRAM);
      SetIndexBuffer(1,DnH); SetIndexStyle(1,DRAW_HISTOGRAM);   
      SetIndexBuffer(2,Up);
      SetIndexBuffer(3,Dn);
      SetIndexBuffer(4,Direction);
      
      //
      //
      //
      //
      //
      
         indicatorFileName = WindowExpertName();
         calculateValue    = TimeFrame=="calculateValue"; if (calculateValue) { return(0); }
         returnBars        = TimeFrame=="returnBars";     if (returnBars)     { return(0); }
         timeFrame         = stringToTimeFrame(TimeFrame);
       
      //
      //
      //
      //
      //
      
      IndicatorShortName(timeFrameToString(timeFrame)+" SuperTrend");
     
return(0);
}

//
//
//
//
//

int deinit() { return(0); }

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int start()
{
   int 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) { UpH[0] = limit+1; return(0); }

   //
   //
   //
   //
   //

   if (calculateValue || timeFrame==Period())
   {
      for(int i=limit; i>=0; i--)
      {
         double atr    = iATR(NULL,0,Nbr_Periods,i);
         double cprice = Close[i];
         double mprice = iMA(NULL,0,1,0,MODE_SMA,PRICE_MEDIAN,i);
                Up[i]  = mprice+Multiplier*atr;
                Dn[i]  = mprice-Multiplier*atr;
         
         //
         //
         //
         //
         //

         UpH[i] = EMPTY_VALUE;
         DnH[i] = EMPTY_VALUE;
         Direction[i] = Direction[i+1];
            if (cprice > Up[i+1])  Direction[i] =  1;
            if (cprice < Dn[i+1])  Direction[i] = -1;
            if (Direction[i] > 0) { Dn[i] = MathMax(Dn[i],Dn[i+1]); }
            else                  { Up[i] = MathMin(Up[i],Up[i+1]); }
            if (Direction[i] == 1) UpH[i] = 1; 
            if (Direction[i] ==-1) DnH[i] = 1; 

      }
      manageAlerts();
      return(0);
   }
   
   //
   //
   //
   //
   //
   
   limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
   for (i=limit; i>=0; i--)
   {
      int y = iBarShift(NULL,timeFrame,Time[i]);
         Direction[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",Nbr_Periods,Multiplier,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,4,y);
         UpH[i] = EMPTY_VALUE;
         DnH[i] = EMPTY_VALUE;
         if (Direction[i] == 1) UpH[i] = 1; 
         if (Direction[i] ==-1) DnH[i] = 1; 
   }
   return(0);
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar=0;
      if (Direction[whichBar] != Direction[whichBar+1])
      {
         if (Direction[whichBar] ==  1) doAlert("up"  );
         if (Direction[whichBar] == -1) 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 =  timeFrameToString(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" super trend changed to "+doWhat;
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(Symbol()+" super trend",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