MADistancewAlerts





//+------------------------------------------------------------------+ 
//|                 MA DISTANCING INDICATOR                          | 
//|                                                                  | 
//|   Copyright © 2008, Tim Hyder aka Hiachiever                     |
//|                                                                  |
//|   PO BOX 768, Hillarys, Western Australia, Australia, 6923       |
//|                                                                  |
//|   CUSTOMISED MQ4 DEVELOPMENT                                     | 
//|   I am available for customised MQ4 development work. Please     |
//|   contact me at hiachiever@gmail.com, to discuss your  coding    |
//|   requirements. My rates are very reasonable and most jobs are   |
//|   a fixed price quote.                                           |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - hiachiever@gmail.com                                  |  
//+------------------------------------------------------------------+ 

#property copyright "Copyright © 2007, Tim Hyder."
#property link      "hiachiever@gmail.com"

#define vers    "25-Jul-2008"
#define major   1
#define minor   1

#property indicator_chart_window

extern string MOVAVG = " --- MOVING AVERAGE SETTINGS --- ";
extern int MA.Period = 5; 
extern int MA.Method = MODE_EMA;
extern int MA.Price = PRICE_CLOSE;
extern bool UseTickdata = True;

extern string DISTANCEA = "==== MOVING AVERAGE DISTANCING ====";
extern int MA.Distance = 10; 
extern color GreaterThanDistance = Red; 
extern color LessThanDistance = Aqua; 
extern int FontSize = 10;
extern int MoveUPDN = 20; 
extern int MoveLeftRight = 20;
extern string Alerts       = "==== ALERT SETTINGS =====";
extern bool   UseAlerts    = True;
extern bool   MsgAlerts    = True;
extern bool   SoundAlerts  = True;
extern string SoundAlertFile = "alert.wav";
extern bool   eMailAlerts = false;

string prefix = "MAD_";
string MainTxt;
bool AllowUpperAlert = True, AllowLowerAlert = True;

//references to this need to be removed from the code - when I have time
bool Corner_of_Chart_RIGHT_TOP = False;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
    MainTxt = prefix + ": " + Symbol()+ ", TF: " + TF2Str(Period());
    return(0);
}
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   int total = ObjectsTotal();  
   for (int i=total-1; i >= 0; i--) 
   {
     string name = ObjectName(i);
     if (StringFind(name, prefix) == 0) ObjectDelete(name);
   }
   return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   string LblText = "-";
   color  LblColor;
   string Subj, Msg;
   
   int Shift = 1;
   if(UseTickdata) Shift = 0;
   
   double MA = iMA(Symbol(),Period(),MA.Period, 0, MA.Method, MA.Price, Shift);
   int Diff = (Close[Shift] - MA)/Point;

   LblColor = GreaterThanDistance; 
   if (MathAbs(Diff)<MA.Distance) LblColor = LessThanDistance; 

   CreateLabel(prefix+"1", "Difference between " + DoubleToStr(MA.Period,0) + " period MA and Price is " + DoubleToStr(Diff,0) + " pips.", FontSize, "Arial Bold", LblColor, Corner_of_Chart_RIGHT_TOP,MoveLeftRight,MoveUPDN);

   if( UseAlerts )
   {
      if ( AllowUpperAlert &&  Diff > MA.Distance ) 
      {  
         Subj = MainTxt + ".  Difference >= " + MA.Distance;
         Msg = Subj + " @ " + TimeToStr(TimeLocal(),TIME_SECONDS);
         DoAlerts(Msg,Subj);
         AllowUpperAlert = False;   
         AllowLowerAlert = True;   
      }
      else if ( AllowLowerAlert &&  Diff < -MA.Distance ) 
      {
         Subj = MainTxt + ".  Difference >= " + MA.Distance;
         Msg = Subj + " @ " + TimeToStr(TimeLocal(),TIME_SECONDS);
         DoAlerts(Msg,Subj);
         AllowLowerAlert = False;   
         AllowUpperAlert = True;   
      }
      else if ( Diff > -MA.Distance && Diff < MA.Distance )
      {
         AllowUpperAlert = True;   
         AllowLowerAlert = True;  
      }   
   }
   return(0);
}
//+------------------------------------------------------------------+

void CreateLabel(string LblName, string LblTxt, double FontSz, string FontName, color FontColor, int Corner, int xPos, int yPos)
{
   if(ObjectFind(LblName) != 0) ObjectCreate(LblName, OBJ_LABEL, 0, 0, 0);

   ObjectSetText(LblName, LblTxt, FontSz, FontName, FontColor);
   ObjectSet(LblName, OBJPROP_CORNER, Corner);
   ObjectSet(LblName, OBJPROP_XDISTANCE, xPos);
   ObjectSet(LblName, OBJPROP_YDISTANCE, yPos); 
}

//+------------------------------------------------------------------+
string TF2Str(int period) 
{
  switch (period) 
  {
    case PERIOD_M1: return("M1");
    case PERIOD_M5: return("M5");
    case PERIOD_M15: return("M15");
    case PERIOD_M30: return("M30");
    case PERIOD_H1: return("H1");
    case PERIOD_H4: return("H4");
    case PERIOD_D1: return("D1");
    case PERIOD_W1: return("W1");
    case PERIOD_MN1: return("MN");
  }
  return (Period());
} 

//+------------------------------------------------------------------+
void DoAlerts(string msgText,string eMailSub)
{
   if (MsgAlerts) Alert(msgText);
   if (SoundAlerts) PlaySound(SoundAlertFile);
   if (eMailAlerts) SendMail(eMailSub, msgText);
}





Sample





Analysis



Market Information Used:

Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:

Moving average indicator


Custom Indicators Used:

Order Management characteristics:

Other Features:

It issuies visual alerts to the screen
It plays sound alerts
It sends emails