//+------------------------------------------------------------------+
//|                Orignal Code by Scorpion                          |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 White
#property indicator_color2 White
#property indicator_color3 Red
#property indicator_color4 Red

extern int Stoch1_K = 5;
extern int Stoch1_D = 3;
extern int Stoch1_Slowing = 3;
extern int Stoch2_K = 20;
extern int Stoch2_D = 10;
extern int Stoch2_Slowing = 20;
extern int Stoch3_K = 50;
extern int Stoch3_D = 10;
extern int Stoch3_Slowing = 20;
extern int Stoch4_K = 100;
extern int Stoch4_D = 10;
extern int Stoch4_Slowing = 20;
extern int PriceField = 0;
extern int Shift_Bars=0;
extern int Bars_Count= 10000;

//---- buffers
double v1[];
double v2[];
double v3[];
double v4[];
  
int init()
  {

   IndicatorBuffers(4);
   SetIndexArrow(0,217);
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(0,-1);
   SetIndexBuffer(0, v1);
   SetIndexLabel(0,"Buy");
   
   SetIndexArrow(1,218);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(1,-1);
   SetIndexBuffer(1, v2);
   SetIndexLabel(1,"Sell");
   
   SetIndexArrow(2,217);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(2,-1);
   SetIndexBuffer(2, v3);
   SetIndexLabel(2,"Buy");
   
   SetIndexArrow(3,218);
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,1);
   SetIndexDrawBegin(3,-1);
   SetIndexBuffer(3, v4);
   SetIndexLabel(3,"Sell");
 
   return(0);
  }

int start()
 {
  double Stoch1_Main, Stoch1_Signal, Stoch2_Main, Stoch2_Signal;
  double Stoch3_Main, Stoch3_Signal, Stoch4_Main, Stoch4_Signal;
  double Stoch1_Main_2, Stoch1_Signal_2;
  int previous;
  int i;
  int shift;
  bool crossed_up, crossed_up2, crossed_down, crossed_down2;
  int counted_bars = IndicatorCounted();
  if (counted_bars > 0) counted_bars--;
  if (Bars_Count > 0 && Bars_Count <= Bars)
  {
    i = Bars_Count - counted_bars;
  }else{
    i = Bars - counted_bars;
  }
  
  while(i>=0)
   {
    shift = i + Shift_Bars;
    
    Stoch1_Main = iStochastic(NULL, 0, Stoch1_K, Stoch1_D, Stoch1_Slowing, 0, PriceField, 0, shift+1);
    Stoch1_Signal = iStochastic(NULL, 0, Stoch1_K, Stoch1_D, Stoch1_Slowing, 0, PriceField, 1, shift+1);
    
    Stoch1_Main_2 = iStochastic(NULL, 0, Stoch1_K, Stoch1_D, Stoch1_Slowing, 0, PriceField, 0, shift+2);
    Stoch1_Signal_2 = iStochastic(NULL, 0, Stoch1_K, Stoch1_D, Stoch1_Slowing, 0, PriceField, 1, shift+2);
    
    Stoch2_Main = iStochastic(NULL, 0, Stoch2_K, Stoch2_D, Stoch2_Slowing, 0, PriceField, 0, shift+1);
    Stoch2_Signal = iStochastic(NULL, 0, Stoch2_K, Stoch2_D, Stoch2_Slowing, 0, PriceField, 1, shift+1);
    
    Stoch3_Main = iStochastic(NULL, 0, Stoch3_K, Stoch3_D, Stoch3_Slowing, 0, PriceField, 0, shift+1);
    Stoch3_Signal = iStochastic(NULL, 0, Stoch3_K, Stoch3_D, Stoch3_Slowing, 0, PriceField, 1, shift+1);
    
    Stoch4_Main = iStochastic(NULL, 0, Stoch4_K, Stoch4_D, Stoch4_Slowing, 0, PriceField, 0, shift+1);
    Stoch4_Signal = iStochastic(NULL, 0, Stoch4_K, Stoch4_D, Stoch4_Slowing, 0, PriceField, 1, shift+1);
     
    crossed_up = (Stoch2_Main > Stoch2_Signal && Stoch3_Main > Stoch3_Signal);
    crossed_down = (Stoch2_Main < Stoch2_Signal && Stoch3_Main < Stoch3_Signal);
    
    if (Stoch1_Main > Stoch1_Signal && Stoch1_Main_2 < Stoch1_Signal_2 && crossed_up) {
      if (Stoch4_Main > Stoch4_Signal) {
         v3[i] = Open[shift];
      }else {
         v1[i] = Open[shift];
      }
    }else if(Stoch1_Main < Stoch1_Signal && Stoch1_Main_2 > Stoch1_Signal_2 && crossed_down) {
      if (Stoch4_Main < Stoch4_Signal) {
         v4[i] = Open[shift];
      }else {
         v2[i] = Open[shift];
      }
    }
    
    i--;
   }   
  return(0);
 }
 
//+------------------------------------------------------------------+