//+------------------------------------------------------------------+
//|                                                 TTM original.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1  clrSienna
#property indicator_color2  clrSienna
#property indicator_color3  clrRed
#property indicator_color4  clrLimeGreen
#property indicator_color5  clrSienna
#property indicator_color6  clrSienna
#property indicator_color7  clrYellow
#property indicator_color8  clrYellow
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  1
#property indicator_width6  1
#property indicator_width7  2
#property indicator_width8  2

//
//
//
//
//

extern int            TTMPeriod = 6;
extern ENUM_MA_METHOD MaMode    = MODE_LWMA;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "ttmc";
extern string             btn_Font              = "Arial";      //"Arial Black"
extern int                btn_FontSize          = 8;                            
extern color              btn_text_ON_color     = clrWhite;
extern color              btn_text_OFF_color    = clrRed;
extern string             btn_pressed           = "ttmc off";            
extern string             btn_unpressed         = "ttmc on";
extern color              btn_background_color  = clrDimGray;
extern color              btn_border_color      = clrDarkGray;
extern int                button_x              = 900;                                 
extern int                button_y              = 0;                                   
extern int                btn_Width             = 60;                                 
extern int                btn_Height            = 20;                                
extern string             soundBT               = "tick.wav";  
input bool                Interpolate         = true;              // Interpolate in multi time frame mode on/off?  

bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;
//
//
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
double trend[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   for (int i=0; i<indicator_buffers; i++) SetIndexStyle(i,DRAW_LINE);
   IndicatorBuffers(9);
   SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,DRAW_HISTOGRAM);
   SetIndexBuffer(4,buffer5); SetIndexStyle(4,DRAW_HISTOGRAM);
   SetIndexBuffer(5,buffer6); SetIndexStyle(5,DRAW_HISTOGRAM);
   SetIndexBuffer(6,buffer7); SetIndexStyle(6,DRAW_HISTOGRAM);
   SetIndexBuffer(7,buffer8); SetIndexStyle(7,DRAW_HISTOGRAM);
   SetIndexBuffer(8,trend);
   
     IndicatorShortName("TTM-c");
  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,0,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,"TTM-c");
   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();
  
}

int start()
{
//template code 
   handleButtonClicks();
   recalc = false;
   
   start2();
   
      SetIndexStyle(0,DRAW_HISTOGRAM);
         SetIndexStyle(1,DRAW_HISTOGRAM);
         SetIndexStyle(2,DRAW_HISTOGRAM);
         SetIndexStyle(3,DRAW_HISTOGRAM);
         SetIndexStyle(4,DRAW_HISTOGRAM);
         SetIndexStyle(5,DRAW_HISTOGRAM);
         SetIndexStyle(6,DRAW_HISTOGRAM);
         SetIndexStyle(7,DRAW_HISTOGRAM);
          SetIndexStyle(8,DRAW_HISTOGRAM);
   
   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);
         SetIndexStyle(1,DRAW_NONE);
         SetIndexStyle(2,DRAW_NONE);
         SetIndexStyle(3,DRAW_NONE);
         SetIndexStyle(4,DRAW_NONE);
         SetIndexStyle(5,DRAW_NONE);
         SetIndexStyle(6,DRAW_NONE);
         SetIndexStyle(7,DRAW_NONE);
         SetIndexStyle(8,DRAW_NONE);
           
   }
   return(0);
}
//
//
//
//
//

int start2()
{
   int counted_bars=IndicatorCounted();
   int i,limit;
   
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
           limit = MathMin(Bars-counted_bars,Bars-1);
 
   //
   //
   //
   //
   //
            
   for (i=limit; i>=0; i--)
   {  
       double Low_ma  = iMA(NULL,0,TTMPeriod,0,MaMode,PRICE_LOW,i);
       double High_ma = iMA(NULL,0,TTMPeriod,0,MaMode,PRICE_HIGH,i);
       double close   = iClose(NULL,0,i);
       double L3      = (High_ma- Low_ma) / 3 + Low_ma;
       double H3      = 2 * (High_ma- Low_ma) / 3 + Low_ma;
            
      //
      //
      //
      //
      //
      
      buffer1[i] = EMPTY_VALUE;
      buffer2[i] = EMPTY_VALUE;
      buffer3[i] = EMPTY_VALUE;
      buffer4[i] = EMPTY_VALUE;
      buffer5[i] = EMPTY_VALUE;
      buffer6[i] = EMPTY_VALUE;
      buffer7[i] = EMPTY_VALUE;
      buffer8[i] = EMPTY_VALUE;
      trend[i] = 0;   
         if (close > H3)               trend[i] = 1;
         if (close < L3)               trend[i] =-1;
         if (close < H3 && close > L3) trend[i] = 0;
         
         if (trend[i] == -1)
         { 
           buffer1[i] = High[i];
           buffer2[i] = Low[i];
           if (Close[i]>Open[i]) { buffer3[i] = Close[i]; buffer4[i] = Open[i]; }  
           else                  { buffer4[i] = Close[i]; buffer3[i] = Open[i]; }  
         }                   
         if (trend[i] == 1)
         {
            buffer1[i] = Low[i];
            buffer2[i] = High[i];
            if (Close[i]<Open[i]) { buffer3[i] = Close[i]; buffer4[i] = Open[i]; }  
            else                  { buffer4[i] = Close[i]; buffer3[i] = Open[i]; }  
         }                   
         if (trend[i] == 0)
         {
            buffer5[i] = Low[i];
            buffer6[i] = High[i];
            buffer7[i] = Open[i];
            buffer8[i] = Close[i];
         }
   }
   return(0);
}