//+------------------------------------------------------------------+
//|                                                    Renko nmc.mq4 |
//+------------------------------------------------------------------+
//modified 10 aug 2020
// - request for show/hide btn
// - buffer style, color & width replaced with externs

#property indicator_chart_window
//#property strict 
#property indicator_buffers 2
//#property indicator_color1 clrRoyalBlue
//#property indicator_color2 clrMaroon
//#property indicator_width1 0
//#property indicator_width2 0

extern double BoxSize = 10.0;
extern color Up_block_color = clrRoyalBlue;
extern color Down_block_color = clrMaroon;
extern int Histo_width = 1;
extern string BTN_settings = "";                    //Button
extern bool btn_show = false;                       //__ show
extern string btn_pressed = "Renko";                //__ text
extern int btn_offset_x = 10;                       //__ x
extern int btn_offset_y = 20;                       //__ y
extern int btn_width = 60;                          //__ width
extern int btn_height = 20;                         //__ height
extern int btn_font_size = 10;                      //__ font size
extern color btn_font_clr = clrBlack;               //__ font color
extern color btn_bg_color = clrGray;                //__ bg color
extern color btn_border_clr = clrWhiteSmoke;        //__ border color


double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   IndicatorBuffers(4);
   SetIndexStyle(0, DRAW_HISTOGRAM,NULL,Histo_width,Up_block_color);
   SetIndexBuffer(0, Buffer1);
   SetIndexStyle(1, DRAW_HISTOGRAM,NULL,Histo_width,Down_block_color);
   SetIndexBuffer(1, Buffer2);
   SetIndexStyle(2, DRAW_NONE);
   SetIndexBuffer(2, Buffer3);
   SetIndexStyle(3, DRAW_NONE);
   SetIndexBuffer(3, Buffer4);
   SetIndexLabel(0, "Up");
   SetIndexLabel(1, "Dn");
   IndicatorDigits(Digits);
  if(btn_show){CreateButton();}
   else if(!btn_show){ObjectDelete(NULL,"renko nmc btn");}

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  double Hi;
   double Lo;
   double result;
   double barsize;
   double digits;
   int  i;
   for(i = MathMax(rates_total-1-prev_calculated,2); i>=0; i--)
  
   Buffer1[Bars] = Close[Bars];
   Buffer2[Bars] = Close[Bars];
   Buffer3[Bars] = Close[Bars];
   Buffer4[Bars] = Close[Bars];
   if (Digits == 5 || Digits == 3) digits = NormalizeDouble(10.0 * BoxSize, Digits);
   else digits = NormalizeDouble(BoxSize, Digits);
   double box = NormalizeDouble(Point * digits, Digits);
   int bars = Bars - i;
   for (int k = bars; k >= 0; k--) {
      Hi = NormalizeDouble(High[k] - (Buffer3[k + 1]) - box, Digits);
      Lo = NormalizeDouble(Low[k] - (Buffer4[k + 1]) + box, Digits);
      if (Hi >= 0.0) {
         barsize = NormalizeDouble((High[k] - (Buffer3[k + 1])) / box, Digits);
         result = NormalizeDouble(MathFloor(barsize), Digits);
         Buffer3[k] = Buffer3[k + 1] + box * result;
         Buffer4[k] = Buffer3[k] - box * result;
         Buffer1[k] = Buffer3[k];
         Buffer2[k] = Buffer4[k];
         Buffer4[k] = Buffer3[k] - box;
      } else {
         if (Lo <= 0.0) {
            barsize = NormalizeDouble((Buffer4[k + 1] - Low[k]) / box, Digits);
            result = NormalizeDouble(MathFloor(barsize), Digits);
            Buffer4[k] = Buffer4[k + 1] - box * result;
            Buffer3[k] = Buffer4[k] + box * result;
            Buffer2[k] = Buffer3[k];
            Buffer1[k] = Buffer4[k];
            Buffer3[k] = Buffer4[k] + box;
         } else {
            Buffer3[k] = Buffer3[k + 1];
            Buffer4[k] = Buffer4[k + 1];
            if (Buffer1[k + 1] > Buffer2[k + 1]) {
               Buffer1[k] = Buffer1[k + 1];
               Buffer2[k] = Buffer1[k] - box;
            }
            if (Buffer2[k + 1] > Buffer1[k + 1]) {
               Buffer1[k] = Buffer1[k + 1];
               Buffer2[k] = Buffer1[k] + box;
            }
         }
      }
   }
   return(rates_total);
  }
//+------------------------------------------------------------------+
void CreateButton()
{
   ButtonCreate(NULL,"renko nmc btn",0,btn_offset_x,btn_offset_y,btn_width,btn_height,CORNER_LEFT_UPPER,btn_pressed,"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=="renko nmc btn") 
   { 
      if(ObjectGetInteger(0,"renko nmc btn",OBJPROP_STATE))
      {
         SetIndexStyle(0,DRAW_HISTOGRAM,NULL,Histo_width,clrNONE);
         SetIndexStyle(1,DRAW_HISTOGRAM,NULL,Histo_width,clrNONE);
      }
      if(!ObjectGetInteger(0,"renko nmc btn",OBJPROP_STATE))
      {
         SetIndexStyle(0,DRAW_HISTOGRAM,NULL,Histo_width,Up_block_color);
         SetIndexStyle(1,DRAW_HISTOGRAM,NULL,Histo_width,Down_block_color);
      }
   }
}
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_YSIZE,height);
   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);
  }
