//+------------------------------------------------------------------+
//|                                            MA Cross Histo.mq4    |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//----
#property indicator_separate_window    
#property indicator_buffers  2
#property indicator_minimum  0
#property indicator_maximum  1
#property indicator_color1 Green
#property indicator_color2 Red

//----
extern int FasterMA = 5;
extern int SlowerMA = 15;
extern int MA1_Type = 1;
extern int MA2_Type = 1;
extern bool AlertsOn = true;
extern   int      TextWindow              = 1;
extern   int      TextCorner              = 3;
extern   int      TextX                   = 5;
extern   int      TextY                   = 5;
extern   string   TextFont                = "Arial Narrow Bold";
extern   int      TextSize                = 18;
extern   string   TextBuy                 = "maX +";
extern   string   TextSell                = "maX -";
extern   color    TextBuyColor            = Green;
extern   color    TextSellColor           = Red;
extern   string   SoundFileBuy            = "alert2.wav";
extern   string   SoundFileSell           = "alert2.wav";

string   label_name = "MA Cross";

//---- indicator buffers
double MAUp[];
double MADown[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
//---- indicator line
 
   SetIndexStyle(0, DRAW_ARROW);
   SetIndexArrow(0, 110);
   SetIndexBuffer(0, MAUp);
   SetIndexStyle(1, DRAW_ARROW);
   SetIndexArrow(1, 110);
   SetIndexBuffer(1, MADown);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MA Cross");
   IndicatorDigits(1);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete( label_name );
   return(0);
  }
//+------------------------------------------------------------------+
//|  GoldenFilter_v1                                                 |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars = IndicatorCounted();
   int i;
   int limit;
//---- check for possible errors
   if(counted_bars < 0) 
       return(-1);
//---- last counted bar will be recounted
   if(counted_bars > 0) 
       counted_bars-=2;
   limit = Bars - counted_bars;
//----   
   for(i = limit - 1; i >= 0; i--)
     {	
	    double  fasterMAnow = iMA(NULL, 0, FasterMA, 0, MA1_Type, 
	                              PRICE_CLOSE, i);
       double  slowerMAnow = iMA(NULL, 0, SlowerMA, 0, MA2_Type, 
                                 PRICE_CLOSE, i);
       
       if(NormalizeDouble(fasterMAnow, Digits+1)==NormalizeDouble(slowerMAnow, Digits+1)){
             MAUp[i] = EMPTY_VALUE;
             MADown[i] = EMPTY_VALUE;
       }else
       if((fasterMAnow > slowerMAnow))// && 
         {
           MAUp[i] = 0.5;
           MADown[i] = EMPTY_VALUE;
         }else
       if((fasterMAnow < slowerMAnow))// && 
         {
           MADown[i] = 0.5;
           MAUp[i] = EMPTY_VALUE;
         }       
     }
   static int _v0=0;
   if(_v0==0)
     if(MAUp[1]!=EMPTY_VALUE){_v0=1;}
       else 
     if(MADown[1]!=EMPTY_VALUE){_v0=-1;}
   if(AlertsOn)
   if(_v0 != 0)
   {
     if(MAUp[1]!=EMPTY_VALUE && _v0!=1){ _v0=1; Print("Buy alert"); PlaySound( SoundFileBuy );}
     else 
     if(MADown[1]!=EMPTY_VALUE && _v0!=-1){ _v0=-1; Print("Sell alert"); PlaySound( SoundFileSell );}
     
     if(MAUp[1]!=EMPTY_VALUE)MakeLabel( TextBuy, TextBuyColor );
     else if(MADown[1]!=EMPTY_VALUE)MakeLabel( TextSell, TextSellColor );
     else
     ObjectDelete( label_name );
   }
   return(0);	
  }
  
void MakeLabel( string text, color col ) 
{
   if ( ObjectFind( label_name )>=0 ) ObjectDelete( label_name );
   
   ObjectCreate( label_name, OBJ_LABEL, TextWindow, 0, 0, 0, 0);
   ObjectSetText( label_name, text, TextSize, TextFont );
   ObjectSet( label_name, OBJPROP_CORNER, TextCorner);
   ObjectSet( label_name, OBJPROP_XDISTANCE, TextX );
   ObjectSet( label_name, OBJPROP_YDISTANCE, TextY );
   ObjectSet( label_name, OBJPROP_COLOR, col);
   ObjectSet( label_name, OBJPROP_BACK, false );
}

//+------------------------------------------------------------------+



