//+------------------------------------------------------------------+ 
//|   _TRO_ALERT_DSR2                                                | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFT AND DONATIONS ACCEPTED                                    | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

#property indicator_chart_window
#property indicator_buffers 0


extern bool my.alert.sound = true ;

extern string mySymbol      = "" ;
extern int    myChartPeriod = 0 ; 
extern int    myBars        = 5; 

double nHH, nLL, nHH1, nLL1, nHH2, nLL2; 

string tChartPeriod ;

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
   
   if( mySymbol == "" ) { mySymbol = Symbol(); }
   
   if( myChartPeriod == 0 ) { myChartPeriod = Period() ; }
   
   tChartPeriod =  TimeFrameToString(myChartPeriod) ;
   
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
  { 
 
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
  { 
  

      nHH  = High[Highest(mySymbol,myChartPeriod,MODE_HIGH,myBars,0)]; 
      nLL  = Low[Lowest(mySymbol,myChartPeriod,MODE_LOW,myBars,0)]; 
      nHH1 = High[Highest(mySymbol,myChartPeriod,MODE_HIGH,myBars,1)]; 
      nLL1 = Low[Lowest(mySymbol,myChartPeriod,MODE_LOW,myBars,1)]; 
      nHH2 = High[Highest(mySymbol,myChartPeriod,MODE_HIGH,myBars,2)]; 
      nLL2 = Low[Lowest(mySymbol,myChartPeriod,MODE_LOW,myBars,2)]; 


      Comment(tChartPeriod,mySymbol,",RESISTANCE  ", DoubleToStr(nHH,Digits), "\n", 
              tChartPeriod,mySymbol,",SUPPORT  ", DoubleToStr(nLL,Digits), "\n","" );

   if(Close[0] == nHH  )    
   {
      if( my.alert.sound) { Alert(tChartPeriod,mySymbol,", NEW RESISTANCE ", DoubleToStr(nHH,Digits)); }
      Comment(tChartPeriod,mySymbol,",NEW RESISTANCE", DoubleToStr(nHH,Digits), "\n", "" ); 
   } 
   else
   if(Close[0] == nLL  )    
   {
      if( my.alert.sound) { Alert(tChartPeriod,mySymbol,", NEW  SUPPORT ", DoubleToStr(nLL,Digits)); }
      Comment(tChartPeriod,mySymbol,",NEW SUPPORT ", DoubleToStr(nLL,Digits), "\n", "" ); 
   } 


   if(Close[0] == Open[0]
   && nHH2 != nHH1
   && nHH  == nHH1 )    
   {
      if( my.alert.sound) { Alert(tChartPeriod,mySymbol,", RESISTANCE2  ", DoubleToStr(nHH,Digits)); }
      Comment(tChartPeriod,mySymbol,",RESISTANCE2", DoubleToStr(nHH,Digits), "\n", "" ); 
   } 
   else
   if(Close[0] == Open[0]  
   && nLL2 != nLL1
   && nLL  == nLL1 )    
   {
      if( my.alert.sound) { Alert(tChartPeriod,mySymbol,", SUPPORT2  ", DoubleToStr(nLL,Digits)); }
      Comment(tChartPeriod,mySymbol,",SUPPORT2  ", DoubleToStr(nLL,Digits), "\n", "" ); 
   } 

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

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