//+------------------------------------------------------------------+                  
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_color1 PowderBlue
#property indicator_color2 Yellow
//#property indicator_color3 Yellow

//---- input parameters
extern int StochPeriod=20;
extern string SecondSymbol="GBPUSD";


//---- buffers
double RatioBuffer[];
double StochBuffer[];
//double DiffBuffer[];

//double HiBuffer[];
//double LoBuffer[];
//double SecondHiBuffer[];
//double SecondLoBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string   short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(3);

//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, RatioBuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, StochBuffer);
//   SetIndexStyle(2,DRAW_LINE);
//   SetIndexBuffer(2, DiffBuffer);

   SetLevelValue(0,0);
   SetLevelValue(1,50);
   SetLevelValue(2,100);
   


//---- name for DataWindow and indicator subwindow label
   short_name="RatioRawStoch("+StochPeriod+") "+Symbol()+" "+SecondSymbol;
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Ratio ");
   SetIndexLabel(1,"RatioRawStoch ");
//   SetIndexLabel(2,"Stoch Diff");

   SetIndexDrawBegin(0,StochPeriod);
   SetIndexDrawBegin(1,StochPeriod);
//   SetIndexDrawBegin(2,StochPeriod);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k;
   int    SecondPeriod=Period();
   int    counted_bars=IndicatorCounted();
   double   dTMP9;

//----
   if(Bars<=StochPeriod+10) return(0);
//---- initial zero
   if(counted_bars<1)
     {
      for(i=1;i<=StochPeriod;i++)
      { RatioBuffer[Bars-i]=0;
        StochBuffer[Bars-i]=0;
//        DiffBuffer[Bars-i]=0;
      }
     }

   i=Bars-StochPeriod;
   if(counted_bars>StochPeriod) i=Bars-counted_bars-1;
   while(i>=0)
     {
//      double min=99999999;
//      double Secondmin=99999999;
      
//      double max=-99999999;
//      double Secondmax=-99999999;
      

      RatioBuffer[i] = Close[i] / iClose(SecondSymbol,0,i);

      
      dTMP9 = ( RatioBuffer[ArrayMaximum(RatioBuffer,StochPeriod,i)] - RatioBuffer[ArrayMinimum(RatioBuffer,StochPeriod,i)]);
      if(dTMP9 != 0)
         StochBuffer[i] = 100* ((RatioBuffer[i] - RatioBuffer[ArrayMinimum(RatioBuffer,StochPeriod,i)]) / dTMP9) ;
      else
         StochBuffer[i] = StochBuffer[i+1];
      




      
      i--;
     }

     
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;


   return(0);
  }
//+------------------------------------------------------------------+