/* 
William Blau Ergodic. 

ECO - Ergodic Candlestick Oscillator 
(MOV(MOV(C-O,5,E))26,E)/MOV(MOV(H-L,5,E))26,E))*100

Linuxser 2007

*/

#property copyright "Under The GNU General Public License"
#property link      "www.gnu.org"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 MediumOrchid
#property indicator_color2 Black
#property indicator_width1 2
#property indicator_width2 0
#property indicator_maximum 40
#property indicator_minimum -40
#property indicator_level1 20
#property indicator_level2 -20
#property indicator_levelcolor Red
#property indicator_levelstyle 2



//---- input parameters
//extern int       Price= 0;   // Apply to Price(0-Close;1-Open;2-High;3-Low;4-Median price;5-Typical price;6-Weighted Close)
extern int       First_R=26;
extern int       Second_S=5;
extern int       Signal=5;
extern int       MA_Mode=1; //MODE_SMA :	0 // MODE_EMA:	1 // MODE_SMMA: 2 // MODE_LWMA: 3 //

//---- buffers
double ECO_Buffer[];
double D_Buffer[];
double HiLo_Buffer[];
double OpenClose_Buffer[];
double EMA_HiLo_Buffer[];
double EMA2_HiLo_Buffer[];
double EMA_OpenClose_Buffer[];
double EMA2_OpenClose_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(8);
   SetIndexBuffer(2, HiLo_Buffer);
   SetIndexBuffer(3, EMA_HiLo_Buffer);
   SetIndexBuffer(4, EMA2_HiLo_Buffer);
   SetIndexBuffer(5, OpenClose_Buffer);
   SetIndexBuffer(6, EMA_OpenClose_Buffer);
   SetIndexBuffer(7, EMA2_OpenClose_Buffer);

   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ECO_Buffer);
   SetIndexLabel(0,"K_%");
   SetIndexStyle(1,DRAW_LINE);
   SetIndexArrow(1,108);
   SetIndexBuffer(1,D_Buffer);
   SetIndexLabel(1,"D_%");
   IndicatorShortName("ECO v1"+"("+First_R+","+Second_S+",["+Signal+"])");

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit,i;
   limit=Bars-counted_bars-1;
   for (i=Bars-1;i>=0;i--)
      {           
      HiLo_Buffer[i]= High[i]-Low[i];       
      OpenClose_Buffer[i]=Close[i]-Open[i];      
      }
      
   for (i=Bars-1;i>=0;i--)
      {
      EMA_HiLo_Buffer[i]=iMAOnArray(HiLo_Buffer,Bars,Second_S,0,MA_Mode,i);
      EMA_OpenClose_Buffer[i]=iMAOnArray(OpenClose_Buffer,Bars,Second_S,0,MA_Mode,i);
      }

   for (i=Bars-1;i>=0;i--)
      {
      EMA2_HiLo_Buffer[i]=iMAOnArray(EMA_HiLo_Buffer,Bars,First_R,0,MA_Mode,i);
      EMA2_OpenClose_Buffer[i]=iMAOnArray(EMA_OpenClose_Buffer,Bars,First_R,0,MA_Mode,i);
      }

   for (i=limit;i>=0;i--)
      {
      if(iMAOnArray(EMA_OpenClose_Buffer,Bars,First_R,0,MA_Mode,i) != 0)
      ECO_Buffer[i]=100.0*(EMA2_OpenClose_Buffer[i]/EMA2_HiLo_Buffer[i]);
      }
   for (i=limit;i>=0;i--)
      {
      D_Buffer[i]=iMAOnArray(ECO_Buffer,Bars,Signal,0,MA_Mode,i);
      }

   
//----
   return(0);
  }
//+------------------------------------------------------------------+