//+------------------------------------------------------------------+
//|                                                          ECO.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Rosh"
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 White
#property indicator_color2 Red
//---- input parameters
extern int       First_R=26;
extern int       Second_S=5;
extern int       Signal=5;
extern bool EMA_Type=true;
double R_ALFA;
double S_ALFA;
//---- buffers
double ECO_Buffer[];
double Signal_Buffer[];
double HiLo_Buffer[];
double OpenClose_Buffer[];
double EMA_HiLo_Buffer[];
double EMA_OpCl_Buffer[];
double EMA2_HiLo_Buffer[];
double EMA2_OpCl_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 6 additional buffers are used for counting.
   IndicatorBuffers(8);   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
   SetIndexBuffer(0,ECO_Buffer);
   if (EMA_Type) SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,Red); else SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,Black);
   SetIndexBuffer(1,Signal_Buffer);
   SetIndexLabel(0,"ECO");
   SetIndexLabel(1,"Signal");
   short_name="ECO("+First_R+","+Second_S+","+Signal+")";
   IndicatorShortName(short_name);
   
   SetIndexBuffer(2, HiLo_Buffer);
   SetIndexBuffer(3, OpenClose_Buffer);
   SetIndexBuffer(4, EMA_HiLo_Buffer);
   SetIndexBuffer(5, EMA_OpCl_Buffer);
   SetIndexBuffer(6, EMA2_HiLo_Buffer);
   SetIndexBuffer(7, EMA2_OpCl_Buffer);
   
   R_ALFA=2.0/(First_R+1);
   S_ALFA=2.0/(Second_S+1);
   if (EMA_Type)
      {
      R_ALFA=1.0-R_ALFA;
      S_ALFA=1.0-S_ALFA;
      }
//----
   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 i,limit;
   if (counted_bars<0) return(-1);
   //if (counted_bars==0) counted_bars--;
   limit=Bars-counted_bars-1;
   //Comment("counted_bars=",counted_bars);
   for (i=limit;i>=0;i--)
      {
      HiLo_Buffer[i]=High[i]-Low[i];
      OpenClose_Buffer[i]=Close[i]-Open[i];
      //ECO_Buffer[i]=High[i]-Low[i];
      //Signal_Buffer[i]=Close[i]-Open[i];
      //Print("HiLo_Buffer["+i+"]=",HiLo_Buffer[i]);
      //Print("OpenClose_Buffer["+i+"]=",OpenClose_Buffer[i]);
      }
   if (counted_bars==0)
      {
      EMA_HiLo_Buffer[limit]=HiLo_Buffer[limit];
      EMA_OpCl_Buffer[limit]=OpenClose_Buffer[limit];
      }
   for (i=limit-1;i>=0;i--)
      {
      EMA_HiLo_Buffer[i]=(1-R_ALFA)*HiLo_Buffer[i]+R_ALFA*EMA_HiLo_Buffer[i+1];
      EMA_OpCl_Buffer[i]=(1-R_ALFA)*OpenClose_Buffer[i]+R_ALFA*OpenClose_Buffer[i+1];
      }
   if (counted_bars==0)
      {
      EMA2_HiLo_Buffer[limit-1]=EMA_HiLo_Buffer[limit-1];
      EMA2_OpCl_Buffer[limit-1]=EMA_OpCl_Buffer[limit-1];
      }
   for (i=limit-2;i>=0;i--)
      {
      EMA2_HiLo_Buffer[i]=(1-S_ALFA)*EMA_HiLo_Buffer[i]+S_ALFA*EMA2_HiLo_Buffer[i+1];
      EMA2_OpCl_Buffer[i]=(1-S_ALFA)*EMA_OpCl_Buffer[i]+S_ALFA*EMA2_OpCl_Buffer[i+1];
      }
   for (i=limit-1;i>=0;i--)  
      {
      ECO_Buffer[i]=100.0*EMA2_OpCl_Buffer[i]/EMA2_HiLo_Buffer[i];
      }
   for (i=limit-2;i>=0;i--)  
      {
      Signal_Buffer[i]=iMAOnArray(ECO_Buffer,0,Signal,0,MODE_EMA,i);
      }      
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+