
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_minimum -35.0
#property indicator_maximum 35.0
#property indicator_buffers 4
#property indicator_color1 Magenta
#property indicator_color2 DodgerBlue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_level2 12.0
#property indicator_level3 -12.0

extern bool            alertsOn                     = true;        // Turn alerts on?
extern bool            alertsOnCurrent              = false;       // Alerts on still open bar?
extern bool            alertsMessage                = true;        // Alerts should show popup message?
extern bool            alertsSound                  = false;       // Alerts should play a sound?
extern bool            alertsEmail                  = false;       // Alerts should send email?
extern bool            alertsNotify                 = false;       // Alerts should send notification?

double AboveBuff[];
double ShortBuff[];
double LongBuffe[];
double BelowBuff[],trend[];

// ---
int init() 
{
   IndicatorBuffers(5);
   SetIndexBuffer(0, AboveBuff); SetIndexStyle(0, DRAW_HISTOGRAM, EMPTY, 2);                          // Lime Above 0
   SetIndexBuffer(1, BelowBuff); SetIndexStyle(1, DRAW_HISTOGRAM, EMPTY, 2);                          // Lime Below 0
   SetIndexBuffer(2, ShortBuff); SetIndexStyle(2, DRAW_ARROW, EMPTY, 2);       SetIndexArrow(2, 108); // Red
   SetIndexBuffer(3, LongBuffe); SetIndexStyle(3, DRAW_ARROW, EMPTY, 2);       SetIndexArrow(3, 108); // Blue
   SetIndexBuffer(4, trend); 
   

   SetIndexLabel(0, "Above");
   SetIndexLabel(1, "Below");   
   SetIndexLabel(2, NULL);  
   SetIndexLabel(3, NULL);  
   
   SetLevelStyle(STYLE_DOT, 0, SteelBlue);

   IndicatorShortName("ProfessionalSwing");
   return (0);
}
	 	    			  		  		   		 	 	 	 	 		 						 	   				  	   	 		 			  			  	     		  	 			   	 	     			 	 		 		     			 	 	  		 	  		   			 			    	
// ---
void deinit() {
   Comment("");
}
		    			 	 			 	 	  	  		  	   	 				 			     		 	 	 	 		 	 	 	 	 		 		 		  	    					   		  	    	 					 	  	    	 			 	 	 		 	 	  	 	   	  	 	
// ---
void start() {
   double Temp;
   double Main;
   double Minr;
   int Limit;
   int CntBr = IndicatorCounted();
   if (CntBr >= 0) {
      if (CntBr > 0) CntBr--;
      Limit = Bars - CntBr;
      for (int i = 0; i < Bars; i++) {
         Temp = 0.0;
         for (int j = i; j < i + 5; j++) Temp += (High[j] + Low[j]) / 2.0;
         Main = Temp / 5.0;
         Temp = 0.0;
         for (j = i; j < i + 5; j++) Temp += High[j] - Low[j];
         Minr = 0.2 * (Temp / 5.0);
         AboveBuff[i] = 3.0 * (High[i]  - Main) / Minr;
         BelowBuff[i] = 3.0 * (Low[i]   - Main) / Minr;
      }
      for (i = 0; i < Limit; i++) 
      {
         trend[i] = 0; 
         ShortBuff[i] = LongBuffe[i] = EMPTY_VALUE;
         if (AboveBuff[i] >  24.0) { ShortBuff[i] =  25; trend[i] =-1;  }
         if (BelowBuff[i] < -24.0) { LongBuffe[i] = -25; trend[i] = 1;  }
      }
   }
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (trend[whichBar] != trend[whichBar+1])
      if (trend[whichBar] == 1)
            doAlert(" upper arrow");
      else  doAlert(" lower arrow");       
   }    
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------

void doAlert(string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[0]) {
          previousAlert  = doWhat;
          previousTime   = Time[0];

          //
          //
          //
          //
          //

          message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" ProfessionalSwing "+doWhat;
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(_Symbol+" ProfessionalSwing ",message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}



