//+------------------------------------------------------------------+
//|                      Simple Moving Average Channel Indicator.mq4 |
//|                                                       Codersguru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Codersguru"
#property link      "http://www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Black
#property indicator_color2 Black


extern int USMA = 10;
extern int LSMA = 8;

extern color UpCross = Gold;
extern color DownCross = DarkGreen; 
extern int ArrowCode1 = 234; 
extern int ArrowCode2 = 233; 
extern int CandlesNumber = 0; 

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];

void DrawArrow( int bar , double value , int trend)
   {
      
      static int cnt = 0;
      cnt++;
      string crossing = "CROSSING_" + cnt;
      string label = "LABEL" + cnt;

      ObjectCreate(crossing, OBJ_ARROW, 0, Time[bar], value);

      
      if(trend == 1)
      {
         ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode1);
         ObjectSet(crossing, OBJPROP_COLOR , UpCross);
      }
      
      if(trend == 2)
      {
         ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode2);
         ObjectSet(crossing, OBJPROP_COLOR , DownCross);

      }

      ObjectSet(crossing, OBJPROP_WIDTH  , 1);
      ObjectsRedraw();  
   }




//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE, STYLE_DOT,1);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE, STYLE_DOT,1);
   SetIndexBuffer(1,ExtMapBuffer2);

   string short_name = "Simple Moving Average Channel Indicator!";
   IndicatorShortName(short_name);
//----

   
   
   return(0);
  }
  
  
  
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   //Delete all the drawn objects!
   int I = WindowsTotal();
   for (int count = 0; count < WindowsTotal(); count++)
   {
      ObjectsDeleteAll(count, OBJ_ARROW);
      ObjectsDeleteAll(count, OBJ_TEXT);
   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   
   double upper, lower;

   int    counted_bars=IndicatorCounted();
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   int    pos=Bars-counted_bars;
   
   static int isCrossed  = 0;
   
//---- main calculation loop
   while(pos>=0)
     {
         
         upper = iMA(NULL,0,USMA,0,MODE_SMA,PRICE_HIGH,pos);
         lower = iMA(NULL,0,LSMA,0,MODE_SMA,PRICE_LOW,pos);
   
   
         if( High[pos+0] < iMA(NULL,0,LSMA,0,MODE_SMA,PRICE_LOW,pos+1) && High[pos+CandlesNumber] < iMA(NULL,0,LSMA,0,MODE_SMA,PRICE_LOW,pos+CandlesNumber))
         {
            DrawArrow (pos , upper , 1);

         }

        if(Low[pos+0] > iMA(NULL,0,USMA,0,MODE_SMA,PRICE_HIGH,pos+1) && Low[pos+CandlesNumber] > iMA(NULL,0,USMA,0,MODE_SMA,PRICE_HIGH,pos+CandlesNumber) )
         {
            DrawArrow (pos , lower , 2);

         }
         ExtMapBuffer1[pos]= upper;
         ExtMapBuffer2[pos]= lower;
         
         pos--;
     }
//----

      

   return(0);
  }
//+------------------------------------------------------------------+