//+------------------------------------------------------------------+
//|                                                Fast&Slow&SAR.mq4 |
//|                                      Copyright © 2006, BearSerg. |
//|                                        http://bearserg.wallst.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, BearSerg."
#property link      "http://bearserg.wallst.ru"

#property indicator_chart_window

#property indicator_buffers 9

#property indicator_color1 Goldenrod
#property indicator_width1 2
#property indicator_color2 SeaGreen
#property indicator_width2 2
#property indicator_color3 Aqua
#property indicator_width3 1
#property indicator_color4 Magenta
#property indicator_width4 2
#property indicator_color5 Chartreuse
#property indicator_width5 2
#property indicator_color6 Magenta
#property indicator_width6 1
#property indicator_color7 Chartreuse
#property indicator_width7 1

//---- input parameters
extern int       Fast=10;
extern int       Slow=40;
//extern int       Stop=50;
//extern int       TakeProfit=100;

//---- indicator buffers
double ExtFastBuffer[];
double ExtSlowBuffer[];
double ExtParabolicBuffer[];
double CrossUp[];
double CrossDown[];
double ArrowUp[];
double ArrowDown[];
bool AlertBuySignal, AttentionBuySignal, AlertSellSignal, AttentionSellSignal;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   ObjectsDeleteAll(0, DRAW_ARROW);
//---- indicators
//---- first positions skipped when drawing
   SetIndexDrawBegin(0,Fast);
   SetIndexDrawBegin(1,Slow);
   SetIndexDrawBegin(3,1);
//---- 3 indicator buffers mapping
   SetIndexBuffer(0,ExtFastBuffer);
   SetIndexBuffer(1,ExtSlowBuffer);
   SetIndexBuffer(2,ExtParabolicBuffer);
   SetIndexBuffer(3, CrossUp);
   SetIndexBuffer(4, CrossDown);
   SetIndexBuffer(5, ArrowUp);
   SetIndexBuffer(6, ArrowDown);
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(1,DRAW_LINE, STYLE_SOLID, 2);
   SetIndexStyle(2,DRAW_ARROW, 2);
   SetIndexStyle(3, DRAW_ARROW, EMPTY);
   SetIndexStyle(4, DRAW_ARROW, EMPTY);
   SetIndexStyle(5, DRAW_ARROW, EMPTY);
   SetIndexStyle(6, DRAW_ARROW, EMPTY);
   SetIndexArrow(2,159);
//---- index labels
   SetIndexLabel(0,"FastEMA(" + Fast + ")");
   SetIndexLabel(1,"SlowEMA(" + Slow + ")");
   SetIndexLabel(2,"Parabolic SAR (0.02, 0.2)");
   SetIndexLabel(3,"Attention for Sell");
   SetIndexLabel(4,"Attention for Buy");
   SetIndexLabel(5,"Buy signal");
   SetIndexLabel(6,"Sell signal");
//---- initialization done
   SetIndexArrow(3, 108);
   SetIndexArrow(4, 108);
   SetIndexArrow(5, 234);
   SetIndexArrow(6, 233);
   
   return(0);
  }

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
    ObjectsDeleteAll(0, DRAW_ARROW);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit, counter;
   double Range, AvgRange, Shift, r, r1;

  int counted_bars=IndicatorCounted();
//---- 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;
//---- main loop
   for(int i=0; i<limit; i++)
     {
         
         ExtFastBuffer[i]=iMA(NULL,0,Fast,0,MODE_EMA,PRICE_MEDIAN,i);
         ExtSlowBuffer[i]=iMA(NULL,0,Slow,0,MODE_EMA,PRICE_MEDIAN,i);
         ExtParabolicBuffer[i]=iSAR(NULL,0,0.02,0.2,i);
      
         if ( (ExtFastBuffer[i-2] < ExtSlowBuffer[i-2]) && 
            (ExtFastBuffer[i] > ExtSlowBuffer[i]) && !AttentionSellSignal )
         {
            CrossUp[i-1] = ExtSlowBuffer[i-1];
            AlertSellSignal = false;
            AlertBuySignal = false;
            AttentionSellSignal = false;
            AttentionBuySignal = false;
         }
         
         if ( (ExtFastBuffer[i-2] > ExtSlowBuffer[i-2]) && 
            (ExtFastBuffer[i] < ExtSlowBuffer[i]) && !AttentionBuySignal )
         {
            CrossDown[i-1] = ExtSlowBuffer[i-1];
            AlertBuySignal = false;
            AlertSellSignal = false;
            AttentionBuySignal = false;
            AttentionSellSignal = false;
         }

         if ( ( (ExtParabolicBuffer[i-1] >= ExtSlowBuffer[i-1]) && 
            (ExtParabolicBuffer[i] <= ExtSlowBuffer[i]) ) &&
            ( (ExtParabolicBuffer[i-1] < ExtFastBuffer[i-1]) &&
            (ExtParabolicBuffer[i] < ExtFastBuffer[i])) )
         {
               ArrowDown[i] = ExtSlowBuffer[i];
               AlertSellSignal = false;
               AlertBuySignal = false;
         }
         if ( ( (ExtParabolicBuffer[i-1] <= ExtSlowBuffer[i-1]) && 
            (ExtParabolicBuffer[i] >= ExtSlowBuffer[i]) ) &&
            ( (ExtParabolicBuffer[i-1] > ExtFastBuffer[i-1]) &&
            (ExtParabolicBuffer[i] > ExtFastBuffer[i]) ) )
         {
            ArrowUp[i] = ExtSlowBuffer[i];
            AlertBuySignal = false;           
            AlertSellSignal = false;
         }
       
         Comment("FastEMA:",ExtFastBuffer[i],"  SlowEMA:",ExtSlowBuffer[i],"  ParabolicSAR:",ExtParabolicBuffer[i]);
     }
//---- done
   return(0);
  }
//+------------------------------------------------------------------+