//+------------------------------------------------------------------+
//|                                          TFX OBOS Oscillator.mq4 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "TFX OBOS Copyright2006"
#property link      "http://www.TodaysForex.com"

#property indicator_separate_window
#property indicator_minimum -100.0
#property indicator_maximum 100.0
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
#property indicator_width1 1

#property indicator_level1 0

extern int Len = 6;

double Buffer1[];

int init() 
{
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, Buffer1);
   return (0);
}

int start() 
{
   double price_close1;
   double price_close2;
   double period2;
   double period_work;
   double length;
   double valeur_flag;
   double period1;
   double checkA;
   double reference;
   double transfert;
   double temp;
   double valeur_max;
   double transitoire;
   int index;
   int period_obos;
   int index_reference;
   int m;
   double Data1[300];
   double Data2[300];
   double Data3[300];
   double Data4[300];
   
   for (int i = Bars - Len - 100; i >= 0; i--) {
      checkA = 0;
      transfert = 0;
      if (index == 0) {
         index = 1;
         valeur_flag = 0;
         if (Len - 1 >= 2) 
            length = Len - 1;
         else 
            length = 2;
            
         period_obos = length + 1.0;
         price_close2 = Close[i];
         Data1[index] = Close[i];
         period1 = period_obos;
         period2 = 12 / (period1 * (period1 - 1.0) * (period1 + 1.0));
         period_work = (period_obos + 1) / 2.0;
      } 
      else 
      {
         if (index <= period_obos) 
               index++;
         else  index = period_obos + 1;
         price_close1 = price_close2;
         price_close2 = Close[i];
         
         if (index > period_obos) 
         {
            for (int k = 2; k <= period_obos; k++) 
               Data1[k - 1] = Data1[k];
               
            Data1[period_obos] = Close[i];
         } 
         else 
            Data1[index] = Close[i];
         
         if (length >= index && price_close1 != price_close2) 
               valeur_flag = 1;
         if (length == index && valeur_flag == 0.0) 
               index = 0;
      }
      if (index >= period_obos) {
         for (int j = 1; j <= period_obos; j++) {
            Data3[j] = j;
            Data4[j] = j;
            Data2[j] = Data1[j];
         }
         for (j = 1; j <= period_obos - 1; j++) {
            valeur_max = Data2[j];
            index_reference = j;
            k = j + 1;
            for (k = j + 1; k <= period_obos; k++) 
            {
               if (Data2[k] < valeur_max) {
                  valeur_max = Data2[k];
                  index_reference = k;
               }
            }
            
            temp = Data2[j];
            Data2[j] = Data2[index_reference];
            Data2[index_reference] = temp;
            temp = Data3[j];
            Data3[j] = Data3[index_reference];
            Data3[index_reference] = temp;
         }
         for (j = 1; period_obos > j; j = k) 
         {
            k = j + 1;
            checkA = 1;
            transfert = Data4[j];
            
            while (checkA != 0.0) 
            {
               if (Data2[j] != Data2[k]) {
                  if (k - j > 1) 
                  {
                     transfert /= (k - j);
                     m = j;
                     for (m = j; m <= k - 1; m++) 
                        Data4[m] = transfert;
                  }
                  checkA = 0;
               } 
               else 
               {
                  transfert += Data4[k];
                  k++;
               }
            }
         }
         transfert = 0;
         for (j = 1; j <= period_obos; j++) 
            transfert += (Data4[j] - period_work) * (Data3[j] - period_work);
         reference = period2 * transfert;
      } 
      else 
         reference = 0;
      transitoire = reference;
      if (transitoire == 0.0) transitoire = 0.00001;
      Buffer1[i] = transitoire*100.0;
   }
   return(0);
}