//+------------------------------------------------------------------+ 
//|                 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                                  |  
//+------------------------------------------------------------------+ 
//| MADistance_TRO_MODIFIED_VERSION                                  |
//| MODIFIED BY AVERY T. HORTON, JR. AKA THERUMPLEDONE@GMAIL.COM     |
//| I am NOT the ORIGINAL author 
//  and I am not claiming authorship of this indicator. 
//  All I did was modify it. I hope you find my modifications useful.|
//|                                                                  |
//+------------------------------------------------------------------+


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

#define vers    "08-May-2008"
#define major   1
#define minor   0

#property indicator_chart_window


//+--------- TRO MODIFICATION ---------------------------------------+ 

extern bool   Sound.Alert    = false ;
//extern bool   Show.PriceBox  = true ;
//extern int    myBoxWidth  = 3;


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;

string prefix = "MAD_";

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

//+--------- TRO MODIFICATION ---------------------------------------+ 
string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

bool Trigger ;

int OldBars = -1 ;

color tColor = Yellow ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
 
//+--------- TRO MODIFICATION ---------------------------------------+  
   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   

   tShortName = "mad"+ symbol + tChartPeriod + MA.Period + MA.Method + MA.Price ;
 
    return(0);
  }
  
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
  
     ObjectDelete(tShortName+"01"); 
  
   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()
{


//+--------- TRO MODIFICATION ---------------------------------------+   
   if( Bars != OldBars ) { Trigger = True ; }
 
   string LblText = "-";
   color  LblColor;
   
   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);



//+--------- TRO MODIFICATION ---------------------------------------+  
      
      if ( Trigger &&  Sound.Alert ) 
      {
        if( MathAbs(DiFF) == MA.Distance) { Trigger = False ; Alert(symbol,"  ", tChartPeriod, " Price at Distance Threshold "+ DoubleToStr(Close[0] ,digits)); }
     }

//   if(Show.PriceBox) { DoBox() ; }
    
   OldBars = Bars ;   

//+--------- TRO MODIFICATION ---------------------------------------+                  
   return(0);
}

//+--------- TRO MODIFICATION ---------------------------------------+  
/*
void DoBox()    
{


       if (ObjectFind(tShortName+"01") != 0)
      {
          ObjectCreate(tShortName+"01",OBJ_ARROW,0,Time[0],UpperBand[0]);
          ObjectSet(tShortName+"01",OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);  
          ObjectSet(tShortName+"01",OBJPROP_WIDTH,myBoxWidth);  
      } 
      else
      {
         ObjectMove(tShortName+"01",0,Time[0],UpperBand[0]);
         ObjectSet(tShortName+"01",OBJPROP_COLOR,indicator_color1);  
      }

 

      

                
   return(0);
}

*/
//+--------- TRO MODIFICATION ---------------------------------------+  

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}



//+--------- TRO MODIFICATION ---------------------------------------+  

//+------------------------------------------------------------------+

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