//+------------------------------------------------------------------+
//|                                  2MA Crossover.mq4 modified from |
//|                                         EMA-Crossover_Signal.mq4 |
//|         Copyright © 2005-07, Jason Robinson (jnrtrading)         |
//|                   http://www.jnrtading.co.uk                     |
//+------------------------------------------------------------------+

/*
  +------------------------------------------------------------------+
  | Allows you to enter two ema periods and it will then show you at |
  | Which point they crossed over. It is more usful on the shorter   |
  | periods that get obscured by the bars / candlesticks and when    |
  | the zoom level is out. Also allows you then to remove the emas   |
  | from the chart. (emas are initially set at 5 and 6)              |
  +------------------------------------------------------------------+
*/   

#property copyright "Copyright © 2005-07, Jason Robinson (jnrtrading)"
#property link      "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Yellow
#property indicator_width1 1
#property indicator_color2 Aqua
#property indicator_width2 1

#property indicator_color3 Blue
#property indicator_color4 Maroon
#property indicator_color5 Maroon

//#include <hanover --- function header.mqh>

extern int      MA1              = 5;
extern int      MA1Mode          = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern int      MA1price         = 0; //0=sma, 1=ema, 2=smma, 3=lwma
extern int      MA2              = 34;
extern int      MA2Mode          = 1; //0=sma, 1=ema, 2=smma, 3=lwma
extern bool     drawMAs          = true;
extern double   arrDistance      = 0.7;
extern int      ArrowType        = 0;
extern string   note7  = "Arrow Type";
extern string   note8  = "0=Thick, 1=Thin, 2=Hollow, 3=Round";
extern string   note9  = "4=Fractal, 5=Diagonal Thin";
extern string   note10 = "6=Diagonal Thick, 7=Diagonal Hollow";
extern string   note11 = "8=Thumb, 9=Finger";
extern string   note5 = "0=sma, 1=ema, 2=smma, 3=lwma";
extern string   note_price = "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
extern string   note4 = "Second MAprice:hilo";

extern int     AlertCandle         = 1;
extern bool    ShowChartAlerts     = false;
extern string  AlertEmailSubject   = "";

datetime       LastAlertTime       = -999999;

string         AlertTextCrossUp    = "MAxMAhilo cross UP";          //---- type your desired text between the quotes
string         AlertTextCrossDown  = "MAxMAhilo cross DOWN";        //---- type your desired text between the quotes

double   CrossUp[], CrossDown[], MA1_buffer[], MA2hi_buffer[], MA2lo_buffer[];

string   MA1short_name, MA2short_name;

//+------------------------------------------------------------------+
int init()  {
//+------------------------------------------------------------------+
  SetIndexStyle(0,DRAW_ARROW);
  SetIndexStyle(1,DRAW_ARROW);
  switch  (ArrowType)   {
    case 0 :   SetIndexArrow(0, 233);   SetIndexArrow(1, 234);   break;
    case 1 :   SetIndexArrow(0, 225);   SetIndexArrow(1, 226);   break;
    case 2 :   SetIndexArrow(0, 241);   SetIndexArrow(1, 242);   break;
    case 3 :   SetIndexArrow(0, 221);   SetIndexArrow(1, 222);   break;
    case 4 :   SetIndexArrow(0, 217);   SetIndexArrow(1, 218);   break;
    case 5 :   SetIndexArrow(0, 228);   SetIndexArrow(1, 230);   break;
    case 6 :   SetIndexArrow(0, 236);   SetIndexArrow(1, 238);   break;
    case 7 :   SetIndexArrow(0, 246);   SetIndexArrow(1, 248);   break;
    case 8 :   SetIndexArrow(0,  67);   SetIndexArrow(1,  68);   break;
    case 9 :   SetIndexArrow(0,  71);   SetIndexArrow(1,  72);   break;
  }
  SetIndexEmptyValue(0, EMPTY_VALUE);
  SetIndexEmptyValue(1, EMPTY_VALUE);

  SetIndexBuffer(0, CrossUp);
  SetIndexBuffer(1, CrossDown);

  if (drawMAs)  {
    SetIndexBuffer(2, MA1_buffer);
    SetIndexBuffer(3, MA2hi_buffer);
    SetIndexBuffer(4, MA2lo_buffer);
  }

  switch(MA1Mode)    {
    case 1  : MA1short_name="EMA";  break;
    case 2  : MA1short_name="SMMA"; break;
    case 3  : MA1short_name="LWMA"; break;
    default :
      MA1Mode=0;
      MA1short_name="SMA";
  }
  switch(MA2Mode)    {
    case 1 : MA2short_name="EMA";  break;
    case 2 : MA2short_name="SMMA"; break;
    case 3 : MA2short_name="LWMA"; break;
    default :
      MA2Mode=0;
      MA2short_name="SMA";
  }
  return(0);
}
//+------------------------------------------------------------------+
int deinit()   {
//+------------------------------------------------------------------+
  return(0);
}

