//+------------------------------------------------------------------+
//|                                                  Momo Counts.mq4 |
//|                              Copyright © 2008, TradingSytemForex |
//|                                http://www.tradingsystemforex.com |
//+------------------------------------------------------------------+

#property copyright "Copyright © 2008, TradingSytemForex"
#property link "http://www.tradingsystemforex.com"

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 SkyBlue
#property indicator_color2 LightSalmon
#property indicator_color3 Blue
#property indicator_color4 Red

//|---------indicator settings

extern color First=Black;
extern color Sell=LightSalmon;
extern color Buy=SkyBlue;
extern color PerfectSell=Red;
extern color PerfectBuy=Blue;
extern bool ShowNumbers=true;
extern bool ShowArrows=false;
extern bool UseAlerts=false;

//|---------indicator buffers

double CrossUp[];
double CrossDown[];
double CrossPUp[];
double CrossPDown[];
double prevtime;
double Range,AvgRange;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int init()
{
   SetIndexStyle(0,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(0,233);
   SetIndexBuffer(0,CrossUp);
   SetIndexStyle(1,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(1,234);
   SetIndexBuffer(1,CrossDown);
   SetIndexStyle(2,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(2,233);
   SetIndexBuffer(2,CrossPUp);
   SetIndexStyle(3,DRAW_ARROW,EMPTY,1);
   SetIndexArrow(3,234);
   SetIndexBuffer(3,CrossPDown);
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

int deinit() 
{
   for(int i=0;i<Bars;i++)
   {
      ObjectDelete("first"+i);
      ObjectDelete("fifth"+i);
      ObjectDelete("nineth"+i);
      ObjectDelete("thirteenth"+i);
   }
   return(0);
}

//+------------------------------------------------------------------+
//| Momo Counts                                                      |
//+------------------------------------------------------------------+

int start()
{
   int iadu;int iadd;
   double ru=0.5,rd=0.25;

   if(Period()==PERIOD_M1){iadu=1;iadd=3;}
   if(Period()==PERIOD_M5){iadu=2;iadd=7;}
   if(Period()==PERIOD_M15){iadu=2;iadd=12;}
   if(Period()==PERIOD_M30){iadu=2;iadd=20;}
   if(Period()==PERIOD_H1){iadu=4;iadd=25;}
   if(Period()==PERIOD_H4){iadu=8;iadd=60;}
   if(Period()==PERIOD_D1){iadu=8;iadd=80;}
   
   double adu=Point*iadu;
   double add=Point*iadd;

   if(ShowNumbers==false){adu=0;add=0;ru=0.5;rd=0.5;}
   
   int limit,counter;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0)return(-1);
   if(counted_bars>0)counted_bars--;
   limit=Bars-counted_bars+10;

   for(int i=limit;i>0;i--)
   {
      counter=i;
      Range=0;
      AvgRange=0;
      for(counter=i;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;

   //|---------buy

      if(Close[i]<Close[i+4] && Close[i+1]>Close[i+5])
      {
         if(ShowNumbers)
         {
            ObjectCreate("first"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("first"+i,""+1,10,"Arial",First);
         }
      }
      if(Close[i]<Close[i+4] && Close[i+1]<Close[i+5] && Close[i+2]<Close[i+6] && Close[i+3]<Close[i+7] && Close[i+4]<Close[i+8] && Close[i+5]>Close[i+9])
      {
         if(ShowArrows||UseAlerts)CrossUp[i]=Low[i]-adu-Range*ru;
         if(ShowNumbers)
         {
            ObjectCreate("fifth"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("fifth"+i,""+5,14,"Arial",Buy);
         }
      }
      if(Close[i]<Close[i+4] && Close[i+1]<Close[i+5] && Close[i+2]<Close[i+6] && Close[i+3]<Close[i+7] && Close[i+4]<Close[i+8] && Close[i+5]<Close[i+9] && Close[i+6]<Close[i+10] && Close[i+7]<Close[i+11] && Close[i+8]<Close[i+12] && Close[i+9]>Close[i+13] && !((Low[i]<Low[i+3] && Low[i]<Low[i+4])||(Low[i+1]<Low[i+3] && Low[i+1]<Low[i+4])))
      {
         if(ShowArrows||UseAlerts)CrossUp[i]=Low[i]-adu-Range*ru;
         if(ShowNumbers)
         {
            ObjectCreate("nineth"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("nineth"+i,""+9,14,"Arial",Buy);
         }
      }
      if(Close[i]<Close[i+4] && Close[i+1]<Close[i+5] && Close[i+2]<Close[i+6] && Close[i+3]<Close[i+7] && Close[i+4]<Close[i+8] && Close[i+5]<Close[i+9] && Close[i+6]<Close[i+10] && Close[i+7]<Close[i+11] && Close[i+8]<Close[i+12] && Close[i+9]>Close[i+13] && ((Low[i]<Low[i+3] && Low[i]<Low[i+4])||(Low[i+1]<Low[i+3] && Low[i+1]<Low[i+4])))
      {
         if(ShowArrows||UseAlerts)CrossPUp[i]=Low[i]-adu-Range*ru;
         if(ShowNumbers)
         {
            ObjectCreate("nineth"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("nineth"+i,""+9,14,"Arial",PerfectBuy);
         }
      }
      if(Close[i]<=High[i+2] && Close[i+1]<=High[i+3] && Close[i+2]<=High[i+4] && Close[i+3]<=High[i+5] && Close[i+4]<=High[i+6] && Close[i+5]<=High[i+7] && Close[i+6]<=High[i+8] && Close[i+7]<=High[i+9] && Close[i+8]<=High[i+10] && Close[i+9]<=High[i+11] && Close[i+10]<=High[i+12] && Close[i+11]<=High[i+13] && Close[i+12]<=High[i+14] && Close[i+13]>High[i+15] && !(Low[i]<=Close[i+5]))
      {
         if(ShowArrows||UseAlerts)CrossUp[i]=Low[i]-adu-Range*ru;
         if(ShowNumbers)
         {
            ObjectCreate("thirteenth"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("thirteenth"+i,""+13,14,"Arial",Buy);
         }
      }
      if(Close[i]<=High[i+2] && Close[i+1]<=High[i+3] && Close[i+2]<=High[i+4] && Close[i+3]<=High[i+5] && Close[i+4]<=High[i+6] && Close[i+5]<=High[i+7] && Close[i+6]<=High[i+8] && Close[i+7]<=High[i+9] && Close[i+8]<=High[i+10] && Close[i+9]<=High[i+11] && Close[i+10]<=High[i+12] && Close[i+11]<=High[i+13] && Close[i+12]<=High[i+14] && Close[i+13]>High[i+15] && Low[i]<=Close[i+5])
      {
         if(ShowArrows||UseAlerts)CrossPUp[i]=Low[i]-adu-Range*ru;
         if(ShowNumbers)
         {
            ObjectCreate("thirteenth"+i,OBJ_TEXT,0,Time[i],Low[i]-adu);
            ObjectSetText("thirteenth"+i,""+13,14,"Arial",PerfectBuy);
         }
      }

   //|---------sell

      if(Close[i]>Close[i+4] && Close[i+1]<Close[i+5])
      {
         if(ShowNumbers)
         {
            ObjectCreate("first"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("first"+i,""+1,10,"Arial",First);
         }
      }
      if(Close[i]>Close[i+4] && Close[i+1]>Close[i+5] && Close[i+2]>Close[i+6] && Close[i+3]>Close[i+7] && Close[i+4]>Close[i+8] && Close[i+5]<Close[i+9])
      {
         if(ShowArrows||UseAlerts)CrossDown[i]=High[i]+add+Range*rd;
         if(ShowNumbers)
         {
            ObjectCreate("fifth"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("fifth"+i,""+5,14,"Arial",Sell);
         }
      }
      if(Close[i]>Close[i+4] && Close[i+1]>Close[i+5] && Close[i+2]>Close[i+6] && Close[i+3]>Close[i+7] && Close[i+4]>Close[i+8] && Close[i+5]>Close[i+9] && Close[i+6]>Close[i+10] && Close[i+7]>Close[i+11] && Close[i+8]>Close[i+12] && Close[i+9]<Close[i+13] && !((High[i]>High[i+3] && High[i]>High[i+4])||(High[i+1]>High[i+3] && High[i+1]>High[i+4])))
      {
         if(ShowArrows||UseAlerts)CrossDown[i]=High[i]+add+Range*rd;
         if(ShowNumbers)
         {
            ObjectCreate("nineth"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("nineth"+i,""+9,14,"Arial",Sell);
         }
      }
      if(Close[i]>Close[i+4] && Close[i+1]>Close[i+5] && Close[i+2]>Close[i+6] && Close[i+3]>Close[i+7] && Close[i+4]>Close[i+8] && Close[i+5]>Close[i+9] && Close[i+6]>Close[i+10] && Close[i+7]>Close[i+11] && Close[i+8]>Close[i+12] && Close[i+9]<Close[i+13] && ((High[i]>High[i+3] && High[i]>High[i+4])||(High[i+1]>High[i+3] && High[i+1]>High[i+4])))
      {
         if(ShowArrows||UseAlerts)CrossPDown[i]=High[i]+add+Range*rd;
         if(ShowNumbers)
         {
            ObjectCreate("nineth"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("nineth"+i,""+9,14,"Arial",PerfectSell);
         }
      }
      if(Close[i]>=Low[i+2] && Close[i+1]>=Low[i+3] && Close[i+2]>=Low[i+4] && Close[i+3]>=Low[i+5] && Close[i+4]>=Low[i+6] && Close[i+5]>=Low[i+7] && Close[i+6]>=Low[i+8] && Close[i+7]>=Low[i+9] && Close[i+8]>=Low[i+10] && Close[i+9]>=Low[i+11] && Close[i+10]>=Low[i+12] && Close[i+11]>=Low[i+13] && Close[i+12]>=Low[i+14] && Close[i+13]<Low[i+15] && !(High[i]>=Close[i+5]))
      {
         if(ShowArrows||UseAlerts)CrossDown[i]=High[i]+add+Range*rd;
         if(ShowNumbers)
         {
            ObjectCreate("thirteenth"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("thirteenth"+i,""+13,14,"Arial",Sell);
         }
      }
      if(Close[i]>=Low[i+2] && Close[i+1]>=Low[i+3] && Close[i+2]>=Low[i+4] && Close[i+3]>=Low[i+5] && Close[i+4]>=Low[i+6] && Close[i+5]>=Low[i+7] && Close[i+6]>=Low[i+8] && Close[i+7]>=Low[i+9] && Close[i+8]>=Low[i+10] && Close[i+9]>=Low[i+11] && Close[i+10]>=Low[i+12] && Close[i+11]>=Low[i+13] && Close[i+12]>=Low[i+14] && Close[i+13]<Low[i+15] && High[i]>=Close[i+5])
      {
         if(ShowArrows||UseAlerts)CrossPDown[i]=High[i]+add+Range*rd;
         if(ShowNumbers)
         {
            ObjectCreate("thirteenth"+i,OBJ_TEXT,0,Time[i],High[i]+add);
            ObjectSetText("thirteenth"+i,""+13,14,"Arial",PerfectSell);
         }
      }
      
   //|---------alerts

      if((CrossUp[0]>2000) && (CrossDown[0]>2000) && (CrossPUp[0]>2000) && (CrossPDown[0]>2000)){prevtime=0;}
      if((CrossUp[0]==Low[i]-adu-Range*ru) && (prevtime!=Time[0]) && UseAlerts)
      {
         prevtime=Time[0];
         Alert(Symbol()," Momo Counts Up @ Hour ",Hour(),"  Minute ",Minute());
         PlaySound("alert.wav");
         SendMail(" Momo Counts Alert "," Signal Up, Symbol=" +Symbol()+ ", Period="+Period());
      } 
      if((CrossDown[0]==High[i]+add+Range*rd) && (prevtime!=Time[0]) && UseAlerts)
      {
         prevtime=Time[0];
         Alert(Symbol()," Momo Counts Down @ Hour ",Hour(),"  Minute ",Minute());
         PlaySound("alert.wav");
         SendMail(" Momo Counts Alert "," Signal Down, Symbol="+Symbol()+", Period="+Period());
      }
      if((CrossPUp[0]==Low[i]-adu-Range*ru) && (prevtime!=Time[0]) && UseAlerts)
      {
         prevtime=Time[0];
         Alert(Symbol()," Momo Counts Perfect Up @ Hour ",Hour(),"  Minute ",Minute());
         PlaySound("alert.wav");
         SendMail(" Momo Counts Alert "," Perfect Signal Up, Symbol="+Symbol()+", Period="+Period());
      } 
      if((CrossPDown[0]==High[i]+add+Range*rd) && (prevtime!=Time[0]) && UseAlerts)
      {
         prevtime=Time[0];
         Alert(Symbol()," Momo Counts Perfect Down @ Hour ",Hour(),"  Minute ",Minute());
         PlaySound("alert.wav");
         SendMail(" Momo Counts Alert "," Perfect Signal Down, Symbol="+Symbol()+", Period="+Period());
      }
   }
   return(0);
}

//+------------------------------------------------------------------+