//+------------------------------------------------------------------+
//|                                                MACrossRibbon.mq4 |
//|               Copyright 2005-2020, original idea by. Jose Silva. |
//| 16.12.20 mod alert. ganztrade                http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2020, MetaQuotes Software Corp."
#property link        "http://www.mql4.com"
#property description "MACrossRibbon"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 2
#property indicator_width4 2
//
extern int    MA1Period=13;
extern ENUM_MA_METHOD   MA1Method=MODE_SMA; 
extern int    MA1Price =PRICE_CLOSE;
extern int    MA2Period=26;
extern int    MA2Method=MODE_SMA;
extern int    MA2Price =PRICE_CLOSE;
extern bool   AlertON = true;
extern bool   MailON = false;
extern bool   PushON = false;
extern bool   Sound =true;
extern string SoundFile="wait.waw";
int Signal=0;
datetime LastAlert=0;
//template code start1
extern color  UpRibbonColor  = C'0,70,0';
extern color  DownRibbonColor    = C'70,0,0';
extern color  DownLineColor   = clrRed;
extern color  UpLineColor   = clrGreen;
extern bool   btn_show       = true;                       //btn__show
extern string btn_pressed    = "RIB on";              //btn__pressed text
extern string btn_unpressed  = "RIB off";            //btn__unpressed text
extern int    btn_offset_x   = 10;                       //btn__x
extern int    btn_offset_y   = 40;                       //btn__y
extern int    btn_width      = 65;                          //btn__width
extern int    btn_height     = 20;                         //btn__height
extern int    btn_font_size  = 10;                      //btn__font size
extern color  btn_font_clr   = clrBlack;               //btn__font color
extern color  btn_bg_color   = clrAquamarine;          //btn__bg color
extern color  btn_border_clr = clrWhiteSmoke;  //btn__border color
//template code end1
double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,buffer3); SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,buffer4); SetIndexStyle(1,DRAW_NONE);
   SetIndexBuffer(2,buffer1); SetIndexStyle(2,DRAW_NONE);
   SetIndexBuffer(3,buffer2); SetIndexStyle(3,DRAW_NONE);
  
//template modified here  
   if(btn_show){CreateButton();}
   if(!btn_show){ObjectDelete(0,"MA Ribbon");}
//end of template modified
//---- initialization done
   return(0);
} 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   ObjectDelete(0,"MA Ribbon");
   ObjectDelete(0,"Signals");
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
{
   int counted_bars=IndicatorCounted();
   int limit,i;   
   string label;
   color clr;
   
   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           
   for(i=limit; i>=0; i--)
   {
      buffer1[i] = iMA(NULL,0,MA1Period,0,MA1Method,MA1Price,i);
      buffer2[i] = iMA(NULL,0,MA2Period,0,MA2Method,MA2Price,i);
      buffer3[i] = buffer1[i];
      buffer4[i] = buffer2[i];    
   }
 //----  
   if (buffer1[Signal]>buffer2[Signal] && LastAlert!=Time[0]) {
      LastAlert=Time[0];
      if (AlertON) Alert(Symbol(), " M", Period(), " MA Cross Buy");
      if (Sound) PlaySound (SoundFile);
      if (MailON) SendMail("Mail sent","MA Cross Buy " + _Symbol+"["+Period()+"m]");
      if (PushON) SendNotification("Push sent MA Cross Buy" + _Symbol+"["+Period()+"m]");
      }
   else if (buffer1[Signal]<buffer2[Signal] && LastAlert!=Time[0]) {
      LastAlert=Time[0];
      if (AlertON) Alert(Symbol(), " M", Period(), " MA Cross Sell");
      if (Sound) PlaySound (SoundFile);
      if (MailON) SendMail("Mail sent","MA Cross Sell " + _Symbol+"["+Period()+"m]");
      if (PushON) SendNotification("Push sent MA Cross Sell" + _Symbol+"["+Period()+"m]");
      }
      
   if (buffer1[Signal]>buffer2[Signal]) { label = "Long";  clr = clrGreen;}
   else if (buffer1[Signal]<buffer2[Signal]) { label = "Short"; clr = clrRed;}   
        
   ObjectCreate("Signals", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("Signals", label, 16, "Arial Bold", clr);
   ObjectSet("Signals", OBJPROP_CORNER, 1);
   ObjectSet("Signals", OBJPROP_XDISTANCE, 10);
   ObjectSet("Signals", OBJPROP_YDISTANCE, 5);
   //---
   return(0);
}
//template start codes2
void CreateButton()
{
   ButtonCreate(NULL,"MA Ribbon",0,btn_offset_x,btn_offset_y,btn_width,btn_height,CORNER_LEFT_UPPER,btn_unpressed,"Arial",btn_font_size,btn_font_clr,btn_bg_color,btn_border_clr,InpBack,false,true,false,false,0);
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
   if (id == CHARTEVENT_OBJECT_CLICK && sparam=="MA Ribbon") 
   { 
      if(ObjectGetInteger(0,"MA Ribbon",OBJPROP_STATE)){
         ObjectSetString(0,"MA Ribbon",OBJPROP_TEXT,btn_unpressed);         
         SetIndexStyle(3,DRAW_LINE,STYLE_DOT,1,UpLineColor);
         SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1,DownLineColor);
         SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_DOT,1,DownRibbonColor);
         SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_DOT,1,UpRibbonColor);
          }
      if(!ObjectGetInteger(0,"MA Ribbon",OBJPROP_STATE)){
         ObjectSetString(0,"MA Ribbon",OBJPROP_TEXT,btn_unpressed);
         SetIndexStyle(3,DRAW_LINE,STYLE_DOT,1,clrNONE);
         SetIndexStyle(2,DRAW_LINE,STYLE_DOT,1,clrNONE);
         SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_DOT,1,clrNONE);
         SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_DOT,1,clrNONE);
          }
   }
}
input bool             InpBack=false;               // Background object
//+------------------------------------------------------------------+
bool ButtonCreate(const long              chart_ID=0,               // chart's ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,                 // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',  // background color
                  const color             border_clr=clrNONE,       // border color
                  const bool              state=false,              // pressed/released
                  const bool              back=false,               // in the background
                  const bool              selectable=true,      //object can be selected        
                  const bool              selection=false,          // highlight to move
                  const bool              hidden=false,              // hidden in the object list
                  const long              z_order=0)                // priority for mouse click
  {
   ResetLastError();
   ObjectCreate    (chart_ID,name,OBJ_BUTTON,          sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,   x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,   y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,       width);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,      corner);
   ObjectSetString (chart_ID,name,OBJPROP_TEXT,        text);
   ObjectSetString (chart_ID,name,OBJPROP_FONT,        font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,    font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,       clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,     back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,        back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,       state);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,  selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,    selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,      hidden);
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,      z_order);
   return(true);
  }
//+------------------------------------------------------------------+

