Navigation´╝ÜHome > Content >

ma_crosses_arrows_lines_mtfalerts-.mq4

Time: 2017-10-09 | Download file:ma_crosses_arrows_lines_mtfalerts-.mq4

//+------------------------------------------------------------------+
//|                                                   ma crosses.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1  clrRed
#property indicator_color2  clrBlue
#property indicator_color3  clrTomato
#property indicator_color4  clrBlue

//

extern string TimeFrame          = "Current time frame";
extern int    FastMa             = 5;
extern string _fmm               = "0=sma,1=ema,2=ssma,3=lwma";
extern int    FastMAMethod       = MODE_EMA;
extern int    FastMAPrice        = PRICE_CLOSE;
extern int    SlowMa             = 13;
extern string _smm               = "0=sma,1=ema,2=ssma,3=lwma";
extern int    SlowMAMethod       = MODE_EMA;
extern int    SlowMAPrice        = PRICE_CLOSE;
extern bool   ShowMaLines        = true;

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

extern string note7              = "Arrow Type";
extern string note8              = "0=default,1=Thick,2=Thin,3=Hollow";
extern string note9              = "4=Round,5=Fractal,6=Diagonal Thin";
extern string note10             = "7=Diagonal Thick,8=Diagonal Hollow";
extern string note11             = "9=Thumb,10=Finger";
extern int    ArrowType          = 3;
extern int    arrowthickness     = 2;

//


double CrossUp[];
double CrossDn[];
double fastMA[];
double slowMA[];
double trend[];

//


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

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

int init()
{
   IndicatorBuffers(5);   
   if (ArrowType == 0) {
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0,DRAW_ARROW,0,arrowthickness); SetIndexArrow(0,119);
   SetIndexBuffer(1, CrossDn ); SetIndexStyle(1,DRAW_ARROW,0,arrowthickness); SetIndexArrow(1,119);
   }
   if (ArrowType == 1) {
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 233);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 234);
   }
   else if (ArrowType == 2) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 225);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 226);
   }
   else if (ArrowType == 3) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 241);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 242);
   }
   else if (ArrowType == 4) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 221);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 222);
   }
   else if (ArrowType == 5) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 217);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 218);
   }
   else if (ArrowType == 6) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 228);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 230);
   }
   else if (ArrowType == 7) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 236);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 238);
   }
   else if (ArrowType == 8) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 246);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 248);
   }
   else if (ArrowType == 9) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 67);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 68);
   }
   else if (ArrowType == 10) { 
   SetIndexBuffer(0, CrossUp);  SetIndexStyle(0, DRAW_ARROW,0,arrowthickness); SetIndexArrow(0, 71);
   SetIndexBuffer(1, CrossDn);  SetIndexStyle(1, DRAW_ARROW,0,arrowthickness); SetIndexArrow(1, 72);
   }
   SetIndexBuffer(2, fastMA);
   SetIndexBuffer(3, slowMA);
   SetIndexBuffer(4, trend);
   
   if (ShowMaLines)
   {
     SetIndexStyle(2,DRAW_LINE); 
     SetIndexStyle(3,DRAW_LINE); 
   }
   else
   {
     SetIndexStyle(2,DRAW_NONE);
     SetIndexStyle(3,DRAW_NONE);
   }       
   
         //
        
         indicatorFileName = WindowExpertName();
         calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
         returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
         timeFrame         = stringToTimeFrame(TimeFrame);
         
         //
      
         
   IndicatorShortName(timeFrameToString(timeFrame)+  " MA Cross");
return(0);
}

//

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

   //
 
   //
   
   if (calculateValue || timeFrame==Period())
   {   
     for(i=limit; i>=0; i--)
     {
       fastMA[i]  = iMA(NULL,0,FastMa,0,FastMAMethod,FastMAPrice,i);
       slowMA[i]  = iMA(NULL,0,SlowMa,0,SlowMAMethod,SlowMAPrice,i);
      
       trend[i] = trend[i+1];
          if (fastMA[i]>slowMA[i]) trend[i] = 1;
          if (fastMA[i]=0; i--)
      {
         int y = iBarShift(NULL,timeFrame,Time[i]);
            trend[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,"",FastMAMethod,FastMAPrice,SlowMa,"",SlowMAMethod,SlowMAPrice,4,y);
            fastMA[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,"",FastMAMethod,FastMAPrice,SlowMa,"",SlowMAMethod,SlowMAPrice,2,y);
            slowMA[i]  = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",FastMa,"",FastMAMethod,FastMAPrice,SlowMa,"",SlowMAMethod,SlowMAPrice,3,y);
            CrossUp[i] = EMPTY_VALUE;
            CrossDn[i] = EMPTY_VALUE;
            if (trend[i]!= trend[i+1])
            if (trend[i] == 1)
                  CrossUp[i] = slowMA[i] - iATR(NULL,0,20,i)/2;
            else  CrossDn[i] = slowMA[i] + iATR(NULL,0,20,i)/2;
      }
  manageAlerts();
return(0);
}

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


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 character = StringGetChar(s, length);
         if((character  > 96 && character  < 123) || (character > 223 && character < 256))
                     s = StringSetChar(s, length, character - 32);
         else if(character > -33 && character < 0)
                     s = StringSetChar(s, length, character + 224);
   }
   return(s);
}

//

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,"buy");
         if (trend[whichBar] ==-1) doAlert(whichBar,"sell");
      }         
   }
}   

//
//
//
//
//

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)," - ",timeFrameToString(timeFrame)+" Ma Cross ",doWhat);
          if (alertsMessage) Alert(message);
          if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Ma Cross "),message);
          if (alertsSound)   PlaySound("alert2.wav");
   }
}

Recommend