Navigation´╝ÜHome > Content >

SuperTrend+Ma_alerts.mq4

Time: 2013-06-17 | Download file:SuperTrend+Ma_alerts.mq4

//+------------------------------------------------------------------+
//|                                               SuperTrend nrp.mq4 |
//|                                                                  |
//| original Super trend for metatrader made by Jason Robinson       |
//| calculation correction, repainting solving and mtf by mladen     |
//+------------------------------------------------------------------+
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 LimeGreen
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_width4 2

//
//
//
//
//

extern int    CciPeriod       = 50;
extern bool   OldCalculation  = false;
extern int    MaPeriod        = 50;
extern int    MAMethod        = MODE_LWMA;
extern int    MAPrice         = PRICE_CLOSE;

extern string note            = "turn on Alert = true; turn off = false";
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = true;
extern bool   alertsEmail     = false;
extern string soundfile       = "alert2.wav";

//
//
//
//
//

double STrend[];
double STrendDoA[];
double STrendDoB[];
double ma[];
double Trend[];
double trens[];

double UpDownShift;

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

int init()
{
   IndicatorBuffers(6);
      SetIndexBuffer(0, STrend);    SetIndexLabel(0,"SuperTrend");
      SetIndexBuffer(1, STrendDoA); SetIndexLabel(1,"SuperTrend");
      SetIndexBuffer(2, STrendDoB); SetIndexLabel(2,"SuperTrend");
      SetIndexBuffer(3, ma);
      SetIndexBuffer(4, Trend);
      SetIndexBuffer(5, trens);
         
         //
         //
         //
         //
         //
         
         if (OldCalculation)
         {
            switch(Period())
            {
               case 1:     UpDownShift = 3;   break;
               case 5:     UpDownShift = 5;   break;
               case 15:    UpDownShift = 7;   break;
               case 30:    UpDownShift = 9;   break;
               case 60:    UpDownShift = 20;  break;
               case 240:   UpDownShift = 35;  break;
               case 1440:  UpDownShift = 40;  break;
               case 10080: UpDownShift = 100; break;
               case 43200: UpDownShift = 120; break;
            }
            UpDownShift *= Point;
            if (Digits==3 || Digits==5)
               UpDownShift *= 10.0;
         }            
         
   
      //
      //
      //
      //
      //
   
   IndicatorShortName("SuperTrend ");    
}
int deinit()
{
   return(0);
}




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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         if (Trend[limit] == -1) CleanPoint(limit,STrendDoA,STrendDoB);

   //
   //
   //
   //
   //

      for(i=limit; i>=0; i--)
      {
         double cciTrend = iCCI(NULL, 0, CciPeriod, PRICE_TYPICAL, i);
                ma[i]    = iMA(NULL,0,MaPeriod,0,MAMethod,MAPrice,i);
         
         //
         //
         //
         //
         //
         
         STrendDoA[i] = EMPTY_VALUE;
         STrendDoB[i] = EMPTY_VALUE;
         Trend[i]     = Trend[i+1];
            if (cciTrend > 0)    Trend[i]    =  1;
            if (cciTrend < 0)    Trend[i]    = -1;
            if (!OldCalculation) UpDownShift =  iATR(NULL,0,5,i);
            if (Trend[i] ==  1) STrend[i] = MathMax(Low[i]  - UpDownShift,STrend[i+1]);
            if (Trend[i] == -1) STrend[i] = MathMin(High[i] + UpDownShift,STrend[i+1]); 
            if (Trend[i] == -1) PlotPoint(i,STrendDoA,STrendDoB,STrend);
            
            trens[i] = trens[i+1];
            if (Close[i] > STrend[i])  trens[i] = 1;
            if (Close[i] < STrend[i])  trens[i] =-1;
      }
      
      //
      //
      //
      //
      //
      
      if (alertsOn)
      {
          if (alertsOnCurrent)
               int whichBar = 0;
          else     whichBar = 1;
          if (trens[whichBar] != trens[whichBar+1])
          if (trens[whichBar] == 1)
                doAlert("Buy");
          else  doAlert("Sell");       
      }
      
return(0);
}

//
//
//
//
//
   
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)," Supertrend cross ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Supertrend cross "),message);
             if (alertsSound)   PlaySound(soundfile);
      }
}

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

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;
      }
}

Recommend