//+------------------------------------------------------------------+
//|                               painted pip nailer with alerts.mq4 |
//|                                                           .....h |
//|                                                 hayseedville.com |
//|                                                                  | 
//|based on http://www.forexfactory.com/showthread.php?t=63805       | 
//|  http://www.forexmoments.com/                                    |
//+------------------------------------------------------------------+

//---- indicator settings
#property  indicator_chart_window
#property  indicator_buffers 7
#property  indicator_color1  Blue
#property  indicator_color2  Red
#property  indicator_color3  Lime
#property  indicator_color4  Red
#property  indicator_color5  Lime
#property  indicator_color6  Red
#include <WinUser32.mqh>



   bool aobuy                                    =  false;
   bool aosell                                   =  false;
   bool acbuy                                    =  false;
   bool acsell                                   =  false;
   bool psarbuy                                  =  false;
   bool psarsell                                 =  false;
  
   extern string choose_between                  = "standard alert or selectable wav";
   extern bool   standardalert                   =   true;
   
   extern bool   selectablewav                   =  false;   
   extern string buy                             = "alert.wav";
   extern string sell                            = "alert.wav";
   
   extern bool   email                           =  false;
 
   double     ind_buffer1[];
   double     ind_buffer2[];

   int        HighBarBuffer[];
   int        LowBarBuffer[];
   double     CrossUp[];
   double     CrossDown[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
 
   {

   //----
   IndicatorBuffers(7);
   SetIndexBuffer(2, HighBarBuffer);
   SetIndexBuffer(3, LowBarBuffer);

   //---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,4);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,4);
 
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
   
  
   if(!SetIndexBuffer(0,ind_buffer1) && !SetIndexBuffer(1,ind_buffer2)
   && !SetIndexBuffer(2,HighBarBuffer) && !SetIndexBuffer(3,LowBarBuffer))
      Print("cannot set indicator buffers!");
      
   
   //---- initialization done
   return(0);
   
   }
  
 //------------------------------------------------------------------
 int deinit()
   { 
   
   ObjectDelete("pip");
    return(0);
   } 
 
 
 
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
  drawLabel("pip"," painted pip nailer with alerts",15,Lime,50,50,"Arial");
   int      limit,i,counter; 
   int      counted_bars=IndicatorCounted();
   double Range, AvgRange;
    //---- check for possible errors
   if(counted_bars<0) return(-1);
   
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   //----Calculation---------------------------
   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;
    
       
   if( (iClose(NULL,0,i) > iSAR(NULL,0,0.02,0.2,i)) && (iClose(NULL,0,i+1) < iSAR(NULL,0,0.02,0.2,i+1)) )       {psarbuy  = true;}  else {psarbuy  = false;}
   
   if( (iClose(NULL,0,i) < iSAR(NULL,0,0.02,0.2,i)) && (iClose(NULL,0,i+1) > iSAR(NULL,0,0.02,0.2,i+1)) )       {psarsell = true;}  else {psarsell = false;}
   
   
   double ac  = iAC(NULL, 0, i);
   
   double ac2 = iAC(NULL, 0, i+1);
   
   double ac3 = iAC(NULL, 0, i+2);
   
   if(ac > ac2 && ac2 < ac3) {acbuy  = true;} else {acbuy  = false;}
   
   if(ac < ac2 && ac2 > ac3) {acsell = true;} else {acsell = false;}
   
   
   double ao  = iAO(NULL, 0, i);
   
   double ao2 = iAO(NULL, 0, i+1);
   
   double ao3 = iAO(NULL, 0, i+2);
   
   if(ao > ao2 && ao2 < ao3) {aobuy  = true;} else {aobuy  = false;}
   
   if(ao < ao2 && ao2 > ao3) {aosell = true;} else {aosell = false;}
     
   
       
      if(psarbuy  && acbuy  && aobuy) 
       {
         ind_buffer2[i]=Low[i];
         ind_buffer1[i]=High[i];
       }

      if(psarsell && acsell && aosell)
       {
         ind_buffer1[i]=Low[i];
         ind_buffer2[i]=High[i];
       }
     
       //------------------------------------added 2/11/08.....h-----------------------
      
       static datetime lastbuysignal;
       if (lastbuysignal != Time[0])
       {
       if(psarbuy  && acbuy  && aobuy) 
       {
       lastbuysignal = Time[0];
       double high = iHigh(NULL,0,1);
       double low  = iLow(NULL,0,1);
       if(email) SendMail("painted pip nailer buy","painted pip nailer buy signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+",   "+"High: "+DoubleToStr(high,4)+" "+"Low: "+DoubleToStr(low,4)+" ,Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
       if(standardalert)  Alert(Symbol(),"  ",Period(),"  "," painted pip nailer buy @  Hour ",Hour(),"  Minute ",Minute());
       if(selectablewav) PlaySound(buy);
       }
       }
       
       
       static datetime lastsellsignal;
       if (lastsellsignal != Time[0])
       {        
       if(psarsell && acsell && aosell)
       {
       lastsellsignal = Time[0];
       high = iHigh(NULL,0,1);
       low  = iLow(NULL,0,1);      
       if(email) SendMail("painted pip nailer sell","painted pip nailer sell signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());
       if(standardalert)  Alert(Symbol()," ",Period(),"  "," painted pip nailer sell @  Hour ",Hour(),"  Minute ",Minute());
       if(selectablewav)  PlaySound(sell);
       }
       }
         
       //----------------------------------
    
       
            
       }

       return(0);
       }
  
  void drawLabel(string id, string text,int fsize, color fcolor,int x,int y,string font="Arial",int window = 0)
       {
      if(ObjectFind(id)==-1)
      { 
        ObjectCreate(id, OBJ_LABEL, window, 0, 0);
        ObjectSetText(id,text, fsize, font, fcolor);
        ObjectSet(id, OBJPROP_CORNER, 0);
        ObjectSet(id, OBJPROP_XDISTANCE, x);
        ObjectSet(id, OBJPROP_YDISTANCE, y);
      }
      else
      {
        
        ObjectDelete(id);
         
        ObjectCreate(id, OBJ_LABEL, window, 0, 0);
        ObjectSetText(id,text, fsize, font, fcolor);
        ObjectSet(id, OBJPROP_CORNER, 0);
        ObjectSet(id, OBJPROP_XDISTANCE, x);
        ObjectSet(id, OBJPROP_YDISTANCE, y);   
      }  
      }
      //http://www.hayseedville.com//layla.wav
    