//+------------------------------------------------------------------+
int start() {
//+------------------------------------------------------------------+
  int limit, i, counter;
  double MA1now, MA2now, MA1previous, MA2previous, MA1after, MA2after;
  double Range, AvgRange;
  int counted_bars=IndicatorCounted();
  if (counted_bars<0) return(-1);
  if (counted_bars>0) counted_bars--;

  limit=Bars-counted_bars;
  for (i=0; i<=limit; i++)  {
    counter   = i;
    Range     = 0;
    AvgRange  = 0;
    for (counter=i; counter<=i+9; counter++)   {
      AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
    }
    Range=AvgRange/10*arrDistance;
      
    double MA1_0   = iMA(NULL, 0, MA1, 0, MA1Mode, MA1price, i);
    double MA1_1   = iMA(NULL, 0, MA1, 0, MA1Mode, MA1price, i+1);
    double MA2hi_0 = iMA(NULL, 0, MA2, 0, MA2Mode, PRICE_HIGH, i);
    double MA2hi_1 = iMA(NULL, 0, MA2, 0, MA2Mode, PRICE_HIGH, i+1);
    double MA2lo_0 = iMA(NULL, 0, MA2, 0, MA2Mode, PRICE_LOW, i);
    double MA2lo_1 = iMA(NULL, 0, MA2, 0, MA2Mode, PRICE_LOW, i+1);

    CrossUp[i]   = EMPTY_VALUE; 
    CrossDown[i] = EMPTY_VALUE;
 
    if ((MA1_0 > MA2hi_0) && (MA1_1 < MA2hi_1))  {
      CrossUp[i] = Low[i] - Range*1.5;
    }
    if ((MA1_0 < MA2lo_0) && (MA1_1 > MA2lo_1))  {
      CrossDown[i] = High[i] + Range*1.5;
    }  
//    if (i<20)    log(i,CrossUp[i],CrossDown[i],alert[i]);

    if (drawMAs)   {
      MA1_buffer[i]   = MA1_0;
      MA2hi_buffer[i] = MA2hi_0;
      MA2lo_buffer[i] = MA2lo_0;
    }
  }
  ProcessAlerts();
   
  return(0);
}

//+------------------------------------------------------------------+
int ProcessAlerts()   {
//+------------------------------------------------------------------+
  if (AlertCandle >= 0  &&  Time[0] > LastAlertTime)   {
    // === Alert processing for crossover UP (indicator line crosses ABOVE signal line) ===
    if (CrossUp[AlertCandle] != EMPTY_VALUE)  {
      string AlertText = Symbol() + "," + TFToStr(Period()) + ": " + AlertTextCrossUp;
      if (ShowChartAlerts)          Alert(AlertText);
      if (AlertEmailSubject > "")   SendMail(AlertEmailSubject,AlertText);
      LastAlertTime = Time[0];
    }
    // === Alert processing for crossover DOWN (indicator line crosses BELOW signal line) ===
    if (CrossDown[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("");
}

//+------------------------------------------------------------------+
//#include <hanover --- extensible functions.mqh>