//+------------------------------------------------------------------+
//|                                   trendline price alert v1.4.mq4 |
//|                                                     superzeeshan | 
//+------------------------------------------------------------------+

//MOD by Chris Merciless   2013-09-11
//
// Added ChangeColour setting to allow user to decide if colour should be changed.
// De-obfuscated code for easier reading...

//MOD by kosmo 2013-11-14
// Added Push setting to make the indicator send notifications to iphone and android apps.

#property copyright "superzeeshan" 

#property indicator_chart_window
extern double
   Distance          = 2; 
extern bool
   MsgAlerts         =  true,
   eMailAlerts       =  false,
   Push              =  true,
   ChangeColour      =  false;
   
extern color
   ColorOfHitsLevel  = Yellow;
   
extern string
   SoundAlert       =  "Alert.wav",
   ExcludeString    =  "close, Trendline, Triangle, Eights, MoveLine",
   IncludeString    =  "Test";
      
extern int     ExcludeLength     =  3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   Comment("");
   if( Digits == 3 || Digits == 5 )
   {
      Distance *= 10;
   }
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----
   Comment("");
//----
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   string objName, objDesc, sExclude;
//---- 
   for(int t = ObjectsTotal() - 1; t >= 0; t-- )
   {
      objName = ObjectName(t);
      objDesc = ObjectDescription( objName );
      sExclude = StringSubstr( objName, 0, ExcludeLength );
      //Print( sExclude );
      if( StringFind( ExcludeString, sExclude, 0 ) > -1 )
      {
         if( StringFind( objDesc, "DONE", 0 ) > -1 )
         {
            //Print( "Line already checked..." );
            continue;
         }
         else
         {
            //Print( sExclude, " found in ", ExcludeString, " - Line will be ignored..." );
            continue;
         }
      }
      
      if( ObjectType( objName ) == OBJ_VLINE || ObjectType( objName ) > OBJ_TRENDBYANGLE || StringFind( ExcludeString, sExclude, 0 ) > -1  )
      {
         continue;
      }
      if( StringFind( ObjectDescription( objName )," DONE", 0 ) < 0 )
      { 
         double CurrValue;
         if(ObjectType( objName ) == OBJ_HLINE )
         {
            CurrValue = ObjectGet( objName, OBJPROP_PRICE1 );
         }
         else
         {
            CurrValue = ObjectGetValueByShift( objName, 0 );
         }
         if( MathAbs( Bid - CurrValue ) <= Distance*Point )
         {
            if( SoundAlert != "" )
            {
               PlaySound( SoundAlert );
               Sleep( 5000 );
            }
            if( MsgAlerts )
            {
               Alert( Symbol() + " " + ActivTF( ObjectGet( objName, OBJPROP_TIMEFRAMES ) ) + "Price " + DoubleToStr( Bid,Digits ), ": ", ObjectDescription( objName ) );
            }
            if( eMailAlerts )
            {
               SendMail( Symbol(), Symbol() + " " + ActivTF( ObjectGet( objName, OBJPROP_TIMEFRAMES ) ) + "Price " + DoubleToStr( Bid,Digits ) + ": " + ObjectDescription( objName ) );
            }
            if( Push )
            {
               SendNotification( Symbol() + " " + ActivTF( ObjectGet( objName, OBJPROP_TIMEFRAMES ) ) + "Price " + DoubleToStr( Bid,Digits ) + ": " + ObjectDescription( objName ) );
            }
            string temptxt = ObjectDescription( objName );
            ObjectSetText( objName, temptxt + " DONE!!!", 10 ); 
            if( ChangeColour )
            {
               ObjectSet( objName, OBJPROP_COLOR, ColorOfHitsLevel );
            }
         }
      }
   }
//----
   return(0);
} 
//+------------------------------------------------------------------+
//|  ActivTF                                                         |
//+------------------------------------------------------------------+
string ActivTF(int tf)
{
   string strTF[11]={"","M1","M5","M15","M30","H1","H4","D1","W1","MN1"}, actTF;
   int    intTF[11]={0,1,2,4,8,16,32,64,128,256}, tmpTF;
   if( tf == 0 )
   {
      return( PerToStr( Period() ) ); 
   }
   tmpTF = tf;
   for(int i = 9; i >= 1; i-- )
   {
      if( tmpTF >= intTF[i] )
      {
         tmpTF = tmpTF - intTF[i];
         actTF = actTF + strTF[i] + ", ";
      }
      if( tmpTF == 0 )
      {
         break;
      }
   }
  return( actTF ); 
}
//+------------------------------------------------------------------+
//|  PerToStr                                                        |
//+------------------------------------------------------------------+
string PerToStr(int p)
{
   string Result = "";
   
   switch( p )
   {
      case PERIOD_M1 : { Result = "M1 "; break; }
      case PERIOD_M5 : { Result = "M5 "; break; }
      case PERIOD_M15: { Result = "M15"; break; }
      case PERIOD_M30: { Result = "M30"; break; }
      case PERIOD_H1 : { Result = "H1 "; break; }
      case PERIOD_H4 : { Result = "H4 "; break; }
      case PERIOD_D1 : { Result = "D1 "; break; }
      case PERIOD_W1 : { Result = "W1 "; break; }
      case PERIOD_MN1: { Result = "MN1"; break; }
      default        : { Result = "Undefined"; break; }
   }
   return( Result );
}  