//+------------------------------------------------------------------+
//|                                                      iMirror.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 3
#property indicator_width4 3

//---- input parameters
extern int       ExtParam1;
extern ENUM_BASE_CORNER   btn_corner            = CORNER_LEFT_UPPER; 
extern string             btn_text              = "mirr";
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           = "mirr off";            
extern string             btn_unpressed         = "mirr 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;                       
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
bool                      show_data             = true;
string IndicatorName, IndicatorObjPrefix ,buttonId ;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
string GenerateIndicatorName(const string target)
{
   string name = target;
   int try = 2;
   while (WindowFind(name) != -1)
   {
      name = target + " #" + IntegerToString(try++);
   }
   return name;
}
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMapBuffer3);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ExtMapBuffer4);   
   
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexBuffer(2,ExtMapBuffer1);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,3);
   SetIndexBuffer(3,ExtMapBuffer2);
   
   IndicatorShortName("mirr");
  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);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
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,"mirr");
   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 );
      
   
   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);
        
           
   }
   return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start2()
  {
  

      int First_Visible_Index=FirstVisibleBar();
      int Last_Visible_Index=FirstVisibleBar()-BarsPerWindow();
      if(Last_Visible_Index<0)Last_Visible_Index=0;      
      
      if(Last_Visible_Index<0)Last_Visible_Index=0;
      double wr=(High[Highest(NULL,0,MODE_HIGH,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)]-Low[Lowest(NULL,0,MODE_LOW,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)])/Point;

      for(int i=0;i<Bars;i++){
         ExtMapBuffer1[i]=EMPTY_VALUE;
         ExtMapBuffer2[i]=EMPTY_VALUE;
         ExtMapBuffer3[i]=EMPTY_VALUE;
         ExtMapBuffer4[i]=EMPTY_VALUE;      
      }
      
      for(i=Last_Visible_Index;i<=First_Visible_Index;i++){
         ExtMapBuffer1[i]=1/Open[i];
         ExtMapBuffer2[i]=1/Close[i]; 
         ExtMapBuffer3[i]=1/High[i];
         ExtMapBuffer4[i]=1/Low[i];       
      }
      double ho=High[Highest(NULL,0,MODE_HIGH,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)];
      double lo=Low[Lowest(NULL,0,MODE_LOW,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)];
      double hn=ExtMapBuffer4[ArrayMaximum(ExtMapBuffer4,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)];
      double ln=ExtMapBuffer3[ArrayMinimum(ExtMapBuffer3,First_Visible_Index-Last_Visible_Index+1,Last_Visible_Index)];
      double RR=ho-lo;
      double NR=hn-ln;
      double KK=RR/NR;
      
      for(i=Last_Visible_Index;i<=First_Visible_Index;i++){
         double hh=lo+KK*(ExtMapBuffer1[i]-ln);
         double ll=lo+KK*(ExtMapBuffer2[i]-ln);
         ExtMapBuffer1[i]=hh;
         ExtMapBuffer2[i]=ll;
         if(Close[i]>Open[i]){
            hh=lo+KK*(ExtMapBuffer3[i]-ln);
            ll=lo+KK*(ExtMapBuffer4[i]-ln);   
            ExtMapBuffer4[i]=hh;
            ExtMapBuffer3[i]=ll;         
         }
         else{
            ExtMapBuffer3[i]=lo+KK*(ExtMapBuffer3[i]-ln);
            ExtMapBuffer4[i]=lo+KK*(ExtMapBuffer4[i]-ln);  
         }
      }      
      
          

   return(0);
  }
//+------------------------------------------------------------------+