//+------------------------------------------------------------------+
//|                                                         KAMA.mq4 |
//|                                    Copyright? 2009, Walter Choy. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright? 2009, Walter Choy."
#property link      ""

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- input parameters
extern int       kama_period = 10;
extern double    fast_ma_period = 2.0;
extern double    slow_ma_period = 30.0;
extern bool btn_show = true;                       //btn__show
extern string btn_pressed = "Kama off";              //btn__pressed text
extern string btn_unpressed = "Kama on";            //btn__unpressed text
extern int btn_offset_x = 10;                       //btn__x
extern int btn_offset_y = 20;                       //btn__y
extern int btn_width = 65;                          //btn__width
extern int btn_height = 20;                         //btn__height
extern int btn_font_size = 10;                      //btn__font size
extern color btn_font_clr = clrBlack;               //btn__font color
extern color btn_bg_color = clrGray;                //btn__bg color
extern color btn_border_clr = clrWhiteSmoke;        //btn__border color
//---- buffers
double KAMA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,KAMA);
   SetIndexLabel(0, "KAMA");
//----
if(btn_show){CreateButton();}
   if(!btn_show){ObjectDelete(0,"Kama");}
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int i = Bars - counted_bars;
   
   double fastest = 2 / (fast_ma_period + 1);
   double slowest = 2 / (slow_ma_period + 1);
   
   while(i>0){
      double er = 1;
      double signal = MathAbs(Close[i] - Close[i+kama_period]);
      double noise = 0;
      for (int j=0; j<kama_period; j++)
         noise += MathAbs(Close[i+j] - Close[i+j+1]);
      if (noise > 0)
         er = signal / noise;
      double sc = MathPow((er * (fastest - slowest) + slowest), 2);
      KAMA[i] = KAMA[i+1] + sc * (Close[i] - KAMA[i+1]);
      i--;
   }
//----
   return(0);
  }
  
  void CreateButton()
{
   ButtonCreate(NULL,"Kama",0,btn_offset_x,btn_offset_y,btn_width,btn_height,CORNER_LEFT_UPPER,btn_unpressed,"Arial",btn_font_size,btn_font_clr,btn_bg_color,btn_border_clr,false,true,false,false,0);
}

void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
{
   if (id == CHARTEVENT_OBJECT_CLICK && sparam=="Kama") 
   { 
      if(ObjectGetInteger(0,"Kama",OBJPROP_STATE)){
         ObjectSetString(0,"Kama",OBJPROP_TEXT,btn_pressed);
         SetIndexStyle(0,DRAW_NONE);
         
         }
      if(!ObjectGetInteger(0,"Kama",OBJPROP_STATE)){
         ObjectSetString(0,"Kama",OBJPROP_TEXT,btn_unpressed);
      SetIndexStyle(0,DRAW_LINE);
         }
   }
}


bool ButtonCreate(const long              chart_ID=0,               // chart's ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,                 // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',  // background color
                  const color             border_clr=clrNONE,       // border color
                  const bool              state=false,              // pressed/released
                  const bool              back=false,               // in the background
                  const bool              selectable=true,      //object can be selected        
                  const bool              selection=false,          // highlight to move
                  const bool              hidden=false,              // hidden in the object list
                  const long              z_order=0)                // priority for mouse click
  {
   ResetLastError();
   ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0);
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
   return(true);
  }
  
void OnDeinit(const int reason)
{
   ObjectDelete(0,"Kama");
}

//+------------------------------------------------------------------+