//+------------------------------------------------------------------+
//|                                             super-signals_v2.mq4 |
//|                Copyright © 2006, Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+


//+----------------------------------------------------------------------------------------------------------------+
//|ALERTS:                                                                                                         |
//|                                                                                                                |
//|Since it was not possible to get alerts to work in the previous version the present alteration allows one of    |
//|three available alert types to be used, in this case the "strong" buy and sell. Available alert types are in-   |
//|cluded in this box and may be substituted.                                                                      | 
//|                                                                                                                |
//|            if (b1[forBar] != EMPTY_VALUE && b3[forBar] != EMPTY_VALUE) doAlert("strong sell");                 |
//|            if (b1[forBar] != EMPTY_VALUE && b3[forBar] == EMPTY_VALUE) doAlert("sell");                        |
//|            if (b1[forBar] == EMPTY_VALUE && b3[forBar] != EMPTY_VALUE) doAlert("minor sell or exit buy");      |
//|            if (b2[forBar] != EMPTY_VALUE && b4[forBar] != EMPTY_VALUE) doAlert("strong buy");                  |
//|            if (b2[forBar] != EMPTY_VALUE && b4[forBar] == EMPTY_VALUE) doAlert("buy");                         |
//|            if (b2[forBar] == EMPTY_VALUE && b4[forBar] != EMPTY_VALUE) doAlert("minor buy or exit sell");      |
//+----------------------------------------------------------------------------------------------------------------+



#property copyright "Copyright © 2006, Nick Bilak"
#property copyright "alterations by Mark Tomlinson"
#property copyright "further alterations by brucewhain using Hanover's DYS Alert Builder Kit, ForexFactory.com"
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 PaleVioletRed
#property indicator_color2 DeepSkyBlue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//
//
//
//
//

extern int     dist2           = 100;
extern int     dist1           = 50; 
extern bool    ShowArrows      = true;
extern int     AlertCandle         = 0;  //---- O = immediate signal; 1 = signal on Open of new bar                                                                                                  
extern bool    ShowChartAlerts     = true;                                                                                                     
extern string  AlertEmailSubject   = "";                                                                                                        
//                                                                                                                                                
datetime       LastAlertTime       = -999999;                                                                                                   
//                                                                                                                                                
string         AlertTextCrossUp    = "SuperSignals Alert HIGH";       //---- type your desired text between the quotes                         
string         AlertTextCrossDown  = "SuperSignals Alert LOW";        //---- type your desired text between the quotes                         
                                             
//
//
//
//
//

double b1[];
double b2[];
double b3[];
double b4[];

//
//
//
//
//

int init()
{
   SetIndexBuffer(0,b1);
   SetIndexBuffer(1,b2);
   SetIndexBuffer(2,b3);
   SetIndexBuffer(3,b4);
   
   if (ShowArrows)
   {
   SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159);
   SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
   }
   else
   {
   SetIndexStyle(2,DRAW_NONE);
   SetIndexStyle(3,DRAW_NONE);
   }
   
   return(0);
}

//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           limit=MathMax(limit,dist1);
           limit=MathMax(limit,dist2);

   //
   //
   //
   //
   //

   for (i = limit; i >= 0; i--)
   {
         int hhb1 = Highest(NULL,0,MODE_HIGH,dist1,i-dist1/2);
         int llb1 = Lowest(NULL,0, MODE_LOW, dist1,i-dist1/2);
         int hhb  = Highest(NULL,0,MODE_HIGH,dist2,i-dist2/2);
         int llb  = Lowest(NULL,0, MODE_LOW, dist2,i-dist2/2);

            b1[i] = EMPTY_VALUE;
            b2[i] = EMPTY_VALUE;
            b3[i] = EMPTY_VALUE;
            b4[i] = EMPTY_VALUE;
      
         double tr = iATR(NULL,0,50,i);
                      b1[i] = High[hhb];
                      b2[i] = Low[llb];
         if (i==hhb1) b3[i] = High[hhb1]+tr/2;
         if (i==llb1) b4[i] = Low[llb1] -tr/2;

      }
    ProcessAlerts();
 return(0);
}

//      
//

int ProcessAlerts()   {

   if (AlertCandle >= 0  &&  Time[0] > LastAlertTime)   {
   
    if (b1[AlertCandle] != EMPTY_VALUE && b3[AlertCandle] != EMPTY_VALUE)  {                  
      string AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossUp;                                                          
      if (ShowChartAlerts)          Alert(AlertText);                                                                                           
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);                                                                      
      LastAlertTime = Time[0];                                                                                                         
    }                                
   
   if (b2[AlertCandle] != EMPTY_VALUE && b4[AlertCandle] != EMPTY_VALUE)  { 
      AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossDown;                                                               
      if (ShowChartAlerts)          Alert(AlertText);                                                                                           
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);                                                                      
      LastAlertTime = Time[0];                                                                                                                  
    }                           
   
    }
 return(0);
}

string TFToStr(int tf)   {

  if (tf == 0)        tf = Period();                                                                                                            
  if (tf >= 43200)    return("MN");                                                                                                             
  if (tf >= 10080)    return("W1");                                                                                                             
  if (tf >=  1440)    return("D1");                                                                                                             
  if (tf >=   240)    return("H4");                                                                                                             
  if (tf >=    60)    return("H1");                                                                                                             
  if (tf >=    30)    return("M30");                                                                                                            
  if (tf >=    15)    return("M15");                                                                                                            
  if (tf >=     5)    return("M5");                                                                                                             
  if (tf >=     1)    return("M1");                                                                                                             
  return("");                                                                                                                                   
}                                  
