//+------------------------------------------------------------------+
//|                                                      TEMA_CUSTOM |
//|                                      Copyright 2015, SergeyVrady |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Sergey Vrady"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Yellow
#property indicator_width1  2
//---
input int                MAPeriod=8;
input ENUM_MA_METHOD     MAMethod=MODE_EMA;
input ENUM_APPLIED_PRICE MAPrice=PRICE_MEDIAN;
extern int                btn_Subwindow = 0;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "tema";
extern string             btn_Font              = "Arial";
extern int                btn_FontSize          = 10;                            
extern color              btn_text_ON_color     = clrLime;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "tema OFF";            
extern string             btn_unpressed         = "tema ON";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrBlack;
extern int                button_x              = 850;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 75;                                 
extern int                btn_Height            = 20;                                
extern string             soundBT               = "tick.wav";  

//---
bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;

double TemaBuffer[];
double Ema[];
double EmaOfEma[];
double EmaOfEmaOfEma[];

string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}

//+------------------------------------------------------------------+
//|            Initialization and input parameters                   |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,TemaBuffer);
   SetIndexBuffer(1,Ema);
   SetIndexBuffer(2,EmaOfEma);
   SetIndexBuffer(3,EmaOfEmaOfEma);
//---
   IndicatorShortName("TEMA("+MAPeriod+")");
//---
   string short_name;
   int    draw_begin=MAPeriod-1;
//---
   switch(MAPrice)
     {
      case PRICE_CLOSE  : short_name="CLOSE(";                break;
      case PRICE_OPEN  : short_name="OPEN(";  draw_begin=0;   break;
      case PRICE_HIGH : short_name="HIGH(";                   break;
      case PRICE_LOW : short_name="LOW(";                     break;
      case PRICE_MEDIAN : short_name="MEDIAN(";               break;
      case PRICE_TYPICAL : short_name="TYPICAL(";             break;
      case PRICE_WEIGHTED : short_name="WEIGHTED(";           break;
      default :        return(INIT_FAILED);
     }
//---
   switch(MAMethod)
     {
      case MODE_SMA  : short_name="SMA(";                break;
      case MODE_EMA  : short_name="EMA(";  draw_begin=0; break;
      case MODE_SMMA : short_name="SMMA(";               break;
      case MODE_LWMA : short_name="LWMA(";               break;
      default :        return(INIT_FAILED);
     }
//---
        IndicatorName = GenerateIndicatorName(btn_text);
   IndicatorObjPrefix = "__" + IndicatorName + "__";
   IndicatorShortName(WindowExpertName());
   IndicatorDigits(Digits);
      double val;
   if (GlobalVariableGet(IndicatorName + "_visibility", val))
   show_data = val != 0;

   ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1);
   buttonId = IndicatorObjPrefix+btn_text;
   createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y);
   ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x);
   
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor)
{
      ObjectDelete    (ChartID(),buttonID);
       ObjectCreate(ChartID(),buttonID,OBJ_BUTTON,btn_Subwindow,0,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height);
      ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font);
      ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999);
      ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999);
}


int deinit()
{
 
 
 ObjectsDeleteAll(0,"tema");
   ObjectsDeleteAll(ChartID(), IndicatorObjPrefix);
   
   return(0);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
 bool recalc = true;
 void handleButtonClicks()
{
   if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
   {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0);
      recalc = true;
      start();
   }
}
void OnChartEvent(const int id, 
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
   handleButtonClicks();
   if (id==CHARTEVENT_OBJECT_CLICK && ObjectGet(sparam,OBJPROP_TYPE)==OBJ_BUTTON)
   {
   if (soundBT!="") PlaySound(soundBT);     
   }
   
  
	
}
//+------------------------------------------------------------------+
//|                    Calculation section                           |
//+------------------------------------------------------------------+
int start()
{

handleButtonClicks();
   recalc = false;
   
   SetIndexStyle(0,DRAW_LINE);
  
  
   start2();
   
  
   
      
   
   if (show_data)
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
     
      }
      else
      {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
       SetIndexStyle(0,DRAW_NONE);
        
        
   
      
   
      //template code     
      }
       return(0);
      }
int start2()
  {
   int i,limit,limit2,limit3,counted_bars=IndicatorCounted();
//---
   if(counted_bars==0)
     {
      limit=Bars-1;
      limit2=limit-MAPeriod;
      limit3=limit2-MAPeriod;
     }
   if(counted_bars>0)
     {
      limit=Bars-counted_bars-1;
      limit2=limit;
      limit3=limit2;
     }
//---
   for(i=limit;i>=0;i--) Ema[i]=iMA(NULL,0,MAPeriod,0,MAMethod,MAPrice,i);
   for(i=limit2;i>=0;i--) EmaOfEma[i]=iMAOnArray(Ema,0,MAPeriod,0,MAMethod,i);
   for(i=limit3;i>=0;i--) EmaOfEmaOfEma[i]=iMAOnArray(EmaOfEma,0,MAPeriod,0,MODE_EMA,i);
   for(i=limit3;i>=0;i--) TemaBuffer[i]=3*Ema[i]-3*EmaOfEma[i]+EmaOfEmaOfEma[i];
//---
   return(0);
  }
//+------------------------------------------------------------------+
