//Osama by Nicholas Engleman
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue
//---- input parameters
extern int MAfast=4;
extern int MAslow=6;
extern int Pandaslow=144;
extern int Pandafast=96;
extern int Pandasignal=96;
//---- buffers
double MAfastbuffer[];
double MAslowbuffer[];
double MACDbuffer[];
double Osmabuffer[];
double Signalbuffer[];
double Pandabuffer[];
//----
int draw_begin1=0;
int draw_begin2=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(6);
   SetIndexBuffer(3,Osmabuffer);
   SetIndexBuffer(4,Signalbuffer);
   SetIndexBuffer(2, MACDbuffer);
   SetIndexBuffer(5, Pandabuffer);
   //---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0, MAfastbuffer);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1, MAslowbuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("(OSMA="+Pandafast+","+Pandaslow+"),(MA="+MAfast+","+MAslow+")");
   SetIndexLabel(0,"MAfast");
   SetIndexLabel(1,"MAslow");
//----
   int draw_begin1=MAfast-1;
   int draw_begin2=MAslow-1;
   SetIndexDrawBegin(0,draw_begin1);
   SetIndexDrawBegin(1,draw_begin2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Stochastic oscillator                                            |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      MACDbuffer[i]=iMA(NULL,0,Pandafast,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,Pandaslow,0,MODE_EMA,PRICE_CLOSE,i);
   for(i=0; i<limit; i++)
      Signalbuffer[i]=iMAOnArray(MACDbuffer,Bars,Pandasignal,0,MODE_SMA,i);
//---- main loop
   for(i=0; i<limit; i++)
      Osmabuffer[i]=MACDbuffer[i]-Signalbuffer[i];
   for(i=0; i<limit; i++)
      Pandabuffer[i]=(MACDbuffer[i]+Osmabuffer[i])/2;
      
      
  if(Bars<=MAfast) return(0);
   int ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   double pr=2.0/(MAfast+1);
   double prr=2.0/(MAslow+1);
   int pos=Bars-2;
   int poss=Bars-2;
   if(ExtCountedBars>2) pos=Bars-ExtCountedBars-1;
   if(ExtCountedBars>2) poss=Bars-ExtCountedBars-1;
   
//---- main calculation loop

   while(pos>=0)
     {
      if(pos==Bars-2)  MAfastbuffer[pos+1]=Pandabuffer[pos+1];
      MAfastbuffer[pos]=Pandabuffer[pos]*pr+ MAfastbuffer[pos+1]*(1-pr);
 	   pos--;
     }
    
  while(poss>=0)
     {
      if(poss==Bars-2)  MAslowbuffer[poss+1]=Pandabuffer[poss+1];
      MAslowbuffer[poss]=Pandabuffer[poss]*prr+ MAslowbuffer[poss+1]*(1-prr);
 	   poss--;
     }
     
  return(0);
  }
//+------------------------------------------------------------------+